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

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

Issue 263953003: Re-enable ActivityLog AdNetworkDatabase unittest (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 7 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
« no previous file with comments | « no previous file | chrome/browser/extensions/activity_log/ad_network_database_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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"
8 #include "base/lazy_instance.h" 7 #include "base/lazy_instance.h"
9 #include "base/memory/ref_counted_memory.h" 8 #include "chrome/browser/extensions/activity_log/hashed_ad_network_database.h"
10 #include "crypto/secure_hash.h"
11 #include "crypto/sha2.h"
12 #include "grit/browser_resources.h" 9 #include "grit/browser_resources.h"
13 #include "ui/base/resource/resource_bundle.h" 10 #include "ui/base/resource/resource_bundle.h"
14 #include "url/gurl.h"
15 11
16 namespace extensions { 12 namespace extensions {
17 13
18 namespace { 14 namespace {
19 15
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
106 class AdNetworkDatabaseFactory { 16 class AdNetworkDatabaseFactory {
107 public: 17 public:
108 AdNetworkDatabaseFactory(); 18 AdNetworkDatabaseFactory();
109 ~AdNetworkDatabaseFactory(); 19 ~AdNetworkDatabaseFactory();
110 20
111 const AdNetworkDatabase* GetDatabase(); 21 const AdNetworkDatabase* GetDatabase();
112 void SetDatabase(scoped_ptr<AdNetworkDatabase> database); 22 void SetDatabase(scoped_ptr<AdNetworkDatabase> database);
113 23
114 private: 24 private:
115 scoped_ptr<AdNetworkDatabase> database_; 25 scoped_ptr<AdNetworkDatabase> database_;
116 }; 26 };
117 27
118 AdNetworkDatabaseFactory::AdNetworkDatabaseFactory() {} 28 AdNetworkDatabaseFactory::AdNetworkDatabaseFactory() {}
119 AdNetworkDatabaseFactory::~AdNetworkDatabaseFactory() {} 29 AdNetworkDatabaseFactory::~AdNetworkDatabaseFactory() {}
120 30
121 const AdNetworkDatabase* AdNetworkDatabaseFactory::GetDatabase() { 31 const AdNetworkDatabase* AdNetworkDatabaseFactory::GetDatabase() {
122 // Construct a new database, if we don't have one. 32 // Construct a new database, if we don't have one.
123 if (!database_.get()) 33 if (!database_.get()) {
124 database_.reset(new AdNetworkDatabaseImpl()); 34 database_.reset(new HashedAdNetworkDatabase(
35 ResourceBundle::GetSharedInstance().LoadDataResourceBytes(
36 IDR_AD_NETWORK_HASHES)));
37 }
125 38
126 return database_.get(); 39 return database_.get();
127 } 40 }
128 41
129 void AdNetworkDatabaseFactory::SetDatabase( 42 void AdNetworkDatabaseFactory::SetDatabase(
130 scoped_ptr<AdNetworkDatabase> database) { 43 scoped_ptr<AdNetworkDatabase> database) {
131 database_.reset(database.release()); 44 database_.reset(database.release());
132 } 45 }
133 46
134 base::LazyInstance<AdNetworkDatabaseFactory> g_factory = 47 base::LazyInstance<AdNetworkDatabaseFactory> g_factory =
135 LAZY_INSTANCE_INITIALIZER; 48 LAZY_INSTANCE_INITIALIZER;
136 49
137 } // namespace 50 } // namespace
138 51
139 AdNetworkDatabase::~AdNetworkDatabase() {} 52 AdNetworkDatabase::~AdNetworkDatabase() {}
140 53
141 // static 54 // static
142 const AdNetworkDatabase* AdNetworkDatabase::Get() { 55 const AdNetworkDatabase* AdNetworkDatabase::Get() {
143 return g_factory.Get().GetDatabase(); 56 return g_factory.Get().GetDatabase();
144 } 57 }
145 58
146 // static 59 // static
147 void AdNetworkDatabase::SetForTesting(scoped_ptr<AdNetworkDatabase> database) { 60 void AdNetworkDatabase::SetForTesting(scoped_ptr<AdNetworkDatabase> database) {
148 g_factory.Get().SetDatabase(database.Pass()); 61 g_factory.Get().SetDatabase(database.Pass());
149 } 62 }
150 63
151 } // namespace extensions 64 } // namespace extensions
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/extensions/activity_log/ad_network_database_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698