Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(210)

Side by Side Diff: chrome/browser/supervised_user/supervised_user_site_list.cc

Issue 615493005: c/b/supervised_user: Use range-based for where appropriate. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
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 SupervisedUserSiteList::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 (const auto* entry : *whitelist) {
80 whitelist_it != whitelist->end(); ++whitelist_it) {
81 std::string pattern; 80 std::string pattern;
82 if (!(*whitelist_it)->GetAsString(&pattern)) { 81 if (!entry->GetAsString(&pattern)) {
83 LOG(ERROR) << "Invalid whitelist entry"; 82 LOG(ERROR) << "Invalid whitelist entry";
84 continue; 83 continue;
85 } 84 }
86 85
87 patterns->push_back(pattern); 86 patterns->push_back(pattern);
88 } 87 }
89 } 88 }
90 89
91 std::vector<std::string>* hashes = &site->hostname_hashes; 90 std::vector<std::string>* hashes = &site->hostname_hashes;
92 const base::ListValue* hash_list = NULL; 91 const base::ListValue* hash_list = NULL;
93 if (site_dict->GetList(kHostnameHashesKey, &hash_list)) { 92 if (site_dict->GetList(kHostnameHashesKey, &hash_list)) {
94 found = true; 93 found = true;
95 for (base::ListValue::const_iterator hash_list_it = hash_list->begin(); 94 for (const auto* entry : *hash_list) {
96 hash_list_it != hash_list->end(); ++hash_list_it) {
97 std::string hash; 95 std::string hash;
98 if (!(*hash_list_it)->GetAsString(&hash)) { 96 if (!entry->GetAsString(&hash)) {
99 LOG(ERROR) << "Invalid whitelist entry"; 97 LOG(ERROR) << "Invalid whitelist entry";
100 continue; 98 continue;
101 } 99 }
102 100
103 hashes->push_back(hash); 101 hashes->push_back(hash);
104 } 102 }
105 } 103 }
106 104
107 if (found) 105 if (found)
108 return; 106 return;
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
152 // TODO(bauerb): Collect custom categories from extensions. 150 // TODO(bauerb): Collect custom categories from extensions.
153 for (size_t i = 0; i < arraysize(g_categories); ++i) { 151 for (size_t i = 0; i < arraysize(g_categories); ++i) {
154 categories->push_back(base::ASCIIToUTF16(g_categories[i].name)); 152 categories->push_back(base::ASCIIToUTF16(g_categories[i].name));
155 } 153 }
156 } 154 }
157 155
158 void SupervisedUserSiteList::GetSites(std::vector<Site>* sites) { 156 void SupervisedUserSiteList::GetSites(std::vector<Site>* sites) {
159 if (!LazyLoad()) 157 if (!LazyLoad())
160 return; 158 return;
161 159
162 for (base::ListValue::iterator entry_it = sites_->begin(); 160 for (const auto* site : *sites_) {
163 entry_it != sites_->end(); ++entry_it) { 161 const base::DictionaryValue* entry = NULL;
164 base::DictionaryValue* entry = NULL; 162 if (!site->GetAsDictionary(&entry)) {
165 if (!(*entry_it)->GetAsDictionary(&entry)) {
166 LOG(ERROR) << "Entry is invalid"; 163 LOG(ERROR) << "Entry is invalid";
167 continue; 164 continue;
168 } 165 }
169 166
170 base::string16 name; 167 base::string16 name;
171 entry->GetString(kNameKey, &name); 168 entry->GetString(kNameKey, &name);
172 169
173 // TODO(bauerb): We need to distinguish between "no category assigned" and 170 // TODO(bauerb): We need to distinguish between "no category assigned" and
174 // "not on any site list". 171 // "not on any site list".
175 int category_id = 0; 172 int category_id = 0;
176 const base::ListValue* categories = NULL; 173 const base::ListValue* categories = NULL;
177 if (entry->GetList(kCategoriesKey, &categories)) { 174 if (entry->GetList(kCategoriesKey, &categories)) {
178 for (base::ListValue::const_iterator it = categories->begin(); 175 for (const auto* category_entry : *categories) {
179 it != categories->end(); ++it) {
180 std::string category; 176 std::string category;
181 if (!(*it)->GetAsString(&category)) { 177 if (!category_entry->GetAsString(&category)) {
182 LOG(ERROR) << "Invalid category"; 178 LOG(ERROR) << "Invalid category";
183 continue; 179 continue;
184 } 180 }
185 category_id = GetCategoryId(category); 181 category_id = GetCategoryId(category);
186 break; 182 break;
187 } 183 }
188 } 184 }
189 sites->push_back(Site(name, category_id)); 185 sites->push_back(Site(name, category_id));
190 AddWhitelistEntries(entry, &sites->back()); 186 AddWhitelistEntries(entry, &sites->back());
191 } 187 }
192 } 188 }
193 189
194 bool SupervisedUserSiteList::LazyLoad() { 190 bool SupervisedUserSiteList::LazyLoad() {
195 if (sites_.get()) 191 if (sites_)
196 return true; 192 return true;
197 193
198 JSONFileValueSerializer serializer(path_); 194 JSONFileValueSerializer serializer(path_);
199 std::string error; 195 std::string error;
200 scoped_ptr<base::Value> value(serializer.Deserialize(NULL, &error)); 196 scoped_ptr<base::Value> value(serializer.Deserialize(NULL, &error));
201 if (!value.get()) { 197 if (!value.get()) {
202 LOG(ERROR) << "Couldn't load site list " << path_.value() << ": " 198 LOG(ERROR) << "Couldn't load site list " << path_.value() << ": "
203 << error; 199 << error;
204 return false; 200 return false;
205 } 201 }
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
247 GURL base_url = 243 GURL base_url =
248 extensions::Extension::GetBaseURLFromExtensionId(extension_id_); 244 extensions::Extension::GetBaseURLFromExtensionId(extension_id_);
249 GURL thumbnail_url = base_url.Resolve(thumbnail); 245 GURL thumbnail_url = base_url.Resolve(thumbnail);
250 if (!thumbnail_url.is_valid()) { 246 if (!thumbnail_url.is_valid()) {
251 LOG(ERROR) << "Invalid thumbnail"; 247 LOG(ERROR) << "Invalid thumbnail";
252 return; 248 return;
253 } 249 }
254 250
255 dest->SetString(kThumbnailUrlKey, thumbnail_url.spec()); 251 dest->SetString(kThumbnailUrlKey, thumbnail_url.spec());
256 } 252 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698