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

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

Issue 2399823002: Extract the SafeSearch client to a separate directory (Closed)
Patch Set: Renamed to safe_search_api 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_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/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(const GURL& url,
103 const GURL& url, 103 std::unique_ptr<net::URLFetcher> fetcher,
104 std::unique_ptr<net::URLFetcher> fetcher, 104 const CheckCallback& callback)
105 const CheckCallback& callback)
106 : url(url), 105 : url(url),
107 fetcher(std::move(fetcher)), 106 fetcher(std::move(fetcher)),
108 callbacks(1, callback), 107 callbacks(1, callback),
109 start_time(base::TimeTicks::Now()) {} 108 start_time(base::TimeTicks::Now()) {}
110 109
111 SupervisedUserAsyncURLChecker::Check::~Check() {} 110 SafeSearchURLChecker::Check::~Check() {}
112 111
113 SupervisedUserAsyncURLChecker::CheckResult::CheckResult( 112 SafeSearchURLChecker::CheckResult::CheckResult(Classification classification,
114 SupervisedUserURLFilter::FilteringBehavior behavior, 113 bool uncertain)
115 bool uncertain) 114 : classification(classification),
116 : behavior(behavior),
117 uncertain(uncertain), 115 uncertain(uncertain),
118 timestamp(base::TimeTicks::Now()) {} 116 timestamp(base::TimeTicks::Now()) {}
119 117
120 SupervisedUserAsyncURLChecker::SupervisedUserAsyncURLChecker( 118 SafeSearchURLChecker::SafeSearchURLChecker(URLRequestContextGetter* context)
121 URLRequestContextGetter* context) 119 : SafeSearchURLChecker(context, kDefaultCacheSize) {}
122 : SupervisedUserAsyncURLChecker(context, kDefaultCacheSize) {}
123 120
124 SupervisedUserAsyncURLChecker::SupervisedUserAsyncURLChecker( 121 SafeSearchURLChecker::SafeSearchURLChecker(URLRequestContextGetter* context,
125 URLRequestContextGetter* context, 122 size_t cache_size)
126 size_t cache_size)
127 : context_(context), 123 : context_(context),
128 cache_(cache_size), 124 cache_(cache_size),
129 cache_timeout_( 125 cache_timeout_(
130 base::TimeDelta::FromSeconds(kDefaultCacheTimeoutSeconds)) {} 126 base::TimeDelta::FromSeconds(kDefaultCacheTimeoutSeconds)) {}
131 127
132 SupervisedUserAsyncURLChecker::~SupervisedUserAsyncURLChecker() {} 128 SafeSearchURLChecker::~SafeSearchURLChecker() {}
133 129
134 bool SupervisedUserAsyncURLChecker::CheckURL(const GURL& url, 130 bool SafeSearchURLChecker::CheckURL(const GURL& url,
135 const CheckCallback& callback) { 131 const CheckCallback& callback) {
136 // TODO(treib): Hack: For now, allow all Google URLs to save QPS. If we ever 132 // 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. 133 // remove this, we should find a way to allow at least the NTP.
138 if (google_util::IsGoogleDomainUrl(url, 134 if (google_util::IsGoogleDomainUrl(url, google_util::ALLOW_SUBDOMAIN,
139 google_util::ALLOW_SUBDOMAIN,
140 google_util::ALLOW_NON_STANDARD_PORTS)) { 135 google_util::ALLOW_NON_STANDARD_PORTS)) {
141 callback.Run(url, SupervisedUserURLFilter::ALLOW, false); 136 callback.Run(url, Classification::SAFE, false);
142 return true; 137 return true;
143 } 138 }
144 // TODO(treib): Hack: For now, allow all YouTube URLs since YouTube has its 139 // TODO(treib): Hack: For now, allow all YouTube URLs since YouTube has its
145 // own Safety Mode anyway. 140 // own Safety Mode anyway.
146 if (google_util::IsYoutubeDomainUrl(url, 141 if (google_util::IsYoutubeDomainUrl(url, google_util::ALLOW_SUBDOMAIN,
147 google_util::ALLOW_SUBDOMAIN,
148 google_util::ALLOW_NON_STANDARD_PORTS)) { 142 google_util::ALLOW_NON_STANDARD_PORTS)) {
149 callback.Run(url, SupervisedUserURLFilter::ALLOW, false); 143 callback.Run(url, Classification::SAFE, false);
150 return true; 144 return true;
151 } 145 }
152 146
153 auto cache_it = cache_.Get(url); 147 auto cache_it = cache_.Get(url);
154 if (cache_it != cache_.end()) { 148 if (cache_it != cache_.end()) {
155 const CheckResult& result = cache_it->second; 149 const CheckResult& result = cache_it->second;
156 base::TimeDelta age = base::TimeTicks::Now() - result.timestamp; 150 base::TimeDelta age = base::TimeTicks::Now() - result.timestamp;
157 if (age < cache_timeout_) { 151 if (age < cache_timeout_) {
158 DVLOG(1) << "Cache hit! " << url.spec() << " is " 152 DVLOG(1) << "Cache hit! " << url.spec() << " is "
159 << (result.behavior == SupervisedUserURLFilter::BLOCK ? "NOT" 153 << (result.classification == Classification::UNSAFE ? "NOT" : "")
160 : "")
161 << " safe; certain: " << !result.uncertain; 154 << " safe; certain: " << !result.uncertain;
162 callback.Run(url, result.behavior, result.uncertain); 155 callback.Run(url, result.classification, result.uncertain);
163 return true; 156 return true;
164 } 157 }
165 DVLOG(1) << "Outdated cache entry for " << url.spec() << ", purging"; 158 DVLOG(1) << "Outdated cache entry for " << url.spec() << ", purging";
166 cache_.Erase(cache_it); 159 cache_.Erase(cache_it);
167 } 160 }
168 161
169 // See if we already have a check in progress for this URL. 162 // See if we already have a check in progress for this URL.
170 for (Check* check : checks_in_progress_) { 163 for (Check* check : checks_in_progress_) {
171 if (check->url == url) { 164 if (check->url == url) {
172 DVLOG(1) << "Adding to pending check for " << url.spec(); 165 DVLOG(1) << "Adding to pending check for " << url.spec();
173 check->callbacks.push_back(callback); 166 check->callbacks.push_back(callback);
174 return false; 167 return false;
175 } 168 }
176 } 169 }
177 170
178 DVLOG(1) << "Checking URL " << url; 171 DVLOG(1) << "Checking URL " << url;
179 std::string api_key = google_apis::GetAPIKey(); 172 std::string api_key = google_apis::GetAPIKey();
180 std::unique_ptr<URLFetcher> fetcher( 173 std::unique_ptr<URLFetcher> fetcher(
181 CreateFetcher(this, context_, api_key, url)); 174 CreateFetcher(this, context_, api_key, url));
182 fetcher->Start(); 175 fetcher->Start();
183 checks_in_progress_.push_back(new Check(url, std::move(fetcher), callback)); 176 checks_in_progress_.push_back(new Check(url, std::move(fetcher), callback));
184 return false; 177 return false;
185 } 178 }
186 179
187 void SupervisedUserAsyncURLChecker::OnURLFetchComplete( 180 void SafeSearchURLChecker::OnURLFetchComplete(const net::URLFetcher* source) {
188 const net::URLFetcher* source) {
189 ScopedVector<Check>::iterator it = checks_in_progress_.begin(); 181 ScopedVector<Check>::iterator it = checks_in_progress_.begin();
190 while (it != checks_in_progress_.end()) { 182 while (it != checks_in_progress_.end()) {
191 if (source == (*it)->fetcher.get()) 183 if (source == (*it)->fetcher.get())
192 break; 184 break;
193 ++it; 185 ++it;
194 } 186 }
195 DCHECK(it != checks_in_progress_.end()); 187 DCHECK(it != checks_in_progress_.end());
196 Check* check = *it; 188 Check* check = *it;
197 189
198 const URLRequestStatus& status = source->GetStatus(); 190 const URLRequestStatus& status = source->GetStatus();
199 if (!status.is_success()) { 191 if (!status.is_success()) {
200 DLOG(WARNING) << "URL request failed! Letting through..."; 192 DLOG(WARNING) << "URL request failed! Letting through...";
201 for (size_t i = 0; i < check->callbacks.size(); i++) 193 for (size_t i = 0; i < check->callbacks.size(); i++)
202 check->callbacks[i].Run(check->url, SupervisedUserURLFilter::ALLOW, true); 194 check->callbacks[i].Run(check->url, Classification::SAFE, true);
203 checks_in_progress_.erase(it); 195 checks_in_progress_.erase(it);
204 return; 196 return;
205 } 197 }
206 198
207 std::string response_body; 199 std::string response_body;
208 source->GetResponseAsString(&response_body); 200 source->GetResponseAsString(&response_body);
209 bool is_porn = false; 201 bool is_porn = false;
210 bool uncertain = !ParseResponse(response_body, &is_porn); 202 bool uncertain = !ParseResponse(response_body, &is_porn);
211 SupervisedUserURLFilter::FilteringBehavior behavior = 203 Classification classification =
212 is_porn ? SupervisedUserURLFilter::BLOCK : SupervisedUserURLFilter::ALLOW; 204 is_porn ? Classification::UNSAFE : Classification::SAFE;
213 205
206 // TODO(msramek): Consider moving this to SupervisedUserResourceThrottle.
214 UMA_HISTOGRAM_TIMES("ManagedUsers.SafeSitesDelay", 207 UMA_HISTOGRAM_TIMES("ManagedUsers.SafeSitesDelay",
215 base::TimeTicks::Now() - check->start_time); 208 base::TimeTicks::Now() - check->start_time);
216 209
217 cache_.Put(check->url, CheckResult(behavior, uncertain)); 210 cache_.Put(check->url, CheckResult(classification, uncertain));
218 211
219 for (size_t i = 0; i < check->callbacks.size(); i++) 212 for (size_t i = 0; i < check->callbacks.size(); i++)
220 check->callbacks[i].Run(check->url, behavior, uncertain); 213 check->callbacks[i].Run(check->url, classification, uncertain);
221 checks_in_progress_.erase(it); 214 checks_in_progress_.erase(it);
222 } 215 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698