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 backends for | 5 // Helper class which handles communication with the SafeBrowsing backends for |
6 // client-side phishing detection. This class can be used to get a file | 6 // client-side phishing detection. This class can be used to get a file |
7 // descriptor to the client-side phishing model and also to send a ping back to | 7 // descriptor to the client-side phishing model and also to send a ping back to |
8 // Google to verify if a particular site is really phishing or not. | 8 // Google to verify if a particular site is really phishing or not. |
9 // | 9 // |
10 // This class is not thread-safe and expects all calls to GetModelFile() and | 10 // This class is not thread-safe and expects all calls to GetModelFile() and |
11 // SendClientReportPhishingRequest() to be made on the UI thread. We also | 11 // SendClientReportPhishingRequest() to be made on the UI thread. We also |
12 // expect that the calling thread runs a message loop and that there is a FILE | 12 // expect that the calling thread runs a message loop and that there is a FILE |
13 // thread running to execute asynchronous file operations. | 13 // thread running to execute asynchronous file operations. |
14 | 14 |
15 #ifndef CHROME_BROWSER_SAFE_BROWSING_CLIENT_SIDE_DETECTION_SERVICE_H_ | 15 #ifndef CHROME_BROWSER_SAFE_BROWSING_CLIENT_SIDE_DETECTION_SERVICE_H_ |
16 #define CHROME_BROWSER_SAFE_BROWSING_CLIENT_SIDE_DETECTION_SERVICE_H_ | 16 #define CHROME_BROWSER_SAFE_BROWSING_CLIENT_SIDE_DETECTION_SERVICE_H_ |
17 #pragma once | 17 #pragma once |
18 | 18 |
19 #include <map> | 19 #include <map> |
20 #include <queue> | 20 #include <queue> |
21 #include <string> | 21 #include <string> |
22 #include <vector> | 22 #include <vector> |
23 | 23 |
24 #include "base/basictypes.h" | 24 #include "base/basictypes.h" |
25 #include "base/callback.h" | 25 #include "base/callback.h" |
26 #include "base/file_path.h" | 26 #include "base/file_path.h" |
27 #include "base/gtest_prod_util.h" | 27 #include "base/gtest_prod_util.h" |
28 #include "base/linked_ptr.h" | |
28 #include "base/platform_file.h" | 29 #include "base/platform_file.h" |
29 #include "base/ref_counted.h" | 30 #include "base/ref_counted.h" |
30 #include "base/scoped_callback_factory.h" | 31 #include "base/scoped_callback_factory.h" |
31 #include "base/scoped_ptr.h" | 32 #include "base/scoped_ptr.h" |
32 #include "base/task.h" | 33 #include "base/task.h" |
33 #include "base/time.h" | 34 #include "base/time.h" |
34 #include "chrome/browser/safe_browsing/csd.pb.h" | 35 #include "chrome/browser/safe_browsing/csd.pb.h" |
35 #include "chrome/common/net/url_fetcher.h" | 36 #include "chrome/common/net/url_fetcher.h" |
36 #include "googleurl/src/gurl.h" | 37 #include "googleurl/src/gurl.h" |
37 | 38 |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
92 | 93 |
93 enum ModelStatus { | 94 enum ModelStatus { |
94 // It's unclear whether or not the model was already fetched. | 95 // It's unclear whether or not the model was already fetched. |
95 UNKNOWN_STATUS, | 96 UNKNOWN_STATUS, |
96 // Model is fetched and is stored on disk. | 97 // Model is fetched and is stored on disk. |
97 READY_STATUS, | 98 READY_STATUS, |
98 // Error occured during fetching or writing. | 99 // Error occured during fetching or writing. |
99 ERROR_STATUS, | 100 ERROR_STATUS, |
100 }; | 101 }; |
101 | 102 |
103 // CacheState holds all information necessary to respond to a caller without | |
104 // actually making a HTTP request. | |
105 struct CacheState { | |
106 bool is_phishing; | |
107 base::Time timestamp; | |
108 | |
109 CacheState(bool phish, base::Time time) | |
110 : is_phishing(phish), | |
111 timestamp(time) {} | |
Brian Ryner
2011/02/08 20:16:48
The style guidelines discourage inlining construct
gcasto (DO NOT USE)
2011/02/09 00:10:52
Moved.
| |
112 }; | |
113 typedef std::map<GURL, linked_ptr<CacheState> > PhishingCache; | |
114 | |
102 static const char kClientReportPhishingUrl[]; | 115 static const char kClientReportPhishingUrl[]; |
103 static const char kClientModelUrl[]; | 116 static const char kClientModelUrl[]; |
104 static const int kMaxReportsPerDay; | 117 static const int kMaxReportsPerDay; |
118 static const base::TimeDelta kNegativeCacheInterval; | |
119 static const base::TimeDelta kPositiveCacheInterval; | |
105 | 120 |
106 // Use Create() method to create an instance of this object. | 121 // Use Create() method to create an instance of this object. |
107 ClientSideDetectionService(const FilePath& model_path, | 122 ClientSideDetectionService(const FilePath& model_path, |
108 URLRequestContextGetter* request_context_getter); | 123 URLRequestContextGetter* request_context_getter); |
109 | 124 |
110 // Sets the model status and invokes all the pending callbacks in | 125 // Sets the model status and invokes all the pending callbacks in |
111 // |open_callbacks_| with the current |model_file_| as parameter. | 126 // |open_callbacks_| with the current |model_file_| as parameter. |
112 void SetModelStatus(ModelStatus status); | 127 void SetModelStatus(ModelStatus status); |
113 | 128 |
114 // Called once the initial open() of the model file is done. If the file | 129 // Called once the initial open() of the model file is done. If the file |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
158 | 173 |
159 // Called by OnURLFetchComplete to handle the server response from | 174 // Called by OnURLFetchComplete to handle the server response from |
160 // sending the client-side phishing request. | 175 // sending the client-side phishing request. |
161 void HandlePhishingVerdict(const URLFetcher* source, | 176 void HandlePhishingVerdict(const URLFetcher* source, |
162 const GURL& url, | 177 const GURL& url, |
163 const net::URLRequestStatus& status, | 178 const net::URLRequestStatus& status, |
164 int response_code, | 179 int response_code, |
165 const ResponseCookies& cookies, | 180 const ResponseCookies& cookies, |
166 const std::string& data); | 181 const std::string& data); |
167 | 182 |
183 // Returns true and sets is_phishing if url is in the cache and valid. | |
184 bool GetCachedResult(GURL url, bool* is_phishing); | |
Brian Ryner
2011/02/08 20:16:48
Have this take a const GURL&, so that it doesn't n
gcasto (DO NOT USE)
2011/02/09 00:10:52
Done.
| |
185 | |
186 // Invalidate cache results which are no longer useful. | |
187 void UpdateCache(); | |
188 | |
168 // Get the number of phishing reports that we have sent over the last 24 | 189 // Get the number of phishing reports that we have sent over the last 24 |
169 // hours. | 190 // hours. |
170 int GetNumReportsPerDay(); | 191 int GetNumReportsPerDay(); |
171 | 192 |
172 FilePath model_path_; | 193 FilePath model_path_; |
173 ModelStatus model_status_; | 194 ModelStatus model_status_; |
174 base::PlatformFile model_file_; | 195 base::PlatformFile model_file_; |
175 scoped_ptr<URLFetcher> model_fetcher_; | 196 scoped_ptr<URLFetcher> model_fetcher_; |
176 scoped_ptr<std::string> tmp_model_string_; | 197 scoped_ptr<std::string> tmp_model_string_; |
177 std::vector<OpenModelDoneCallback*> open_callbacks_; | 198 std::vector<OpenModelDoneCallback*> open_callbacks_; |
178 | 199 |
179 // Map of client report phishing request to the corresponding callback that | 200 // Map of client report phishing request to the corresponding callback that |
180 // has to be invoked when the request is done. | 201 // has to be invoked when the request is done. |
181 struct ClientReportInfo; | 202 struct ClientReportInfo; |
182 std::map<const URLFetcher*, ClientReportInfo*> client_phishing_reports_; | 203 std::map<const URLFetcher*, ClientReportInfo*> client_phishing_reports_; |
183 | 204 |
205 // Cache of completed requests. Used to satisfy requests for the same urls | |
206 // as long as the next request falls within our caching window (which is | |
207 // determined by kNegativeCacheInterval and kPositiveCacheInterval). | |
208 // TODO(gcasto): Serialize this so that it doesn't reset on browser restart. | |
209 PhishingCache cache_; | |
210 | |
184 // Timestamp of when we sent a phishing request. Used to limit the number | 211 // Timestamp of when we sent a phishing request. Used to limit the number |
185 // of phishing requests that we send in a day. | 212 // of phishing requests that we send in a day. |
186 // TODO(gcasto): Serialize this so that it doesn't reset on browser restart. | 213 // TODO(gcasto): Serialize this so that it doesn't reset on browser restart. |
187 std::queue<base::Time> phishing_report_times_; | 214 std::queue<base::Time> phishing_report_times_; |
188 | 215 |
189 // Used to asynchronously call the callbacks for GetModelFile and | 216 // Used to asynchronously call the callbacks for GetModelFile and |
190 // SendClientReportPhishingRequest. | 217 // SendClientReportPhishingRequest. |
191 ScopedRunnableMethodFactory<ClientSideDetectionService> method_factory_; | 218 ScopedRunnableMethodFactory<ClientSideDetectionService> method_factory_; |
192 | 219 |
193 // The client-side detection service object (this) might go away before some | 220 // The client-side detection service object (this) might go away before some |
194 // of the callbacks are done (e.g., asynchronous file operations). The | 221 // of the callbacks are done (e.g., asynchronous file operations). The |
195 // callback factory will revoke all pending callbacks if this goes away to | 222 // callback factory will revoke all pending callbacks if this goes away to |
196 // avoid a crash. | 223 // avoid a crash. |
197 base::ScopedCallbackFactory<ClientSideDetectionService> callback_factory_; | 224 base::ScopedCallbackFactory<ClientSideDetectionService> callback_factory_; |
198 | 225 |
199 // The context we use to issue network requests. | 226 // The context we use to issue network requests. |
200 scoped_refptr<URLRequestContextGetter> request_context_getter_; | 227 scoped_refptr<URLRequestContextGetter> request_context_getter_; |
201 | 228 |
202 DISALLOW_COPY_AND_ASSIGN(ClientSideDetectionService); | 229 DISALLOW_COPY_AND_ASSIGN(ClientSideDetectionService); |
203 }; | 230 }; |
204 | 231 |
205 } // namepsace safe_browsing | 232 } // namepsace safe_browsing |
206 | 233 |
207 #endif // CHROME_BROWSER_SAFE_BROWSING_CLIENT_SIDE_DETECTION_SERVICE_H_ | 234 #endif // CHROME_BROWSER_SAFE_BROWSING_CLIENT_SIDE_DETECTION_SERVICE_H_ |
OLD | NEW |