fake_safe_browsing_database_manager.cc revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
1// Copyright 2013 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "chrome/browser/extensions/fake_safe_browsing_database_manager.h"
6
7#include <algorithm>
8#include <iterator>
9#include <vector>
10
11#include "base/bind_helpers.h"
12#include "base/memory/ref_counted.h"
13#include "base/message_loop_proxy.h"
14#include "chrome/browser/safe_browsing/safe_browsing_service.h"
15#include "chrome/browser/safe_browsing/safe_browsing_util.h"
16
17namespace extensions {
18
19FakeSafeBrowsingDatabaseManager::FakeSafeBrowsingDatabaseManager()
20    : SafeBrowsingDatabaseManager(
21          make_scoped_refptr(SafeBrowsingService::CreateSafeBrowsingService())),
22      enabled_(false) {
23}
24
25FakeSafeBrowsingDatabaseManager::~FakeSafeBrowsingDatabaseManager() {
26}
27
28bool FakeSafeBrowsingDatabaseManager::CheckExtensionIDs(
29    const std::set<std::string>& extension_ids,
30    Client* client) {
31  if (!enabled_)
32    return true;
33
34  // Need to construct the full SafeBrowsingCheck rather than calling
35  // OnCheckExtensionsResult directly because it's protected. Grr!
36  std::vector<std::string> extension_ids_vector(extension_ids.begin(),
37                                                extension_ids.end());
38  std::vector<SBFullHash> extension_id_hashes;
39  std::transform(extension_ids_vector.begin(), extension_ids_vector.end(),
40                 std::back_inserter(extension_id_hashes),
41                 safe_browsing_util::StringToSBFullHash);
42
43  scoped_ptr<SafeBrowsingCheck> safe_browsing_check(
44      new SafeBrowsingCheck(std::vector<GURL>(),
45                             extension_id_hashes,
46                             client,
47                             safe_browsing_util::EXTENSIONBLACKLIST));
48
49  for (size_t i = 0; i < extension_ids_vector.size(); ++i) {
50    const std::string& extension_id = extension_ids_vector[i];
51    if (unsafe_ids_.count(extension_id))
52      safe_browsing_check->full_hash_results[i] = SB_THREAT_TYPE_EXTENSION;
53  }
54
55  base::MessageLoopProxy::current()->PostTask(
56      FROM_HERE,
57      base::Bind(&FakeSafeBrowsingDatabaseManager::OnSafeBrowsingResult,
58                 this,
59                 base::Passed(&safe_browsing_check),
60                 client));
61  return false;
62}
63
64void FakeSafeBrowsingDatabaseManager::OnSafeBrowsingResult(
65    scoped_ptr<SafeBrowsingCheck> result,
66    Client* client) {
67  client->OnSafeBrowsingResult(*result);
68}
69
70}  // namespace extensions
71