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

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: nits 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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"
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"
16 #include "base/strings/stringprintf.h" 16 #include "base/strings/stringprintf.h"
17 #include "base/time/time.h" 17 #include "base/time/time.h"
18 #include "base/values.h" 18 #include "base/values.h"
19 #include "components/google/core/browser/google_util.h" 19 #include "components/google/core/browser/google_util.h"
20 #include "google_apis/google_api_keys.h" 20 #include "google_apis/google_api_keys.h"
21 #include "net/base/escape.h" 21 #include "net/base/escape.h"
22 #include "net/base/load_flags.h" 22 #include "net/base/load_flags.h"
23 #include "net/traffic_annotation/network_traffic_annotation.h"
23 #include "net/url_request/url_fetcher.h" 24 #include "net/url_request/url_fetcher.h"
24 #include "net/url_request/url_request_context.h" 25 #include "net/url_request/url_request_context.h"
25 #include "url/url_constants.h" 26 #include "url/url_constants.h"
26 27
27 using net::URLFetcher; 28 using net::URLFetcher;
28 using net::URLFetcherDelegate; 29 using net::URLFetcherDelegate;
29 using net::URLRequestContextGetter; 30 using net::URLRequestContextGetter;
30 using net::URLRequestStatus; 31 using net::URLRequestStatus;
31 32
32 namespace { 33 namespace {
33 34
34 const char kApiUrl[] = "https://safesearch.googleapis.com/v1:classify"; 35 const char kApiUrl[] = "https://safesearch.googleapis.com/v1:classify";
35 const char kDataContentType[] = "application/x-www-form-urlencoded"; 36 const char kDataContentType[] = "application/x-www-form-urlencoded";
36 const char kDataFormat[] = "key=%s&urls=%s"; 37 const char kDataFormat[] = "key=%s&urls=%s";
37 38
38 const size_t kDefaultCacheSize = 1000; 39 const size_t kDefaultCacheSize = 1000;
39 const size_t kDefaultCacheTimeoutSeconds = 3600; 40 const size_t kDefaultCacheTimeoutSeconds = 3600;
40 41
41 // Builds the POST data for SafeSearch API requests. 42 // Builds the POST data for SafeSearch API requests.
42 std::string BuildRequestData(const std::string& api_key, const GURL& url) { 43 std::string BuildRequestData(const std::string& api_key, const GURL& url) {
43 std::string query = net::EscapeQueryParamValue(url.spec(), true); 44 std::string query = net::EscapeQueryParamValue(url.spec(), true);
44 return base::StringPrintf(kDataFormat, api_key.c_str(), query.c_str()); 45 return base::StringPrintf(kDataFormat, api_key.c_str(), query.c_str());
45 } 46 }
46 47
47 // Creates a URLFetcher to call the SafeSearch API for |url|. 48 // Creates a URLFetcher to call the SafeSearch API for |url|.
48 std::unique_ptr<net::URLFetcher> CreateFetcher(URLFetcherDelegate* delegate, 49 std::unique_ptr<net::URLFetcher> CreateFetcher(URLFetcherDelegate* delegate,
49 URLRequestContextGetter* context, 50 URLRequestContextGetter* context,
50 const std::string& api_key, 51 const std::string& api_key,
51 const GURL& url) { 52 const GURL& url) {
52 std::unique_ptr<net::URLFetcher> fetcher = 53 net::NetworkTrafficAnnotationTag traffic_annotation =
53 URLFetcher::Create(0, GURL(kApiUrl), URLFetcher::POST, delegate); 54 net::DefineNetworkTrafficAnnotation("safe_search_api", R"(
55 semantics {
56 sender: "SafeSearch API"
57 description:
58 "Checks whether a given URL (or set of URLs) is considered safe by "
59 "Google SafeSearch."
60 trigger:
61 "If the parent enabled this feature for the child account, this is "
62 "sent for every navigation."
63 data: "URL(s) to be checked."
64 destination: GOOGLE_OWNED_SERVICE
65 }
66 policy {
67 cookies_allowed: false
68 setting:
69 "This feature is only used in child accounts and cannot be "
70 "disabled by settings. Parent accounts can disable it in "
71 "Supervised Users dashboard."
Marc Treib 2017/03/16 09:05:38 s/Supervised Users/the family/ ("Supervised Users
Ramin Halavati 2017/03/16 12:28:07 Done.
72 policy_exception_justification: "Not implemented."
73 })");
74 std::unique_ptr<net::URLFetcher> fetcher = URLFetcher::Create(
75 0, GURL(kApiUrl), URLFetcher::POST, delegate, traffic_annotation);
54 fetcher->SetUploadData(kDataContentType, BuildRequestData(api_key, url)); 76 fetcher->SetUploadData(kDataContentType, BuildRequestData(api_key, url));
55 fetcher->SetRequestContext(context); 77 fetcher->SetRequestContext(context);
56 fetcher->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES | 78 fetcher->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES |
57 net::LOAD_DO_NOT_SAVE_COOKIES); 79 net::LOAD_DO_NOT_SAVE_COOKIES);
58 return fetcher; 80 return fetcher;
59 } 81 }
60 82
61 // Parses a SafeSearch API |response| and stores the result in |is_porn|. 83 // Parses a SafeSearch API |response| and stores the result in |is_porn|.
62 // On errors, returns false and doesn't set |is_porn|. 84 // On errors, returns false and doesn't set |is_porn|.
63 bool ParseResponse(const std::string& response, bool* is_porn) { 85 bool ParseResponse(const std::string& response, bool* is_porn) {
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after
206 // TODO(msramek): Consider moving this to SupervisedUserResourceThrottle. 228 // TODO(msramek): Consider moving this to SupervisedUserResourceThrottle.
207 UMA_HISTOGRAM_TIMES("ManagedUsers.SafeSitesDelay", 229 UMA_HISTOGRAM_TIMES("ManagedUsers.SafeSitesDelay",
208 base::TimeTicks::Now() - check->start_time); 230 base::TimeTicks::Now() - check->start_time);
209 231
210 cache_.Put(check->url, CheckResult(classification, uncertain)); 232 cache_.Put(check->url, CheckResult(classification, uncertain));
211 233
212 for (size_t i = 0; i < check->callbacks.size(); i++) 234 for (size_t i = 0; i < check->callbacks.size(); i++)
213 check->callbacks[i].Run(check->url, classification, uncertain); 235 check->callbacks[i].Run(check->url, classification, uncertain);
214 checks_in_progress_.erase(it); 236 checks_in_progress_.erase(it);
215 } 237 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698