Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 // | |
| 5 // The Safe Browsing service is responsible for downloading anti-phishing and | |
| 6 // anti-malware tables and checking urls against them. | |
| 7 | |
| 8 #ifndef COMPONENTS_SAFE_BROWSING_BASE_SAFE_BROWSING_SERVICE_H_ | |
| 9 #define COMPONENTS_SAFE_BROWSING_BASE_SAFE_BROWSING_SERVICE_H_ | |
| 10 | |
| 11 #include <map> | |
| 12 #include <memory> | |
| 13 #include <string> | |
| 14 | |
| 15 #include "base/callback_list.h" | |
| 16 #include "base/macros.h" | |
| 17 #include "base/memory/ref_counted.h" | |
| 18 #include "content/public/browser/browser_thread.h" | |
| 19 #include "content/public/browser/notification_observer.h" | |
| 20 | |
| 21 namespace net { | |
| 22 class URLRequest; | |
| 23 class URLRequestContextGetter; | |
| 24 } | |
| 25 | |
| 26 namespace safe_browsing { | |
| 27 struct ResourceRequestInfo; | |
| 28 class SafeBrowsingDatabaseManager; | |
| 29 | |
| 30 // Construction needs to happen on the main thread. | |
| 31 // 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
| |
| 32 // the heavylifting of safebrowsing service. Both of these managers stay | |
| 33 // alive until SafeBrowsingService is destroyed, however, they are disabled | |
| 34 // permanently when Shutdown method is called. | |
| 35 class BaseSafeBrowsingService : public base::RefCountedThreadSafe< | |
| 36 BaseSafeBrowsingService, | |
| 37 content::BrowserThread::DeleteOnUIThread>, | |
| 38 public content::NotificationObserver { | |
| 39 public: | |
| 40 // // Create an instance of the safe browsing service. | |
| 41 // static SafeBrowsingService* CreateSafeBrowsingService(); | |
| 42 | |
| 43 // Called on the UI thread to initialize the service. | |
| 44 virtual void Initialize(); | |
| 45 | |
| 46 // Called on the main thread to let us know that the io_thread is going away. | |
| 47 virtual void ShutDown(); | |
| 48 | |
| 49 // Returns the client_name field for both V3 and V4 protocol manager configs. | |
| 50 std::string GetProtocolConfigClientName() const; | |
| 51 | |
| 52 // Get current enabled status. Must be called on IO thread. | |
| 53 bool enabled() const { | |
| 54 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | |
| 55 return enabled_; | |
| 56 } | |
| 57 | |
| 58 // Whether the service is enabled by the current set of profiles. | |
| 59 bool enabled_by_prefs() const { | |
| 60 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 61 return enabled_by_prefs_; | |
| 62 } | |
| 63 | |
| 64 virtual scoped_refptr<net::URLRequestContextGetter> url_request_context(); | |
| 65 | |
| 66 // This returns either the v3 or the v4 database manager, depending on | |
| 67 // the experiment settings. | |
| 68 virtual const scoped_refptr<SafeBrowsingDatabaseManager>& database_manager() | |
| 69 const; | |
| 70 | |
| 71 // Observes resource requests made by the renderer and reports suspicious | |
| 72 // activity. | |
| 73 void OnResourceRequest(const net::URLRequest* request); | |
| 74 | |
| 75 // Type for subscriptions to SafeBrowsing service state. | |
| 76 typedef base::CallbackList<void(void)>::Subscription StateSubscription; | |
| 77 | |
| 78 // Adds a listener for when SafeBrowsing preferences might have changed. | |
| 79 // To get the current state, the callback should call enabled_by_prefs(). | |
| 80 // Should only be called on the UI thread. | |
| 81 std::unique_ptr<StateSubscription> RegisterStateCallback( | |
| 82 const base::Callback<void(void)>& callback); | |
| 83 | |
| 84 protected: | |
| 85 // Creates the safe browsing service. Need to initialize before using. | |
| 86 BaseSafeBrowsingService(); | |
| 87 | |
| 88 ~BaseSafeBrowsingService() override; | |
| 89 | |
| 90 virtual SafeBrowsingDatabaseManager* CreateDatabaseManager(); | |
| 91 | |
| 92 // Whether the service is running. 'enabled_' is used by SafeBrowsingService | |
| 93 // on the IO thread during normal operations. | |
| 94 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
| |
| 95 | |
| 96 // Whether SafeBrowsing is enabled by the current set of profiles. | |
| 97 // Accessed on UI thread. | |
| 98 bool enabled_by_prefs_; | |
| 99 | |
| 100 // Whether SafeBrowsing needs to be enabled in V4Only mode. In this mode, all | |
| 101 // SafeBrowsing decisions are made using the PVer4 implementation. | |
| 102 bool enabled_v4_only_; | |
| 103 | |
| 104 // The database manager handles the database and download logic. Accessed on | |
| 105 // both UI and IO thread. | |
| 106 scoped_refptr<SafeBrowsingDatabaseManager> database_manager_; | |
| 107 | |
| 108 // Called to initialize objects that are used on the io_thread. This may be | |
| 109 // called multiple times during the life of the SafeBrowsingService. | |
| 110 virtual void StartOnIOThread( | |
| 111 net::URLRequestContextGetter* url_request_context_getter); | |
| 112 | |
| 113 // Called to stop or shutdown operations on the io_thread. This may be called | |
| 114 // multiple times to stop during the life of the SafeBrowsingService. If | |
| 115 // shutdown is true, then the operations on the io thread are shutdown | |
| 116 // permanently and cannot be restarted. | |
| 117 virtual void StopOnIOThread(bool shutdown); | |
| 118 | |
| 119 // Start up SafeBrowsing objects. This can be called at browser start, or when | |
| 120 // the user checks the "Enable SafeBrowsing" option in the Advanced options | |
| 121 // UI. | |
| 122 virtual void Start(); | |
| 123 | |
| 124 // Stops the SafeBrowsingService. This can be called when the safe browsing | |
| 125 // preference is disabled. When shutdown is true, operation is permanently | |
| 126 // shutdown and cannot be restarted. | |
| 127 virtual void Stop(bool shutdown); | |
| 128 | |
| 129 // content::NotificationObserver override | |
| 130 void Observe(int type, | |
| 131 const content::NotificationSource& source, | |
| 132 const content::NotificationDetails& details) override; | |
| 133 | |
| 134 // Checks if any profile is currently using the safe browsing service, and | |
| 135 // starts or stops the service accordingly. | |
| 136 virtual void RefreshState(); | |
| 137 | |
| 138 // Callbacks when SafeBrowsing state might have changed. | |
| 139 // Should only be accessed on the UI thread. | |
| 140 base::CallbackList<void(void)> state_callback_list_; | |
| 141 | |
| 142 private: | |
| 143 friend struct content::BrowserThread::DeleteOnThread< | |
| 144 content::BrowserThread::UI>; | |
| 145 friend class base::DeleteHelper<BaseSafeBrowsingService>; | |
| 146 | |
| 147 // Process the observed resource requests on the UI thread. | |
| 148 void ProcessResourceRequest(const ResourceRequestInfo& request); | |
| 149 | |
| 150 DISALLOW_COPY_AND_ASSIGN(BaseSafeBrowsingService); | |
| 151 }; | |
| 152 | |
| 153 } // namespace safe_browsing | |
| 154 | |
| 155 #endif // COMPONENTS_SAFE_BROWSING_BASE_SAFE_BROWSING_SERVICE_H_ | |
| OLD | NEW |