Remove aura enum from DesktopMediaID to fix desktop mirroring audio (CrOS).
[chromium-blink-merge.git] / chrome / browser / safe_browsing / ping_manager.cc
bloba86d244a2f84df9695e0bc637812cb013fdeae95
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
3 // found in the LICENSE file.
5 #include "chrome/browser/safe_browsing/ping_manager.h"
7 #include "base/logging.h"
8 #include "base/stl_util.h"
9 #include "base/strings/string_util.h"
10 #include "base/strings/stringprintf.h"
11 #include "chrome/browser/net/certificate_error_reporter.h"
12 #include "chrome/common/env_vars.h"
13 #include "content/public/browser/browser_thread.h"
14 #include "google_apis/google_api_keys.h"
15 #include "net/base/escape.h"
16 #include "net/base/load_flags.h"
17 #include "net/ssl/ssl_info.h"
18 #include "net/url_request/certificate_report_sender.h"
19 #include "net/url_request/url_fetcher.h"
20 #include "net/url_request/url_request_context_getter.h"
21 #include "net/url_request/url_request_status.h"
22 #include "url/gurl.h"
24 using chrome_browser_net::CertificateErrorReporter;
25 using content::BrowserThread;
27 namespace {
28 // URLs to upload invalid certificate chain reports. The HTTP URL is
29 // preferred since a client seeing an invalid cert might not be able to
30 // make an HTTPS connection to report it.
31 const char kExtendedReportingUploadUrlInsecure[] =
32 "http://safebrowsing.googleusercontent.com/safebrowsing/clientreport/"
33 "chrome-certs";
34 const char kExtendedReportingUploadUrlSecure[] =
35 "https://sb-ssl.google.com/safebrowsing/clientreport/chrome-certs";
36 } // namespace
38 // SafeBrowsingPingManager implementation ----------------------------------
40 // static
41 SafeBrowsingPingManager* SafeBrowsingPingManager::Create(
42 net::URLRequestContextGetter* request_context_getter,
43 const SafeBrowsingProtocolConfig& config) {
44 DCHECK_CURRENTLY_ON(BrowserThread::IO);
45 return new SafeBrowsingPingManager(request_context_getter, config);
48 SafeBrowsingPingManager::SafeBrowsingPingManager(
49 net::URLRequestContextGetter* request_context_getter,
50 const SafeBrowsingProtocolConfig& config)
51 : client_name_(config.client_name),
52 request_context_getter_(request_context_getter),
53 url_prefix_(config.url_prefix) {
54 DCHECK(!url_prefix_.empty());
56 if (request_context_getter) {
57 // Set the upload URL and whether or not to send cookies with
58 // certificate reports sent to Safe Browsing servers.
59 bool use_insecure_certificate_upload_url =
60 CertificateErrorReporter::IsHttpUploadUrlSupported();
62 net::CertificateReportSender::CookiesPreference cookies_preference;
63 GURL certificate_upload_url;
64 if (use_insecure_certificate_upload_url) {
65 cookies_preference = net::CertificateReportSender::DO_NOT_SEND_COOKIES;
66 certificate_upload_url = GURL(kExtendedReportingUploadUrlInsecure);
67 } else {
68 cookies_preference = net::CertificateReportSender::SEND_COOKIES;
69 certificate_upload_url = GURL(kExtendedReportingUploadUrlSecure);
72 certificate_error_reporter_.reset(new CertificateErrorReporter(
73 request_context_getter->GetURLRequestContext(), certificate_upload_url,
74 cookies_preference));
77 version_ = SafeBrowsingProtocolManagerHelper::Version();
80 SafeBrowsingPingManager::~SafeBrowsingPingManager() {
81 // Delete in-progress safebrowsing reports (hits and details).
82 STLDeleteContainerPointers(safebrowsing_reports_.begin(),
83 safebrowsing_reports_.end());
86 // net::URLFetcherDelegate implementation ----------------------------------
88 // All SafeBrowsing request responses are handled here.
89 void SafeBrowsingPingManager::OnURLFetchComplete(
90 const net::URLFetcher* source) {
91 Reports::iterator sit = safebrowsing_reports_.find(source);
92 DCHECK(sit != safebrowsing_reports_.end());
93 delete *sit;
94 safebrowsing_reports_.erase(sit);
97 // Sends a SafeBrowsing "hit" for UMA users.
98 void SafeBrowsingPingManager::ReportSafeBrowsingHit(
99 const GURL& malicious_url,
100 const GURL& page_url,
101 const GURL& referrer_url,
102 bool is_subresource,
103 SBThreatType threat_type,
104 const std::string& post_data) {
105 GURL report_url = SafeBrowsingHitUrl(malicious_url, page_url,
106 referrer_url, is_subresource,
107 threat_type);
108 net::URLFetcher* report =
109 net::URLFetcher::Create(
110 report_url,
111 post_data.empty() ? net::URLFetcher::GET : net::URLFetcher::POST,
112 this).release();
113 report->SetLoadFlags(net::LOAD_DISABLE_CACHE);
114 report->SetRequestContext(request_context_getter_.get());
115 if (!post_data.empty())
116 report->SetUploadData("text/plain", post_data);
117 safebrowsing_reports_.insert(report);
118 report->Start();
121 // Sends malware details for users who opt-in.
122 void SafeBrowsingPingManager::ReportMalwareDetails(
123 const std::string& report) {
124 GURL report_url = MalwareDetailsUrl();
125 net::URLFetcher* fetcher =
126 net::URLFetcher::Create(report_url, net::URLFetcher::POST, this)
127 .release();
128 fetcher->SetLoadFlags(net::LOAD_DISABLE_CACHE);
129 fetcher->SetRequestContext(request_context_getter_.get());
130 fetcher->SetUploadData("application/octet-stream", report);
131 // Don't try too hard to send reports on failures.
132 fetcher->SetAutomaticallyRetryOn5xx(false);
133 fetcher->Start();
134 safebrowsing_reports_.insert(fetcher);
137 void SafeBrowsingPingManager::ReportInvalidCertificateChain(
138 const std::string& serialized_report) {
139 DCHECK(certificate_error_reporter_);
140 certificate_error_reporter_->SendReport(
141 CertificateErrorReporter::REPORT_TYPE_EXTENDED_REPORTING,
142 serialized_report);
145 void SafeBrowsingPingManager::SetCertificateErrorReporterForTesting(
146 scoped_ptr<CertificateErrorReporter> certificate_error_reporter) {
147 certificate_error_reporter_ = certificate_error_reporter.Pass();
150 GURL SafeBrowsingPingManager::SafeBrowsingHitUrl(
151 const GURL& malicious_url, const GURL& page_url,
152 const GURL& referrer_url, bool is_subresource,
153 SBThreatType threat_type) const {
154 DCHECK(threat_type == SB_THREAT_TYPE_URL_MALWARE ||
155 threat_type == SB_THREAT_TYPE_URL_PHISHING ||
156 threat_type == SB_THREAT_TYPE_URL_UNWANTED ||
157 threat_type == SB_THREAT_TYPE_BINARY_MALWARE_URL ||
158 threat_type == SB_THREAT_TYPE_CLIENT_SIDE_PHISHING_URL ||
159 threat_type == SB_THREAT_TYPE_CLIENT_SIDE_MALWARE_URL);
160 std::string url = SafeBrowsingProtocolManagerHelper::ComposeUrl(
161 url_prefix_, "report", client_name_, version_, std::string());
162 std::string threat_list = "none";
163 switch (threat_type) {
164 case SB_THREAT_TYPE_URL_MALWARE:
165 threat_list = "malblhit";
166 break;
167 case SB_THREAT_TYPE_URL_PHISHING:
168 threat_list = "phishblhit";
169 break;
170 case SB_THREAT_TYPE_URL_UNWANTED:
171 threat_list = "uwsblhit";
172 break;
173 case SB_THREAT_TYPE_BINARY_MALWARE_URL:
174 threat_list = "binurlhit";
175 break;
176 case SB_THREAT_TYPE_CLIENT_SIDE_PHISHING_URL:
177 threat_list = "phishcsdhit";
178 break;
179 case SB_THREAT_TYPE_CLIENT_SIDE_MALWARE_URL:
180 threat_list = "malcsdhit";
181 break;
182 default:
183 NOTREACHED();
185 return GURL(base::StringPrintf("%s&evts=%s&evtd=%s&evtr=%s&evhr=%s&evtb=%d",
186 url.c_str(), threat_list.c_str(),
187 net::EscapeQueryParamValue(malicious_url.spec(), true).c_str(),
188 net::EscapeQueryParamValue(page_url.spec(), true).c_str(),
189 net::EscapeQueryParamValue(referrer_url.spec(), true).c_str(),
190 is_subresource));
193 GURL SafeBrowsingPingManager::MalwareDetailsUrl() const {
194 std::string url = base::StringPrintf(
195 "%s/clientreport/malware?client=%s&appver=%s&pver=1.0",
196 url_prefix_.c_str(),
197 client_name_.c_str(),
198 version_.c_str());
199 std::string api_key = google_apis::GetAPIKey();
200 if (!api_key.empty()) {
201 base::StringAppendF(&url, "&key=%s",
202 net::EscapeQueryParamValue(api_key, true).c_str());
204 return GURL(url);