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

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

Issue 2361963002: Adding NetLog support to SafeBrowsingPingManager. (Closed)
Patch Set: Segfault fix: call netlog before std::moving the fetcher 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
« no previous file with comments | « chrome/browser/safe_browsing/ping_manager.h ('k') | net/log/net_log_event_type_list.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 // Return a dictionary with "url"=|url-spec| and "data"=|payload| for
sadrul 2016/09/23 16:38:21 *Returns
lpz 2016/09/23 18:11:58 Done.
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 // Return a dictionary with "url"=|url-spec|, "status"=|status| and
sadrul 2016/09/23 16:38:21 ditto
lpz 2016/09/23 18:11:58 Done.
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) {
83 auto it = 122 auto it =
84 std::find_if(safebrowsing_reports_.begin(), safebrowsing_reports_.end(), 123 std::find_if(safebrowsing_reports_.begin(), safebrowsing_reports_.end(),
85 [source](const std::unique_ptr<net::URLFetcher>& ptr) { 124 [source](const std::unique_ptr<net::URLFetcher>& ptr) {
86 return ptr.get() == source; 125 return ptr.get() == source;
87 }); 126 });
88 DCHECK(it != safebrowsing_reports_.end()); 127 DCHECK(it != safebrowsing_reports_.end());
89 safebrowsing_reports_.erase(it); 128 safebrowsing_reports_.erase(it);
129
130 net_log_.EndEvent(
131 net::NetLogEventType::SAFE_BROWSING_PING,
132 base::Bind(&NetLogPingEndCallback, net_log_, source->GetURL(),
133 source->GetStatus()));
90 } 134 }
91 135
92 // Sends a SafeBrowsing "hit" report. 136 // Sends a SafeBrowsing "hit" report.
93 void SafeBrowsingPingManager::ReportSafeBrowsingHit( 137 void SafeBrowsingPingManager::ReportSafeBrowsingHit(
94 const safe_browsing::HitReport& hit_report) { 138 const safe_browsing::HitReport& hit_report) {
95 GURL report_url = SafeBrowsingHitUrl(hit_report); 139 GURL report_url = SafeBrowsingHitUrl(hit_report);
96 std::unique_ptr<net::URLFetcher> report_ptr = net::URLFetcher::Create( 140 std::unique_ptr<net::URLFetcher> report_ptr = net::URLFetcher::Create(
97 report_url, hit_report.post_data.empty() ? net::URLFetcher::GET 141 report_url, hit_report.post_data.empty() ? net::URLFetcher::GET
98 : net::URLFetcher::POST, 142 : net::URLFetcher::POST,
99 this); 143 this);
100 net::URLFetcher* report = report_ptr.get(); 144 net::URLFetcher* report = report_ptr.get();
101 report_ptr->SetLoadFlags(net::LOAD_DISABLE_CACHE); 145 report_ptr->SetLoadFlags(net::LOAD_DISABLE_CACHE);
102 report_ptr->SetRequestContext(request_context_getter_.get()); 146 report_ptr->SetRequestContext(request_context_getter_.get());
103 if (!hit_report.post_data.empty()) 147 if (!hit_report.post_data.empty())
104 report_ptr->SetUploadData("text/plain", hit_report.post_data); 148 report_ptr->SetUploadData("text/plain", hit_report.post_data);
149
150 net_log_.BeginEvent(
151 net::NetLogEventType::SAFE_BROWSING_PING,
152 base::Bind(&NetLogPingStartCallback, net_log_, report_ptr->GetURL(),
153 hit_report.post_data));
154
155 report->Start();
105 safebrowsing_reports_.insert(std::move(report_ptr)); 156 safebrowsing_reports_.insert(std::move(report_ptr));
106 report->Start();
107 } 157 }
108 158
109 // Sends threat details for users who opt-in. 159 // Sends threat details for users who opt-in.
110 void SafeBrowsingPingManager::ReportThreatDetails(const std::string& report) { 160 void SafeBrowsingPingManager::ReportThreatDetails(const std::string& report) {
111 GURL report_url = ThreatDetailsUrl(); 161 GURL report_url = ThreatDetailsUrl();
112 std::unique_ptr<net::URLFetcher> fetcher = 162 std::unique_ptr<net::URLFetcher> fetcher =
113 net::URLFetcher::Create(report_url, net::URLFetcher::POST, this); 163 net::URLFetcher::Create(report_url, net::URLFetcher::POST, this);
114 fetcher->SetLoadFlags(net::LOAD_DISABLE_CACHE); 164 fetcher->SetLoadFlags(net::LOAD_DISABLE_CACHE);
115 fetcher->SetRequestContext(request_context_getter_.get()); 165 fetcher->SetRequestContext(request_context_getter_.get());
116 fetcher->SetUploadData("application/octet-stream", report); 166 fetcher->SetUploadData("application/octet-stream", report);
117 // Don't try too hard to send reports on failures. 167 // Don't try too hard to send reports on failures.
118 fetcher->SetAutomaticallyRetryOn5xx(false); 168 fetcher->SetAutomaticallyRetryOn5xx(false);
169
170 net_log_.BeginEvent(
171 net::NetLogEventType::SAFE_BROWSING_PING,
172 base::Bind(&NetLogPingStartCallback, net_log_, fetcher->GetURL(),
173 report));
sadrul 2016/09/23 16:38:21 Are these callbacks always expected to run synchro
lpz 2016/09/23 18:11:58 Yes the docs suggest that this is guaranteed - the
174
119 fetcher->Start(); 175 fetcher->Start();
120 safebrowsing_reports_.insert(std::move(fetcher)); 176 safebrowsing_reports_.insert(std::move(fetcher));
177
121 } 178 }
122 179
123 void SafeBrowsingPingManager::ReportInvalidCertificateChain( 180 void SafeBrowsingPingManager::ReportInvalidCertificateChain(
124 const std::string& serialized_report) { 181 const std::string& serialized_report) {
125 DCHECK(certificate_error_reporter_); 182 DCHECK(certificate_error_reporter_);
126 certificate_error_reporter_->SendExtendedReportingReport(serialized_report); 183 certificate_error_reporter_->SendExtendedReportingReport(serialized_report);
127 } 184 }
128 185
129 void SafeBrowsingPingManager::SetCertificateErrorReporterForTesting( 186 void SafeBrowsingPingManager::SetCertificateErrorReporterForTesting(
130 std::unique_ptr<certificate_reporting::ErrorReporter> 187 std::unique_ptr<certificate_reporting::ErrorReporter>
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
227 version_.c_str()); 284 version_.c_str());
228 std::string api_key = google_apis::GetAPIKey(); 285 std::string api_key = google_apis::GetAPIKey();
229 if (!api_key.empty()) { 286 if (!api_key.empty()) {
230 base::StringAppendF(&url, "&key=%s", 287 base::StringAppendF(&url, "&key=%s",
231 net::EscapeQueryParamValue(api_key, true).c_str()); 288 net::EscapeQueryParamValue(api_key, true).c_str());
232 } 289 }
233 return GURL(url); 290 return GURL(url);
234 } 291 }
235 292
236 } // namespace safe_browsing 293 } // namespace safe_browsing
OLDNEW
« no previous file with comments | « chrome/browser/safe_browsing/ping_manager.h ('k') | net/log/net_log_event_type_list.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698