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

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

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

Powered by Google App Engine
This is Rietveld 408576698