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 #include <utility> |
8 | 9 |
9 #include "base/callback.h" | 10 #include "base/callback.h" |
10 #include "base/json/json_reader.h" | 11 #include "base/json/json_reader.h" |
11 #include "base/metrics/histogram.h" | 12 #include "base/metrics/histogram.h" |
12 #include "base/stl_util.h" | 13 #include "base/stl_util.h" |
13 #include "base/strings/string_piece.h" | 14 #include "base/strings/string_piece.h" |
14 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
15 #include "base/strings/stringprintf.h" | 16 #include "base/strings/stringprintf.h" |
16 #include "base/time/time.h" | 17 #include "base/time/time.h" |
17 #include "base/values.h" | 18 #include "base/values.h" |
(...skipping 29 matching lines...) Expand all Loading... |
47 scoped_ptr<net::URLFetcher> CreateFetcher(URLFetcherDelegate* delegate, | 48 scoped_ptr<net::URLFetcher> CreateFetcher(URLFetcherDelegate* delegate, |
48 URLRequestContextGetter* context, | 49 URLRequestContextGetter* context, |
49 const std::string& api_key, | 50 const std::string& api_key, |
50 const GURL& url) { | 51 const GURL& url) { |
51 scoped_ptr<net::URLFetcher> fetcher = URLFetcher::Create( | 52 scoped_ptr<net::URLFetcher> fetcher = URLFetcher::Create( |
52 0, GURL(kApiUrl), URLFetcher::POST, delegate); | 53 0, GURL(kApiUrl), URLFetcher::POST, delegate); |
53 fetcher->SetUploadData(kDataContentType, BuildRequestData(api_key, url)); | 54 fetcher->SetUploadData(kDataContentType, BuildRequestData(api_key, url)); |
54 fetcher->SetRequestContext(context); | 55 fetcher->SetRequestContext(context); |
55 fetcher->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | | 56 fetcher->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | |
56 net::LOAD_DO_NOT_SAVE_COOKIES); | 57 net::LOAD_DO_NOT_SAVE_COOKIES); |
57 return fetcher.Pass(); | 58 return fetcher; |
58 } | 59 } |
59 | 60 |
60 // Parses a SafeSearch API |response| and stores the result in |is_porn|. | 61 // Parses a SafeSearch API |response| and stores the result in |is_porn|. |
61 // On errors, returns false and doesn't set |is_porn|. | 62 // On errors, returns false and doesn't set |is_porn|. |
62 bool ParseResponse(const std::string& response, bool* is_porn) { | 63 bool ParseResponse(const std::string& response, bool* is_porn) { |
63 scoped_ptr<base::Value> value = base::JSONReader::Read(response); | 64 scoped_ptr<base::Value> value = base::JSONReader::Read(response); |
64 const base::DictionaryValue* dict = nullptr; | 65 const base::DictionaryValue* dict = nullptr; |
65 if (!value || !value->GetAsDictionary(&dict)) { | 66 if (!value || !value->GetAsDictionary(&dict)) { |
66 DLOG(WARNING) << "ParseResponse failed to parse global dictionary"; | 67 DLOG(WARNING) << "ParseResponse failed to parse global dictionary"; |
67 return false; | 68 return false; |
(...skipping 23 matching lines...) Expand all Loading... |
91 scoped_ptr<net::URLFetcher> fetcher, | 92 scoped_ptr<net::URLFetcher> fetcher, |
92 const CheckCallback& callback); | 93 const CheckCallback& callback); |
93 ~Check(); | 94 ~Check(); |
94 | 95 |
95 GURL url; | 96 GURL url; |
96 scoped_ptr<net::URLFetcher> fetcher; | 97 scoped_ptr<net::URLFetcher> fetcher; |
97 std::vector<CheckCallback> callbacks; | 98 std::vector<CheckCallback> callbacks; |
98 base::TimeTicks start_time; | 99 base::TimeTicks start_time; |
99 }; | 100 }; |
100 | 101 |
101 SupervisedUserAsyncURLChecker::Check::Check( | 102 SupervisedUserAsyncURLChecker::Check::Check(const GURL& url, |
102 const GURL& url, | 103 scoped_ptr<net::URLFetcher> fetcher, |
103 scoped_ptr<net::URLFetcher> fetcher, | 104 const CheckCallback& callback) |
104 const CheckCallback& callback) | |
105 : url(url), | 105 : url(url), |
106 fetcher(fetcher.Pass()), | 106 fetcher(std::move(fetcher)), |
107 callbacks(1, callback), | 107 callbacks(1, callback), |
108 start_time(base::TimeTicks::Now()) { | 108 start_time(base::TimeTicks::Now()) {} |
109 } | |
110 | 109 |
111 SupervisedUserAsyncURLChecker::Check::~Check() {} | 110 SupervisedUserAsyncURLChecker::Check::~Check() {} |
112 | 111 |
113 SupervisedUserAsyncURLChecker::CheckResult::CheckResult( | 112 SupervisedUserAsyncURLChecker::CheckResult::CheckResult( |
114 SupervisedUserURLFilter::FilteringBehavior behavior, | 113 SupervisedUserURLFilter::FilteringBehavior behavior, |
115 bool uncertain) | 114 bool uncertain) |
116 : behavior(behavior), | 115 : behavior(behavior), |
117 uncertain(uncertain), | 116 uncertain(uncertain), |
118 timestamp(base::TimeTicks::Now()) {} | 117 timestamp(base::TimeTicks::Now()) {} |
119 | 118 |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
172 DVLOG(1) << "Adding to pending check for " << url.spec(); | 171 DVLOG(1) << "Adding to pending check for " << url.spec(); |
173 check->callbacks.push_back(callback); | 172 check->callbacks.push_back(callback); |
174 return false; | 173 return false; |
175 } | 174 } |
176 } | 175 } |
177 | 176 |
178 DVLOG(1) << "Checking URL " << url; | 177 DVLOG(1) << "Checking URL " << url; |
179 std::string api_key = google_apis::GetSafeSitesAPIKey(); | 178 std::string api_key = google_apis::GetSafeSitesAPIKey(); |
180 scoped_ptr<URLFetcher> fetcher(CreateFetcher(this, context_, api_key, url)); | 179 scoped_ptr<URLFetcher> fetcher(CreateFetcher(this, context_, api_key, url)); |
181 fetcher->Start(); | 180 fetcher->Start(); |
182 checks_in_progress_.push_back(new Check(url, fetcher.Pass(), callback)); | 181 checks_in_progress_.push_back(new Check(url, std::move(fetcher), callback)); |
183 return false; | 182 return false; |
184 } | 183 } |
185 | 184 |
186 void SupervisedUserAsyncURLChecker::OnURLFetchComplete( | 185 void SupervisedUserAsyncURLChecker::OnURLFetchComplete( |
187 const net::URLFetcher* source) { | 186 const net::URLFetcher* source) { |
188 ScopedVector<Check>::iterator it = checks_in_progress_.begin(); | 187 ScopedVector<Check>::iterator it = checks_in_progress_.begin(); |
189 while (it != checks_in_progress_.end()) { | 188 while (it != checks_in_progress_.end()) { |
190 if (source == (*it)->fetcher.get()) | 189 if (source == (*it)->fetcher.get()) |
191 break; | 190 break; |
192 ++it; | 191 ++it; |
(...skipping 19 matching lines...) Expand all Loading... |
212 | 211 |
213 UMA_HISTOGRAM_TIMES("ManagedUsers.SafeSitesDelay", | 212 UMA_HISTOGRAM_TIMES("ManagedUsers.SafeSitesDelay", |
214 base::TimeTicks::Now() - check->start_time); | 213 base::TimeTicks::Now() - check->start_time); |
215 | 214 |
216 cache_.Put(check->url, CheckResult(behavior, uncertain)); | 215 cache_.Put(check->url, CheckResult(behavior, uncertain)); |
217 | 216 |
218 for (size_t i = 0; i < check->callbacks.size(); i++) | 217 for (size_t i = 0; i < check->callbacks.size(); i++) |
219 check->callbacks[i].Run(check->url, behavior, uncertain); | 218 check->callbacks[i].Run(check->url, behavior, uncertain); |
220 checks_in_progress_.erase(it); | 219 checks_in_progress_.erase(it); |
221 } | 220 } |
OLD | NEW |