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

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

Issue 2681783003: Since SafeBrowsingNavigationObserverManager cleans up navigation events every two minutes, if downl… (Closed)
Patch Set: Created 3 years, 10 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/download_protection_service.h" 5 #include "chrome/browser/safe_browsing/download_protection_service.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <memory> 9 #include <memory>
10 10
11 #include "base/bind.h" 11 #include "base/bind.h"
12 #include "base/callback_helpers.h" 12 #include "base/callback_helpers.h"
13 #include "base/command_line.h" 13 #include "base/command_line.h"
14 #include "base/compiler_specific.h" 14 #include "base/compiler_specific.h"
15 #include "base/format_macros.h" 15 #include "base/format_macros.h"
16 #include "base/macros.h" 16 #include "base/macros.h"
17 #include "base/memory/weak_ptr.h" 17 #include "base/memory/weak_ptr.h"
18 #include "base/metrics/field_trial.h" 18 #include "base/metrics/field_trial.h"
19 #include "base/metrics/histogram_macros.h" 19 #include "base/metrics/histogram_macros.h"
20 #include "base/metrics/sparse_histogram.h" 20 #include "base/metrics/sparse_histogram.h"
21 #include "base/rand_util.h" 21 #include "base/rand_util.h"
22 #include "base/scoped_observer.h"
22 #include "base/sequenced_task_runner_helpers.h" 23 #include "base/sequenced_task_runner_helpers.h"
23 #include "base/sha1.h" 24 #include "base/sha1.h"
24 #include "base/stl_util.h" 25 #include "base/stl_util.h"
25 #include "base/strings/string_number_conversions.h" 26 #include "base/strings/string_number_conversions.h"
26 #include "base/strings/string_split.h" 27 #include "base/strings/string_split.h"
27 #include "base/strings/string_util.h" 28 #include "base/strings/string_util.h"
28 #include "base/strings/stringprintf.h" 29 #include "base/strings/stringprintf.h"
29 #include "base/task/cancelable_task_tracker.h" 30 #include "base/task/cancelable_task_tracker.h"
30 #include "base/threading/sequenced_worker_pool.h" 31 #include "base/threading/sequenced_worker_pool.h"
31 #include "base/time/time.h" 32 #include "base/time/time.h"
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
81 const int64_t kDownloadRequestTimeoutMs = 7000; 82 const int64_t kDownloadRequestTimeoutMs = 7000;
82 // We sample 1% of whitelisted downloads to still send out download pings. 83 // We sample 1% of whitelisted downloads to still send out download pings.
83 const double kWhitelistDownloadSampleRate = 0.01; 84 const double kWhitelistDownloadSampleRate = 0.01;
84 85
85 // The number of user gestures we trace back for download attribution. 86 // The number of user gestures we trace back for download attribution.
86 const int kDownloadAttributionUserGestureLimit = 2; 87 const int kDownloadAttributionUserGestureLimit = 2;
87 88
88 const char kDownloadExtensionUmaName[] = "SBClientDownload.DownloadExtensions"; 89 const char kDownloadExtensionUmaName[] = "SBClientDownload.DownloadExtensions";
89 const char kUnsupportedSchemeUmaPrefix[] = "SBClientDownload.UnsupportedScheme"; 90 const char kUnsupportedSchemeUmaPrefix[] = "SBClientDownload.UnsupportedScheme";
90 91
92 const void* const kDownloadReferrerChainDataKey =
93 &kDownloadReferrerChainDataKey;
94
91 enum WhitelistType { 95 enum WhitelistType {
92 NO_WHITELIST_MATCH, 96 NO_WHITELIST_MATCH,
93 URL_WHITELIST, 97 URL_WHITELIST,
94 SIGNATURE_WHITELIST, 98 SIGNATURE_WHITELIST,
95 WHITELIST_TYPE_MAX 99 WHITELIST_TYPE_MAX
96 }; 100 };
97 101
98 void RecordCountOfWhitelistedDownload(WhitelistType type) { 102 void RecordCountOfWhitelistedDownload(WhitelistType type) {
99 UMA_HISTOGRAM_ENUMERATION("SBClientDownload.CheckWhitelistResult", type, 103 UMA_HISTOGRAM_ENUMERATION("SBClientDownload.CheckWhitelistResult", type,
100 WHITELIST_TYPE_MAX); 104 WHITELIST_TYPE_MAX);
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
153 DOWNLOAD_HASH_CHECKS_MALWARE, 157 DOWNLOAD_HASH_CHECKS_MALWARE,
154 158
155 // Memory space for histograms is determined by the max. 159 // Memory space for histograms is determined by the max.
156 // ALWAYS ADD NEW VALUES BEFORE THIS ONE. 160 // ALWAYS ADD NEW VALUES BEFORE THIS ONE.
157 DOWNLOAD_CHECKS_MAX 161 DOWNLOAD_CHECKS_MAX
158 }; 162 };
159 163
160 } // namespace 164 } // namespace
161 165
162 // Parent SafeBrowsing::Client class used to lookup the bad binary 166 // Parent SafeBrowsing::Client class used to lookup the bad binary
163 // URL and digest list. There are two sub-classes (one for each list). 167 // URL and digest list.
164 class DownloadSBClient 168 class DownloadSBClient
165 : public SafeBrowsingDatabaseManager::Client, 169 : public SafeBrowsingDatabaseManager::Client,
166 public base::RefCountedThreadSafe<DownloadSBClient> { 170 public content::DownloadItem::Observer,
171 public base::RefCountedThreadSafe<
172 DownloadSBClient,
173 BrowserThread::DeleteOnUIThread> {
167 public: 174 public:
168 DownloadSBClient( 175 DownloadSBClient(
169 const content::DownloadItem& item, 176 content::DownloadItem* item,
177 DownloadProtectionService* service,
170 const DownloadProtectionService::CheckDownloadCallback& callback, 178 const DownloadProtectionService::CheckDownloadCallback& callback,
171 const scoped_refptr<SafeBrowsingUIManager>& ui_manager, 179 const scoped_refptr<SafeBrowsingUIManager>& ui_manager,
172 SBStatsType total_type, 180 SBStatsType total_type,
173 SBStatsType dangerous_type) 181 SBStatsType dangerous_type)
174 : sha256_hash_(item.GetHash()), 182 : observer_(this),
175 url_chain_(item.GetUrlChain()), 183 item_(item),
176 referrer_url_(item.GetReferrerUrl()), 184 sha256_hash_(item->GetHash()),
185 url_chain_(item->GetUrlChain()),
186 referrer_url_(item->GetReferrerUrl()),
187 service_(service),
177 callback_(callback), 188 callback_(callback),
178 ui_manager_(ui_manager), 189 ui_manager_(ui_manager),
179 start_time_(base::TimeTicks::Now()), 190 start_time_(base::TimeTicks::Now()),
180 total_type_(total_type), 191 total_type_(total_type),
181 dangerous_type_(dangerous_type) { 192 dangerous_type_(dangerous_type) {
182 Profile* profile = Profile::FromBrowserContext(item.GetBrowserContext()); 193 DCHECK_CURRENTLY_ON(BrowserThread::UI);
194 DCHECK(item_);
195 DCHECK(service_);
196 observer_.Add(item_);
197 Profile* profile = Profile::FromBrowserContext(item_->GetBrowserContext());
183 extended_reporting_level_ = 198 extended_reporting_level_ =
184 profile ? GetExtendedReportingLevel(*profile->GetPrefs()) 199 profile ? GetExtendedReportingLevel(*profile->GetPrefs())
185 : SBER_LEVEL_OFF; 200 : SBER_LEVEL_OFF;
201 download_attribution_enabled_ = service_->navigation_observer_manager() &&
202 base::FeatureList::IsEnabled(
203 SafeBrowsingNavigationObserverManager::kDownloadAttribution);
186 } 204 }
187 205
188 virtual void StartCheck() = 0; 206 virtual void StartCheck() = 0;
189 virtual bool IsDangerous(SBThreatType threat_type) const = 0; 207 virtual bool IsDangerous(SBThreatType threat_type) const = 0;
190 208
209 // Implements DownloadItem::Observer.
210 void OnDownloadDestroyed(content::DownloadItem* download) override {
211 item_ = nullptr;
212 }
213
191 protected: 214 protected:
192 friend class base::RefCountedThreadSafe<DownloadSBClient>; 215 friend class base::RefCountedThreadSafe<DownloadSBClient>;
193 ~DownloadSBClient() override {} 216 friend struct BrowserThread::DeleteOnThread<BrowserThread::UI>;
217 friend class base::DeleteHelper<DownloadSBClient>;
218 ~DownloadSBClient() override {
219 DCHECK_CURRENTLY_ON(BrowserThread::UI);
220 }
194 221
195 void CheckDone(SBThreatType threat_type) { 222 void CheckDone(SBThreatType threat_type) {
196 DownloadProtectionService::DownloadCheckResult result = 223 DownloadProtectionService::DownloadCheckResult result =
197 IsDangerous(threat_type) ? 224 IsDangerous(threat_type) ?
198 DownloadProtectionService::DANGEROUS : 225 DownloadProtectionService::DANGEROUS :
199 DownloadProtectionService::SAFE; 226 DownloadProtectionService::SAFE;
200 BrowserThread::PostTask(BrowserThread::UI,
201 FROM_HERE,
202 base::Bind(callback_, result));
203 UpdateDownloadCheckStats(total_type_); 227 UpdateDownloadCheckStats(total_type_);
204 if (threat_type != SB_THREAT_TYPE_SAFE) { 228 if (threat_type != SB_THREAT_TYPE_SAFE) {
205 UpdateDownloadCheckStats(dangerous_type_); 229 UpdateDownloadCheckStats(dangerous_type_);
206 BrowserThread::PostTask( 230 BrowserThread::PostTask(
207 BrowserThread::UI, 231 BrowserThread::UI,
208 FROM_HERE, 232 FROM_HERE,
209 base::Bind(&DownloadSBClient::ReportMalware, 233 base::Bind(&DownloadSBClient::ReportMalware,
210 this, threat_type)); 234 this, threat_type));
235 } else if (download_attribution_enabled_) {
236 // Identify download referrer chain, which will be used in
237 // ClientDownloadRequest.
238 BrowserThread::PostTask(
239 BrowserThread::UI,
240 FROM_HERE,
241 base::Bind(&DownloadSBClient::IdentifyReferrerChain,
242 this));
211 } 243 }
244 BrowserThread::PostTask(BrowserThread::UI,
245 FROM_HERE,
246 base::Bind(callback_, result));
212 } 247 }
213 248
214 void ReportMalware(SBThreatType threat_type) { 249 void ReportMalware(SBThreatType threat_type) {
215 std::string post_data; 250 std::string post_data;
216 if (!sha256_hash_.empty()) 251 if (!sha256_hash_.empty()) {
217 post_data += base::HexEncode(sha256_hash_.data(), 252 post_data += base::HexEncode(sha256_hash_.data(),
218 sha256_hash_.size()) + "\n"; 253 sha256_hash_.size()) + "\n";
254 }
219 for (size_t i = 0; i < url_chain_.size(); ++i) { 255 for (size_t i = 0; i < url_chain_.size(); ++i) {
220 post_data += url_chain_[i].spec() + "\n"; 256 post_data += url_chain_[i].spec() + "\n";
221 } 257 }
222 258
223 safe_browsing::HitReport hit_report; 259 safe_browsing::HitReport hit_report;
224 hit_report.malicious_url = url_chain_.back(); 260 hit_report.malicious_url = url_chain_.back();
225 hit_report.page_url = url_chain_.front(); 261 hit_report.page_url = url_chain_.front();
226 hit_report.referrer_url = referrer_url_; 262 hit_report.referrer_url = referrer_url_;
227 hit_report.is_subresource = true; 263 hit_report.is_subresource = true;
228 hit_report.threat_type = threat_type; 264 hit_report.threat_type = threat_type;
229 // TODO(nparker) Replace this with database_manager_->GetThreatSource(); 265 // TODO(nparker) Replace this with database_manager_->GetThreatSource();
230 hit_report.threat_source = safe_browsing::ThreatSource::LOCAL_PVER3; 266 hit_report.threat_source = safe_browsing::ThreatSource::LOCAL_PVER3;
231 // TODO(nparker) Populate hit_report.population_id once Pver4 is used here. 267 // TODO(nparker) Populate hit_report.population_id once Pver4 is used here.
232 hit_report.post_data = post_data; 268 hit_report.post_data = post_data;
233 hit_report.extended_reporting_level = extended_reporting_level_; 269 hit_report.extended_reporting_level = extended_reporting_level_;
234 hit_report.is_metrics_reporting_active = 270 hit_report.is_metrics_reporting_active =
235 ChromeMetricsServiceAccessor::IsMetricsAndCrashReportingEnabled(); 271 ChromeMetricsServiceAccessor::IsMetricsAndCrashReportingEnabled();
236 272
237 ui_manager_->MaybeReportSafeBrowsingHit(hit_report); 273 ui_manager_->MaybeReportSafeBrowsingHit(hit_report);
238 } 274 }
239 275
276 void IdentifyReferrerChain() {
277 DCHECK_CURRENTLY_ON(BrowserThread::UI);
278 if (!item_)
279 return;
280
281 item_->SetUserData(kDownloadReferrerChainDataKey,
282 new ReferrerChainData(
283 service_->IdentifyReferrerChain(
284 item_->GetURL(), item_->GetWebContents())));
285 }
286
240 void UpdateDownloadCheckStats(SBStatsType stat_type) { 287 void UpdateDownloadCheckStats(SBStatsType stat_type) {
241 UMA_HISTOGRAM_ENUMERATION("SB2.DownloadChecks", 288 UMA_HISTOGRAM_ENUMERATION("SB2.DownloadChecks",
242 stat_type, 289 stat_type,
243 DOWNLOAD_CHECKS_MAX); 290 DOWNLOAD_CHECKS_MAX);
244 } 291 }
245 292
293 ScopedObserver<content::DownloadItem,
294 content::DownloadItem::Observer> observer_;
295 // The DownloadItem we are checking. Must be accessed only on UI thread.
296 content::DownloadItem* item_;
297 // Copies of data from |item_| for access on other threads.
246 std::string sha256_hash_; 298 std::string sha256_hash_;
247 std::vector<GURL> url_chain_; 299 std::vector<GURL> url_chain_;
248 GURL referrer_url_; 300 GURL referrer_url_;
301 DownloadProtectionService* service_;
249 DownloadProtectionService::CheckDownloadCallback callback_; 302 DownloadProtectionService::CheckDownloadCallback callback_;
250 scoped_refptr<SafeBrowsingUIManager> ui_manager_; 303 scoped_refptr<SafeBrowsingUIManager> ui_manager_;
251 base::TimeTicks start_time_; 304 base::TimeTicks start_time_;
305 bool download_attribution_enabled_;
252 306
253 private: 307 private:
254 const SBStatsType total_type_; 308 const SBStatsType total_type_;
255 const SBStatsType dangerous_type_; 309 const SBStatsType dangerous_type_;
256 ExtendedReportingLevel extended_reporting_level_; 310 ExtendedReportingLevel extended_reporting_level_;
257 311
258 DISALLOW_COPY_AND_ASSIGN(DownloadSBClient); 312 DISALLOW_COPY_AND_ASSIGN(DownloadSBClient);
259 }; 313 };
260 314
261 class DownloadUrlSBClient : public DownloadSBClient { 315 class DownloadUrlSBClient : public DownloadSBClient {
262 public: 316 public:
263 DownloadUrlSBClient( 317 DownloadUrlSBClient(
264 const content::DownloadItem& item, 318 content::DownloadItem* item,
319 DownloadProtectionService* service,
265 const DownloadProtectionService::CheckDownloadCallback& callback, 320 const DownloadProtectionService::CheckDownloadCallback& callback,
266 const scoped_refptr<SafeBrowsingUIManager>& ui_manager, 321 const scoped_refptr<SafeBrowsingUIManager>& ui_manager,
267 const scoped_refptr<SafeBrowsingDatabaseManager>& database_manager) 322 const scoped_refptr<SafeBrowsingDatabaseManager>& database_manager)
268 : DownloadSBClient(item, callback, ui_manager, 323 : DownloadSBClient(item, service, callback, ui_manager,
269 DOWNLOAD_URL_CHECKS_TOTAL, 324 DOWNLOAD_URL_CHECKS_TOTAL,
270 DOWNLOAD_URL_CHECKS_MALWARE), 325 DOWNLOAD_URL_CHECKS_MALWARE),
271 database_manager_(database_manager) { } 326 database_manager_(database_manager) { }
272 327
273 void StartCheck() override { 328 void StartCheck() override {
274 DCHECK_CURRENTLY_ON(BrowserThread::IO); 329 DCHECK_CURRENTLY_ON(BrowserThread::IO);
275 if (!database_manager_.get() || 330 if (!database_manager_.get() ||
276 database_manager_->CheckDownloadUrl(url_chain_, this)) { 331 database_manager_->CheckDownloadUrl(url_chain_, this)) {
277 CheckDone(SB_THREAT_TYPE_SAFE); 332 CheckDone(SB_THREAT_TYPE_SAFE);
278 } else { 333 } else {
279 AddRef(); // SafeBrowsingService takes a pointer not a scoped_refptr. 334 AddRef(); // SafeBrowsingService takes a pointer not a scoped_refptr.
280 } 335 }
281 } 336 }
282 337
283 bool IsDangerous(SBThreatType threat_type) const override { 338 bool IsDangerous(SBThreatType threat_type) const override {
284 return threat_type == SB_THREAT_TYPE_BINARY_MALWARE_URL; 339 return threat_type == SB_THREAT_TYPE_BINARY_MALWARE_URL;
285 } 340 }
286 341
287 void OnCheckDownloadUrlResult(const std::vector<GURL>& url_chain, 342 void OnCheckDownloadUrlResult(const std::vector<GURL>& url_chain,
288 SBThreatType threat_type) override { 343 SBThreatType threat_type) override {
289 CheckDone(threat_type); 344 CheckDone(threat_type);
290 UMA_HISTOGRAM_TIMES("SB2.DownloadUrlCheckDuration", 345 UMA_HISTOGRAM_TIMES("SB2.DownloadUrlCheckDuration",
291 base::TimeTicks::Now() - start_time_); 346 base::TimeTicks::Now() - start_time_);
292 Release(); 347 Release();
293 } 348 }
294 349
295 protected: 350 private:
296 ~DownloadUrlSBClient() override {} 351 ~DownloadUrlSBClient() override {
352 DCHECK_CURRENTLY_ON(BrowserThread::UI);
353 }
297 354
298 private:
299 scoped_refptr<SafeBrowsingDatabaseManager> database_manager_; 355 scoped_refptr<SafeBrowsingDatabaseManager> database_manager_;
300 356
301 DISALLOW_COPY_AND_ASSIGN(DownloadUrlSBClient); 357 DISALLOW_COPY_AND_ASSIGN(DownloadUrlSBClient);
302 }; 358 };
303 359
304 class DownloadProtectionService::CheckClientDownloadRequest 360 class DownloadProtectionService::CheckClientDownloadRequest
305 : public base::RefCountedThreadSafe< 361 : public base::RefCountedThreadSafe<
306 DownloadProtectionService::CheckClientDownloadRequest, 362 DownloadProtectionService::CheckClientDownloadRequest,
307 BrowserThread::DeleteOnUIThread>, 363 BrowserThread::DeleteOnUIThread>,
308 public net::URLFetcherDelegate, 364 public net::URLFetcherDelegate,
(...skipping 690 matching lines...) Expand 10 before | Expand all | Expand 10 after
999 if (type_ == ClientDownloadRequest::SAMPLED_UNSUPPORTED_FILE) { 1055 if (type_ == ClientDownloadRequest::SAMPLED_UNSUPPORTED_FILE) {
1000 request.set_file_basename( 1056 request.set_file_basename(
1001 base::FilePath(item_->GetTargetFilePath().Extension()) 1057 base::FilePath(item_->GetTargetFilePath().Extension())
1002 .AsUTF8Unsafe()); 1058 .AsUTF8Unsafe());
1003 } else { 1059 } else {
1004 request.set_file_basename( 1060 request.set_file_basename(
1005 item_->GetTargetFilePath().BaseName().AsUTF8Unsafe()); 1061 item_->GetTargetFilePath().BaseName().AsUTF8Unsafe());
1006 } 1062 }
1007 request.set_download_type(type_); 1063 request.set_download_type(type_);
1008 1064
1009 service_->AddReferrerChainToClientDownloadRequest( 1065 ReferrerChainData* referrer_chain_data =
1010 item_->GetURL(), 1066 static_cast<ReferrerChainData*>(
1011 item_->GetWebContents(), 1067 item_->GetUserData(kDownloadReferrerChainDataKey));
1012 &request); 1068 if (referrer_chain_data &&
1069 !referrer_chain_data->GetReferrerChain()->empty()) {
1070 request.mutable_referrer_chain()->Swap(
1071 referrer_chain_data->GetReferrerChain());
1072 }
1013 1073
1014 if (archive_is_valid_ != ArchiveValid::UNSET) 1074 if (archive_is_valid_ != ArchiveValid::UNSET)
1015 request.set_archive_valid(archive_is_valid_ == ArchiveValid::VALID); 1075 request.set_archive_valid(archive_is_valid_ == ArchiveValid::VALID);
1016 request.mutable_signature()->CopyFrom(signature_info_); 1076 request.mutable_signature()->CopyFrom(signature_info_);
1017 if (image_headers_) 1077 if (image_headers_)
1018 request.set_allocated_image_headers(image_headers_.release()); 1078 request.set_allocated_image_headers(image_headers_.release());
1019 if (archived_executable_) 1079 if (archived_executable_)
1020 request.mutable_archived_binary()->Swap(&archived_binary_); 1080 request.mutable_archived_binary()->Swap(&archived_binary_);
1021 if (!request.SerializeToString(&client_download_request_data_)) { 1081 if (!request.SerializeToString(&client_download_request_data_)) {
1022 FinishRequest(UNKNOWN, REASON_INVALID_REQUEST_PROTO); 1082 FinishRequest(UNKNOWN, REASON_INVALID_REQUEST_PROTO);
(...skipping 583 matching lines...) Expand 10 before | Expand all | Expand 10 after
1606 const CheckDownloadCallback& callback) { 1666 const CheckDownloadCallback& callback) {
1607 scoped_refptr<CheckClientDownloadRequest> request( 1667 scoped_refptr<CheckClientDownloadRequest> request(
1608 new CheckClientDownloadRequest(item, callback, this, 1668 new CheckClientDownloadRequest(item, callback, this,
1609 database_manager_, 1669 database_manager_,
1610 binary_feature_extractor_.get())); 1670 binary_feature_extractor_.get()));
1611 download_requests_.insert(request); 1671 download_requests_.insert(request);
1612 request->Start(); 1672 request->Start();
1613 } 1673 }
1614 1674
1615 void DownloadProtectionService::CheckDownloadUrl( 1675 void DownloadProtectionService::CheckDownloadUrl(
1616 const content::DownloadItem& item, 1676 content::DownloadItem* item,
1617 const CheckDownloadCallback& callback) { 1677 const CheckDownloadCallback& callback) {
1618 DCHECK(!item.GetUrlChain().empty()); 1678 DCHECK(!item->GetUrlChain().empty());
1619 scoped_refptr<DownloadUrlSBClient> client( 1679 scoped_refptr<DownloadUrlSBClient> client(
1620 new DownloadUrlSBClient(item, callback, ui_manager_, database_manager_)); 1680 new DownloadUrlSBClient(item, this, callback, ui_manager_,
1681 database_manager_));
1621 // The client will release itself once it is done. 1682 // The client will release itself once it is done.
1622 BrowserThread::PostTask( 1683 BrowserThread::PostTask(
1623 BrowserThread::IO, 1684 BrowserThread::IO,
1624 FROM_HERE, 1685 FROM_HERE,
1625 base::Bind(&DownloadUrlSBClient::StartCheck, client)); 1686 base::Bind(&DownloadUrlSBClient::StartCheck, client));
1626 } 1687 }
1627 1688
1628 bool DownloadProtectionService::IsSupportedDownload( 1689 bool DownloadProtectionService::IsSupportedDownload(
1629 const content::DownloadItem& item, 1690 const content::DownloadItem& item,
1630 const base::FilePath& target_path) const { 1691 const base::FilePath& target_path) const {
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after
1813 // static 1874 // static
1814 GURL DownloadProtectionService::GetDownloadRequestUrl() { 1875 GURL DownloadProtectionService::GetDownloadRequestUrl() {
1815 GURL url(kDownloadRequestUrl); 1876 GURL url(kDownloadRequestUrl);
1816 std::string api_key = google_apis::GetAPIKey(); 1877 std::string api_key = google_apis::GetAPIKey();
1817 if (!api_key.empty()) 1878 if (!api_key.empty())
1818 url = url.Resolve("?key=" + net::EscapeQueryParamValue(api_key, true)); 1879 url = url.Resolve("?key=" + net::EscapeQueryParamValue(api_key, true));
1819 1880
1820 return url; 1881 return url;
1821 } 1882 }
1822 1883
1823 void DownloadProtectionService::AddReferrerChainToClientDownloadRequest( 1884 std::unique_ptr<ReferrerChain> DownloadProtectionService::IdentifyReferrerChain(
1824 const GURL& download_url, 1885 const GURL& download_url,
1825 content::WebContents* web_contents, 1886 content::WebContents* web_contents) {
1826 ClientDownloadRequest* out_request) { 1887 std::unique_ptr<ReferrerChain> referrer_chain =
1827 if (!base::FeatureList::IsEnabled( 1888 base::MakeUnique<ReferrerChain>();
1828 SafeBrowsingNavigationObserverManager::kDownloadAttribution) ||
1829 !navigation_observer_manager_) {
1830 return;
1831 }
1832
1833 int download_tab_id = SessionTabHelper::IdForTab(web_contents); 1889 int download_tab_id = SessionTabHelper::IdForTab(web_contents);
1834 UMA_HISTOGRAM_BOOLEAN( 1890 UMA_HISTOGRAM_BOOLEAN(
1835 "SafeBrowsing.ReferrerHasInvalidTabID.DownloadAttribution", 1891 "SafeBrowsing.ReferrerHasInvalidTabID.DownloadAttribution",
1836 download_tab_id == -1); 1892 download_tab_id == -1);
1837 SafeBrowsingNavigationObserverManager::ReferrerChain attribution_chain;
1838 SafeBrowsingNavigationObserverManager::AttributionResult result = 1893 SafeBrowsingNavigationObserverManager::AttributionResult result =
1839 navigation_observer_manager_->IdentifyReferrerChainForDownload( 1894 navigation_observer_manager_->IdentifyReferrerChainForDownload(
1840 download_url, 1895 download_url,
1841 download_tab_id, 1896 download_tab_id,
1842 kDownloadAttributionUserGestureLimit, 1897 kDownloadAttributionUserGestureLimit,
1843 &attribution_chain); 1898 referrer_chain.get());
1844 UMA_HISTOGRAM_COUNTS_100( 1899 UMA_HISTOGRAM_COUNTS_100(
1845 "SafeBrowsing.ReferrerURLChainSize.DownloadAttribution", 1900 "SafeBrowsing.ReferrerURLChainSize.DownloadAttribution",
1846 attribution_chain.size()); 1901 referrer_chain->size());
1847 UMA_HISTOGRAM_ENUMERATION( 1902 UMA_HISTOGRAM_ENUMERATION(
1848 "SafeBrowsing.ReferrerAttributionResult.DownloadAttribution", result, 1903 "SafeBrowsing.ReferrerAttributionResult.DownloadAttribution", result,
1849 SafeBrowsingNavigationObserverManager::ATTRIBUTION_FAILURE_TYPE_MAX); 1904 SafeBrowsingNavigationObserverManager::ATTRIBUTION_FAILURE_TYPE_MAX);
1850 for (auto& entry : attribution_chain) 1905 return referrer_chain;
1851 out_request->add_referrer_chain()->Swap(entry.get());
1852 } 1906 }
1853 1907
1854 void DownloadProtectionService::AddReferrerChainToPPAPIClientDownloadRequest( 1908 void DownloadProtectionService::AddReferrerChainToPPAPIClientDownloadRequest(
1855 const GURL& initiating_frame_url, 1909 const GURL& initiating_frame_url,
1856 int tab_id, 1910 int tab_id,
1857 bool has_user_gesture, 1911 bool has_user_gesture,
1858 ClientDownloadRequest* out_request) { 1912 ClientDownloadRequest* out_request) {
1859 if (!base::FeatureList::IsEnabled( 1913 if (!base::FeatureList::IsEnabled(
1860 SafeBrowsingNavigationObserverManager::kDownloadAttribution) || 1914 SafeBrowsingNavigationObserverManager::kDownloadAttribution) ||
1861 !navigation_observer_manager_) { 1915 !navigation_observer_manager_) {
1862 return; 1916 return;
1863 } 1917 }
1864 1918
1865 UMA_HISTOGRAM_BOOLEAN( 1919 UMA_HISTOGRAM_BOOLEAN(
1866 "SafeBrowsing.ReferrerHasInvalidTabID.DownloadAttribution", 1920 "SafeBrowsing.ReferrerHasInvalidTabID.DownloadAttribution",
1867 tab_id == -1); 1921 tab_id == -1);
1868 SafeBrowsingNavigationObserverManager::ReferrerChain attribution_chain;
1869 SafeBrowsingNavigationObserverManager::AttributionResult result = 1922 SafeBrowsingNavigationObserverManager::AttributionResult result =
1870 navigation_observer_manager_->IdentifyReferrerChainForPPAPIDownload( 1923 navigation_observer_manager_->IdentifyReferrerChainForPPAPIDownload(
1871 initiating_frame_url, 1924 initiating_frame_url,
1872 tab_id, 1925 tab_id,
1873 has_user_gesture, 1926 has_user_gesture,
1874 kDownloadAttributionUserGestureLimit, 1927 kDownloadAttributionUserGestureLimit,
1875 &attribution_chain); 1928 out_request->mutable_referrer_chain());
1876 UMA_HISTOGRAM_COUNTS_100( 1929 UMA_HISTOGRAM_COUNTS_100(
1877 "SafeBrowsing.ReferrerURLChainSize.PPAPIDownloadAttribution", 1930 "SafeBrowsing.ReferrerURLChainSize.PPAPIDownloadAttribution",
1878 attribution_chain.size()); 1931 out_request->referrer_chain_size());
1879 UMA_HISTOGRAM_ENUMERATION( 1932 UMA_HISTOGRAM_ENUMERATION(
1880 "SafeBrowsing.ReferrerAttributionResult.PPAPIDownloadAttribution", result, 1933 "SafeBrowsing.ReferrerAttributionResult.PPAPIDownloadAttribution", result,
1881 SafeBrowsingNavigationObserverManager::ATTRIBUTION_FAILURE_TYPE_MAX); 1934 SafeBrowsingNavigationObserverManager::ATTRIBUTION_FAILURE_TYPE_MAX);
1882 for (auto& entry : attribution_chain)
1883 out_request->add_referrer_chain()->Swap(entry.get());
1884 } 1935 }
1885 1936
1886 } // namespace safe_browsing 1937 } // namespace safe_browsing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698