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 // The Safe Browsing service is responsible for downloading anti-phishing and | 5 // The Safe Browsing service is responsible for downloading anti-phishing and |
6 // anti-malware tables and checking urls against them. | 6 // anti-malware tables and checking urls against them. |
7 | 7 |
8 #ifndef CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_SERVICE_H_ | 8 #ifndef CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_SERVICE_H_ |
9 #define CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_SERVICE_H_ | 9 #define CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_SERVICE_H_ |
10 #pragma once | 10 #pragma once |
11 | 11 |
12 #include <deque> | 12 #include <deque> |
13 #include <map> | 13 #include <map> |
14 #include <set> | 14 #include <set> |
15 #include <string> | 15 #include <string> |
16 #include <vector> | 16 #include <vector> |
17 | 17 |
18 #include "base/hash_tables.h" | 18 #include "base/hash_tables.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/observer_list.h" |
21 #include "base/synchronization/lock.h" | 22 #include "base/synchronization/lock.h" |
22 #include "base/task.h" | 23 #include "base/task.h" |
23 #include "base/time.h" | 24 #include "base/time.h" |
24 #include "chrome/browser/safe_browsing/safe_browsing_util.h" | 25 #include "chrome/browser/safe_browsing/safe_browsing_util.h" |
25 #include "content/common/notification_observer.h" | 26 #include "content/common/notification_observer.h" |
26 #include "content/common/notification_registrar.h" | 27 #include "content/common/notification_registrar.h" |
27 #include "googleurl/src/gurl.h" | 28 #include "googleurl/src/gurl.h" |
28 | 29 |
29 class MalwareDetails; | 30 class MalwareDetails; |
30 class PrefChangeRegistrar; | 31 class PrefChangeRegistrar; |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
99 // this class. | 100 // this class. |
100 // TODO(lzheng): We should consider to use this time out check | 101 // TODO(lzheng): We should consider to use this time out check |
101 // for browsing too (instead of implementin in | 102 // for browsing too (instead of implementin in |
102 // safe_browsing_resource_handler.cc). | 103 // safe_browsing_resource_handler.cc). |
103 CancelableTask* timeout_task; | 104 CancelableTask* timeout_task; |
104 | 105 |
105 private: | 106 private: |
106 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingCheck); | 107 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingCheck); |
107 }; | 108 }; |
108 | 109 |
| 110 // Observer class can be used to get notified when a SafeBrowsing hit |
| 111 // was found. |
| 112 class Observer { |
| 113 public: |
| 114 // The |resource| must not be accessed after OnSafeBrowsingHit returns. |
| 115 // This method will be called on the UI thread. |
| 116 virtual void OnSafeBrowsingHit(const UnsafeResource& resource) = 0; |
| 117 |
| 118 protected: |
| 119 Observer() {} |
| 120 virtual ~Observer() {} |
| 121 |
| 122 private: |
| 123 DISALLOW_COPY_AND_ASSIGN(Observer); |
| 124 }; |
| 125 |
109 class Client { | 126 class Client { |
110 public: | 127 public: |
111 virtual ~Client() {} | 128 virtual ~Client() {} |
112 | 129 |
113 void OnSafeBrowsingResult(const SafeBrowsingCheck& check); | 130 void OnSafeBrowsingResult(const SafeBrowsingCheck& check); |
114 | 131 |
115 // Called when the user has made a decision about how to handle the | 132 // Called when the user has made a decision about how to handle the |
116 // SafeBrowsing interstitial page. | 133 // SafeBrowsing interstitial page. |
117 virtual void OnBlockingPageComplete(bool proceed) {} | 134 virtual void OnBlockingPageComplete(bool proceed) {} |
118 | 135 |
119 protected: | 136 protected: |
120 // Called when the result of checking a browse URL is known. | 137 // Called when the result of checking a browse URL is known. |
121 virtual void OnBrowseUrlCheckResult(const GURL& url, | 138 virtual void OnBrowseUrlCheckResult(const GURL& url, |
122 UrlCheckResult result) {} | 139 UrlCheckResult result) {} |
123 | 140 |
124 // Called when the result of checking a download URL is known. | 141 // Called when the result of checking a download URL is known. |
125 virtual void OnDownloadUrlCheckResult(const std::vector<GURL>& url_chain, | 142 virtual void OnDownloadUrlCheckResult(const std::vector<GURL>& url_chain, |
126 UrlCheckResult result) {} | 143 UrlCheckResult result) {} |
127 | 144 |
128 // Called when the result of checking a download binary hash is known. | 145 // Called when the result of checking a download binary hash is known. |
129 virtual void OnDownloadHashCheckResult(const std::string& hash, | 146 virtual void OnDownloadHashCheckResult(const std::string& hash, |
130 UrlCheckResult result) {} | 147 UrlCheckResult result) {} |
131 }; | 148 }; |
132 | 149 |
133 | |
134 // Makes the passed |factory| the factory used to instanciate | 150 // Makes the passed |factory| the factory used to instanciate |
135 // a SafeBrowsingService. Useful for tests. | 151 // a SafeBrowsingService. Useful for tests. |
136 static void RegisterFactory(SafeBrowsingServiceFactory* factory) { | 152 static void RegisterFactory(SafeBrowsingServiceFactory* factory) { |
137 factory_ = factory; | 153 factory_ = factory; |
138 } | 154 } |
139 | 155 |
140 // Create an instance of the safe browsing service. | 156 // Create an instance of the safe browsing service. |
141 static SafeBrowsingService* CreateSafeBrowsingService(); | 157 static SafeBrowsingService* CreateSafeBrowsingService(); |
142 | 158 |
143 // Called on the UI thread to initialize the service. | 159 // Called on the UI thread to initialize the service. |
(...skipping 110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
254 // Report hits to the unsafe contents (malware, phishing, unsafe download URL) | 270 // Report hits to the unsafe contents (malware, phishing, unsafe download URL) |
255 // to the server. Can only be called on UI thread. If |post_data| is | 271 // to the server. Can only be called on UI thread. If |post_data| is |
256 // non-empty, the request will be sent as a POST instead of a GET. | 272 // non-empty, the request will be sent as a POST instead of a GET. |
257 virtual void ReportSafeBrowsingHit(const GURL& malicious_url, | 273 virtual void ReportSafeBrowsingHit(const GURL& malicious_url, |
258 const GURL& page_url, | 274 const GURL& page_url, |
259 const GURL& referrer_url, | 275 const GURL& referrer_url, |
260 bool is_subresource, | 276 bool is_subresource, |
261 UrlCheckResult threat_type, | 277 UrlCheckResult threat_type, |
262 const std::string& post_data); | 278 const std::string& post_data); |
263 | 279 |
| 280 // Add and remove observers. These methods must be invoked on the UI thread. |
| 281 void AddObserver(Observer* observer); |
| 282 void RemoveObserver(Observer* remove); |
| 283 |
264 protected: | 284 protected: |
265 // Creates the safe browsing service. Need to initialize before using. | 285 // Creates the safe browsing service. Need to initialize before using. |
266 SafeBrowsingService(); | 286 SafeBrowsingService(); |
267 | 287 |
268 virtual ~SafeBrowsingService(); | 288 virtual ~SafeBrowsingService(); |
269 | 289 |
270 private: | 290 private: |
271 friend class SafeBrowsingServiceFactoryImpl; | 291 friend class SafeBrowsingServiceFactoryImpl; |
272 | 292 |
273 typedef std::set<SafeBrowsingCheck*> CurrentChecks; | 293 typedef std::set<SafeBrowsingCheck*> CurrentChecks; |
(...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
482 | 502 |
483 std::deque<QueuedCheck> queued_checks_; | 503 std::deque<QueuedCheck> queued_checks_; |
484 | 504 |
485 // When download url check takes this long, client's callback will be called | 505 // When download url check takes this long, client's callback will be called |
486 // without waiting for the result. | 506 // without waiting for the result. |
487 int64 download_urlcheck_timeout_ms_; | 507 int64 download_urlcheck_timeout_ms_; |
488 | 508 |
489 // Similar to |download_urlcheck_timeout_ms_|, but for download hash checks. | 509 // Similar to |download_urlcheck_timeout_ms_|, but for download hash checks. |
490 int64 download_hashcheck_timeout_ms_; | 510 int64 download_hashcheck_timeout_ms_; |
491 | 511 |
| 512 ObserverList<Observer> observer_list_; |
| 513 |
492 // Used to track purge memory notifications. Lives on the IO thread. | 514 // Used to track purge memory notifications. Lives on the IO thread. |
493 NotificationRegistrar registrar_; | 515 NotificationRegistrar registrar_; |
494 | 516 |
495 // Tracks existing PrefServices, and the safe browsing preference on each. | 517 // Tracks existing PrefServices, and the safe browsing preference on each. |
496 // This is used to determine if any profile is currently using the safe | 518 // This is used to determine if any profile is currently using the safe |
497 // browsing service, and to start it up or shut it down accordingly. | 519 // browsing service, and to start it up or shut it down accordingly. |
498 std::map<PrefService*, PrefChangeRegistrar*> prefs_map_; | 520 std::map<PrefService*, PrefChangeRegistrar*> prefs_map_; |
499 | 521 |
500 // Used to track creation and destruction of profiles on the UI thread. | 522 // Used to track creation and destruction of profiles on the UI thread. |
501 NotificationRegistrar prefs_registrar_; | 523 NotificationRegistrar prefs_registrar_; |
502 | 524 |
503 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingService); | 525 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingService); |
504 }; | 526 }; |
505 | 527 |
506 // Factory for creating SafeBrowsingService. Useful for tests. | 528 // Factory for creating SafeBrowsingService. Useful for tests. |
507 class SafeBrowsingServiceFactory { | 529 class SafeBrowsingServiceFactory { |
508 public: | 530 public: |
509 SafeBrowsingServiceFactory() { } | 531 SafeBrowsingServiceFactory() { } |
510 virtual ~SafeBrowsingServiceFactory() { } | 532 virtual ~SafeBrowsingServiceFactory() { } |
511 virtual SafeBrowsingService* CreateSafeBrowsingService() = 0; | 533 virtual SafeBrowsingService* CreateSafeBrowsingService() = 0; |
512 private: | 534 private: |
513 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingServiceFactory); | 535 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingServiceFactory); |
514 }; | 536 }; |
515 | 537 |
516 #endif // CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_SERVICE_H_ | 538 #endif // CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_SERVICE_H_ |
OLD | NEW |