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