OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 is used to fetch the client-side | 6 // client-side phishing detection. This class is used to fetch the client-side |
7 // model and send it to all renderers. This class is also used to send a ping | 7 // model and send it to all renderers. This class is also used to send a ping |
8 // back to Google to verify if a particular site is really phishing or not. | 8 // back to 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 be made on the UI | 10 // This class is not thread-safe and expects all calls to be made on the UI |
11 // thread. We also expect that the calling thread runs a message loop. | 11 // thread. We also expect that the calling thread runs a message loop. |
12 | 12 |
13 #ifndef CHROME_BROWSER_SAFE_BROWSING_CLIENT_SIDE_DETECTION_SERVICE_H_ | 13 #ifndef CHROME_BROWSER_SAFE_BROWSING_CLIENT_SIDE_DETECTION_SERVICE_H_ |
14 #define CHROME_BROWSER_SAFE_BROWSING_CLIENT_SIDE_DETECTION_SERVICE_H_ | 14 #define CHROME_BROWSER_SAFE_BROWSING_CLIENT_SIDE_DETECTION_SERVICE_H_ |
15 | 15 |
16 #include <map> | 16 #include <map> |
17 #include <memory> | 17 #include <memory> |
18 #include <queue> | 18 #include <queue> |
19 #include <set> | 19 #include <set> |
20 #include <string> | 20 #include <string> |
21 #include <utility> | 21 #include <utility> |
22 #include <vector> | 22 #include <vector> |
23 | 23 |
24 #include "base/callback_forward.h" | 24 #include "base/callback_forward.h" |
25 #include "base/gtest_prod_util.h" | 25 #include "base/gtest_prod_util.h" |
26 #include "base/macros.h" | 26 #include "base/macros.h" |
27 #include "base/memory/linked_ptr.h" | |
28 #include "base/memory/ref_counted.h" | 27 #include "base/memory/ref_counted.h" |
29 #include "base/memory/weak_ptr.h" | 28 #include "base/memory/weak_ptr.h" |
30 #include "base/time/time.h" | 29 #include "base/time/time.h" |
31 #include "chrome/browser/safe_browsing/client_side_model_loader.h" | 30 #include "chrome/browser/safe_browsing/client_side_model_loader.h" |
32 #include "content/public/browser/browser_thread.h" | 31 #include "content/public/browser/browser_thread.h" |
33 #include "content/public/browser/notification_observer.h" | 32 #include "content/public/browser/notification_observer.h" |
34 #include "content/public/browser/notification_registrar.h" | 33 #include "content/public/browser/notification_registrar.h" |
35 #include "net/url_request/url_fetcher_delegate.h" | 34 #include "net/url_request/url_fetcher_delegate.h" |
36 #include "url/gurl.h" | 35 #include "url/gurl.h" |
37 | 36 |
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
156 SetEnabledAndRefreshState); | 155 SetEnabledAndRefreshState); |
157 | 156 |
158 // CacheState holds all information necessary to respond to a caller without | 157 // CacheState holds all information necessary to respond to a caller without |
159 // actually making a HTTP request. | 158 // actually making a HTTP request. |
160 struct CacheState { | 159 struct CacheState { |
161 bool is_phishing; | 160 bool is_phishing; |
162 base::Time timestamp; | 161 base::Time timestamp; |
163 | 162 |
164 CacheState(bool phish, base::Time time); | 163 CacheState(bool phish, base::Time time); |
165 }; | 164 }; |
166 typedef std::map<GURL, linked_ptr<CacheState> > PhishingCache; | |
167 | 165 |
168 static const char kClientReportMalwareUrl[]; | 166 static const char kClientReportMalwareUrl[]; |
169 static const char kClientReportPhishingUrl[]; | 167 static const char kClientReportPhishingUrl[]; |
170 static const int kMaxReportsPerInterval; | 168 static const int kMaxReportsPerInterval; |
171 static const int kInitialClientModelFetchDelayMs; | 169 static const int kInitialClientModelFetchDelayMs; |
172 static const int kReportsIntervalDays; | 170 static const int kReportsIntervalDays; |
173 static const int kNegativeCacheIntervalDays; | 171 static const int kNegativeCacheIntervalDays; |
174 static const int kPositiveCacheIntervalMinutes; | 172 static const int kPositiveCacheIntervalMinutes; |
175 | 173 |
176 // Starts sending the request to the client-side detection frontends. | 174 // Starts sending the request to the client-side detection frontends. |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
223 // it won't download the model nor report detected phishing URLs. | 221 // it won't download the model nor report detected phishing URLs. |
224 bool enabled_; | 222 bool enabled_; |
225 | 223 |
226 // We load two models: One for stadard Safe Browsing profiles, | 224 // We load two models: One for stadard Safe Browsing profiles, |
227 // and one for those opted into extended reporting. | 225 // and one for those opted into extended reporting. |
228 std::unique_ptr<ModelLoader> model_loader_standard_; | 226 std::unique_ptr<ModelLoader> model_loader_standard_; |
229 std::unique_ptr<ModelLoader> model_loader_extended_; | 227 std::unique_ptr<ModelLoader> model_loader_extended_; |
230 | 228 |
231 // Map of client report phishing request to the corresponding callback that | 229 // Map of client report phishing request to the corresponding callback that |
232 // has to be invoked when the request is done. | 230 // has to be invoked when the request is done. |
233 struct ClientReportInfo; | 231 struct ClientPhishingReportInfo; |
234 std::map<const net::URLFetcher*, ClientReportInfo*> | 232 std::map<const net::URLFetcher*, std::unique_ptr<ClientPhishingReportInfo>> |
235 client_phishing_reports_; | 233 client_phishing_reports_; |
236 // Map of client malware ip request to the corresponding callback that | 234 // Map of client malware ip request to the corresponding callback that |
237 // has to be invoked when the request is done. | 235 // has to be invoked when the request is done. |
238 struct ClientMalwareReportInfo; | 236 struct ClientMalwareReportInfo; |
239 std::map<const net::URLFetcher*, ClientMalwareReportInfo*> | 237 std::map<const net::URLFetcher*, std::unique_ptr<ClientMalwareReportInfo>> |
240 client_malware_reports_; | 238 client_malware_reports_; |
241 | 239 |
242 // Cache of completed requests. Used to satisfy requests for the same urls | 240 // Cache of completed requests. Used to satisfy requests for the same urls |
243 // as long as the next request falls within our caching window (which is | 241 // as long as the next request falls within our caching window (which is |
244 // determined by kNegativeCacheInterval and kPositiveCacheInterval). The | 242 // determined by kNegativeCacheInterval and kPositiveCacheInterval). The |
245 // size of this cache is limited by kMaxReportsPerDay * | 243 // size of this cache is limited by kMaxReportsPerDay * |
246 // ceil(InDays(max(kNegativeCacheInterval, kPositiveCacheInterval))). | 244 // ceil(InDays(max(kNegativeCacheInterval, kPositiveCacheInterval))). |
247 // TODO(gcasto): Serialize this so that it doesn't reset on browser restart. | 245 // TODO(gcasto): Serialize this so that it doesn't reset on browser restart. |
248 PhishingCache cache_; | 246 std::map<GURL, std::unique_ptr<CacheState>> cache_; |
249 | 247 |
250 // Timestamp of when we sent a phishing request. Used to limit the number | 248 // Timestamp of when we sent a phishing request. Used to limit the number |
251 // of phishing requests that we send in a day. | 249 // of phishing requests that we send in a day. |
252 // TODO(gcasto): Serialize this so that it doesn't reset on browser restart. | 250 // TODO(gcasto): Serialize this so that it doesn't reset on browser restart. |
253 std::queue<base::Time> phishing_report_times_; | 251 std::queue<base::Time> phishing_report_times_; |
254 | 252 |
255 // Timestamp of when we sent a malware request. Used to limit the number | 253 // Timestamp of when we sent a malware request. Used to limit the number |
256 // of malware requests that we send in a day. | 254 // of malware requests that we send in a day. |
257 std::queue<base::Time> malware_report_times_; | 255 std::queue<base::Time> malware_report_times_; |
258 | 256 |
259 // The context we use to issue network requests. | 257 // The context we use to issue network requests. |
260 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; | 258 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; |
261 | 259 |
262 content::NotificationRegistrar registrar_; | 260 content::NotificationRegistrar registrar_; |
263 | 261 |
264 // Used to asynchronously call the callbacks for | 262 // Used to asynchronously call the callbacks for |
265 // SendClientReportPhishingRequest. | 263 // SendClientReportPhishingRequest. |
266 base::WeakPtrFactory<ClientSideDetectionService> weak_factory_; | 264 base::WeakPtrFactory<ClientSideDetectionService> weak_factory_; |
267 | 265 |
268 DISALLOW_COPY_AND_ASSIGN(ClientSideDetectionService); | 266 DISALLOW_COPY_AND_ASSIGN(ClientSideDetectionService); |
269 }; | 267 }; |
270 | 268 |
271 } // namespace safe_browsing | 269 } // namespace safe_browsing |
272 | 270 |
273 #endif // CHROME_BROWSER_SAFE_BROWSING_CLIENT_SIDE_DETECTION_SERVICE_H_ | 271 #endif // CHROME_BROWSER_SAFE_BROWSING_CLIENT_SIDE_DETECTION_SERVICE_H_ |
OLD | NEW |