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/hashed_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/logging.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" |
(...skipping 13 matching lines...) Expand all Loading... |
24 // worth the (very small) amount of noise. | 24 // worth the (very small) amount of noise. |
25 // 3. It fits nicely into a int64. | 25 // 3. It fits nicely into a int64. |
26 const size_t kUrlHashSize = 8u; | 26 const size_t kUrlHashSize = 8u; |
27 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); |
28 | 28 |
29 const size_t kChecksumHashSize = 32u; | 29 const size_t kChecksumHashSize = 32u; |
30 | 30 |
31 } // namespace | 31 } // namespace |
32 | 32 |
33 HashedAdNetworkDatabase::HashedAdNetworkDatabase( | 33 HashedAdNetworkDatabase::HashedAdNetworkDatabase( |
34 scoped_refptr<base::RefCountedStaticMemory> entries_memory) { | 34 scoped_refptr<base::RefCountedStaticMemory> entries_memory) |
| 35 : is_valid_(false) { |
35 // This can legitimately happen in unit tests. | 36 // This can legitimately happen in unit tests. |
36 if (!entries_memory) | 37 if (!entries_memory) |
37 return; | 38 return; |
38 | 39 |
39 const size_t size = entries_memory->size(); | 40 const size_t size = entries_memory->size(); |
40 const unsigned char* const front = entries_memory->front(); | 41 const unsigned char* const front = entries_memory->front(); |
41 if (size < kChecksumHashSize || | 42 if (size < kChecksumHashSize || |
42 (size - kChecksumHashSize) % kUrlHashSize != 0) { | 43 (size - kChecksumHashSize) % kUrlHashSize != 0) { |
43 NOTREACHED(); | 44 NOTREACHED(); |
44 return; | 45 return; |
(...skipping 15 matching lines...) Expand all Loading... |
60 } | 61 } |
61 | 62 |
62 // Construct and insert all hashes. | 63 // Construct and insert all hashes. |
63 for (const unsigned char* index = front + kChecksumHashSize; | 64 for (const unsigned char* index = front + kChecksumHashSize; |
64 index < front + size; | 65 index < front + size; |
65 index += kUrlHashSize) { | 66 index += kUrlHashSize) { |
66 int64 value = 0; | 67 int64 value = 0; |
67 memcpy(&value, index, kUrlHashSize); | 68 memcpy(&value, index, kUrlHashSize); |
68 entries_.insert(value); | 69 entries_.insert(value); |
69 } | 70 } |
| 71 |
| 72 is_valid_ = true; |
70 } | 73 } |
71 | 74 |
72 HashedAdNetworkDatabase::~HashedAdNetworkDatabase() {} | 75 HashedAdNetworkDatabase::~HashedAdNetworkDatabase() {} |
73 | 76 |
74 bool HashedAdNetworkDatabase::IsAdNetwork(const GURL& url) const { | 77 bool HashedAdNetworkDatabase::IsAdNetwork(const GURL& url) const { |
75 int64 hash = 0; | 78 int64 hash = 0; |
76 crypto::SHA256HashString(url.host(), &hash, sizeof(hash)); | 79 crypto::SHA256HashString(url.host(), &hash, sizeof(hash)); |
77 // If initialization failed (most likely because this is a unittest), then | 80 // If initialization failed (most likely because this is a unittest), then |
78 // |entries_| is never populated and we are guaranteed to return false - which | 81 // |entries_| is never populated and we are guaranteed to return false - which |
79 // is desired default behavior. | 82 // is desired default behavior. |
80 return entries_.count(hash) != 0; | 83 return entries_.count(hash) != 0; |
81 } | 84 } |
82 | 85 |
83 } // namespace extensions | 86 } // namespace extensions |
OLD | NEW |