OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 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. | |
4 | |
5 #ifndef CHROME_BROWSER_DOWNLOAD_DOWNLOAD_SAFE_BROWSING_CLIENT_H_ | |
6 #define CHROME_BROWSER_DOWNLOAD_DOWNLOAD_SAFE_BROWSING_CLIENT_H_ | |
7 #pragma once | |
8 | |
9 #include "base/callback.h" | |
10 #include "base/memory/ref_counted.h" | |
11 #include "base/time.h" | |
12 #include "chrome/browser/safe_browsing/safe_browsing_service.h" | |
13 | |
14 // This is a helper class used by DownloadManager to check a download URL with | |
15 // SafeBrowsingService. The client is refcounted and will be released once there | |
16 // is no reference to it. | |
17 // Usage: | |
18 // { | |
19 // scoped_refptr<DownloadSBClient> client_ = new DownloadSBClient(...); | |
20 // client_->CheckDownloadUrl( | |
21 // ..., base::Bind(&DownloadManager::UrlCallBack, | |
22 // base::Unretained(this))); | |
23 // or | |
24 // client_->CheckDownloadHash( | |
25 // ..., base::Bind(&DownloadManager::HashCallBack, | |
26 // base::Unretained(this))); | |
27 // } | |
28 // DownloadManager::UrlCallBack(...) or HashCallCall { | |
29 // // After this, the |client_| is gone. | |
30 // } | |
31 class DownloadSBClient | |
32 : public SafeBrowsingService::Client, | |
33 public base::RefCountedThreadSafe<DownloadSBClient> { | |
34 public: | |
35 typedef base::Callback<void(int32, bool)> UrlDoneCallback; | |
36 typedef base::Callback<void(int32, bool)> HashDoneCallback; | |
37 | |
38 DownloadSBClient(int32 download_id, | |
39 const std::vector<GURL>& url_chain, | |
40 const GURL& referrer_url, | |
41 bool safe_browsing_enabled); | |
42 | |
43 // Call safebrowsing service to verify the download. | |
44 // For each DownloadSBClient instance, either CheckDownloadUrl or | |
45 // CheckDownloadHash can be called, and be called only once. | |
46 // DownloadSBClient instance. | |
47 void CheckDownloadUrl(const UrlDoneCallback& callback); | |
48 void CheckDownloadHash(const std::string& hash, | |
49 const HashDoneCallback& callback); | |
50 | |
51 private: | |
52 // Call SafeBrowsingService on IO thread to verify the download URL or | |
53 // hash of downloaded file. | |
54 void CheckDownloadUrlOnIOThread(const std::vector<GURL>& url_chain); | |
55 void CheckDownloadHashOnIOThread(const std::string& hash); | |
56 | |
57 // Callback interfaces for SafeBrowsingService::Client. | |
58 virtual void OnDownloadUrlCheckResult( | |
59 const std::vector<GURL>& url_chain, | |
60 SafeBrowsingService::UrlCheckResult result) OVERRIDE; | |
61 virtual void OnDownloadHashCheckResult( | |
62 const std::string& hash, | |
63 SafeBrowsingService::UrlCheckResult result) OVERRIDE; | |
64 | |
65 // Enumerate for histogramming purposes. | |
66 // DO NOT CHANGE THE ORDERING OF THESE VALUES (different histogram data will | |
67 // be mixed together based on their values). | |
68 enum SBStatsType { | |
69 DOWNLOAD_URL_CHECKS_TOTAL, | |
70 DOWNLOAD_URL_CHECKS_CANCELED, | |
71 DOWNLOAD_URL_CHECKS_MALWARE, | |
72 | |
73 DOWNLOAD_HASH_CHECKS_TOTAL, | |
74 DOWNLOAD_HASH_CHECKS_MALWARE, | |
75 | |
76 // Memory space for histograms is determined by the max. | |
77 // ALWAYS ADD NEW VALUES BEFORE THIS ONE. | |
78 DOWNLOAD_CHECKS_MAX | |
79 }; | |
80 | |
81 friend class base::RefCountedThreadSafe<DownloadSBClient>; | |
82 friend class DownloadSBClientTest_UrlHit_Test; | |
83 friend class DownloadSBClientTest_DigestHit_Test; | |
84 | |
85 // Set the |sb_service_| manually for testing purposes. | |
86 void SetSBService(SafeBrowsingService* service) { | |
87 sb_service_ = service; | |
88 } | |
89 | |
90 virtual ~DownloadSBClient(); | |
91 | |
92 // Call DownloadManager on UI thread for download URL or hash check. | |
93 void SafeBrowsingCheckUrlDone(SafeBrowsingService::UrlCheckResult result); | |
94 void SafeBrowsingCheckHashDone(SafeBrowsingService::UrlCheckResult result, | |
95 const std::string& hash); | |
96 | |
97 // Report malware hits to safebrowsing service. |hash| should be empty if | |
98 // this is an URL check result. | |
99 void ReportMalware(SafeBrowsingService::UrlCheckResult result, | |
100 const std::string& hash); | |
101 | |
102 // Update the UMA stats. | |
103 void UpdateDownloadCheckStats(SBStatsType stat_type); | |
104 | |
105 UrlDoneCallback url_done_callback_; | |
106 HashDoneCallback hash_done_callback_; | |
107 | |
108 int32 download_id_; | |
109 scoped_refptr<SafeBrowsingService> sb_service_; | |
110 | |
111 // These URLs are used to report malware to safe browsing service. | |
112 std::vector<GURL> url_chain_; | |
113 GURL referrer_url_; | |
114 | |
115 // When a safebrowsing check starts, for stats purpose. | |
116 base::TimeTicks start_time_; | |
117 | |
118 // Whether the profile from which this client was created has enabled the | |
119 // safe browsing service. | |
120 bool safe_browsing_enabled_; | |
121 | |
122 DISALLOW_COPY_AND_ASSIGN(DownloadSBClient); | |
123 }; | |
124 | |
125 #endif // CHROME_BROWSER_DOWNLOAD_DOWNLOAD_SAFE_BROWSING_CLIENT_H_ | |
OLD | NEW |