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

Side by Side Diff: chrome/browser/safe_browsing/client_side_detection_service.h

Issue 6374017: Add caching to phishing client side detection. (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Add UMA stat for not being able to serialize request. Created 9 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | chrome/browser/safe_browsing/client_side_detection_service.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 };
111 typedef std::map<GURL, linked_ptr<CacheState> > PhishingCache;
112
102 static const char kClientReportPhishingUrl[]; 113 static const char kClientReportPhishingUrl[];
103 static const char kClientModelUrl[]; 114 static const char kClientModelUrl[];
104 static const int kMaxReportsPerDay; 115 static const int kMaxReportsPerInterval;
116 static const base::TimeDelta kReportsInterval;
117 static const base::TimeDelta kNegativeCacheInterval;
118 static const base::TimeDelta kPositiveCacheInterval;
105 119
106 // Use Create() method to create an instance of this object. 120 // Use Create() method to create an instance of this object.
107 ClientSideDetectionService(const FilePath& model_path, 121 ClientSideDetectionService(const FilePath& model_path,
108 URLRequestContextGetter* request_context_getter); 122 URLRequestContextGetter* request_context_getter);
109 123
110 // Sets the model status and invokes all the pending callbacks in 124 // Sets the model status and invokes all the pending callbacks in
111 // |open_callbacks_| with the current |model_file_| as parameter. 125 // |open_callbacks_| with the current |model_file_| as parameter.
112 void SetModelStatus(ModelStatus status); 126 void SetModelStatus(ModelStatus status);
113 127
114 // Called once the initial open() of the model file is done. If the file 128 // 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
158 172
159 // Called by OnURLFetchComplete to handle the server response from 173 // Called by OnURLFetchComplete to handle the server response from
160 // sending the client-side phishing request. 174 // sending the client-side phishing request.
161 void HandlePhishingVerdict(const URLFetcher* source, 175 void HandlePhishingVerdict(const URLFetcher* source,
162 const GURL& url, 176 const GURL& url,
163 const net::URLRequestStatus& status, 177 const net::URLRequestStatus& status,
164 int response_code, 178 int response_code,
165 const ResponseCookies& cookies, 179 const ResponseCookies& cookies,
166 const std::string& data); 180 const std::string& data);
167 181
168 // Get the number of phishing reports that we have sent over the last 24 182 // Returns true and sets is_phishing if url is in the cache and valid.
169 // hours. 183 bool GetCachedResult(const GURL& url, bool* is_phishing);
170 int GetNumReportsPerDay(); 184
185 // Invalidate cache results which are no longer useful.
186 void UpdateCache();
187
188 // Get the number of phishing reports that we have sent over kReportsInterval
189 int GetNumReports();
171 190
172 FilePath model_path_; 191 FilePath model_path_;
173 ModelStatus model_status_; 192 ModelStatus model_status_;
174 base::PlatformFile model_file_; 193 base::PlatformFile model_file_;
175 scoped_ptr<URLFetcher> model_fetcher_; 194 scoped_ptr<URLFetcher> model_fetcher_;
176 scoped_ptr<std::string> tmp_model_string_; 195 scoped_ptr<std::string> tmp_model_string_;
177 std::vector<OpenModelDoneCallback*> open_callbacks_; 196 std::vector<OpenModelDoneCallback*> open_callbacks_;
178 197
179 // Map of client report phishing request to the corresponding callback that 198 // Map of client report phishing request to the corresponding callback that
180 // has to be invoked when the request is done. 199 // has to be invoked when the request is done.
181 struct ClientReportInfo; 200 struct ClientReportInfo;
182 std::map<const URLFetcher*, ClientReportInfo*> client_phishing_reports_; 201 std::map<const URLFetcher*, ClientReportInfo*> client_phishing_reports_;
183 202
203 // Cache of completed requests. Used to satisfy requests for the same urls
204 // as long as the next request falls within our caching window (which is
205 // determined by kNegativeCacheInterval and kPositiveCacheInterval). The
206 // size of this cache is limited by kMaxReportsPerDay *
207 // ceil(InDays(max(kNegativeCacheInterval, 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_
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/safe_browsing/client_side_detection_service.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698