| OLD | NEW |
| 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/hashed_ad_network_database.h" |
| 6 | 6 |
| 7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/lazy_instance.h" | 8 #include "base/logging.h" |
| 9 #include "base/memory/ref_counted_memory.h" | 9 #include "base/memory/ref_counted_memory.h" |
| 10 #include "crypto/secure_hash.h" | 10 #include "crypto/secure_hash.h" |
| 11 #include "crypto/sha2.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" | 12 #include "url/gurl.h" |
| 15 | 13 |
| 16 namespace extensions { | 14 namespace extensions { |
| 17 | 15 |
| 18 namespace { | 16 namespace { |
| 19 | 17 |
| 20 // We use a hash size of 8 for these for three reasons. | 18 // 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 | 19 // 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 | 20 // (reading from disk would be far too slow because these checks are |
| 23 // performed synchronously), that space is important. | 21 // performed synchronously), that space is important. |
| 24 // 2. Since we don't store full hashes, reconstructing the list is more | 22 // 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 | 23 // difficult. This may mean we get a few incorrect hits, but the security is |
| 26 // worth the (very small) amount of noise. | 24 // worth the (very small) amount of noise. |
| 27 // 3. It fits nicely into a int64. | 25 // 3. It fits nicely into a int64. |
| 28 const size_t kUrlHashSize = 8u; | 26 const size_t kUrlHashSize = 8u; |
| 29 COMPILE_ASSERT(kUrlHashSize <= sizeof(int64), url_hashes_must_fit_into_a_int64); | 27 COMPILE_ASSERT(kUrlHashSize <= sizeof(int64), url_hashes_must_fit_into_a_int64); |
| 30 | 28 |
| 31 const size_t kChecksumHashSize = 32u; | 29 const size_t kChecksumHashSize = 32u; |
| 32 | 30 |
| 33 class AdNetworkDatabaseImpl : public AdNetworkDatabase { | 31 } // namespace |
| 34 public: | |
| 35 AdNetworkDatabaseImpl(); | |
| 36 virtual ~AdNetworkDatabaseImpl(); | |
| 37 | 32 |
| 38 private: | 33 HashedAdNetworkDatabase::HashedAdNetworkDatabase( |
| 39 virtual bool IsAdNetwork(const GURL& url) const OVERRIDE; | 34 scoped_refptr<base::RefCountedStaticMemory> entries_memory) { |
| 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. | 35 // This can legitimately happen in unit tests. |
| 61 if (!entries_memory) | 36 if (!entries_memory) |
| 62 return; | 37 return; |
| 63 | 38 |
| 64 const size_t size = entries_memory->size(); | 39 const size_t size = entries_memory->size(); |
| 65 const unsigned char* const front = entries_memory->front(); | 40 const unsigned char* const front = entries_memory->front(); |
| 66 if (size < kChecksumHashSize || | 41 if (size < kChecksumHashSize || |
| 67 (size - kChecksumHashSize) % kUrlHashSize != 0) { | 42 (size - kChecksumHashSize) % kUrlHashSize != 0) { |
| 68 NOTREACHED(); | 43 NOTREACHED(); |
| 69 return; | 44 return; |
| (...skipping 17 matching lines...) Expand all Loading... |
| 87 // Construct and insert all hashes. | 62 // Construct and insert all hashes. |
| 88 for (const unsigned char* index = front + kChecksumHashSize; | 63 for (const unsigned char* index = front + kChecksumHashSize; |
| 89 index < front + size; | 64 index < front + size; |
| 90 index += kUrlHashSize) { | 65 index += kUrlHashSize) { |
| 91 int64 value = 0; | 66 int64 value = 0; |
| 92 memcpy(&value, index, kUrlHashSize); | 67 memcpy(&value, index, kUrlHashSize); |
| 93 entries_.insert(value); | 68 entries_.insert(value); |
| 94 } | 69 } |
| 95 } | 70 } |
| 96 | 71 |
| 97 bool AdNetworkDatabaseImpl::IsAdNetwork(const GURL& url) const { | 72 HashedAdNetworkDatabase::~HashedAdNetworkDatabase() {} |
| 73 |
| 74 bool HashedAdNetworkDatabase::IsAdNetwork(const GURL& url) const { |
| 98 int64 hash = 0; | 75 int64 hash = 0; |
| 99 crypto::SHA256HashString(url.host(), &hash, sizeof(hash)); | 76 crypto::SHA256HashString(url.host(), &hash, sizeof(hash)); |
| 100 // If initialization failed (most likely because this is a unittest), then | 77 // If initialization failed (most likely because this is a unittest), then |
| 101 // |entries_| is never populated and we are guaranteed to return false - which | 78 // |entries_| is never populated and we are guaranteed to return false - which |
| 102 // is desired default behavior. | 79 // is desired default behavior. |
| 103 return entries_.count(hash) != 0; | 80 return entries_.count(hash) != 0; |
| 104 } | 81 } |
| 105 | 82 |
| 106 class AdNetworkDatabaseFactory { | |
| 107 public: | |
| 108 AdNetworkDatabaseFactory(); | |
| 109 ~AdNetworkDatabaseFactory(); | |
| 110 | |
| 111 const AdNetworkDatabase* GetDatabase(); | |
| 112 void SetDatabase(scoped_ptr<AdNetworkDatabase> database); | |
| 113 | |
| 114 private: | |
| 115 scoped_ptr<AdNetworkDatabase> database_; | |
| 116 }; | |
| 117 | |
| 118 AdNetworkDatabaseFactory::AdNetworkDatabaseFactory() {} | |
| 119 AdNetworkDatabaseFactory::~AdNetworkDatabaseFactory() {} | |
| 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 | |
| 129 void AdNetworkDatabaseFactory::SetDatabase( | |
| 130 scoped_ptr<AdNetworkDatabase> database) { | |
| 131 database_.reset(database.release()); | |
| 132 } | |
| 133 | |
| 134 base::LazyInstance<AdNetworkDatabaseFactory> g_factory = | |
| 135 LAZY_INSTANCE_INITIALIZER; | |
| 136 | |
| 137 } // namespace | |
| 138 | |
| 139 AdNetworkDatabase::~AdNetworkDatabase() {} | |
| 140 | |
| 141 // static | |
| 142 const AdNetworkDatabase* AdNetworkDatabase::Get() { | |
| 143 return g_factory.Get().GetDatabase(); | |
| 144 } | |
| 145 | |
| 146 // static | |
| 147 void AdNetworkDatabase::SetForTesting(scoped_ptr<AdNetworkDatabase> database) { | |
| 148 g_factory.Get().SetDatabase(database.Pass()); | |
| 149 } | |
| 150 | |
| 151 } // namespace extensions | 83 } // namespace extensions |
| OLD | NEW |