Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(270)

Side by Side Diff: chrome/browser/extensions/activity_log/ad_network_database.cc

Issue 242303002: Add implementation for Ad Networks Database (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Latest master Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/extensions/activity_log/ad_network_database.h" 5 #include "chrome/browser/extensions/activity_log/ad_network_database.h"
6 6
7 #include "base/basictypes.h"
7 #include "base/lazy_instance.h" 8 #include "base/lazy_instance.h"
9 #include "base/memory/ref_counted_memory.h"
10 #include "crypto/secure_hash.h"
11 #include "crypto/sha2.h"
12 #include "grit/browser_resources.h"
13 #include "ui/base/resource/resource_bundle.h"
14 #include "url/gurl.h"
8 15
9 namespace extensions { 16 namespace extensions {
10 17
11 namespace { 18 namespace {
12 19
20 // We use a hash size of 8 for these for three reasons.
21 // 1. It saves us a bit on space, and, since we have to store these in memory
22 // (reading from disk would be far too slow because these checks are
23 // performed synchronously), that space is important.
24 // 2. Since we don't store full hashes, reconstructing the list is more
25 // difficult. This may mean we get a few incorrect hits, but the security is
26 // worth the (very small) amount of noise.
27 // 3. It fits nicely into a int64.
28 const size_t kUrlHashSize = 8u;
29 COMPILE_ASSERT(kUrlHashSize <= sizeof(int64), url_hashes_must_fit_into_a_int64);
30
31 const size_t kChecksumHashSize = 32u;
32
33 class AdNetworkDatabaseImpl : public AdNetworkDatabase {
34 public:
35 AdNetworkDatabaseImpl();
36 virtual ~AdNetworkDatabaseImpl();
37
38 private:
39 virtual bool IsAdNetwork(const GURL& url) const OVERRIDE;
40
41 // Initialize the AdNetworkDatabase. This means initializing the set of
42 // hashes from the shared memory.
43 void Init();
44
45 // The set of partial hashes for known ad networks.
46 base::hash_set<int64> entries_;
47 };
48
49 AdNetworkDatabaseImpl::AdNetworkDatabaseImpl() {
50 Init();
51 }
52
53 AdNetworkDatabaseImpl::~AdNetworkDatabaseImpl() {}
54
55 void AdNetworkDatabaseImpl::Init() {
56 base::RefCountedStaticMemory* entries_memory =
57 ResourceBundle::GetSharedInstance().LoadDataResourceBytes(
58 IDR_AD_NETWORK_HASHES);
59
60 // This can legitimately happen in unit tests.
61 if (!entries_memory)
62 return;
63
64 const size_t size = entries_memory->size();
65 const unsigned char* const front = entries_memory->front();
66 if (size < kChecksumHashSize ||
67 (size - kChecksumHashSize) % kUrlHashSize != 0) {
68 NOTREACHED();
69 return;
70 }
71
72 // The format of the data resource is fairly straight-forward:
73 // <32-bit checksum><list of 64-bit hashes of hosts>, with no linebreaks or
74 // other separations.
75 scoped_ptr<crypto::SecureHash> hash(
76 crypto::SecureHash::Create(crypto::SecureHash::SHA256));
77
78 hash->Update(front + kChecksumHashSize, size - kChecksumHashSize);
79 char hash_value[kChecksumHashSize];
80 hash->Finish(hash_value, kChecksumHashSize);
81 // If the checksum doesn't match, abort.
82 if (memcmp(hash_value, front, kChecksumHashSize) != 0) {
83 NOTREACHED();
84 return;
85 }
86
87 // Construct and insert all hashes.
88 for (const unsigned char* index = front + kChecksumHashSize;
89 index < front + size;
90 index += kUrlHashSize) {
91 int64 value = 0;
92 memcpy(&value, index, kUrlHashSize);
93 entries_.insert(value);
94 }
95 }
96
97 bool AdNetworkDatabaseImpl::IsAdNetwork(const GURL& url) const {
98 int64 hash = 0;
99 crypto::SHA256HashString(url.host(), &hash, sizeof(hash));
100 // If initialization failed (most likely because this is a unittest), then
101 // |entries_| is never populated and we are guaranteed to return false - which
102 // is desired default behavior.
103 return entries_.count(hash) != 0;
104 }
105
13 class AdNetworkDatabaseFactory { 106 class AdNetworkDatabaseFactory {
14 public: 107 public:
15 AdNetworkDatabaseFactory(); 108 AdNetworkDatabaseFactory();
16 ~AdNetworkDatabaseFactory(); 109 ~AdNetworkDatabaseFactory();
17 110
111 const AdNetworkDatabase* GetDatabase();
18 void SetDatabase(scoped_ptr<AdNetworkDatabase> database); 112 void SetDatabase(scoped_ptr<AdNetworkDatabase> database);
19 113
20 const AdNetworkDatabase* database() const { return database_.get(); }
21
22 private: 114 private:
23 scoped_ptr<AdNetworkDatabase> database_; 115 scoped_ptr<AdNetworkDatabase> database_;
24 }; 116 };
25 117
26 AdNetworkDatabaseFactory::AdNetworkDatabaseFactory() {} 118 AdNetworkDatabaseFactory::AdNetworkDatabaseFactory() {}
27 AdNetworkDatabaseFactory::~AdNetworkDatabaseFactory() {} 119 AdNetworkDatabaseFactory::~AdNetworkDatabaseFactory() {}
28 120
121 const AdNetworkDatabase* AdNetworkDatabaseFactory::GetDatabase() {
122 // Construct a new database, if we don't have one.
123 if (!database_.get())
124 database_.reset(new AdNetworkDatabaseImpl());
125
126 return database_.get();
127 }
128
29 void AdNetworkDatabaseFactory::SetDatabase( 129 void AdNetworkDatabaseFactory::SetDatabase(
30 scoped_ptr<AdNetworkDatabase> database) { 130 scoped_ptr<AdNetworkDatabase> database) {
31 database_.reset(database.release()); 131 database_.reset(database.release());
32 } 132 }
33 133
34 base::LazyInstance<AdNetworkDatabaseFactory> g_factory = 134 base::LazyInstance<AdNetworkDatabaseFactory> g_factory =
35 LAZY_INSTANCE_INITIALIZER; 135 LAZY_INSTANCE_INITIALIZER;
36 136
37 } // namespace 137 } // namespace
38 138
39 AdNetworkDatabase::~AdNetworkDatabase() {} 139 AdNetworkDatabase::~AdNetworkDatabase() {}
40 140
41 // static 141 // static
42 const AdNetworkDatabase* AdNetworkDatabase::Get() { 142 const AdNetworkDatabase* AdNetworkDatabase::Get() {
43 return g_factory.Get().database(); 143 return g_factory.Get().GetDatabase();
44 } 144 }
45 145
46 // static 146 // static
47 void AdNetworkDatabase::SetForTesting(scoped_ptr<AdNetworkDatabase> database) { 147 void AdNetworkDatabase::SetForTesting(scoped_ptr<AdNetworkDatabase> database) {
48 g_factory.Get().SetDatabase(database.Pass()); 148 g_factory.Get().SetDatabase(database.Pass());
49 } 149 }
50 150
51 } // namespace extensions 151 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698