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 <map> | 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/gtest_prod_util.h" | 18 #include "base/gtest_prod_util.h" |
19 #include "base/memory/ref_counted.h" | 19 #include "base/memory/ref_counted.h" |
20 #include "base/memory/scoped_ptr.h" | 20 #include "base/memory/scoped_ptr.h" |
| 21 #include "base/memory/weak_ptr.h" |
21 #include "base/time.h" | 22 #include "base/time.h" |
22 #include "content/common/net/url_fetcher.h" | 23 #include "content/common/net/url_fetcher.h" |
23 #include "googleurl/src/gurl.h" | 24 #include "googleurl/src/gurl.h" |
24 | 25 |
25 namespace net { | 26 namespace net { |
26 class URLRequestContextGetter; | 27 class URLRequestContextGetter; |
27 class URLRequestStatus; | 28 class URLRequestStatus; |
28 } // namespace net | 29 } // namespace net |
29 class SafeBrowsingService; | 30 class SafeBrowsingService; |
30 | 31 |
31 namespace safe_browsing { | 32 namespace safe_browsing { |
32 | 33 |
33 // This class provides an asynchronous API to check whether a particular | 34 // This class provides an asynchronous API to check whether a particular |
34 // client download is malicious or not. | 35 // client download is malicious or not. |
35 class DownloadProtectionService | 36 class DownloadProtectionService |
36 : public base::RefCountedThreadSafe<DownloadProtectionService>, | 37 : public base::RefCountedThreadSafe<DownloadProtectionService>, |
37 public URLFetcher::Delegate { | 38 public URLFetcher::Delegate { |
38 public: | 39 public: |
39 // TODO(noelutz): we're missing some fields here: filename to get | 40 // TODO(noelutz): we're missing some fields here: server IPs, |
40 // the signature, server IPs, tab URL redirect chain, ... | 41 // tab URL redirect chain, ... |
41 struct DownloadInfo { | 42 struct DownloadInfo { |
| 43 FilePath local_file; |
42 std::vector<GURL> download_url_chain; | 44 std::vector<GURL> download_url_chain; |
43 GURL referrer_url; | 45 GURL referrer_url; |
44 std::string sha256_hash; | 46 std::string sha256_hash; |
45 int64 total_bytes; | 47 int64 total_bytes; |
46 bool user_initiated; | 48 bool user_initiated; |
47 DownloadInfo(); | 49 DownloadInfo(); |
48 ~DownloadInfo(); | 50 ~DownloadInfo(); |
49 }; | 51 }; |
50 | 52 |
51 enum DownloadCheckResult { | 53 enum DownloadCheckResult { |
52 SAFE, | 54 SAFE, |
53 MALICIOUS, | 55 MALICIOUS, |
54 // In the future we may introduce a third category which corresponds to | 56 // In the future we may introduce a third category which corresponds to |
55 // suspicious downloads that are not known to be malicious. | 57 // suspicious downloads that are not known to be malicious. |
56 }; | 58 }; |
57 | 59 |
58 // Callback type which is invoked once the download request is done. | 60 // Callback type which is invoked once the download request is done. |
59 typedef base::Callback<void(DownloadCheckResult)> CheckDownloadCallback; | 61 typedef base::Callback<void(DownloadCheckResult)> CheckDownloadCallback; |
60 | 62 |
61 // Creates a download service. The service is initially disabled. You need | 63 // Creates a download service. The service is initially disabled. You need |
62 // to call SetEnabled() to start it. We keep scoped references to both of | 64 // to call SetEnabled() to start it. We keep scoped references to both of |
63 // these objects. | 65 // these objects. |
64 DownloadProtectionService( | 66 DownloadProtectionService( |
65 SafeBrowsingService* sb_service, | 67 const base::WeakPtr<SafeBrowsingService>& sb_service, |
66 net::URLRequestContextGetter* request_context_getter); | 68 net::URLRequestContextGetter* request_context_getter); |
67 | 69 |
68 // From the URLFetcher::Delegate interface. | 70 // From the URLFetcher::Delegate interface. |
69 virtual void OnURLFetchComplete(const URLFetcher* source, | 71 virtual void OnURLFetchComplete(const URLFetcher* source, |
70 const GURL& url, | 72 const GURL& url, |
71 const net::URLRequestStatus& status, | 73 const net::URLRequestStatus& status, |
72 int response_code, | 74 int response_code, |
73 const net::ResponseCookies& cookies, | 75 const net::ResponseCookies& cookies, |
74 const std::string& data) OVERRIDE; | 76 const std::string& data) OVERRIDE; |
75 | 77 |
(...skipping 24 matching lines...) Expand all Loading... |
100 // Enum to keep track why a particular download verdict was chosen. | 102 // Enum to keep track why a particular download verdict was chosen. |
101 // This is used to keep some stats around. | 103 // This is used to keep some stats around. |
102 enum DownloadCheckResultReason { | 104 enum DownloadCheckResultReason { |
103 REASON_INVALID_URL, | 105 REASON_INVALID_URL, |
104 REASON_SB_DISABLED, | 106 REASON_SB_DISABLED, |
105 REASON_WHITELISTED_URL, | 107 REASON_WHITELISTED_URL, |
106 REASON_WHITELISTED_REFERRER, | 108 REASON_WHITELISTED_REFERRER, |
107 REASON_INVALID_REQUEST_PROTO, | 109 REASON_INVALID_REQUEST_PROTO, |
108 REASON_SERVER_PING_FAILED, | 110 REASON_SERVER_PING_FAILED, |
109 REASON_INVALID_RESPONSE_PROTO, | 111 REASON_INVALID_RESPONSE_PROTO, |
| 112 REASON_NOT_BINARY_FILE, |
110 REASON_MAX // Always add new values before this one. | 113 REASON_MAX // Always add new values before this one. |
111 }; | 114 }; |
112 | 115 |
113 virtual ~DownloadProtectionService(); | 116 virtual ~DownloadProtectionService(); |
114 | 117 |
115 private: | 118 private: |
116 friend class base::RefCountedThreadSafe<DownloadProtectionService>; | 119 friend class base::RefCountedThreadSafe<DownloadProtectionService>; |
117 friend class DownloadProtectionServiceTest; | 120 friend class DownloadProtectionServiceTest; |
118 FRIEND_TEST_ALL_PREFIXES(DownloadProtectionServiceTest, | 121 FRIEND_TEST_ALL_PREFIXES(DownloadProtectionServiceTest, |
119 CheckClientDownloadValidateRequest); | 122 CheckClientDownloadValidateRequest); |
120 FRIEND_TEST_ALL_PREFIXES(DownloadProtectionServiceTest, | 123 FRIEND_TEST_ALL_PREFIXES(DownloadProtectionServiceTest, |
121 CheckClientDownloadSuccess); | 124 CheckClientDownloadSuccess); |
122 FRIEND_TEST_ALL_PREFIXES(DownloadProtectionServiceTest, | 125 FRIEND_TEST_ALL_PREFIXES(DownloadProtectionServiceTest, |
123 CheckClientDownloadFetchFailed); | 126 CheckClientDownloadFetchFailed); |
124 | 127 |
125 static const char kDownloadRequestUrl[]; | 128 static const char kDownloadRequestUrl[]; |
126 | 129 |
| 130 // Extracts features from the downloaded file. Runs in the file thread. |
| 131 // When finished, invokes StartCheckClientDownload on the IO thread. |
| 132 void ExtractFileFeatures(const DownloadInfo& info, |
| 133 const CheckDownloadCallback& callback); |
| 134 |
127 // Same as above but this method is called on the IO thread after we have | 135 // Same as above but this method is called on the IO thread after we have |
128 // done some basic checks to see whether the download is definitely not | 136 // done some basic checks to see whether the download is definitely not |
129 // safe. | 137 // safe. |
130 void StartCheckClientDownload(const DownloadInfo& info, | 138 void StartCheckClientDownload(const DownloadInfo& info, |
131 const CheckDownloadCallback& callback); | 139 const CheckDownloadCallback& callback); |
132 | 140 |
133 // This function must run on the UI thread and will invoke the callback | 141 // This function must run on the UI thread and will invoke the callback |
134 // with the given result. | 142 // with the given result. |
135 void EndCheckClientDownload(DownloadCheckResult result, | 143 void EndCheckClientDownload(DownloadCheckResult result, |
136 DownloadCheckResultReason reason, | 144 DownloadCheckResultReason reason, |
137 const CheckDownloadCallback& callback); | 145 const CheckDownloadCallback& callback); |
138 | 146 |
139 void RecordStats(DownloadCheckResultReason reason); | 147 void RecordStats(DownloadCheckResultReason reason); |
140 | 148 |
141 // SetEnabled(bool) calls this method on the IO thread. | 149 // SetEnabled(bool) calls this method on the IO thread. |
142 void SetEnabledOnIOThread(bool enableed); | 150 void SetEnabledOnIOThread(bool enableed); |
143 | 151 |
144 // This pointer may be NULL if SafeBrowsing is disabled. | 152 // This pointer may be NULL if SafeBrowsing is disabled. |
145 scoped_refptr<SafeBrowsingService> sb_service_; | 153 base::WeakPtr<SafeBrowsingService> sb_service_; |
146 | 154 |
147 // The context we use to issue network requests. | 155 // The context we use to issue network requests. |
148 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; | 156 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; |
149 | 157 |
150 // Map of client download request to the corresponding callback that | 158 // Map of client download request to the corresponding callback that |
151 // has to be invoked when the request is done. This map contains all | 159 // has to be invoked when the request is done. This map contains all |
152 // pending server requests. | 160 // pending server requests. |
153 std::map<const URLFetcher*, CheckDownloadCallback> download_requests_; | 161 std::map<const URLFetcher*, CheckDownloadCallback> download_requests_; |
154 | 162 |
155 // Keeps track of the state of the service. | 163 // Keeps track of the state of the service. |
156 bool enabled_; | 164 bool enabled_; |
157 | 165 |
158 DISALLOW_COPY_AND_ASSIGN(DownloadProtectionService); | 166 DISALLOW_COPY_AND_ASSIGN(DownloadProtectionService); |
159 }; | 167 }; |
160 } // namespace safe_browsing | 168 } // namespace safe_browsing |
161 | 169 |
162 #endif // CHROME_BROWSER_SAFE_BROWSING_DOWNLOAD_PROTECTION_SERVICE_H_ | 170 #endif // CHROME_BROWSER_SAFE_BROWSING_DOWNLOAD_PROTECTION_SERVICE_H_ |
OLD | NEW |