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/safe_search_api/safe_search_url_checker.h" | 5 #include "chrome/browser/safe_search_api/safe_search_url_checker.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/callback.h" | 10 #include "base/callback.h" |
11 #include "base/json/json_reader.h" | 11 #include "base/json/json_reader.h" |
| 12 #include "base/memory/ptr_util.h" |
12 #include "base/metrics/histogram_macros.h" | 13 #include "base/metrics/histogram_macros.h" |
13 #include "base/stl_util.h" | 14 #include "base/stl_util.h" |
14 #include "base/strings/string_piece.h" | 15 #include "base/strings/string_piece.h" |
15 #include "base/strings/string_util.h" | 16 #include "base/strings/string_util.h" |
16 #include "base/strings/stringprintf.h" | 17 #include "base/strings/stringprintf.h" |
17 #include "base/time/time.h" | 18 #include "base/time/time.h" |
18 #include "base/values.h" | 19 #include "base/values.h" |
19 #include "components/google/core/browser/google_util.h" | 20 #include "components/google/core/browser/google_util.h" |
20 #include "google_apis/google_api_keys.h" | 21 #include "google_apis/google_api_keys.h" |
21 #include "net/base/escape.h" | 22 #include "net/base/escape.h" |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
160 << (result.classification == Classification::UNSAFE ? "NOT" : "") | 161 << (result.classification == Classification::UNSAFE ? "NOT" : "") |
161 << " safe; certain: " << !result.uncertain; | 162 << " safe; certain: " << !result.uncertain; |
162 callback.Run(url, result.classification, result.uncertain); | 163 callback.Run(url, result.classification, result.uncertain); |
163 return true; | 164 return true; |
164 } | 165 } |
165 DVLOG(1) << "Outdated cache entry for " << url.spec() << ", purging"; | 166 DVLOG(1) << "Outdated cache entry for " << url.spec() << ", purging"; |
166 cache_.Erase(cache_it); | 167 cache_.Erase(cache_it); |
167 } | 168 } |
168 | 169 |
169 // See if we already have a check in progress for this URL. | 170 // See if we already have a check in progress for this URL. |
170 for (Check* check : checks_in_progress_) { | 171 for (const auto& check : checks_in_progress_) { |
171 if (check->url == url) { | 172 if (check->url == url) { |
172 DVLOG(1) << "Adding to pending check for " << url.spec(); | 173 DVLOG(1) << "Adding to pending check for " << url.spec(); |
173 check->callbacks.push_back(callback); | 174 check->callbacks.push_back(callback); |
174 return false; | 175 return false; |
175 } | 176 } |
176 } | 177 } |
177 | 178 |
178 DVLOG(1) << "Checking URL " << url; | 179 DVLOG(1) << "Checking URL " << url; |
179 std::string api_key = google_apis::GetAPIKey(); | 180 std::string api_key = google_apis::GetAPIKey(); |
180 std::unique_ptr<URLFetcher> fetcher( | 181 std::unique_ptr<URLFetcher> fetcher( |
181 CreateFetcher(this, context_, api_key, url, traffic_annotation_)); | 182 CreateFetcher(this, context_, api_key, url, traffic_annotation_)); |
182 fetcher->Start(); | 183 fetcher->Start(); |
183 checks_in_progress_.push_back(new Check(url, std::move(fetcher), callback)); | 184 checks_in_progress_.push_back( |
| 185 base::MakeUnique<Check>(url, std::move(fetcher), callback)); |
184 return false; | 186 return false; |
185 } | 187 } |
186 | 188 |
187 void SafeSearchURLChecker::OnURLFetchComplete(const net::URLFetcher* source) { | 189 void SafeSearchURLChecker::OnURLFetchComplete(const net::URLFetcher* source) { |
188 ScopedVector<Check>::iterator it = checks_in_progress_.begin(); | 190 auto it = checks_in_progress_.begin(); |
189 while (it != checks_in_progress_.end()) { | 191 while (it != checks_in_progress_.end()) { |
190 if (source == (*it)->fetcher.get()) | 192 if (source == (*it)->fetcher.get()) |
191 break; | 193 break; |
192 ++it; | 194 ++it; |
193 } | 195 } |
194 DCHECK(it != checks_in_progress_.end()); | 196 DCHECK(it != checks_in_progress_.end()); |
195 Check* check = *it; | 197 Check* check = it->get(); |
196 | 198 |
197 const URLRequestStatus& status = source->GetStatus(); | 199 const URLRequestStatus& status = source->GetStatus(); |
198 if (!status.is_success()) { | 200 if (!status.is_success()) { |
199 DLOG(WARNING) << "URL request failed! Letting through..."; | 201 DLOG(WARNING) << "URL request failed! Letting through..."; |
200 for (size_t i = 0; i < check->callbacks.size(); i++) | 202 for (size_t i = 0; i < check->callbacks.size(); i++) |
201 check->callbacks[i].Run(check->url, Classification::SAFE, true); | 203 check->callbacks[i].Run(check->url, Classification::SAFE, true); |
202 checks_in_progress_.erase(it); | 204 checks_in_progress_.erase(it); |
203 return; | 205 return; |
204 } | 206 } |
205 | 207 |
206 std::string response_body; | 208 std::string response_body; |
207 source->GetResponseAsString(&response_body); | 209 source->GetResponseAsString(&response_body); |
208 bool is_porn = false; | 210 bool is_porn = false; |
209 bool uncertain = !ParseResponse(response_body, &is_porn); | 211 bool uncertain = !ParseResponse(response_body, &is_porn); |
210 Classification classification = | 212 Classification classification = |
211 is_porn ? Classification::UNSAFE : Classification::SAFE; | 213 is_porn ? Classification::UNSAFE : Classification::SAFE; |
212 | 214 |
213 // TODO(msramek): Consider moving this to SupervisedUserResourceThrottle. | 215 // TODO(msramek): Consider moving this to SupervisedUserResourceThrottle. |
214 UMA_HISTOGRAM_TIMES("ManagedUsers.SafeSitesDelay", | 216 UMA_HISTOGRAM_TIMES("ManagedUsers.SafeSitesDelay", |
215 base::TimeTicks::Now() - check->start_time); | 217 base::TimeTicks::Now() - check->start_time); |
216 | 218 |
217 cache_.Put(check->url, CheckResult(classification, uncertain)); | 219 cache_.Put(check->url, CheckResult(classification, uncertain)); |
218 | 220 |
219 for (size_t i = 0; i < check->callbacks.size(); i++) | 221 for (size_t i = 0; i < check->callbacks.size(); i++) |
220 check->callbacks[i].Run(check->url, classification, uncertain); | 222 check->callbacks[i].Run(check->url, classification, uncertain); |
221 checks_in_progress_.erase(it); | 223 checks_in_progress_.erase(it); |
222 } | 224 } |
OLD | NEW |