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/experimental/supervised_user_async_url_
checker.h" | 5 #include "chrome/browser/supervised_user/experimental/supervised_user_async_url_
checker.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/callback.h" | 9 #include "base/callback.h" |
10 #include "base/json/json_reader.h" | 10 #include "base/json/json_reader.h" |
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
174 | 174 |
175 SupervisedUserAsyncURLChecker::Check::~Check() {} | 175 SupervisedUserAsyncURLChecker::Check::~Check() {} |
176 | 176 |
177 SupervisedUserAsyncURLChecker::CheckResult::CheckResult( | 177 SupervisedUserAsyncURLChecker::CheckResult::CheckResult( |
178 SupervisedUserURLFilter::FilteringBehavior behavior, bool uncertain) | 178 SupervisedUserURLFilter::FilteringBehavior behavior, bool uncertain) |
179 : behavior(behavior), uncertain(uncertain) { | 179 : behavior(behavior), uncertain(uncertain) { |
180 } | 180 } |
181 | 181 |
182 SupervisedUserAsyncURLChecker::SupervisedUserAsyncURLChecker( | 182 SupervisedUserAsyncURLChecker::SupervisedUserAsyncURLChecker( |
183 URLRequestContextGetter* context, | 183 URLRequestContextGetter* context, |
184 const std::string& cx) | 184 const std::string& cx, |
| 185 const std::string& api_key) |
185 : context_(context), cx_(cx), cache_(kDefaultCacheSize) { | 186 : context_(context), cx_(cx), cache_(kDefaultCacheSize) { |
| 187 SetApiKey(api_key); |
186 } | 188 } |
187 | 189 |
188 SupervisedUserAsyncURLChecker::SupervisedUserAsyncURLChecker( | 190 SupervisedUserAsyncURLChecker::SupervisedUserAsyncURLChecker( |
189 URLRequestContextGetter* context, | 191 URLRequestContextGetter* context, |
190 const std::string& cx, | 192 const std::string& cx, |
| 193 const std::string& api_key, |
191 size_t cache_size) | 194 size_t cache_size) |
192 : context_(context), cx_(cx), cache_(cache_size) { | 195 : context_(context), cx_(cx), cache_(cache_size) { |
| 196 SetApiKey(api_key); |
193 } | 197 } |
194 | 198 |
195 SupervisedUserAsyncURLChecker::~SupervisedUserAsyncURLChecker() {} | 199 SupervisedUserAsyncURLChecker::~SupervisedUserAsyncURLChecker() {} |
196 | 200 |
197 bool SupervisedUserAsyncURLChecker::CheckURL(const GURL& url, | 201 bool SupervisedUserAsyncURLChecker::CheckURL(const GURL& url, |
198 const CheckCallback& callback) { | 202 const CheckCallback& callback) { |
199 // TODO(treib): Hack: For now, allow all Google URLs to save search QPS. If we | 203 // TODO(treib): Hack: For now, allow all Google URLs to save search QPS. If we |
200 // ever remove this, we should find a way to allow at least the NTP. | 204 // ever remove this, we should find a way to allow at least the NTP. |
201 if (google_util::IsGoogleDomainUrl(url, | 205 if (google_util::IsGoogleDomainUrl(url, |
202 google_util::ALLOW_SUBDOMAIN, | 206 google_util::ALLOW_SUBDOMAIN, |
(...skipping 23 matching lines...) Expand all Loading... |
226 // See if we already have a check in progress for this URL. | 230 // See if we already have a check in progress for this URL. |
227 for (Check* check : checks_in_progress_) { | 231 for (Check* check : checks_in_progress_) { |
228 if (check->url == url) { | 232 if (check->url == url) { |
229 DVLOG(1) << "Adding to pending check for " << url.spec(); | 233 DVLOG(1) << "Adding to pending check for " << url.spec(); |
230 check->callbacks.push_back(callback); | 234 check->callbacks.push_back(callback); |
231 return false; | 235 return false; |
232 } | 236 } |
233 } | 237 } |
234 | 238 |
235 DVLOG(1) << "Checking URL " << url; | 239 DVLOG(1) << "Checking URL " << url; |
236 std::string api_key = google_apis::GetSafeSitesAPIKey(); | |
237 scoped_ptr<URLFetcher> fetcher_safe( | 240 scoped_ptr<URLFetcher> fetcher_safe( |
238 CreateFetcher(this, context_, cx_, api_key, url, true)); | 241 CreateFetcher(this, context_, cx_, api_key_, url, true)); |
239 scoped_ptr<URLFetcher> fetcher_unsafe( | 242 scoped_ptr<URLFetcher> fetcher_unsafe( |
240 CreateFetcher(this, context_, cx_, api_key, url, false)); | 243 CreateFetcher(this, context_, cx_, api_key_, url, false)); |
241 fetcher_safe->Start(); | 244 fetcher_safe->Start(); |
242 fetcher_unsafe->Start(); | 245 fetcher_unsafe->Start(); |
243 checks_in_progress_.push_back( | 246 checks_in_progress_.push_back( |
244 new Check(url, fetcher_safe.Pass(), fetcher_unsafe.Pass(), callback)); | 247 new Check(url, fetcher_safe.Pass(), fetcher_unsafe.Pass(), callback)); |
245 return false; | 248 return false; |
246 } | 249 } |
247 | 250 |
| 251 void SupervisedUserAsyncURLChecker::SetApiKey(const std::string& api_key) { |
| 252 api_key_ = api_key.empty() ? google_apis::GetAPIKey() : api_key; |
| 253 } |
| 254 |
248 void SupervisedUserAsyncURLChecker::OnURLFetchComplete( | 255 void SupervisedUserAsyncURLChecker::OnURLFetchComplete( |
249 const net::URLFetcher* source) { | 256 const net::URLFetcher* source) { |
250 ScopedVector<Check>::iterator it = checks_in_progress_.begin(); | 257 ScopedVector<Check>::iterator it = checks_in_progress_.begin(); |
251 bool is_safe_search_request = false; | 258 bool is_safe_search_request = false; |
252 while (it != checks_in_progress_.end()) { | 259 while (it != checks_in_progress_.end()) { |
253 if (source == (*it)->fetcher_safe.get()) { | 260 if (source == (*it)->fetcher_safe.get()) { |
254 is_safe_search_request = true; | 261 is_safe_search_request = true; |
255 (*it)->safe_done = true; | 262 (*it)->safe_done = true; |
256 break; | 263 break; |
257 } else if (source == (*it)->fetcher_unsafe.get()) { | 264 } else if (source == (*it)->fetcher_unsafe.get()) { |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
316 | 323 |
317 UMA_HISTOGRAM_TIMES("ManagedUsers.SafeSitesDelay", | 324 UMA_HISTOGRAM_TIMES("ManagedUsers.SafeSitesDelay", |
318 base::Time::Now() - check->start_time); | 325 base::Time::Now() - check->start_time); |
319 | 326 |
320 cache_.Put(check->url, CheckResult(behavior, uncertain)); | 327 cache_.Put(check->url, CheckResult(behavior, uncertain)); |
321 | 328 |
322 for (size_t i = 0; i < check->callbacks.size(); i++) | 329 for (size_t i = 0; i < check->callbacks.size(); i++) |
323 check->callbacks[i].Run(check->url, behavior, uncertain); | 330 check->callbacks[i].Run(check->url, behavior, uncertain); |
324 checks_in_progress_.erase(it); | 331 checks_in_progress_.erase(it); |
325 } | 332 } |
OLD | NEW |