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/supervised_user/supervised_user_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" |
11 #include "extensions/common/extension.h" | 11 #include "extensions/common/extension.h" |
12 | 12 |
13 using base::DictionaryValue; | 13 using base::DictionaryValue; |
14 using base::ListValue; | 14 using base::ListValue; |
15 using base::Value; | 15 using base::Value; |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 base::DictionaryValue* site_dict, | 71 void AddWhitelistEntries(const base::DictionaryValue* site_dict, |
72 ManagedModeSiteList::Site* site) { | 72 SupervisedUserSiteList::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; |
82 if (!(*whitelist_it)->GetAsString(&pattern)) { | 82 if (!(*whitelist_it)->GetAsString(&pattern)) { |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 if (!url.is_valid()) { | 118 if (!url.is_valid()) { |
119 LOG(ERROR) << "URL " << url_str << " is invalid"; | 119 LOG(ERROR) << "URL " << url_str << " is invalid"; |
120 return; | 120 return; |
121 } | 121 } |
122 | 122 |
123 patterns->push_back(url.host()); | 123 patterns->push_back(url.host()); |
124 } | 124 } |
125 | 125 |
126 } // namespace | 126 } // namespace |
127 | 127 |
128 ManagedModeSiteList::Site::Site(const base::string16& name, | 128 SupervisedUserSiteList::Site::Site(const base::string16& name, |
129 int category_id) | 129 int category_id) |
130 : name(name), | 130 : name(name), |
131 category_id(category_id) {} | 131 category_id(category_id) {} |
132 | 132 |
133 ManagedModeSiteList::Site::~Site() {} | 133 SupervisedUserSiteList::Site::~Site() {} |
134 | 134 |
135 ManagedModeSiteList::ManagedModeSiteList( | 135 SupervisedUserSiteList::SupervisedUserSiteList( |
136 const std::string& extension_id, | 136 const std::string& extension_id, |
137 const base::FilePath& path) | 137 const base::FilePath& path) |
138 : extension_id_(extension_id), | 138 : extension_id_(extension_id), |
139 path_(path) { | 139 path_(path) { |
140 } | 140 } |
141 | 141 |
142 ManagedModeSiteList::~ManagedModeSiteList() { | 142 SupervisedUserSiteList::~SupervisedUserSiteList() { |
143 } | 143 } |
144 | 144 |
145 ManagedModeSiteList* ManagedModeSiteList::Clone() { | 145 SupervisedUserSiteList* SupervisedUserSiteList::Clone() { |
146 return new ManagedModeSiteList(extension_id_, path_); | 146 return new SupervisedUserSiteList(extension_id_, path_); |
147 } | 147 } |
148 | 148 |
149 // static | 149 // static |
150 void ManagedModeSiteList::GetCategoryNames( | 150 void SupervisedUserSiteList::GetCategoryNames( |
151 std::vector<base::string16>* categories) { | 151 std::vector<base::string16>* categories) { |
152 // TODO(bauerb): Collect custom categories from extensions. | 152 // TODO(bauerb): Collect custom categories from extensions. |
153 for (size_t i = 0; i < arraysize(g_categories); ++i) { | 153 for (size_t i = 0; i < arraysize(g_categories); ++i) { |
154 categories->push_back(base::ASCIIToUTF16(g_categories[i].name)); | 154 categories->push_back(base::ASCIIToUTF16(g_categories[i].name)); |
155 } | 155 } |
156 } | 156 } |
157 | 157 |
158 void ManagedModeSiteList::GetSites(std::vector<Site>* sites) { | 158 void SupervisedUserSiteList::GetSites(std::vector<Site>* sites) { |
159 if (!LazyLoad()) | 159 if (!LazyLoad()) |
160 return; | 160 return; |
161 | 161 |
162 for (base::ListValue::iterator entry_it = sites_->begin(); | 162 for (base::ListValue::iterator entry_it = sites_->begin(); |
163 entry_it != sites_->end(); ++entry_it) { | 163 entry_it != sites_->end(); ++entry_it) { |
164 base::DictionaryValue* entry = NULL; | 164 base::DictionaryValue* entry = NULL; |
165 if (!(*entry_it)->GetAsDictionary(&entry)) { | 165 if (!(*entry_it)->GetAsDictionary(&entry)) { |
166 LOG(ERROR) << "Entry is invalid"; | 166 LOG(ERROR) << "Entry is invalid"; |
167 continue; | 167 continue; |
168 } | 168 } |
(...skipping 15 matching lines...) Expand all Loading... |
184 } | 184 } |
185 category_id = GetCategoryId(category); | 185 category_id = GetCategoryId(category); |
186 break; | 186 break; |
187 } | 187 } |
188 } | 188 } |
189 sites->push_back(Site(name, category_id)); | 189 sites->push_back(Site(name, category_id)); |
190 AddWhitelistEntries(entry, &sites->back()); | 190 AddWhitelistEntries(entry, &sites->back()); |
191 } | 191 } |
192 } | 192 } |
193 | 193 |
194 bool ManagedModeSiteList::LazyLoad() { | 194 bool SupervisedUserSiteList::LazyLoad() { |
195 if (sites_.get()) | 195 if (sites_.get()) |
196 return true; | 196 return true; |
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; |
(...skipping 20 matching lines...) Expand all Loading... |
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 base::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 base::DictionaryValue* source, | 235 void SupervisedUserSiteList::CopyThumbnailUrl( |
236 base::DictionaryValue* dest) { | 236 const base::DictionaryValue* source, |
| 237 base::DictionaryValue* dest) { |
237 if (!source->HasKey(kThumbnailKey)) | 238 if (!source->HasKey(kThumbnailKey)) |
238 return; | 239 return; |
239 | 240 |
240 std::string thumbnail; | 241 std::string thumbnail; |
241 if (!source->GetString(kThumbnailKey, &thumbnail)) { | 242 if (!source->GetString(kThumbnailKey, &thumbnail)) { |
242 LOG(ERROR) << "Invalid thumbnail"; | 243 LOG(ERROR) << "Invalid thumbnail"; |
243 return; | 244 return; |
244 } | 245 } |
245 | 246 |
246 GURL base_url = | 247 GURL base_url = |
247 extensions::Extension::GetBaseURLFromExtensionId(extension_id_); | 248 extensions::Extension::GetBaseURLFromExtensionId(extension_id_); |
248 GURL thumbnail_url = base_url.Resolve(thumbnail); | 249 GURL thumbnail_url = base_url.Resolve(thumbnail); |
249 if (!thumbnail_url.is_valid()) { | 250 if (!thumbnail_url.is_valid()) { |
250 LOG(ERROR) << "Invalid thumbnail"; | 251 LOG(ERROR) << "Invalid thumbnail"; |
251 return; | 252 return; |
252 } | 253 } |
253 | 254 |
254 dest->SetString(kThumbnailUrlKey, thumbnail_url.spec()); | 255 dest->SetString(kThumbnailUrlKey, thumbnail_url.spec()); |
255 } | 256 } |
OLD | NEW |