Chromium Code Reviews| Index: components/safe_browsing/base_safe_browsing_service.h |
| diff --git a/components/safe_browsing/base_safe_browsing_service.h b/components/safe_browsing/base_safe_browsing_service.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..9c38da0236214f4e413a3707e1d449b08695a31f |
| --- /dev/null |
| +++ b/components/safe_browsing/base_safe_browsing_service.h |
| @@ -0,0 +1,155 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| +// |
| +// The Safe Browsing service is responsible for downloading anti-phishing and |
| +// anti-malware tables and checking urls against them. |
| + |
| +#ifndef COMPONENTS_SAFE_BROWSING_BASE_SAFE_BROWSING_SERVICE_H_ |
| +#define COMPONENTS_SAFE_BROWSING_BASE_SAFE_BROWSING_SERVICE_H_ |
| + |
| +#include <map> |
| +#include <memory> |
| +#include <string> |
| + |
| +#include "base/callback_list.h" |
| +#include "base/macros.h" |
| +#include "base/memory/ref_counted.h" |
| +#include "content/public/browser/browser_thread.h" |
| +#include "content/public/browser/notification_observer.h" |
| + |
| +namespace net { |
| +class URLRequest; |
| +class URLRequestContextGetter; |
| +} |
| + |
| +namespace safe_browsing { |
| +struct ResourceRequestInfo; |
| +class SafeBrowsingDatabaseManager; |
| + |
| +// Construction needs to happen on the main thread. |
| +// The SafeBrowsingService owns both the UI and Database managers which do |
|
Jialiu Lin
2016/12/29 17:41:37
Please update this comment.
Nate Fischer
2017/01/03 22:39:53
Done
|
| +// the heavylifting of safebrowsing service. Both of these managers stay |
| +// alive until SafeBrowsingService is destroyed, however, they are disabled |
| +// permanently when Shutdown method is called. |
| +class BaseSafeBrowsingService : public base::RefCountedThreadSafe< |
| + BaseSafeBrowsingService, |
| + content::BrowserThread::DeleteOnUIThread>, |
| + public content::NotificationObserver { |
| + public: |
| + // // Create an instance of the safe browsing service. |
| + // static SafeBrowsingService* CreateSafeBrowsingService(); |
| + |
| + // Called on the UI thread to initialize the service. |
| + virtual void Initialize(); |
| + |
| + // Called on the main thread to let us know that the io_thread is going away. |
| + virtual void ShutDown(); |
| + |
| + // Returns the client_name field for both V3 and V4 protocol manager configs. |
| + std::string GetProtocolConfigClientName() const; |
| + |
| + // Get current enabled status. Must be called on IO thread. |
| + bool enabled() const { |
| + DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| + return enabled_; |
| + } |
| + |
| + // Whether the service is enabled by the current set of profiles. |
| + bool enabled_by_prefs() const { |
| + DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| + return enabled_by_prefs_; |
| + } |
| + |
| + virtual scoped_refptr<net::URLRequestContextGetter> url_request_context(); |
| + |
| + // This returns either the v3 or the v4 database manager, depending on |
| + // the experiment settings. |
| + virtual const scoped_refptr<SafeBrowsingDatabaseManager>& database_manager() |
| + const; |
| + |
| + // Observes resource requests made by the renderer and reports suspicious |
| + // activity. |
| + void OnResourceRequest(const net::URLRequest* request); |
| + |
| + // Type for subscriptions to SafeBrowsing service state. |
| + typedef base::CallbackList<void(void)>::Subscription StateSubscription; |
| + |
| + // Adds a listener for when SafeBrowsing preferences might have changed. |
| + // To get the current state, the callback should call enabled_by_prefs(). |
| + // Should only be called on the UI thread. |
| + std::unique_ptr<StateSubscription> RegisterStateCallback( |
| + const base::Callback<void(void)>& callback); |
| + |
| + protected: |
| + // Creates the safe browsing service. Need to initialize before using. |
| + BaseSafeBrowsingService(); |
| + |
| + ~BaseSafeBrowsingService() override; |
| + |
| + virtual SafeBrowsingDatabaseManager* CreateDatabaseManager(); |
| + |
| + // Whether the service is running. 'enabled_' is used by SafeBrowsingService |
| + // on the IO thread during normal operations. |
| + bool enabled_; |
|
Jialiu Lin
2016/12/29 17:41:37
nit: member functions should be in front of member
Nate Fischer
2017/01/03 22:39:53
Done
|
| + |
| + // Whether SafeBrowsing is enabled by the current set of profiles. |
| + // Accessed on UI thread. |
| + bool enabled_by_prefs_; |
| + |
| + // Whether SafeBrowsing needs to be enabled in V4Only mode. In this mode, all |
| + // SafeBrowsing decisions are made using the PVer4 implementation. |
| + bool enabled_v4_only_; |
| + |
| + // The database manager handles the database and download logic. Accessed on |
| + // both UI and IO thread. |
| + scoped_refptr<SafeBrowsingDatabaseManager> database_manager_; |
| + |
| + // Called to initialize objects that are used on the io_thread. This may be |
| + // called multiple times during the life of the SafeBrowsingService. |
| + virtual void StartOnIOThread( |
| + net::URLRequestContextGetter* url_request_context_getter); |
| + |
| + // Called to stop or shutdown operations on the io_thread. This may be called |
| + // multiple times to stop during the life of the SafeBrowsingService. If |
| + // shutdown is true, then the operations on the io thread are shutdown |
| + // permanently and cannot be restarted. |
| + virtual void StopOnIOThread(bool shutdown); |
| + |
| + // Start up SafeBrowsing objects. This can be called at browser start, or when |
| + // the user checks the "Enable SafeBrowsing" option in the Advanced options |
| + // UI. |
| + virtual void Start(); |
| + |
| + // Stops the SafeBrowsingService. This can be called when the safe browsing |
| + // preference is disabled. When shutdown is true, operation is permanently |
| + // shutdown and cannot be restarted. |
| + virtual void Stop(bool shutdown); |
| + |
| + // content::NotificationObserver override |
| + void Observe(int type, |
| + const content::NotificationSource& source, |
| + const content::NotificationDetails& details) override; |
| + |
| + // Checks if any profile is currently using the safe browsing service, and |
| + // starts or stops the service accordingly. |
| + virtual void RefreshState(); |
| + |
| + // Callbacks when SafeBrowsing state might have changed. |
| + // Should only be accessed on the UI thread. |
| + base::CallbackList<void(void)> state_callback_list_; |
| + |
| + private: |
| + friend struct content::BrowserThread::DeleteOnThread< |
| + content::BrowserThread::UI>; |
| + friend class base::DeleteHelper<BaseSafeBrowsingService>; |
| + |
| + // Process the observed resource requests on the UI thread. |
| + void ProcessResourceRequest(const ResourceRequestInfo& request); |
| + |
| + DISALLOW_COPY_AND_ASSIGN(BaseSafeBrowsingService); |
| +}; |
| + |
| +} // namespace safe_browsing |
| + |
| +#endif // COMPONENTS_SAFE_BROWSING_BASE_SAFE_BROWSING_SERVICE_H_ |