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/task_runner_util.h" | 11 #include "base/task_runner_util.h" |
12 #include "base/values.h" | 12 #include "base/values.h" |
13 #include "components/safe_json/safe_json_parser.h" | 13 #include "components/safe_json/safe_json_parser.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 = 1; | 17 const int kSitelistFormatVersion = 1; |
18 | 18 |
19 const char kHostnameHashesKey[] = "hostname_hashes"; | 19 const char kHostnameHashesKey[] = "hostname_hashes"; |
20 const char kNameKey[] = "name"; | 20 const char kNameKey[] = "name"; |
21 const char kSitesKey[] = "sites"; | 21 const char kSitesKey[] = "sites"; |
22 const char kSitelistFormatVersionKey[] = "version"; | 22 const char kSitelistFormatVersionKey[] = "version"; |
23 const char kUrlKey[] = "url"; | 23 const char kUrlKey[] = "url"; |
24 const char kWhitelistKey[] = "whitelist"; | 24 const char kWhitelistKey[] = "whitelist"; |
25 | 25 |
26 namespace { | 26 namespace { |
27 | 27 |
28 bool g_load_in_process = false; | |
29 | |
30 std::string ReadFileOnBlockingThread(const base::FilePath& path) { | 28 std::string ReadFileOnBlockingThread(const base::FilePath& path) { |
31 SCOPED_UMA_HISTOGRAM_TIMER("ManagedUsers.Whitelist.ReadDuration"); | 29 SCOPED_UMA_HISTOGRAM_TIMER("ManagedUsers.Whitelist.ReadDuration"); |
32 std::string contents; | 30 std::string contents; |
33 base::ReadFileToString(path, &contents); | 31 base::ReadFileToString(path, &contents); |
34 return contents; | 32 return contents; |
35 } | 33 } |
36 | 34 |
37 void HandleError(const base::FilePath& path, const std::string& error) { | 35 void HandleError(const base::FilePath& path, const std::string& error) { |
38 LOG(ERROR) << "Couldn't load site list " << path.value() << ": " << error; | 36 LOG(ERROR) << "Couldn't load site list " << path.value() << ": " << error; |
39 } | 37 } |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
107 const LoadedCallback& callback) { | 105 const LoadedCallback& callback) { |
108 base::PostTaskAndReplyWithResult( | 106 base::PostTaskAndReplyWithResult( |
109 content::BrowserThread::GetBlockingPool() | 107 content::BrowserThread::GetBlockingPool() |
110 ->GetTaskRunnerWithShutdownBehavior( | 108 ->GetTaskRunnerWithShutdownBehavior( |
111 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN).get(), | 109 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN).get(), |
112 FROM_HERE, | 110 FROM_HERE, |
113 base::Bind(&ReadFileOnBlockingThread, path), | 111 base::Bind(&ReadFileOnBlockingThread, path), |
114 base::Bind(&SupervisedUserSiteList::ParseJson, path, callback)); | 112 base::Bind(&SupervisedUserSiteList::ParseJson, path, callback)); |
115 } | 113 } |
116 | 114 |
117 // static | |
118 void SupervisedUserSiteList::SetLoadInProcessForTesting(bool in_process) { | |
119 g_load_in_process = in_process; | |
120 } | |
121 | |
122 SupervisedUserSiteList::SupervisedUserSiteList(const base::ListValue& sites) { | 115 SupervisedUserSiteList::SupervisedUserSiteList(const base::ListValue& sites) { |
123 for (const base::Value* site : sites) { | 116 for (const base::Value* site : sites) { |
124 const base::DictionaryValue* entry = nullptr; | 117 const base::DictionaryValue* entry = nullptr; |
125 if (!site->GetAsDictionary(&entry)) { | 118 if (!site->GetAsDictionary(&entry)) { |
126 LOG(ERROR) << "Entry is invalid"; | 119 LOG(ERROR) << "Entry is invalid"; |
127 continue; | 120 continue; |
128 } | 121 } |
129 | 122 |
130 base::string16 name; | 123 base::string16 name; |
131 entry->GetString(kNameKey, &name); | 124 entry->GetString(kNameKey, &name); |
132 sites_.push_back(Site(name)); | 125 sites_.push_back(Site(name)); |
133 AddWhitelistEntries(entry, &sites_.back()); | 126 AddWhitelistEntries(entry, &sites_.back()); |
134 } | 127 } |
135 } | 128 } |
136 | 129 |
137 SupervisedUserSiteList::~SupervisedUserSiteList() { | 130 SupervisedUserSiteList::~SupervisedUserSiteList() { |
138 } | 131 } |
139 | 132 |
140 // static | 133 // static |
141 void SupervisedUserSiteList::ParseJson( | 134 void SupervisedUserSiteList::ParseJson( |
142 const base::FilePath& path, | 135 const base::FilePath& path, |
143 const SupervisedUserSiteList::LoadedCallback& callback, | 136 const SupervisedUserSiteList::LoadedCallback& callback, |
144 const std::string& json) { | 137 const std::string& json) { |
145 if (g_load_in_process) { | 138 // TODO(bauerb): Use JSONSanitizer to sanitize whitelists on installation |
146 JSONFileValueDeserializer deserializer(path); | 139 // instead of using the expensive SafeJsonParser on every load. |
147 std::string error; | 140 safe_json::SafeJsonParser::Parse( |
148 scoped_ptr<base::Value> value(deserializer.Deserialize(nullptr, &error)); | 141 json, base::Bind(&SupervisedUserSiteList::OnJsonParseSucceeded, path, |
149 if (!value) { | 142 base::TimeTicks::Now(), callback), |
150 HandleError(path, error); | 143 base::Bind(&HandleError, path)); |
151 return; | |
152 } | |
153 | |
154 OnJsonParseSucceeded(path, base::TimeTicks(), callback, value.Pass()); | |
155 return; | |
156 } | |
157 | |
158 // TODO(bauerb): Use batch mode to load multiple whitelists? | |
159 scoped_refptr<safe_json::SafeJsonParser> parser( | |
160 new safe_json::SafeJsonParser( | |
161 json, base::Bind(&SupervisedUserSiteList::OnJsonParseSucceeded, path, | |
162 base::TimeTicks::Now(), callback), | |
163 base::Bind(&HandleError, path))); | |
164 parser->Start(); | |
165 } | 144 } |
166 | 145 |
167 // static | 146 // static |
168 void SupervisedUserSiteList::OnJsonParseSucceeded( | 147 void SupervisedUserSiteList::OnJsonParseSucceeded( |
169 const base::FilePath& path, | 148 const base::FilePath& path, |
170 base::TimeTicks start_time, | 149 base::TimeTicks start_time, |
171 const SupervisedUserSiteList::LoadedCallback& callback, | 150 const SupervisedUserSiteList::LoadedCallback& callback, |
172 scoped_ptr<base::Value> value) { | 151 scoped_ptr<base::Value> value) { |
173 if (!start_time.is_null()) { | 152 if (!start_time.is_null()) { |
174 UMA_HISTOGRAM_TIMES("ManagedUsers.Whitelist.JsonParseDuration", | 153 UMA_HISTOGRAM_TIMES("ManagedUsers.Whitelist.JsonParseDuration", |
(...skipping 18 matching lines...) Expand all Loading... |
193 } | 172 } |
194 | 173 |
195 base::ListValue* sites = nullptr; | 174 base::ListValue* sites = nullptr; |
196 if (!dict->GetList(kSitesKey, &sites)) { | 175 if (!dict->GetList(kSitesKey, &sites)) { |
197 LOG(ERROR) << "Site list " << path.value() << " does not contain any sites"; | 176 LOG(ERROR) << "Site list " << path.value() << " does not contain any sites"; |
198 return; | 177 return; |
199 } | 178 } |
200 | 179 |
201 callback.Run(make_scoped_refptr(new SupervisedUserSiteList(*sites))); | 180 callback.Run(make_scoped_refptr(new SupervisedUserSiteList(*sites))); |
202 } | 181 } |
OLD | NEW |