Chromium Code Reviews| 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 // 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 | 10 |
| 11 #include <map> | 11 #include <map> |
| 12 #include <memory> | 12 #include <memory> |
| 13 #include <string> | 13 #include <string> |
| 14 | 14 |
| 15 #include "base/callback.h" | 15 #include "base/callback.h" |
| 16 #include "base/callback_list.h" | |
| 17 #include "base/files/file_path.h" | 16 #include "base/files/file_path.h" |
| 18 #include "base/macros.h" | 17 #include "base/macros.h" |
| 19 #include "base/memory/ref_counted.h" | |
| 20 #include "base/observer_list.h" | |
| 21 #include "base/sequenced_task_runner_helpers.h" | 18 #include "base/sequenced_task_runner_helpers.h" |
| 22 #include "chrome/browser/safe_browsing/services_delegate.h" | 19 #include "chrome/browser/safe_browsing/services_delegate.h" |
| 23 #include "components/safe_browsing_db/util.h" | 20 #include "components/safe_browsing/base_safe_browsing_service.h" |
| 24 #include "content/public/browser/browser_thread.h" | |
| 25 #include "content/public/browser/notification_observer.h" | |
| 26 #include "content/public/browser/notification_registrar.h" | 21 #include "content/public/browser/notification_registrar.h" |
| 27 | 22 |
| 28 #if defined(FULL_SAFE_BROWSING) | 23 #if defined(FULL_SAFE_BROWSING) |
| 29 #include "chrome/browser/safe_browsing/incident_reporting/delayed_analysis_callb ack.h" | 24 #include "chrome/browser/safe_browsing/incident_reporting/delayed_analysis_callb ack.h" |
| 30 #endif | 25 #endif |
| 31 | 26 |
| 32 class PrefChangeRegistrar; | 27 class PrefChangeRegistrar; |
| 33 class PrefService; | 28 class PrefService; |
| 34 class Profile; | 29 class Profile; |
| 35 class TrackedPreferenceValidationDelegate; | 30 class TrackedPreferenceValidationDelegate; |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 56 class SafeBrowsingServiceFactory; | 51 class SafeBrowsingServiceFactory; |
| 57 class SafeBrowsingUIManager; | 52 class SafeBrowsingUIManager; |
| 58 class SafeBrowsingURLRequestContextGetter; | 53 class SafeBrowsingURLRequestContextGetter; |
| 59 struct V4ProtocolConfig; | 54 struct V4ProtocolConfig; |
| 60 | 55 |
| 61 // Construction needs to happen on the main thread. | 56 // Construction needs to happen on the main thread. |
| 62 // The SafeBrowsingService owns both the UI and Database managers which do | 57 // The SafeBrowsingService owns both the UI and Database managers which do |
| 63 // the heavylifting of safebrowsing service. Both of these managers stay | 58 // the heavylifting of safebrowsing service. Both of these managers stay |
| 64 // alive until SafeBrowsingService is destroyed, however, they are disabled | 59 // alive until SafeBrowsingService is destroyed, however, they are disabled |
| 65 // permanently when Shutdown method is called. | 60 // permanently when Shutdown method is called. |
| 66 class SafeBrowsingService : public base::RefCountedThreadSafe< | 61 class SafeBrowsingService : public BaseSafeBrowsingService { |
| 67 SafeBrowsingService, | |
| 68 content::BrowserThread::DeleteOnUIThread>, | |
| 69 public content::NotificationObserver { | |
| 70 public: | 62 public: |
| 71 // Makes the passed |factory| the factory used to instanciate | 63 // Makes the passed |factory| the factory used to instanciate |
| 72 // a SafeBrowsingService. Useful for tests. | 64 // a SafeBrowsingService. Useful for tests. |
| 73 static void RegisterFactory(SafeBrowsingServiceFactory* factory) { | 65 static void RegisterFactory(SafeBrowsingServiceFactory* factory) { |
| 74 factory_ = factory; | 66 factory_ = factory; |
| 75 } | 67 } |
| 76 | 68 |
| 77 static base::FilePath GetCookieFilePathForTesting(); | 69 static base::FilePath GetCookieFilePathForTesting(); |
| 78 | 70 |
| 79 static base::FilePath GetBaseFilename(); | 71 static base::FilePath GetBaseFilename(); |
| 80 | 72 |
| 81 // Create an instance of the safe browsing service. | 73 // Create an instance of the safe browsing service. |
| 82 static SafeBrowsingService* CreateSafeBrowsingService(); | 74 static SafeBrowsingService* CreateSafeBrowsingService(); |
| 83 | 75 |
| 84 // Called on the UI thread to initialize the service. | 76 // Called on the UI thread to initialize the service. |
| 85 void Initialize(); | 77 void Initialize() override; |
| 86 | 78 |
| 87 // Called on the main thread to let us know that the io_thread is going away. | 79 // Called on the main thread to let us know that the io_thread is going away. |
| 88 void ShutDown(); | 80 void ShutDown() override; |
| 89 | 81 |
| 90 // Called on UI thread to decide if the download file's sha256 hash | 82 // Called on UI thread to decide if the download file's sha256 hash |
| 91 // should be calculated for safebrowsing. | 83 // should be calculated for safebrowsing. |
| 92 bool DownloadBinHashNeeded() const; | 84 bool DownloadBinHashNeeded() const; |
| 93 | 85 |
| 94 // Create a protocol config struct. | 86 // Create a protocol config struct. |
| 95 virtual SafeBrowsingProtocolConfig GetProtocolConfig() const; | 87 virtual SafeBrowsingProtocolConfig GetProtocolConfig() const; |
| 96 | 88 |
| 97 // Create a v4 protocol config struct. | 89 // Create a v4 protocol config struct. |
| 98 virtual V4ProtocolConfig GetV4ProtocolConfig() const; | 90 virtual V4ProtocolConfig GetV4ProtocolConfig() const; |
| 99 | 91 |
| 100 // Returns the client_name field for both V3 and V4 protocol manager configs. | |
| 101 std::string GetProtocolConfigClientName() const; | |
| 102 | |
| 103 // Get current enabled status. Must be called on IO thread. | |
| 104 bool enabled() const { | |
| 105 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | |
| 106 return enabled_; | |
| 107 } | |
| 108 | |
| 109 // Whether the service is enabled by the current set of profiles. | |
| 110 bool enabled_by_prefs() const { | |
| 111 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 112 return enabled_by_prefs_; | |
| 113 } | |
| 114 | |
| 115 ClientSideDetectionService* safe_browsing_detection_service() const { | 92 ClientSideDetectionService* safe_browsing_detection_service() const { |
| 116 return services_delegate_->GetCsdService(); | 93 return services_delegate_->GetCsdService(); |
| 117 } | 94 } |
| 118 | 95 |
| 119 // The DownloadProtectionService is not valid after the SafeBrowsingService | 96 // The DownloadProtectionService is not valid after the SafeBrowsingService |
| 120 // is destroyed. | 97 // is destroyed. |
| 121 DownloadProtectionService* download_protection_service() const { | 98 DownloadProtectionService* download_protection_service() const { |
| 122 return services_delegate_->GetDownloadService(); | 99 return services_delegate_->GetDownloadService(); |
| 123 } | 100 } |
| 124 | 101 |
| 125 scoped_refptr<net::URLRequestContextGetter> url_request_context(); | 102 scoped_refptr<net::URLRequestContextGetter> url_request_context() override; |
| 126 | 103 |
| 127 const scoped_refptr<SafeBrowsingUIManager>& ui_manager() const; | 104 const scoped_refptr<SafeBrowsingUIManager>& ui_manager() const; |
| 128 | 105 |
| 129 // This returns either the v3 or the v4 database manager, depending on | 106 // This returns either the v3 or the v4 database manager, depending on |
| 130 // the experiment settings. | 107 // the experiment settings. |
| 131 const scoped_refptr<SafeBrowsingDatabaseManager>& database_manager() const; | 108 const scoped_refptr<SafeBrowsingDatabaseManager>& database_manager() |
| 109 const override; | |
| 132 | 110 |
| 133 scoped_refptr<SafeBrowsingNavigationObserverManager> | 111 scoped_refptr<SafeBrowsingNavigationObserverManager> |
| 134 navigation_observer_manager(); | 112 navigation_observer_manager(); |
| 135 | 113 |
| 136 SafeBrowsingProtocolManager* protocol_manager() const; | 114 SafeBrowsingProtocolManager* protocol_manager() const; |
| 137 | 115 |
| 138 SafeBrowsingPingManager* ping_manager() const; | 116 SafeBrowsingPingManager* ping_manager() const; |
| 139 | 117 |
| 140 // This may be NULL if v4 is not enabled by experiment. | 118 // This may be NULL if v4 is not enabled by experiment. |
| 141 const scoped_refptr<SafeBrowsingDatabaseManager>& v4_local_database_manager() | 119 const scoped_refptr<SafeBrowsingDatabaseManager>& v4_local_database_manager() |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 179 | 157 |
| 180 // Sends serialized download report to backend. | 158 // Sends serialized download report to backend. |
| 181 virtual void SendSerializedDownloadReport(const std::string& report); | 159 virtual void SendSerializedDownloadReport(const std::string& report); |
| 182 | 160 |
| 183 protected: | 161 protected: |
| 184 // Creates the safe browsing service. Need to initialize before using. | 162 // Creates the safe browsing service. Need to initialize before using. |
| 185 SafeBrowsingService(); | 163 SafeBrowsingService(); |
| 186 | 164 |
| 187 ~SafeBrowsingService() override; | 165 ~SafeBrowsingService() override; |
| 188 | 166 |
| 189 virtual SafeBrowsingDatabaseManager* CreateDatabaseManager(); | 167 SafeBrowsingDatabaseManager* CreateDatabaseManager() override; |
| 190 | 168 |
| 191 virtual SafeBrowsingUIManager* CreateUIManager(); | 169 virtual SafeBrowsingUIManager* CreateUIManager(); |
| 192 | 170 |
| 193 // Registers all the delayed analysis with the incident reporting service. | 171 // Registers all the delayed analysis with the incident reporting service. |
| 194 // This is where you register your process-wide, profile-independent analysis. | 172 // This is where you register your process-wide, profile-independent analysis. |
| 195 virtual void RegisterAllDelayedAnalysis(); | 173 virtual void RegisterAllDelayedAnalysis(); |
| 196 | 174 |
| 197 // Return a ptr to DatabaseManager's delegate, or NULL if it doesn't have one. | 175 // Return a ptr to DatabaseManager's delegate, or NULL if it doesn't have one. |
| 198 virtual SafeBrowsingProtocolManagerDelegate* GetProtocolManagerDelegate(); | 176 virtual SafeBrowsingProtocolManagerDelegate* GetProtocolManagerDelegate(); |
| 199 | 177 |
| 200 std::unique_ptr<ServicesDelegate> services_delegate_; | 178 std::unique_ptr<ServicesDelegate> services_delegate_; |
| 201 | 179 |
| 202 private: | |
| 203 friend class SafeBrowsingServiceFactoryImpl; | |
| 204 friend struct content::BrowserThread::DeleteOnThread< | |
| 205 content::BrowserThread::UI>; | |
| 206 friend class base::DeleteHelper<SafeBrowsingService>; | |
| 207 friend class SafeBrowsingServerTest; | |
| 208 friend class SafeBrowsingServiceTest; | |
| 209 friend class SafeBrowsingURLRequestContextGetter; | |
| 210 friend class TestSafeBrowsingService; | |
| 211 friend class TestSafeBrowsingServiceFactory; | |
| 212 | |
| 213 // Called to initialize objects that are used on the io_thread. This may be | 180 // Called to initialize objects that are used on the io_thread. This may be |
| 214 // called multiple times during the life of the SafeBrowsingService. | 181 // called multiple times during the life of the SafeBrowsingService. |
| 215 void StartOnIOThread( | 182 void StartOnIOThread( |
| 216 net::URLRequestContextGetter* url_request_context_getter); | 183 net::URLRequestContextGetter* url_request_context_getter) override; |
|
Jialiu Lin
2016/12/29 17:41:37
Any particular reason for making StartOnIOThread(.
Nate Fischer
2017/01/03 22:39:52
I originally moved them to protected so that the s
| |
| 217 | 184 |
| 218 // Called to stop or shutdown operations on the io_thread. This may be called | 185 // Called to stop or shutdown operations on the io_thread. This may be called |
| 219 // multiple times to stop during the life of the SafeBrowsingService. If | 186 // multiple times to stop during the life of the SafeBrowsingService. If |
| 220 // shutdown is true, then the operations on the io thread are shutdown | 187 // shutdown is true, then the operations on the io thread are shutdown |
| 221 // permanently and cannot be restarted. | 188 // permanently and cannot be restarted. |
| 222 void StopOnIOThread(bool shutdown); | 189 void StopOnIOThread(bool shutdown) override; |
| 223 | 190 |
| 224 // Start up SafeBrowsing objects. This can be called at browser start, or when | 191 // Start up SafeBrowsing objects. This can be called at browser start, or when |
| 225 // the user checks the "Enable SafeBrowsing" option in the Advanced options | 192 // the user checks the "Enable SafeBrowsing" option in the Advanced options |
| 226 // UI. | 193 // UI. |
| 227 void Start(); | 194 void Start() override; |
| 228 | 195 |
| 229 // Stops the SafeBrowsingService. This can be called when the safe browsing | 196 // Stops the SafeBrowsingService. This can be called when the safe browsing |
| 230 // preference is disabled. When shutdown is true, operation is permanently | 197 // preference is disabled. When shutdown is true, operation is permanently |
| 231 // shutdown and cannot be restarted. | 198 // shutdown and cannot be restarted. |
| 232 void Stop(bool shutdown); | 199 void Stop(bool shutdown) override; |
| 233 | 200 |
| 234 // content::NotificationObserver override | 201 // content::NotificationObserver override |
| 235 void Observe(int type, | 202 void Observe(int type, |
| 236 const content::NotificationSource& source, | 203 const content::NotificationSource& source, |
| 237 const content::NotificationDetails& details) override; | 204 const content::NotificationDetails& details) override; |
| 238 | 205 |
| 239 // Starts following the safe browsing preference on |pref_service|. | 206 // Starts following the safe browsing preference on |pref_service|. |
| 240 void AddPrefService(PrefService* pref_service); | 207 void AddPrefService(PrefService* pref_service); |
| 241 | 208 |
| 242 // Stop following the safe browsing preference on |pref_service|. | 209 // Stop following the safe browsing preference on |pref_service|. |
| 243 void RemovePrefService(PrefService* pref_service); | 210 void RemovePrefService(PrefService* pref_service); |
| 244 | 211 |
| 245 // Checks if any profile is currently using the safe browsing service, and | 212 // Checks if any profile is currently using the safe browsing service, and |
| 246 // starts or stops the service accordingly. | 213 // starts or stops the service accordingly. |
| 247 void RefreshState(); | 214 void RefreshState() override; |
| 215 | |
| 216 private: | |
| 217 friend class SafeBrowsingServiceFactoryImpl; | |
| 218 friend class base::DeleteHelper<SafeBrowsingService>; | |
| 219 friend class SafeBrowsingServerTest; | |
| 220 friend class SafeBrowsingServiceTest; | |
| 221 friend class SafeBrowsingURLRequestContextGetter; | |
| 222 friend class TestSafeBrowsingService; | |
| 223 friend class TestSafeBrowsingServiceFactory; | |
| 248 | 224 |
| 249 void OnSendSerializedDownloadReport(const std::string& report); | 225 void OnSendSerializedDownloadReport(const std::string& report); |
| 250 | 226 |
| 251 // Process the observed resource requests on the UI thread. | 227 // Process the observed resource requests on the UI thread. |
| 252 void ProcessResourceRequest(const ResourceRequestInfo& request); | 228 void ProcessResourceRequest(const ResourceRequestInfo& request); |
| 253 | 229 |
| 254 // The factory used to instantiate a SafeBrowsingService object. | 230 // The factory used to instantiate a SafeBrowsingService object. |
| 255 // Useful for tests, so they can provide their own implementation of | 231 // Useful for tests, so they can provide their own implementation of |
| 256 // SafeBrowsingService. | 232 // SafeBrowsingService. |
| 257 static SafeBrowsingServiceFactory* factory_; | 233 static SafeBrowsingServiceFactory* factory_; |
| 258 | 234 |
| 259 // The SafeBrowsingURLRequestContextGetter used to access | 235 // The SafeBrowsingURLRequestContextGetter used to access |
| 260 // |url_request_context_|. Accessed on UI thread. | 236 // |url_request_context_|. Accessed on UI thread. |
| 261 scoped_refptr<SafeBrowsingURLRequestContextGetter> | 237 scoped_refptr<SafeBrowsingURLRequestContextGetter> |
| 262 url_request_context_getter_; | 238 url_request_context_getter_; |
| 263 | 239 |
| 264 #if defined(SAFE_BROWSING_DB_LOCAL) | 240 #if defined(SAFE_BROWSING_DB_LOCAL) |
| 265 // Handles interaction with SafeBrowsing servers. Accessed on IO thread. | 241 // Handles interaction with SafeBrowsing servers. Accessed on IO thread. |
| 266 std::unique_ptr<SafeBrowsingProtocolManager> protocol_manager_; | 242 std::unique_ptr<SafeBrowsingProtocolManager> protocol_manager_; |
| 267 #endif | 243 #endif |
| 268 | 244 |
| 269 // Provides phishing and malware statistics. Accessed on IO thread. | 245 // Provides phishing and malware statistics. Accessed on IO thread. |
| 270 std::unique_ptr<SafeBrowsingPingManager> ping_manager_; | 246 std::unique_ptr<SafeBrowsingPingManager> ping_manager_; |
| 271 | 247 |
| 272 // Whether the service is running. 'enabled_' is used by SafeBrowsingService | |
| 273 // on the IO thread during normal operations. | |
| 274 bool enabled_; | |
| 275 | |
| 276 // Whether SafeBrowsing is enabled by the current set of profiles. | |
| 277 // Accessed on UI thread. | |
| 278 bool enabled_by_prefs_; | |
| 279 | |
| 280 // Whether SafeBrowsing needs to be enabled in V4Only mode. In this mode, all | |
| 281 // SafeBrowsing decisions are made using the PVer4 implementation. | |
| 282 bool enabled_v4_only_; | |
|
Jialiu Lin
2016/12/29 17:41:37
It probably makes more sense to keep enabled_v4_on
Nate Fischer
2017/01/03 22:39:52
Sounds good to me. I also moved enabled_by_prefs_,
| |
| 283 | |
| 284 // Tracks existing PrefServices, and the safe browsing preference on each. | 248 // Tracks existing PrefServices, and the safe browsing preference on each. |
| 285 // This is used to determine if any profile is currently using the safe | 249 // This is used to determine if any profile is currently using the safe |
| 286 // browsing service, and to start it up or shut it down accordingly. | 250 // browsing service, and to start it up or shut it down accordingly. |
| 287 // Accessed on UI thread. | 251 // Accessed on UI thread. |
| 288 std::map<PrefService*, std::unique_ptr<PrefChangeRegistrar>> prefs_map_; | 252 std::map<PrefService*, std::unique_ptr<PrefChangeRegistrar>> prefs_map_; |
| 289 | 253 |
| 290 // Used to track creation and destruction of profiles on the UI thread. | 254 // Used to track creation and destruction of profiles on the UI thread. |
| 291 content::NotificationRegistrar prefs_registrar_; | 255 content::NotificationRegistrar prefs_registrar_; |
| 292 | 256 |
| 293 // Callbacks when SafeBrowsing state might have changed. | 257 // Callbacks when SafeBrowsing state might have changed. |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 319 SafeBrowsingServiceFactory() { } | 283 SafeBrowsingServiceFactory() { } |
| 320 virtual ~SafeBrowsingServiceFactory() { } | 284 virtual ~SafeBrowsingServiceFactory() { } |
| 321 virtual SafeBrowsingService* CreateSafeBrowsingService() = 0; | 285 virtual SafeBrowsingService* CreateSafeBrowsingService() = 0; |
| 322 private: | 286 private: |
| 323 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingServiceFactory); | 287 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingServiceFactory); |
| 324 }; | 288 }; |
| 325 | 289 |
| 326 } // namespace safe_browsing | 290 } // namespace safe_browsing |
| 327 | 291 |
| 328 #endif // CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_SERVICE_H_ | 292 #endif // CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_SERVICE_H_ |
| OLD | NEW |