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

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

Issue 2399823002: Extract the SafeSearch client to a separate directory (Closed)
Patch Set: Created 4 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/experimental/supervised_user_async_url_ checker.h" 5 #include "chrome/browser/safe_search/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/metrics/histogram_macros.h" 12 #include "base/metrics/histogram_macros.h"
13 #include "base/stl_util.h" 13 #include "base/stl_util.h"
14 #include "base/strings/string_piece.h" 14 #include "base/strings/string_piece.h"
15 #include "base/strings/string_util.h" 15 #include "base/strings/string_util.h"
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after
80 if (!classifications_list->GetDictionary(0, &classification_dict)) { 80 if (!classifications_list->GetDictionary(0, &classification_dict)) {
81 DLOG(WARNING) << "ParseResponse failed to parse classification dict"; 81 DLOG(WARNING) << "ParseResponse failed to parse classification dict";
82 return false; 82 return false;
83 } 83 }
84 classification_dict->GetBoolean("pornography", is_porn); 84 classification_dict->GetBoolean("pornography", is_porn);
85 return true; 85 return true;
86 } 86 }
87 87
88 } // namespace 88 } // namespace
89 89
90 struct SupervisedUserAsyncURLChecker::Check { 90 struct SafeSearchURLChecker::Check {
91 Check(const GURL& url, 91 Check(const GURL& url,
92 std::unique_ptr<net::URLFetcher> fetcher, 92 std::unique_ptr<net::URLFetcher> fetcher,
93 const CheckCallback& callback); 93 const CheckCallback& callback);
94 ~Check(); 94 ~Check();
95 95
96 GURL url; 96 GURL url;
97 std::unique_ptr<net::URLFetcher> fetcher; 97 std::unique_ptr<net::URLFetcher> fetcher;
98 std::vector<CheckCallback> callbacks; 98 std::vector<CheckCallback> callbacks;
99 base::TimeTicks start_time; 99 base::TimeTicks start_time;
100 }; 100 };
101 101
102 SupervisedUserAsyncURLChecker::Check::Check( 102 SafeSearchURLChecker::Check::Check(
103 const GURL& url, 103 const GURL& url,
104 std::unique_ptr<net::URLFetcher> fetcher, 104 std::unique_ptr<net::URLFetcher> fetcher,
105 const CheckCallback& callback) 105 const CheckCallback& callback)
106 : url(url), 106 : url(url),
107 fetcher(std::move(fetcher)), 107 fetcher(std::move(fetcher)),
108 callbacks(1, callback), 108 callbacks(1, callback),
109 start_time(base::TimeTicks::Now()) {} 109 start_time(base::TimeTicks::Now()) {}
110 110
111 SupervisedUserAsyncURLChecker::Check::~Check() {} 111 SafeSearchURLChecker::Check::~Check() {}
112 112
113 SupervisedUserAsyncURLChecker::CheckResult::CheckResult( 113 SafeSearchURLChecker::CheckResult::CheckResult(
114 SupervisedUserURLFilter::FilteringBehavior behavior, 114 Classification classification,
Marc Treib 2016/10/06 13:16:42 nit: I think this fits on the previous line now. (
msramek 2016/10/06 15:08:41 Done. I intentionally didn't run format at first t
115 bool uncertain) 115 bool uncertain)
116 : behavior(behavior), 116 : classification(classification),
117 uncertain(uncertain), 117 uncertain(uncertain),
118 timestamp(base::TimeTicks::Now()) {} 118 timestamp(base::TimeTicks::Now()) {}
119 119
120 SupervisedUserAsyncURLChecker::SupervisedUserAsyncURLChecker( 120 SafeSearchURLChecker::SafeSearchURLChecker(
121 URLRequestContextGetter* context) 121 URLRequestContextGetter* context)
122 : SupervisedUserAsyncURLChecker(context, kDefaultCacheSize) {} 122 : SafeSearchURLChecker(context, kDefaultCacheSize) {}
123 123
124 SupervisedUserAsyncURLChecker::SupervisedUserAsyncURLChecker( 124 SafeSearchURLChecker::SafeSearchURLChecker(
125 URLRequestContextGetter* context, 125 URLRequestContextGetter* context,
126 size_t cache_size) 126 size_t cache_size)
127 : context_(context), 127 : context_(context),
128 cache_(cache_size), 128 cache_(cache_size),
129 cache_timeout_( 129 cache_timeout_(
130 base::TimeDelta::FromSeconds(kDefaultCacheTimeoutSeconds)) {} 130 base::TimeDelta::FromSeconds(kDefaultCacheTimeoutSeconds)) {}
131 131
132 SupervisedUserAsyncURLChecker::~SupervisedUserAsyncURLChecker() {} 132 SafeSearchURLChecker::~SafeSearchURLChecker() {}
133 133
134 bool SupervisedUserAsyncURLChecker::CheckURL(const GURL& url, 134 bool SafeSearchURLChecker::CheckURL(const GURL& url,
135 const CheckCallback& callback) { 135 const CheckCallback& callback) {
136 // TODO(treib): Hack: For now, allow all Google URLs to save QPS. If we ever 136 // TODO(treib): Hack: For now, allow all Google URLs to save QPS. If we ever
137 // remove this, we should find a way to allow at least the NTP. 137 // remove this, we should find a way to allow at least the NTP.
138 if (google_util::IsGoogleDomainUrl(url, 138 if (google_util::IsGoogleDomainUrl(url,
139 google_util::ALLOW_SUBDOMAIN, 139 google_util::ALLOW_SUBDOMAIN,
140 google_util::ALLOW_NON_STANDARD_PORTS)) { 140 google_util::ALLOW_NON_STANDARD_PORTS)) {
141 callback.Run(url, SupervisedUserURLFilter::ALLOW, false); 141 callback.Run(url, SAFE, false);
142 return true; 142 return true;
143 } 143 }
144 // TODO(treib): Hack: For now, allow all YouTube URLs since YouTube has its 144 // TODO(treib): Hack: For now, allow all YouTube URLs since YouTube has its
145 // own Safety Mode anyway. 145 // own Safety Mode anyway.
146 if (google_util::IsYoutubeDomainUrl(url, 146 if (google_util::IsYoutubeDomainUrl(url,
147 google_util::ALLOW_SUBDOMAIN, 147 google_util::ALLOW_SUBDOMAIN,
148 google_util::ALLOW_NON_STANDARD_PORTS)) { 148 google_util::ALLOW_NON_STANDARD_PORTS)) {
149 callback.Run(url, SupervisedUserURLFilter::ALLOW, false); 149 callback.Run(url, SAFE, false);
150 return true; 150 return true;
151 } 151 }
152 152
153 auto cache_it = cache_.Get(url); 153 auto cache_it = cache_.Get(url);
154 if (cache_it != cache_.end()) { 154 if (cache_it != cache_.end()) {
155 const CheckResult& result = cache_it->second; 155 const CheckResult& result = cache_it->second;
156 base::TimeDelta age = base::TimeTicks::Now() - result.timestamp; 156 base::TimeDelta age = base::TimeTicks::Now() - result.timestamp;
157 if (age < cache_timeout_) { 157 if (age < cache_timeout_) {
158 DVLOG(1) << "Cache hit! " << url.spec() << " is " 158 DVLOG(1) << "Cache hit! " << url.spec() << " is "
159 << (result.behavior == SupervisedUserURLFilter::BLOCK ? "NOT" 159 << (result.classification == UNSAFE ? "NOT" : "")
160 : "")
161 << " safe; certain: " << !result.uncertain; 160 << " safe; certain: " << !result.uncertain;
162 callback.Run(url, result.behavior, result.uncertain); 161 callback.Run(url, result.classification, result.uncertain);
163 return true; 162 return true;
164 } 163 }
165 DVLOG(1) << "Outdated cache entry for " << url.spec() << ", purging"; 164 DVLOG(1) << "Outdated cache entry for " << url.spec() << ", purging";
166 cache_.Erase(cache_it); 165 cache_.Erase(cache_it);
167 } 166 }
168 167
169 // See if we already have a check in progress for this URL. 168 // See if we already have a check in progress for this URL.
170 for (Check* check : checks_in_progress_) { 169 for (Check* check : checks_in_progress_) {
171 if (check->url == url) { 170 if (check->url == url) {
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::GetAPIKey(); 178 std::string api_key = google_apis::GetAPIKey();
180 std::unique_ptr<URLFetcher> fetcher( 179 std::unique_ptr<URLFetcher> fetcher(
181 CreateFetcher(this, context_, api_key, url)); 180 CreateFetcher(this, context_, api_key, url));
182 fetcher->Start(); 181 fetcher->Start();
183 checks_in_progress_.push_back(new Check(url, std::move(fetcher), callback)); 182 checks_in_progress_.push_back(new Check(url, std::move(fetcher), callback));
184 return false; 183 return false;
185 } 184 }
186 185
187 void SupervisedUserAsyncURLChecker::OnURLFetchComplete( 186 void SafeSearchURLChecker::OnURLFetchComplete(
188 const net::URLFetcher* source) { 187 const net::URLFetcher* source) {
189 ScopedVector<Check>::iterator it = checks_in_progress_.begin(); 188 ScopedVector<Check>::iterator it = checks_in_progress_.begin();
190 while (it != checks_in_progress_.end()) { 189 while (it != checks_in_progress_.end()) {
191 if (source == (*it)->fetcher.get()) 190 if (source == (*it)->fetcher.get())
192 break; 191 break;
193 ++it; 192 ++it;
194 } 193 }
195 DCHECK(it != checks_in_progress_.end()); 194 DCHECK(it != checks_in_progress_.end());
196 Check* check = *it; 195 Check* check = *it;
197 196
198 const URLRequestStatus& status = source->GetStatus(); 197 const URLRequestStatus& status = source->GetStatus();
199 if (!status.is_success()) { 198 if (!status.is_success()) {
200 DLOG(WARNING) << "URL request failed! Letting through..."; 199 DLOG(WARNING) << "URL request failed! Letting through...";
201 for (size_t i = 0; i < check->callbacks.size(); i++) 200 for (size_t i = 0; i < check->callbacks.size(); i++)
202 check->callbacks[i].Run(check->url, SupervisedUserURLFilter::ALLOW, true); 201 check->callbacks[i].Run(check->url, SAFE, true);
203 checks_in_progress_.erase(it); 202 checks_in_progress_.erase(it);
204 return; 203 return;
205 } 204 }
206 205
207 std::string response_body; 206 std::string response_body;
208 source->GetResponseAsString(&response_body); 207 source->GetResponseAsString(&response_body);
209 bool is_porn = false; 208 bool is_porn = false;
210 bool uncertain = !ParseResponse(response_body, &is_porn); 209 bool uncertain = !ParseResponse(response_body, &is_porn);
211 SupervisedUserURLFilter::FilteringBehavior behavior = 210 Classification classification = is_porn ? UNSAFE : SAFE;
212 is_porn ? SupervisedUserURLFilter::BLOCK : SupervisedUserURLFilter::ALLOW;
213 211
212 // TODO(msramek): Should we rename the histogram?
Marc Treib 2016/10/06 13:16:42 Hm, probably. If not, then we should at least upda
msramek 2016/10/06 15:08:41 Actually, now that I think about it, the "average
Marc Treib 2016/10/06 15:17:15 It's not a total page load delay, it's specificall
msramek 2016/10/07 08:59:51 Acknowledged. I made a note in the linked bug.
214 UMA_HISTOGRAM_TIMES("ManagedUsers.SafeSitesDelay", 213 UMA_HISTOGRAM_TIMES("ManagedUsers.SafeSitesDelay",
215 base::TimeTicks::Now() - check->start_time); 214 base::TimeTicks::Now() - check->start_time);
216 215
217 cache_.Put(check->url, CheckResult(behavior, uncertain)); 216 cache_.Put(check->url, CheckResult(classification, uncertain));
218 217
219 for (size_t i = 0; i < check->callbacks.size(); i++) 218 for (size_t i = 0; i < check->callbacks.size(); i++)
220 check->callbacks[i].Run(check->url, behavior, uncertain); 219 check->callbacks[i].Run(check->url, classification, uncertain);
221 checks_in_progress_.erase(it); 220 checks_in_progress_.erase(it);
222 } 221 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698