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) { | |
36 // This can legitimately happen in unit tests. | 35 // This can legitimately happen in unit tests. |
37 if (!entries_memory) | 36 if (!entries_memory) |
38 return; | 37 return; |
39 | 38 |
40 const size_t size = entries_memory->size(); | 39 const size_t size = entries_memory->size(); |
41 const unsigned char* const front = entries_memory->front(); | 40 const unsigned char* const front = entries_memory->front(); |
42 if (size < kChecksumHashSize || | 41 if (size < kChecksumHashSize || |
43 (size - kChecksumHashSize) % kUrlHashSize != 0) { | 42 (size - kChecksumHashSize) % kUrlHashSize != 0) { |
44 NOTREACHED(); | 43 NOTREACHED(); |
45 return; | 44 return; |
(...skipping 15 matching lines...) Expand all Loading... |
61 } | 60 } |
62 | 61 |
63 // Construct and insert all hashes. | 62 // Construct and insert all hashes. |
64 for (const unsigned char* index = front + kChecksumHashSize; | 63 for (const unsigned char* index = front + kChecksumHashSize; |
65 index < front + size; | 64 index < front + size; |
66 index += kUrlHashSize) { | 65 index += kUrlHashSize) { |
67 int64 value = 0; | 66 int64 value = 0; |
68 memcpy(&value, index, kUrlHashSize); | 67 memcpy(&value, index, kUrlHashSize); |
69 entries_.insert(value); | 68 entries_.insert(value); |
70 } | 69 } |
71 | |
72 is_valid_ = true; | |
73 } | 70 } |
74 | 71 |
75 HashedAdNetworkDatabase::~HashedAdNetworkDatabase() {} | 72 HashedAdNetworkDatabase::~HashedAdNetworkDatabase() {} |
76 | 73 |
77 bool HashedAdNetworkDatabase::IsAdNetwork(const GURL& url) const { | 74 bool HashedAdNetworkDatabase::IsAdNetwork(const GURL& url) const { |
78 int64 hash = 0; | 75 int64 hash = 0; |
79 crypto::SHA256HashString(url.host(), &hash, sizeof(hash)); | 76 crypto::SHA256HashString(url.host(), &hash, sizeof(hash)); |
80 // If initialization failed (most likely because this is a unittest), then | 77 // If initialization failed (most likely because this is a unittest), then |
81 // |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 |
82 // is desired default behavior. | 79 // is desired default behavior. |
83 return entries_.count(hash) != 0; | 80 return entries_.count(hash) != 0; |
84 } | 81 } |
85 | 82 |
86 } // namespace extensions | 83 } // namespace extensions |
OLD | NEW |