OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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 // Helper class which handles communication with the SafeBrowsing servers for | 5 // Helper class which handles communication with the SafeBrowsing servers for |
6 // improved binary download protection. | 6 // improved binary download protection. |
7 | 7 |
8 #ifndef CHROME_BROWSER_SAFE_BROWSING_DOWNLOAD_PROTECTION_SERVICE_H_ | 8 #ifndef CHROME_BROWSER_SAFE_BROWSING_DOWNLOAD_PROTECTION_SERVICE_H_ |
9 #define CHROME_BROWSER_SAFE_BROWSING_DOWNLOAD_PROTECTION_SERVICE_H_ | 9 #define CHROME_BROWSER_SAFE_BROWSING_DOWNLOAD_PROTECTION_SERVICE_H_ |
10 #pragma once | 10 #pragma once |
11 | 11 |
12 #include <set> | 12 #include <map> |
13 #include <string> | 13 #include <string> |
14 #include <vector> | 14 #include <vector> |
15 | 15 |
16 #include "base/basictypes.h" | 16 #include "base/basictypes.h" |
17 #include "base/callback.h" | 17 #include "base/callback.h" |
18 #include "base/file_path.h" | |
19 #include "base/gtest_prod_util.h" | 18 #include "base/gtest_prod_util.h" |
20 #include "base/memory/ref_counted.h" | 19 #include "base/memory/ref_counted.h" |
| 20 #include "base/memory/scoped_ptr.h" |
| 21 #include "base/time.h" |
| 22 #include "content/public/common/url_fetcher_delegate.h" |
21 #include "googleurl/src/gurl.h" | 23 #include "googleurl/src/gurl.h" |
22 | 24 |
23 namespace net { | 25 namespace net { |
24 class URLRequestContextGetter; | 26 class URLRequestContextGetter; |
25 class URLRequestStatus; | 27 class URLRequestStatus; |
26 } // namespace net | 28 } // namespace net |
27 class DownloadItem; | |
28 class SafeBrowsingService; | 29 class SafeBrowsingService; |
29 | 30 |
30 namespace safe_browsing { | 31 namespace safe_browsing { |
31 | 32 |
32 // This class provides an asynchronous API to check whether a particular | 33 // This class provides an asynchronous API to check whether a particular |
33 // client download is malicious or not. | 34 // client download is malicious or not. |
34 class DownloadProtectionService { | 35 class DownloadProtectionService |
| 36 : public base::RefCountedThreadSafe<DownloadProtectionService>, |
| 37 public content::URLFetcherDelegate { |
35 public: | 38 public: |
36 // TODO(noelutz): we're missing some fields here: server IPs, | 39 // TODO(noelutz): we're missing some fields here: filename to get |
37 // tab URL redirect chain, ... | 40 // the signature, server IPs, tab URL redirect chain, ... |
38 struct DownloadInfo { | 41 struct DownloadInfo { |
39 FilePath local_file; | |
40 std::vector<GURL> download_url_chain; | 42 std::vector<GURL> download_url_chain; |
41 GURL referrer_url; | 43 GURL referrer_url; |
42 std::string sha256_hash; | 44 std::string sha256_hash; |
43 int64 total_bytes; | 45 int64 total_bytes; |
44 bool user_initiated; | 46 bool user_initiated; |
45 DownloadInfo(); | 47 DownloadInfo(); |
46 ~DownloadInfo(); | 48 ~DownloadInfo(); |
47 | |
48 // Creates a DownloadInfo from a DownloadItem object. | |
49 static DownloadInfo FromDownloadItem(const DownloadItem& item); | |
50 }; | 49 }; |
51 | 50 |
52 enum DownloadCheckResult { | 51 enum DownloadCheckResult { |
53 SAFE, | 52 SAFE, |
54 MALICIOUS, | 53 MALICIOUS, |
55 // In the future we may introduce a third category which corresponds to | 54 // In the future we may introduce a third category which corresponds to |
56 // suspicious downloads that are not known to be malicious. | 55 // suspicious downloads that are not known to be malicious. |
57 }; | 56 }; |
58 | 57 |
59 // Callback type which is invoked once the download request is done. | 58 // Callback type which is invoked once the download request is done. |
60 typedef base::Callback<void(DownloadCheckResult)> CheckDownloadCallback; | 59 typedef base::Callback<void(DownloadCheckResult)> CheckDownloadCallback; |
61 | 60 |
62 // Creates a download service. The service is initially disabled. You need | 61 // Creates a download service. The service is initially disabled. You need |
63 // to call SetEnabled() to start it. |sb_service| owns this object; we | 62 // to call SetEnabled() to start it. We keep scoped references to both of |
64 // keep a reference to |request_context_getter|. | 63 // these objects. |
65 DownloadProtectionService( | 64 DownloadProtectionService( |
66 SafeBrowsingService* sb_service, | 65 SafeBrowsingService* sb_service, |
67 net::URLRequestContextGetter* request_context_getter); | 66 net::URLRequestContextGetter* request_context_getter); |
68 | 67 |
69 virtual ~DownloadProtectionService(); | 68 // From the content::URLFetcherDelegate interface. |
| 69 virtual void OnURLFetchComplete(const content::URLFetcher* source) OVERRIDE; |
70 | 70 |
71 // Checks whether the given client download is likely to be malicious or not. | 71 // Checks whether the given client download is likely to be |
72 // The result is delivered asynchronously via the given callback. This | 72 // malicious or not. If this method returns true it means the |
73 // method must be called on the UI thread, and the callback will also be | 73 // download is safe or we're unable to tell whether it is safe or |
74 // invoked on the UI thread. | 74 // not. In this case the callback will never be called. If this |
75 virtual void CheckClientDownload(const DownloadInfo& info, | 75 // method returns false we will asynchronously check whether the |
| 76 // download is safe and call the callback when we have the response. |
| 77 // This method should be called on the UI thread. The callback will |
| 78 // be called on the UI thread and will always be called asynchronously. |
| 79 virtual bool CheckClientDownload(const DownloadInfo& info, |
76 const CheckDownloadCallback& callback); | 80 const CheckDownloadCallback& callback); |
77 | 81 |
78 // Enables or disables the service. This is usually called by the | 82 // Enables or disables the service. This is usually called by the |
79 // SafeBrowsingService, which tracks whether any profile uses these services | 83 // SafeBrowsingService, which tracks whether any profile uses these services |
80 // at all. Disabling causes any pending and future requests to have their | 84 // at all. Disabling cancels any pending requests; existing requests will |
81 // callbacks called with "SAFE" results. | 85 // have their callbacks called with "SAFE" results. Note: SetEnabled() is |
| 86 // asynchronous because this method is called on the UI thread but most |
| 87 // everything else happens on the IO thread. |
82 void SetEnabled(bool enabled); | 88 void SetEnabled(bool enabled); |
83 | 89 |
84 bool enabled() const { | 90 bool enabled() const { |
85 return enabled_; | 91 return enabled_; |
86 } | 92 } |
87 | 93 |
88 protected: | 94 protected: |
89 // Enum to keep track why a particular download verdict was chosen. | 95 // Enum to keep track why a particular download verdict was chosen. |
90 // This is used to keep some stats around. | 96 // This is used to keep some stats around. |
91 enum DownloadCheckResultReason { | 97 enum DownloadCheckResultReason { |
92 REASON_INVALID_URL, | 98 REASON_INVALID_URL, |
93 REASON_SB_DISABLED, | 99 REASON_SB_DISABLED, |
94 REASON_WHITELISTED_URL, | 100 REASON_WHITELISTED_URL, |
95 REASON_WHITELISTED_REFERRER, | 101 REASON_WHITELISTED_REFERRER, |
96 REASON_INVALID_REQUEST_PROTO, | 102 REASON_INVALID_REQUEST_PROTO, |
97 REASON_SERVER_PING_FAILED, | 103 REASON_SERVER_PING_FAILED, |
98 REASON_INVALID_RESPONSE_PROTO, | 104 REASON_INVALID_RESPONSE_PROTO, |
99 REASON_NOT_BINARY_FILE, | |
100 REASON_MAX // Always add new values before this one. | 105 REASON_MAX // Always add new values before this one. |
101 }; | 106 }; |
102 | 107 |
| 108 virtual ~DownloadProtectionService(); |
| 109 |
103 private: | 110 private: |
104 class CheckClientDownloadRequest; // Per-request state | 111 friend class base::RefCountedThreadSafe<DownloadProtectionService>; |
105 friend class DownloadProtectionServiceTest; | 112 friend class DownloadProtectionServiceTest; |
106 FRIEND_TEST_ALL_PREFIXES(DownloadProtectionServiceTest, | 113 FRIEND_TEST_ALL_PREFIXES(DownloadProtectionServiceTest, |
107 CheckClientDownloadValidateRequest); | 114 CheckClientDownloadValidateRequest); |
108 FRIEND_TEST_ALL_PREFIXES(DownloadProtectionServiceTest, | 115 FRIEND_TEST_ALL_PREFIXES(DownloadProtectionServiceTest, |
109 CheckClientDownloadSuccess); | 116 CheckClientDownloadSuccess); |
110 FRIEND_TEST_ALL_PREFIXES(DownloadProtectionServiceTest, | 117 FRIEND_TEST_ALL_PREFIXES(DownloadProtectionServiceTest, |
111 CheckClientDownloadFetchFailed); | 118 CheckClientDownloadFetchFailed); |
112 | 119 |
113 static const char kDownloadRequestUrl[]; | 120 static const char kDownloadRequestUrl[]; |
114 | 121 |
115 // Cancels all requests in |download_requests_|, and empties it, releasing | 122 // Same as above but this method is called on the IO thread after we have |
116 // the references to the requests. | 123 // done some basic checks to see whether the download is definitely not |
117 void CancelPendingRequests(); | 124 // safe. |
| 125 void StartCheckClientDownload(const DownloadInfo& info, |
| 126 const CheckDownloadCallback& callback); |
118 | 127 |
119 // Called by a CheckClientDownloadRequest instance when it finishes, to | 128 // This function must run on the UI thread and will invoke the callback |
120 // remove it from |download_requests_|. | 129 // with the given result. |
121 void RequestFinished(CheckClientDownloadRequest* request); | 130 void EndCheckClientDownload(DownloadCheckResult result, |
| 131 DownloadCheckResultReason reason, |
| 132 const CheckDownloadCallback& callback); |
122 | 133 |
123 static void FillDownloadInfo(const DownloadItem& item, | 134 void RecordStats(DownloadCheckResultReason reason); |
124 DownloadInfo* download_info); | |
125 | 135 |
126 // This pointer may be NULL if SafeBrowsing is disabled. The | 136 // SetEnabled(bool) calls this method on the IO thread. |
127 // SafeBrowsingService owns us, so we don't need to hold a reference to it. | 137 void SetEnabledOnIOThread(bool enableed); |
128 SafeBrowsingService* sb_service_; | 138 |
| 139 // This pointer may be NULL if SafeBrowsing is disabled. |
| 140 scoped_refptr<SafeBrowsingService> sb_service_; |
129 | 141 |
130 // The context we use to issue network requests. | 142 // The context we use to issue network requests. |
131 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; | 143 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; |
132 | 144 |
133 // Map of client download request to the corresponding callback that | 145 // Map of client download request to the corresponding callback that |
134 // has to be invoked when the request is done. This map contains all | 146 // has to be invoked when the request is done. This map contains all |
135 // pending server requests. | 147 // pending server requests. |
136 std::set<scoped_refptr<CheckClientDownloadRequest> > download_requests_; | 148 typedef std::map<const content::URLFetcher*, CheckDownloadCallback> |
| 149 DownloadRequests; |
| 150 DownloadRequests download_requests_; |
137 | 151 |
138 // Keeps track of the state of the service. | 152 // Keeps track of the state of the service. |
139 bool enabled_; | 153 bool enabled_; |
140 | 154 |
141 DISALLOW_COPY_AND_ASSIGN(DownloadProtectionService); | 155 DISALLOW_COPY_AND_ASSIGN(DownloadProtectionService); |
142 }; | 156 }; |
143 } // namespace safe_browsing | 157 } // namespace safe_browsing |
144 | 158 |
145 #endif // CHROME_BROWSER_SAFE_BROWSING_DOWNLOAD_PROTECTION_SERVICE_H_ | 159 #endif // CHROME_BROWSER_SAFE_BROWSING_DOWNLOAD_PROTECTION_SERVICE_H_ |
OLD | NEW |