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/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; | |
14 using base::ListValue; | |
15 using base::Value; | |
16 | |
17 const int kSitelistFormatVersion = 1; | 13 const int kSitelistFormatVersion = 1; |
18 | 14 |
19 const char kCategoriesKey[] = "categories"; | 15 const char kCategoriesKey[] = "categories"; |
20 const char kHostnameHashesKey[] = "hostname_hashes"; | 16 const char kHostnameHashesKey[] = "hostname_hashes"; |
21 const char kNameKey[] = "name"; | 17 const char kNameKey[] = "name"; |
22 const char kSitesKey[] = "sites"; | 18 const char kSitesKey[] = "sites"; |
23 const char kSitelistFormatVersionKey[] = "version"; | 19 const char kSitelistFormatVersionKey[] = "version"; |
24 const char kThumbnailKey[] = "thumbnail"; | 20 const char kThumbnailKey[] = "thumbnail"; |
25 const char kThumbnailUrlKey[] = "thumbnail_url"; | 21 const char kThumbnailUrlKey[] = "thumbnail_url"; |
26 const char kUrlKey[] = "url"; | 22 const char kUrlKey[] = "url"; |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
69 // (via URL patterns or hostname hashes) and the URL in the corresponding Site | 65 // (via URL patterns or hostname hashes) and the URL in the corresponding Site |
70 // struct. | 66 // struct. |
71 void AddWhitelistEntries(const base::DictionaryValue* site_dict, | 67 void AddWhitelistEntries(const base::DictionaryValue* site_dict, |
72 SupervisedUserSiteList::Site* site) { | 68 SupervisedUserSiteList::Site* site) { |
73 std::vector<std::string>* patterns = &site->patterns; | 69 std::vector<std::string>* patterns = &site->patterns; |
74 | 70 |
75 bool found = false; | 71 bool found = false; |
76 const base::ListValue* whitelist = NULL; | 72 const base::ListValue* whitelist = NULL; |
77 if (site_dict->GetList(kWhitelistKey, &whitelist)) { | 73 if (site_dict->GetList(kWhitelistKey, &whitelist)) { |
78 found = true; | 74 found = true; |
79 for (base::ListValue::const_iterator whitelist_it = whitelist->begin(); | 75 for (const base::Value* entry : *whitelist) { |
80 whitelist_it != whitelist->end(); ++whitelist_it) { | |
81 std::string pattern; | 76 std::string pattern; |
82 if (!(*whitelist_it)->GetAsString(&pattern)) { | 77 if (!entry->GetAsString(&pattern)) { |
83 LOG(ERROR) << "Invalid whitelist entry"; | 78 LOG(ERROR) << "Invalid whitelist entry"; |
84 continue; | 79 continue; |
85 } | 80 } |
86 | 81 |
87 patterns->push_back(pattern); | 82 patterns->push_back(pattern); |
88 } | 83 } |
89 } | 84 } |
90 | 85 |
91 std::vector<std::string>* hashes = &site->hostname_hashes; | 86 std::vector<std::string>* hashes = &site->hostname_hashes; |
92 const base::ListValue* hash_list = NULL; | 87 const base::ListValue* hash_list = NULL; |
93 if (site_dict->GetList(kHostnameHashesKey, &hash_list)) { | 88 if (site_dict->GetList(kHostnameHashesKey, &hash_list)) { |
94 found = true; | 89 found = true; |
95 for (base::ListValue::const_iterator hash_list_it = hash_list->begin(); | 90 for (const base::Value* entry : *hash_list) { |
96 hash_list_it != hash_list->end(); ++hash_list_it) { | |
97 std::string hash; | 91 std::string hash; |
98 if (!(*hash_list_it)->GetAsString(&hash)) { | 92 if (!entry->GetAsString(&hash)) { |
99 LOG(ERROR) << "Invalid whitelist entry"; | 93 LOG(ERROR) << "Invalid whitelist entry"; |
100 continue; | 94 continue; |
101 } | 95 } |
102 | 96 |
103 hashes->push_back(hash); | 97 hashes->push_back(hash); |
104 } | 98 } |
105 } | 99 } |
106 | 100 |
107 if (found) | 101 if (found) |
108 return; | 102 return; |
(...skipping 26 matching lines...) Expand all Loading... |
135 SupervisedUserSiteList::SupervisedUserSiteList( | 129 SupervisedUserSiteList::SupervisedUserSiteList( |
136 const std::string& extension_id, | 130 const std::string& extension_id, |
137 const base::FilePath& path) | 131 const base::FilePath& path) |
138 : extension_id_(extension_id), | 132 : extension_id_(extension_id), |
139 path_(path) { | 133 path_(path) { |
140 } | 134 } |
141 | 135 |
142 SupervisedUserSiteList::~SupervisedUserSiteList() { | 136 SupervisedUserSiteList::~SupervisedUserSiteList() { |
143 } | 137 } |
144 | 138 |
145 SupervisedUserSiteList* SupervisedUserSiteList::Clone() { | 139 SupervisedUserSiteList* SupervisedUserSiteList::Clone() const { |
146 return new SupervisedUserSiteList(extension_id_, path_); | 140 return new SupervisedUserSiteList(extension_id_, path_); |
147 } | 141 } |
148 | 142 |
149 // static | 143 // static |
150 void SupervisedUserSiteList::GetCategoryNames( | 144 void SupervisedUserSiteList::GetCategoryNames( |
151 std::vector<base::string16>* categories) { | 145 std::vector<base::string16>* categories) { |
152 // TODO(bauerb): Collect custom categories from extensions. | 146 // TODO(bauerb): Collect custom categories from extensions. |
153 for (size_t i = 0; i < arraysize(g_categories); ++i) { | 147 for (size_t i = 0; i < arraysize(g_categories); ++i) { |
154 categories->push_back(base::ASCIIToUTF16(g_categories[i].name)); | 148 categories->push_back(base::ASCIIToUTF16(g_categories[i].name)); |
155 } | 149 } |
156 } | 150 } |
157 | 151 |
158 void SupervisedUserSiteList::GetSites(std::vector<Site>* sites) { | 152 void SupervisedUserSiteList::GetSites(std::vector<Site>* sites) { |
159 if (!LazyLoad()) | 153 if (!LazyLoad()) |
160 return; | 154 return; |
161 | 155 |
162 for (base::ListValue::iterator entry_it = sites_->begin(); | 156 for (const base::Value* site : *sites_) { |
163 entry_it != sites_->end(); ++entry_it) { | 157 const base::DictionaryValue* entry = NULL; |
164 base::DictionaryValue* entry = NULL; | 158 if (!site->GetAsDictionary(&entry)) { |
165 if (!(*entry_it)->GetAsDictionary(&entry)) { | |
166 LOG(ERROR) << "Entry is invalid"; | 159 LOG(ERROR) << "Entry is invalid"; |
167 continue; | 160 continue; |
168 } | 161 } |
169 | 162 |
170 base::string16 name; | 163 base::string16 name; |
171 entry->GetString(kNameKey, &name); | 164 entry->GetString(kNameKey, &name); |
172 | 165 |
173 // TODO(bauerb): We need to distinguish between "no category assigned" and | 166 // TODO(bauerb): We need to distinguish between "no category assigned" and |
174 // "not on any site list". | 167 // "not on any site list". |
175 int category_id = 0; | 168 int category_id = 0; |
176 const base::ListValue* categories = NULL; | 169 const base::ListValue* categories = NULL; |
177 if (entry->GetList(kCategoriesKey, &categories)) { | 170 if (entry->GetList(kCategoriesKey, &categories)) { |
178 for (base::ListValue::const_iterator it = categories->begin(); | 171 for (const base::Value* category_entry : *categories) { |
179 it != categories->end(); ++it) { | |
180 std::string category; | 172 std::string category; |
181 if (!(*it)->GetAsString(&category)) { | 173 if (!category_entry->GetAsString(&category)) { |
182 LOG(ERROR) << "Invalid category"; | 174 LOG(ERROR) << "Invalid category"; |
183 continue; | 175 continue; |
184 } | 176 } |
185 category_id = GetCategoryId(category); | 177 category_id = GetCategoryId(category); |
186 break; | 178 break; |
187 } | 179 } |
188 } | 180 } |
189 sites->push_back(Site(name, category_id)); | 181 sites->push_back(Site(name, category_id)); |
190 AddWhitelistEntries(entry, &sites->back()); | 182 AddWhitelistEntries(entry, &sites->back()); |
191 } | 183 } |
192 } | 184 } |
193 | 185 |
194 bool SupervisedUserSiteList::LazyLoad() { | 186 bool SupervisedUserSiteList::LazyLoad() { |
195 if (sites_.get()) | 187 if (sites_) |
196 return true; | 188 return true; |
197 | 189 |
198 JSONFileValueSerializer serializer(path_); | 190 JSONFileValueSerializer serializer(path_); |
199 std::string error; | 191 std::string error; |
200 scoped_ptr<base::Value> value(serializer.Deserialize(NULL, &error)); | 192 scoped_ptr<base::Value> value(serializer.Deserialize(NULL, &error)); |
201 if (!value.get()) { | 193 if (!value.get()) { |
202 LOG(ERROR) << "Couldn't load site list " << path_.value() << ": " | 194 LOG(ERROR) << "Couldn't load site list " << path_.value() << ": " |
203 << error; | 195 << error; |
204 return false; | 196 return false; |
205 } | 197 } |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
247 GURL base_url = | 239 GURL base_url = |
248 extensions::Extension::GetBaseURLFromExtensionId(extension_id_); | 240 extensions::Extension::GetBaseURLFromExtensionId(extension_id_); |
249 GURL thumbnail_url = base_url.Resolve(thumbnail); | 241 GURL thumbnail_url = base_url.Resolve(thumbnail); |
250 if (!thumbnail_url.is_valid()) { | 242 if (!thumbnail_url.is_valid()) { |
251 LOG(ERROR) << "Invalid thumbnail"; | 243 LOG(ERROR) << "Invalid thumbnail"; |
252 return; | 244 return; |
253 } | 245 } |
254 | 246 |
255 dest->SetString(kThumbnailUrlKey, thumbnail_url.spec()); | 247 dest->SetString(kThumbnailUrlKey, thumbnail_url.spec()); |
256 } | 248 } |
OLD | NEW |