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/supervised_user/supervised_user_site_list.h" | 5 #include "chrome/browser/supervised_user/supervised_user_site_list.h" |
6 | 6 |
7 #include <algorithm> | |
8 | |
7 #include "base/files/file_util.h" | 9 #include "base/files/file_util.h" |
8 #include "base/json/json_file_value_serializer.h" | 10 #include "base/json/json_file_value_serializer.h" |
9 #include "base/logging.h" | 11 #include "base/logging.h" |
10 #include "base/metrics/histogram_macros.h" | 12 #include "base/metrics/histogram_macros.h" |
11 #include "base/sha1.h" | 13 #include "base/strings/string_number_conversions.h" |
12 #include "base/task_runner_util.h" | 14 #include "base/task_runner_util.h" |
13 #include "base/values.h" | 15 #include "base/values.h" |
14 #include "content/public/browser/browser_thread.h" | 16 #include "content/public/browser/browser_thread.h" |
15 #include "url/gurl.h" | 17 #include "url/gurl.h" |
16 | 18 |
17 const int kSitelistFormatVersion = 2; | 19 const int kSitelistFormatVersion = 2; |
18 | 20 |
19 const char kEntryPointUrlKey[] = "entry_point_url"; | 21 const char kEntryPointUrlKey[] = "entry_point_url"; |
20 const char kHostnameHashesKey[] = "hostname_hashes"; | 22 const char kHostnameHashesKey[] = "hostname_hashes"; |
21 const char kSitelistFormatVersionKey[] = "version"; | 23 const char kSitelistFormatVersionKey[] = "version"; |
(...skipping 10 matching lines...) Expand all Loading... | |
32 deserializer.Deserialize(&error_code, &error_msg); | 34 deserializer.Deserialize(&error_code, &error_msg); |
33 if (!value) { | 35 if (!value) { |
34 LOG(ERROR) << "Couldn't load site list " << path.value() << ": " | 36 LOG(ERROR) << "Couldn't load site list " << path.value() << ": " |
35 << error_msg; | 37 << error_msg; |
36 } | 38 } |
37 return value.Pass(); | 39 return value.Pass(); |
38 } | 40 } |
39 | 41 |
40 } // namespace | 42 } // namespace |
41 | 43 |
44 SupervisedUserSiteList::HostnameHash::HostnameHash( | |
45 const std::string& hostname) { | |
46 base::SHA1HashBytes(reinterpret_cast<const unsigned char*>(hostname.c_str()), | |
47 hostname.size(), bytes_.data()); | |
48 } | |
49 | |
50 SupervisedUserSiteList::HostnameHash::HostnameHash( | |
51 const std::vector<uint8>& bytes) { | |
52 DCHECK_GE(bytes.size(), base::kSHA1Length); | |
Bernhard Bauer
2015/12/03 11:43:21
Would you mind making this a CHECK just to be a bi
Marc Treib
2015/12/03 12:25:11
Hm... sure, I guess. Done.
| |
53 std::copy_n(bytes.begin(), base::kSHA1Length, bytes_.begin()); | |
Bernhard Bauer
2015/12/03 11:43:21
Actually, is copy_n already allowed? I can't find
Marc Treib
2015/12/03 12:25:11
Hm, <algorithms> is still under "to be discussed".
| |
54 } | |
55 | |
56 bool SupervisedUserSiteList::HostnameHash::operator==( | |
57 const HostnameHash& rhs) const { | |
58 return bytes_ == rhs.bytes_; | |
59 } | |
60 | |
61 size_t SupervisedUserSiteList::HostnameHash::hash() const { | |
62 // This just returns the first sizeof(size_t) bytes of |bytes_|. | |
63 return *reinterpret_cast<const size_t*>(bytes_.data()); | |
64 } | |
65 | |
42 void SupervisedUserSiteList::Load(const base::string16& title, | 66 void SupervisedUserSiteList::Load(const base::string16& title, |
43 const base::FilePath& path, | 67 const base::FilePath& path, |
44 const LoadedCallback& callback) { | 68 const LoadedCallback& callback) { |
45 base::PostTaskAndReplyWithResult( | 69 base::PostTaskAndReplyWithResult( |
46 content::BrowserThread::GetBlockingPool() | 70 content::BrowserThread::GetBlockingPool() |
47 ->GetTaskRunnerWithShutdownBehavior( | 71 ->GetTaskRunnerWithShutdownBehavior( |
48 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN) | 72 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN) |
49 .get(), | 73 .get(), |
50 FROM_HERE, base::Bind(&ReadFileOnBlockingThread, path), | 74 FROM_HERE, base::Bind(&ReadFileOnBlockingThread, path), |
51 base::Bind(&SupervisedUserSiteList::OnJsonLoaded, title, path, | 75 base::Bind(&SupervisedUserSiteList::OnJsonLoaded, title, path, |
(...skipping 13 matching lines...) Expand all Loading... | |
65 LOG(ERROR) << "Invalid whitelist entry"; | 89 LOG(ERROR) << "Invalid whitelist entry"; |
66 continue; | 90 continue; |
67 } | 91 } |
68 | 92 |
69 patterns_.push_back(pattern); | 93 patterns_.push_back(pattern); |
70 } | 94 } |
71 } | 95 } |
72 | 96 |
73 if (hostname_hashes) { | 97 if (hostname_hashes) { |
74 for (const base::Value* entry : *hostname_hashes) { | 98 for (const base::Value* entry : *hostname_hashes) { |
75 // |hash| should be a hex-encoded SHA1 hash. | 99 // |hash_str| should be a hex-encoded SHA1 hash string. |
76 std::string hash; | 100 std::string hash_str; |
77 if (!entry->GetAsString(&hash) || hash.size() != 2 * base::kSHA1Length) { | 101 std::vector<uint8> hash_bytes; |
102 if (!entry->GetAsString(&hash_str) || | |
103 hash_str.size() != 2 * base::kSHA1Length || | |
104 !base::HexStringToBytes(hash_str, &hash_bytes)) { | |
78 LOG(ERROR) << "Invalid hostname_hashes entry"; | 105 LOG(ERROR) << "Invalid hostname_hashes entry"; |
79 continue; | 106 continue; |
80 } | 107 } |
81 // TODO(treib): Check that |hash| has only characters from [0-9a-fA-F]. | 108 DCHECK(hash_bytes.size() == base::kSHA1Length); |
Bernhard Bauer
2015/12/03 11:43:21
DCHECK_EQ?
Marc Treib
2015/12/03 12:25:11
Done.
| |
82 // Or just store the raw bytes (from base::HexStringToBytes). | 109 hostname_hashes_.push_back(HostnameHash(hash_bytes)); |
83 hostname_hashes_.push_back(hash); | |
84 } | 110 } |
85 } | 111 } |
86 | 112 |
87 if (patterns_.empty() && hostname_hashes_.empty()) | 113 if (patterns_.empty() && hostname_hashes_.empty()) |
88 LOG(WARNING) << "Site list is empty!"; | 114 LOG(WARNING) << "Site list is empty!"; |
89 } | 115 } |
90 | 116 |
91 SupervisedUserSiteList::~SupervisedUserSiteList() { | 117 SupervisedUserSiteList::~SupervisedUserSiteList() { |
92 } | 118 } |
93 | 119 |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
128 | 154 |
129 base::ListValue* patterns = nullptr; | 155 base::ListValue* patterns = nullptr; |
130 dict->GetList(kWhitelistKey, &patterns); | 156 dict->GetList(kWhitelistKey, &patterns); |
131 | 157 |
132 base::ListValue* hostname_hashes = nullptr; | 158 base::ListValue* hostname_hashes = nullptr; |
133 dict->GetList(kHostnameHashesKey, &hostname_hashes); | 159 dict->GetList(kHostnameHashesKey, &hostname_hashes); |
134 | 160 |
135 callback.Run(make_scoped_refptr(new SupervisedUserSiteList( | 161 callback.Run(make_scoped_refptr(new SupervisedUserSiteList( |
136 title, GURL(entry_point_url), patterns, hostname_hashes))); | 162 title, GURL(entry_point_url), patterns, hostname_hashes))); |
137 } | 163 } |
OLD | NEW |