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

Side by Side Diff: chrome/browser/safe_search_api/safe_search_url_checker.cc

Issue 2743923002: Network traffic annotation added to safe_search_url_checker. (Closed)
Patch Set: Annotation updated. Created 3 years, 9 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/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"
(...skipping 27 matching lines...) Expand all
38 const size_t kDefaultCacheSize = 1000; 38 const size_t kDefaultCacheSize = 1000;
39 const size_t kDefaultCacheTimeoutSeconds = 3600; 39 const size_t kDefaultCacheTimeoutSeconds = 3600;
40 40
41 // Builds the POST data for SafeSearch API requests. 41 // Builds the POST data for SafeSearch API requests.
42 std::string BuildRequestData(const std::string& api_key, const GURL& url) { 42 std::string BuildRequestData(const std::string& api_key, const GURL& url) {
43 std::string query = net::EscapeQueryParamValue(url.spec(), true); 43 std::string query = net::EscapeQueryParamValue(url.spec(), true);
44 return base::StringPrintf(kDataFormat, api_key.c_str(), query.c_str()); 44 return base::StringPrintf(kDataFormat, api_key.c_str(), query.c_str());
45 } 45 }
46 46
47 // Creates a URLFetcher to call the SafeSearch API for |url|. 47 // Creates a URLFetcher to call the SafeSearch API for |url|.
48 std::unique_ptr<net::URLFetcher> CreateFetcher(URLFetcherDelegate* delegate, 48 std::unique_ptr<net::URLFetcher> CreateFetcher(
49 URLRequestContextGetter* context, 49 URLFetcherDelegate* delegate,
50 const std::string& api_key, 50 URLRequestContextGetter* context,
51 const GURL& url) { 51 const std::string& api_key,
52 std::unique_ptr<net::URLFetcher> fetcher = 52 const GURL& url,
53 URLFetcher::Create(0, GURL(kApiUrl), URLFetcher::POST, delegate); 53 const net::NetworkTrafficAnnotationTag& traffic_annotation) {
54 std::unique_ptr<net::URLFetcher> fetcher = URLFetcher::Create(
55 0, GURL(kApiUrl), URLFetcher::POST, delegate, traffic_annotation);
54 fetcher->SetUploadData(kDataContentType, BuildRequestData(api_key, url)); 56 fetcher->SetUploadData(kDataContentType, BuildRequestData(api_key, url));
55 fetcher->SetRequestContext(context); 57 fetcher->SetRequestContext(context);
56 fetcher->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | 58 fetcher->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES |
57 net::LOAD_DO_NOT_SAVE_COOKIES); 59 net::LOAD_DO_NOT_SAVE_COOKIES);
58 return fetcher; 60 return fetcher;
59 } 61 }
60 62
61 // Parses a SafeSearch API |response| and stores the result in |is_porn|. 63 // Parses a SafeSearch API |response| and stores the result in |is_porn|.
62 // On errors, returns false and doesn't set |is_porn|. 64 // On errors, returns false and doesn't set |is_porn|.
63 bool ParseResponse(const std::string& response, bool* is_porn) { 65 bool ParseResponse(const std::string& response, bool* is_porn) {
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
108 start_time(base::TimeTicks::Now()) {} 110 start_time(base::TimeTicks::Now()) {}
109 111
110 SafeSearchURLChecker::Check::~Check() {} 112 SafeSearchURLChecker::Check::~Check() {}
111 113
112 SafeSearchURLChecker::CheckResult::CheckResult(Classification classification, 114 SafeSearchURLChecker::CheckResult::CheckResult(Classification classification,
113 bool uncertain) 115 bool uncertain)
114 : classification(classification), 116 : classification(classification),
115 uncertain(uncertain), 117 uncertain(uncertain),
116 timestamp(base::TimeTicks::Now()) {} 118 timestamp(base::TimeTicks::Now()) {}
117 119
118 SafeSearchURLChecker::SafeSearchURLChecker(URLRequestContextGetter* context) 120 SafeSearchURLChecker::SafeSearchURLChecker(
119 : SafeSearchURLChecker(context, kDefaultCacheSize) {} 121 URLRequestContextGetter* context,
122 const net::NetworkTrafficAnnotationTag& traffic_annotation)
123 : SafeSearchURLChecker(context, traffic_annotation, kDefaultCacheSize) {}
120 124
121 SafeSearchURLChecker::SafeSearchURLChecker(URLRequestContextGetter* context, 125 SafeSearchURLChecker::SafeSearchURLChecker(
122 size_t cache_size) 126 URLRequestContextGetter* context,
127 const net::NetworkTrafficAnnotationTag& traffic_annotation,
128 size_t cache_size)
123 : context_(context), 129 : context_(context),
130 traffic_annotation_(traffic_annotation),
124 cache_(cache_size), 131 cache_(cache_size),
125 cache_timeout_( 132 cache_timeout_(
126 base::TimeDelta::FromSeconds(kDefaultCacheTimeoutSeconds)) {} 133 base::TimeDelta::FromSeconds(kDefaultCacheTimeoutSeconds)) {}
127 134
128 SafeSearchURLChecker::~SafeSearchURLChecker() {} 135 SafeSearchURLChecker::~SafeSearchURLChecker() {}
129 136
130 bool SafeSearchURLChecker::CheckURL(const GURL& url, 137 bool SafeSearchURLChecker::CheckURL(const GURL& url,
131 const CheckCallback& callback) { 138 const CheckCallback& callback) {
132 // TODO(treib): Hack: For now, allow all Google URLs to save QPS. If we ever 139 // TODO(treib): Hack: For now, allow all Google URLs to save QPS. If we ever
133 // remove this, we should find a way to allow at least the NTP. 140 // remove this, we should find a way to allow at least the NTP.
(...skipping 30 matching lines...) Expand all
164 if (check->url == url) { 171 if (check->url == url) {
165 DVLOG(1) << "Adding to pending check for " << url.spec(); 172 DVLOG(1) << "Adding to pending check for " << url.spec();
166 check->callbacks.push_back(callback); 173 check->callbacks.push_back(callback);
167 return false; 174 return false;
168 } 175 }
169 } 176 }
170 177
171 DVLOG(1) << "Checking URL " << url; 178 DVLOG(1) << "Checking URL " << url;
172 std::string api_key = google_apis::GetAPIKey(); 179 std::string api_key = google_apis::GetAPIKey();
173 std::unique_ptr<URLFetcher> fetcher( 180 std::unique_ptr<URLFetcher> fetcher(
174 CreateFetcher(this, context_, api_key, url)); 181 CreateFetcher(this, context_, api_key, url, traffic_annotation_));
175 fetcher->Start(); 182 fetcher->Start();
176 checks_in_progress_.push_back(new Check(url, std::move(fetcher), callback)); 183 checks_in_progress_.push_back(new Check(url, std::move(fetcher), callback));
177 return false; 184 return false;
178 } 185 }
179 186
180 void SafeSearchURLChecker::OnURLFetchComplete(const net::URLFetcher* source) { 187 void SafeSearchURLChecker::OnURLFetchComplete(const net::URLFetcher* source) {
181 ScopedVector<Check>::iterator it = checks_in_progress_.begin(); 188 ScopedVector<Check>::iterator it = checks_in_progress_.begin();
182 while (it != checks_in_progress_.end()) { 189 while (it != checks_in_progress_.end()) {
183 if (source == (*it)->fetcher.get()) 190 if (source == (*it)->fetcher.get())
184 break; 191 break;
(...skipping 21 matching lines...) Expand all
206 // TODO(msramek): Consider moving this to SupervisedUserResourceThrottle. 213 // TODO(msramek): Consider moving this to SupervisedUserResourceThrottle.
207 UMA_HISTOGRAM_TIMES("ManagedUsers.SafeSitesDelay", 214 UMA_HISTOGRAM_TIMES("ManagedUsers.SafeSitesDelay",
208 base::TimeTicks::Now() - check->start_time); 215 base::TimeTicks::Now() - check->start_time);
209 216
210 cache_.Put(check->url, CheckResult(classification, uncertain)); 217 cache_.Put(check->url, CheckResult(classification, uncertain));
211 218
212 for (size_t i = 0; i < check->callbacks.size(); i++) 219 for (size_t i = 0; i < check->callbacks.size(); i++)
213 check->callbacks[i].Run(check->url, classification, uncertain); 220 check->callbacks[i].Run(check->url, classification, uncertain);
214 checks_in_progress_.erase(it); 221 checks_in_progress_.erase(it);
215 } 222 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698