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

Side by Side Diff: chrome/browser/safe_browsing/ping_manager.cc

Issue 2361963002: Adding NetLog support to SafeBrowsingPingManager. (Closed)
Patch Set: Addressing review comments. 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 (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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_browsing/ping_manager.h" 5 #include "chrome/browser/safe_browsing/ping_manager.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/memory/ptr_util.h" 10 #include "base/memory/ptr_util.h"
11 #include "base/strings/string_util.h" 11 #include "base/strings/string_util.h"
12 #include "base/strings/stringprintf.h" 12 #include "base/strings/stringprintf.h"
13 #include "base/values.h"
13 #include "chrome/browser/safe_browsing/permission_reporter.h" 14 #include "chrome/browser/safe_browsing/permission_reporter.h"
14 #include "components/certificate_reporting/error_reporter.h" 15 #include "components/certificate_reporting/error_reporter.h"
15 #include "content/public/browser/browser_thread.h" 16 #include "content/public/browser/browser_thread.h"
16 #include "google_apis/google_api_keys.h" 17 #include "google_apis/google_api_keys.h"
17 #include "net/base/escape.h" 18 #include "net/base/escape.h"
18 #include "net/base/load_flags.h" 19 #include "net/base/load_flags.h"
20 #include "net/log/net_log_source_type.h"
19 #include "net/ssl/ssl_info.h" 21 #include "net/ssl/ssl_info.h"
20 #include "net/url_request/report_sender.h" 22 #include "net/url_request/report_sender.h"
21 #include "net/url_request/url_fetcher.h" 23 #include "net/url_request/url_fetcher.h"
24 #include "net/url_request/url_request_context.h"
22 #include "net/url_request/url_request_context_getter.h" 25 #include "net/url_request/url_request_context_getter.h"
23 #include "net/url_request/url_request_status.h" 26 #include "net/url_request/url_request_status.h"
24 #include "url/gurl.h" 27 #include "url/gurl.h"
25 28
26 using content::BrowserThread; 29 using content::BrowserThread;
27 30
28 namespace { 31 namespace {
29 // URL to upload invalid certificate chain reports. An HTTP URL is 32 // URL to upload invalid certificate chain reports. An HTTP URL is
30 // used because a client seeing an invalid cert might not be able to 33 // used because a client seeing an invalid cert might not be able to
31 // make an HTTPS connection to report it. 34 // make an HTTPS connection to report it.
32 const char kExtendedReportingUploadUrlInsecure[] = 35 const char kExtendedReportingUploadUrlInsecure[] =
33 "http://safebrowsing.googleusercontent.com/safebrowsing/clientreport/" 36 "http://safebrowsing.googleusercontent.com/safebrowsing/clientreport/"
34 "chrome-certs"; 37 "chrome-certs";
38
39 // Returns a dictionary with "url"=|url-spec| and "data"=|payload| for
40 // netlogging the start phase of a ping.
41 std::unique_ptr<base::Value> NetLogPingStartCallback(
42 const net::NetLogWithSource& net_log,
43 const GURL& url,
44 const std::string& payload,
45 net::NetLogCaptureMode) {
46 std::unique_ptr<base::DictionaryValue> event_params(
47 new base::DictionaryValue());
48 event_params->SetString("url", url.spec());
49 event_params->SetString("payload", payload);
50 net_log.source().AddToEventParameters(event_params.get());
51 return std::move(event_params);
52 }
53
54 // Returns a dictionary with "url"=|url-spec|, "status"=|status| and
55 // "error"=|error| for netlogging the end phase of a ping.
56 std::unique_ptr<base::Value> NetLogPingEndCallback(
57 const net::NetLogWithSource& net_log,
58 const GURL& url,
59 const net::URLRequestStatus& status,
60 net::NetLogCaptureMode) {
61 std::unique_ptr<base::DictionaryValue> event_params(
62 new base::DictionaryValue());
63 event_params->SetString("url", url.spec());
64 event_params->SetInteger("status", status.status());
65 event_params->SetInteger("error", status.error());
66 net_log.source().AddToEventParameters(event_params.get());
67 return std::move(event_params);
68 }
69
35 } // namespace 70 } // namespace
36 71
37 namespace safe_browsing { 72 namespace safe_browsing {
38 73
39 // SafeBrowsingPingManager implementation ---------------------------------- 74 // SafeBrowsingPingManager implementation ----------------------------------
40 75
41 // static 76 // static
42 std::unique_ptr<SafeBrowsingPingManager> SafeBrowsingPingManager::Create( 77 std::unique_ptr<SafeBrowsingPingManager> SafeBrowsingPingManager::Create(
43 net::URLRequestContextGetter* request_context_getter, 78 net::URLRequestContextGetter* request_context_getter,
44 const SafeBrowsingProtocolConfig& config) { 79 const SafeBrowsingProtocolConfig& config) {
(...skipping 15 matching lines...) Expand all
60 GURL certificate_upload_url; 95 GURL certificate_upload_url;
61 cookies_preference = net::ReportSender::DO_NOT_SEND_COOKIES; 96 cookies_preference = net::ReportSender::DO_NOT_SEND_COOKIES;
62 certificate_upload_url = GURL(kExtendedReportingUploadUrlInsecure); 97 certificate_upload_url = GURL(kExtendedReportingUploadUrlInsecure);
63 98
64 certificate_error_reporter_.reset(new certificate_reporting::ErrorReporter( 99 certificate_error_reporter_.reset(new certificate_reporting::ErrorReporter(
65 request_context_getter->GetURLRequestContext(), certificate_upload_url, 100 request_context_getter->GetURLRequestContext(), certificate_upload_url,
66 cookies_preference)); 101 cookies_preference));
67 102
68 permission_reporter_.reset( 103 permission_reporter_.reset(
69 new PermissionReporter(request_context_getter->GetURLRequestContext())); 104 new PermissionReporter(request_context_getter->GetURLRequestContext()));
105
106 net_log_ = net::NetLogWithSource::Make(
107 request_context_getter->GetURLRequestContext()->net_log(),
108 net::NetLogSourceType::SAFE_BROWSING);
70 } 109 }
71 110
72 version_ = SafeBrowsingProtocolManagerHelper::Version(); 111 version_ = SafeBrowsingProtocolManagerHelper::Version();
73 } 112 }
74 113
75 SafeBrowsingPingManager::~SafeBrowsingPingManager() { 114 SafeBrowsingPingManager::~SafeBrowsingPingManager() {
76 } 115 }
77 116
78 // net::URLFetcherDelegate implementation ---------------------------------- 117 // net::URLFetcherDelegate implementation ----------------------------------
79 118
80 // All SafeBrowsing request responses are handled here. 119 // All SafeBrowsing request responses are handled here.
81 void SafeBrowsingPingManager::OnURLFetchComplete( 120 void SafeBrowsingPingManager::OnURLFetchComplete(
82 const net::URLFetcher* source) { 121 const net::URLFetcher* source) {
122 net_log_.EndEvent(
123 net::NetLogEventType::SAFE_BROWSING_PING,
124 base::Bind(&NetLogPingEndCallback, net_log_, source->GetOriginalURL(),
125 source->GetStatus()));
83 auto it = 126 auto it =
84 std::find_if(safebrowsing_reports_.begin(), safebrowsing_reports_.end(), 127 std::find_if(safebrowsing_reports_.begin(), safebrowsing_reports_.end(),
85 [source](const std::unique_ptr<net::URLFetcher>& ptr) { 128 [source](const std::unique_ptr<net::URLFetcher>& ptr) {
86 return ptr.get() == source; 129 return ptr.get() == source;
87 }); 130 });
88 DCHECK(it != safebrowsing_reports_.end()); 131 DCHECK(it != safebrowsing_reports_.end());
89 safebrowsing_reports_.erase(it); 132 safebrowsing_reports_.erase(it);
90 } 133 }
91 134
92 // Sends a SafeBrowsing "hit" report. 135 // Sends a SafeBrowsing "hit" report.
93 void SafeBrowsingPingManager::ReportSafeBrowsingHit( 136 void SafeBrowsingPingManager::ReportSafeBrowsingHit(
94 const safe_browsing::HitReport& hit_report) { 137 const safe_browsing::HitReport& hit_report) {
95 GURL report_url = SafeBrowsingHitUrl(hit_report); 138 GURL report_url = SafeBrowsingHitUrl(hit_report);
96 std::unique_ptr<net::URLFetcher> report_ptr = net::URLFetcher::Create( 139 std::unique_ptr<net::URLFetcher> report_ptr = net::URLFetcher::Create(
97 report_url, hit_report.post_data.empty() ? net::URLFetcher::GET 140 report_url, hit_report.post_data.empty() ? net::URLFetcher::GET
98 : net::URLFetcher::POST, 141 : net::URLFetcher::POST,
99 this); 142 this);
100 net::URLFetcher* report = report_ptr.get(); 143 net::URLFetcher* report = report_ptr.get();
101 report_ptr->SetLoadFlags(net::LOAD_DISABLE_CACHE); 144 report_ptr->SetLoadFlags(net::LOAD_DISABLE_CACHE);
102 report_ptr->SetRequestContext(request_context_getter_.get()); 145 report_ptr->SetRequestContext(request_context_getter_.get());
103 if (!hit_report.post_data.empty()) 146 if (!hit_report.post_data.empty())
104 report_ptr->SetUploadData("text/plain", hit_report.post_data); 147 report_ptr->SetUploadData("text/plain", hit_report.post_data);
148
149 net_log_.BeginEvent(
150 net::NetLogEventType::SAFE_BROWSING_PING,
151 base::Bind(&NetLogPingStartCallback, net_log_,
152 report_ptr->GetOriginalURL(), hit_report.post_data));
Nathan Parker 2016/09/23 18:43:09 Is the post data already base64 encoded? It'd be
lpz 2016/09/26 19:55:39 Good call, encoded both payloads.
153
154 report->Start();
105 safebrowsing_reports_.insert(std::move(report_ptr)); 155 safebrowsing_reports_.insert(std::move(report_ptr));
106 report->Start();
107 } 156 }
108 157
109 // Sends threat details for users who opt-in. 158 // Sends threat details for users who opt-in.
110 void SafeBrowsingPingManager::ReportThreatDetails(const std::string& report) { 159 void SafeBrowsingPingManager::ReportThreatDetails(const std::string& report) {
111 GURL report_url = ThreatDetailsUrl(); 160 GURL report_url = ThreatDetailsUrl();
112 std::unique_ptr<net::URLFetcher> fetcher = 161 std::unique_ptr<net::URLFetcher> fetcher =
113 net::URLFetcher::Create(report_url, net::URLFetcher::POST, this); 162 net::URLFetcher::Create(report_url, net::URLFetcher::POST, this);
114 fetcher->SetLoadFlags(net::LOAD_DISABLE_CACHE); 163 fetcher->SetLoadFlags(net::LOAD_DISABLE_CACHE);
115 fetcher->SetRequestContext(request_context_getter_.get()); 164 fetcher->SetRequestContext(request_context_getter_.get());
116 fetcher->SetUploadData("application/octet-stream", report); 165 fetcher->SetUploadData("application/octet-stream", report);
117 // Don't try too hard to send reports on failures. 166 // Don't try too hard to send reports on failures.
118 fetcher->SetAutomaticallyRetryOn5xx(false); 167 fetcher->SetAutomaticallyRetryOn5xx(false);
168
169 net_log_.BeginEvent(
170 net::NetLogEventType::SAFE_BROWSING_PING,
171 base::Bind(&NetLogPingStartCallback, net_log_, fetcher->GetOriginalURL(),
172 report));
173
119 fetcher->Start(); 174 fetcher->Start();
120 safebrowsing_reports_.insert(std::move(fetcher)); 175 safebrowsing_reports_.insert(std::move(fetcher));
121 } 176 }
122 177
123 void SafeBrowsingPingManager::ReportInvalidCertificateChain( 178 void SafeBrowsingPingManager::ReportInvalidCertificateChain(
124 const std::string& serialized_report) { 179 const std::string& serialized_report) {
125 DCHECK(certificate_error_reporter_); 180 DCHECK(certificate_error_reporter_);
126 certificate_error_reporter_->SendExtendedReportingReport(serialized_report); 181 certificate_error_reporter_->SendExtendedReportingReport(serialized_report);
127 } 182 }
128 183
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
227 version_.c_str()); 282 version_.c_str());
228 std::string api_key = google_apis::GetAPIKey(); 283 std::string api_key = google_apis::GetAPIKey();
229 if (!api_key.empty()) { 284 if (!api_key.empty()) {
230 base::StringAppendF(&url, "&key=%s", 285 base::StringAppendF(&url, "&key=%s",
231 net::EscapeQueryParamValue(api_key, true).c_str()); 286 net::EscapeQueryParamValue(api_key, true).c_str());
232 } 287 }
233 return GURL(url); 288 return GURL(url);
234 } 289 }
235 290
236 } // namespace safe_browsing 291 } // namespace safe_browsing
OLDNEW
« no previous file with comments | « chrome/browser/safe_browsing/ping_manager.h ('k') | chrome/browser/safe_browsing/ping_manager_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698