OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/managed_mode/managed_mode_site_list.h" | 5 #include "chrome/browser/managed_mode/managed_mode_site_list.h" |
6 | 6 |
7 #include "base/json/json_file_value_serializer.h" | 7 #include "base/json/json_file_value_serializer.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
10 #include "base/values.h" | 10 #include "base/values.h" |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
61 for (size_t i = 0; i < arraysize(g_categories); ++i) { | 61 for (size_t i = 0; i < arraysize(g_categories); ++i) { |
62 if (g_categories[i].identifier == category) | 62 if (g_categories[i].identifier == category) |
63 return i + 1; | 63 return i + 1; |
64 } | 64 } |
65 return 0; | 65 return 0; |
66 } | 66 } |
67 | 67 |
68 // Takes a DictionaryValue entry from the JSON file and fills the whitelist | 68 // Takes a DictionaryValue entry from the JSON file and fills the whitelist |
69 // (via URL patterns or hostname hashes) and the URL in the corresponding Site | 69 // (via URL patterns or hostname hashes) and the URL in the corresponding Site |
70 // struct. | 70 // struct. |
71 void AddWhitelistEntries(const DictionaryValue* site_dict, | 71 void AddWhitelistEntries(const base::DictionaryValue* site_dict, |
72 ManagedModeSiteList::Site* site) { | 72 ManagedModeSiteList::Site* site) { |
73 std::vector<std::string>* patterns = &site->patterns; | 73 std::vector<std::string>* patterns = &site->patterns; |
74 | 74 |
75 bool found = false; | 75 bool found = false; |
76 const base::ListValue* whitelist = NULL; | 76 const base::ListValue* whitelist = NULL; |
77 if (site_dict->GetList(kWhitelistKey, &whitelist)) { | 77 if (site_dict->GetList(kWhitelistKey, &whitelist)) { |
78 found = true; | 78 found = true; |
79 for (base::ListValue::const_iterator whitelist_it = whitelist->begin(); | 79 for (base::ListValue::const_iterator whitelist_it = whitelist->begin(); |
80 whitelist_it != whitelist->end(); ++whitelist_it) { | 80 whitelist_it != whitelist->end(); ++whitelist_it) { |
81 std::string pattern; | 81 std::string pattern; |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
166 LOG(ERROR) << "Entry is invalid"; | 166 LOG(ERROR) << "Entry is invalid"; |
167 continue; | 167 continue; |
168 } | 168 } |
169 | 169 |
170 base::string16 name; | 170 base::string16 name; |
171 entry->GetString(kNameKey, &name); | 171 entry->GetString(kNameKey, &name); |
172 | 172 |
173 // TODO(bauerb): We need to distinguish between "no category assigned" and | 173 // TODO(bauerb): We need to distinguish between "no category assigned" and |
174 // "not on any site list". | 174 // "not on any site list". |
175 int category_id = 0; | 175 int category_id = 0; |
176 const ListValue* categories = NULL; | 176 const base::ListValue* categories = NULL; |
177 if (entry->GetList(kCategoriesKey, &categories)) { | 177 if (entry->GetList(kCategoriesKey, &categories)) { |
178 for (base::ListValue::const_iterator it = categories->begin(); | 178 for (base::ListValue::const_iterator it = categories->begin(); |
179 it != categories->end(); ++it) { | 179 it != categories->end(); ++it) { |
180 std::string category; | 180 std::string category; |
181 if (!(*it)->GetAsString(&category)) { | 181 if (!(*it)->GetAsString(&category)) { |
182 LOG(ERROR) << "Invalid category"; | 182 LOG(ERROR) << "Invalid category"; |
183 continue; | 183 continue; |
184 } | 184 } |
185 category_id = GetCategoryId(category); | 185 category_id = GetCategoryId(category); |
186 break; | 186 break; |
(...skipping 10 matching lines...) Expand all Loading... |
197 | 197 |
198 JSONFileValueSerializer serializer(path_); | 198 JSONFileValueSerializer serializer(path_); |
199 std::string error; | 199 std::string error; |
200 scoped_ptr<base::Value> value(serializer.Deserialize(NULL, &error)); | 200 scoped_ptr<base::Value> value(serializer.Deserialize(NULL, &error)); |
201 if (!value.get()) { | 201 if (!value.get()) { |
202 LOG(ERROR) << "Couldn't load site list " << path_.value() << ": " | 202 LOG(ERROR) << "Couldn't load site list " << path_.value() << ": " |
203 << error; | 203 << error; |
204 return false; | 204 return false; |
205 } | 205 } |
206 | 206 |
207 DictionaryValue* dict = NULL; | 207 base::DictionaryValue* dict = NULL; |
208 if (!value->GetAsDictionary(&dict)) { | 208 if (!value->GetAsDictionary(&dict)) { |
209 LOG(ERROR) << "Site list " << path_.value() << " is invalid"; | 209 LOG(ERROR) << "Site list " << path_.value() << " is invalid"; |
210 return false; | 210 return false; |
211 } | 211 } |
212 | 212 |
213 int version = 0; | 213 int version = 0; |
214 if (!dict->GetInteger(kSitelistFormatVersionKey, &version)) { | 214 if (!dict->GetInteger(kSitelistFormatVersionKey, &version)) { |
215 LOG(ERROR) << "Site list " << path_.value() << " has invalid version"; | 215 LOG(ERROR) << "Site list " << path_.value() << " has invalid version"; |
216 return false; | 216 return false; |
217 } | 217 } |
218 | 218 |
219 if (version > kSitelistFormatVersion) { | 219 if (version > kSitelistFormatVersion) { |
220 LOG(ERROR) << "Site list " << path_.value() << " has a too new format"; | 220 LOG(ERROR) << "Site list " << path_.value() << " has a too new format"; |
221 return false; | 221 return false; |
222 } | 222 } |
223 | 223 |
224 ListValue* sites = NULL; | 224 base::ListValue* sites = NULL; |
225 if (dict->GetList(kSitesKey, &sites)) | 225 if (dict->GetList(kSitesKey, &sites)) |
226 sites_.reset(sites->DeepCopy()); | 226 sites_.reset(sites->DeepCopy()); |
227 | 227 |
228 DictionaryValue* categories = NULL; | 228 base::DictionaryValue* categories = NULL; |
229 if (dict->GetDictionary(kCategoriesKey, &categories)) | 229 if (dict->GetDictionary(kCategoriesKey, &categories)) |
230 categories_.reset(categories->DeepCopy()); | 230 categories_.reset(categories->DeepCopy()); |
231 | 231 |
232 return true; | 232 return true; |
233 } | 233 } |
234 | 234 |
235 void ManagedModeSiteList::CopyThumbnailUrl(const DictionaryValue* source, | 235 void ManagedModeSiteList::CopyThumbnailUrl(const base::DictionaryValue* source, |
236 DictionaryValue* dest) { | 236 base::DictionaryValue* dest) { |
237 if (!source->HasKey(kThumbnailKey)) | 237 if (!source->HasKey(kThumbnailKey)) |
238 return; | 238 return; |
239 | 239 |
240 std::string thumbnail; | 240 std::string thumbnail; |
241 if (!source->GetString(kThumbnailKey, &thumbnail)) { | 241 if (!source->GetString(kThumbnailKey, &thumbnail)) { |
242 LOG(ERROR) << "Invalid thumbnail"; | 242 LOG(ERROR) << "Invalid thumbnail"; |
243 return; | 243 return; |
244 } | 244 } |
245 | 245 |
246 GURL base_url = | 246 GURL base_url = |
247 extensions::Extension::GetBaseURLFromExtensionId(extension_id_); | 247 extensions::Extension::GetBaseURLFromExtensionId(extension_id_); |
248 GURL thumbnail_url = base_url.Resolve(thumbnail); | 248 GURL thumbnail_url = base_url.Resolve(thumbnail); |
249 if (!thumbnail_url.is_valid()) { | 249 if (!thumbnail_url.is_valid()) { |
250 LOG(ERROR) << "Invalid thumbnail"; | 250 LOG(ERROR) << "Invalid thumbnail"; |
251 return; | 251 return; |
252 } | 252 } |
253 | 253 |
254 dest->SetString(kThumbnailUrlKey, thumbnail_url.spec()); | 254 dest->SetString(kThumbnailUrlKey, thumbnail_url.spec()); |
255 } | 255 } |
OLD | NEW |