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.
5 // The Safe Browsing service is responsible for downloading anti-phishing and
6 // anti-malware tables and checking urls against them.
8 #ifndef CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_SERVICE_H_
9 #define CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_SERVICE_H_
14 #include "base/callback.h"
15 #include "base/callback_list.h"
16 #include "base/files/file_path.h"
17 #include "base/memory/ref_counted.h"
18 #include "base/memory/scoped_ptr.h"
19 #include "base/observer_list.h"
20 #include "base/sequenced_task_runner_helpers.h"
21 #include "chrome/browser/safe_browsing/safe_browsing_util.h"
22 #include "content/public/browser/browser_thread.h"
23 #include "content/public/browser/notification_observer.h"
24 #include "content/public/browser/notification_registrar.h"
26 #if defined(FULL_SAFE_BROWSING)
27 #include "chrome/browser/safe_browsing/incident_reporting/delayed_analysis_callback.h"
30 class PrefChangeRegistrar
;
33 struct SafeBrowsingProtocolConfig
;
34 class SafeBrowsingDatabaseManager
;
35 class SafeBrowsingPingManager
;
36 class SafeBrowsingProtocolManager
;
37 class SafeBrowsingProtocolManagerDelegate
;
38 class SafeBrowsingServiceFactory
;
39 class SafeBrowsingUIManager
;
40 class SafeBrowsingURLRequestContextGetter
;
41 class TrackedPreferenceValidationDelegate
;
48 class DownloadManager
;
53 class URLRequestContext
;
54 class URLRequestContextGetter
;
57 namespace safe_browsing
{
58 class ClientSideDetectionService
;
59 class DownloadProtectionService
;
61 #if defined(FULL_SAFE_BROWSING)
62 class IncidentReportingService
;
63 class OffDomainInclusionDetector
;
64 class ResourceRequestDetector
;
68 // Construction needs to happen on the main thread.
69 // The SafeBrowsingService owns both the UI and Database managers which do
70 // the heavylifting of safebrowsing service. Both of these managers stay
71 // alive until SafeBrowsingService is destroyed, however, they are disabled
72 // permanently when Shutdown method is called.
73 class SafeBrowsingService
74 : public base::RefCountedThreadSafe
<
76 content::BrowserThread::DeleteOnUIThread
>,
77 public content::NotificationObserver
{
79 // Makes the passed |factory| the factory used to instanciate
80 // a SafeBrowsingService. Useful for tests.
81 static void RegisterFactory(SafeBrowsingServiceFactory
* factory
) {
85 static base::FilePath
GetCookieFilePathForTesting();
87 static base::FilePath
GetBaseFilename();
89 // Create an instance of the safe browsing service.
90 static SafeBrowsingService
* CreateSafeBrowsingService();
92 // Called on the UI thread to initialize the service.
95 // Called on the main thread to let us know that the io_thread is going away.
98 // Called on UI thread to decide if the download file's sha256 hash
99 // should be calculated for safebrowsing.
100 bool DownloadBinHashNeeded() const;
102 // Create a protocol config struct.
103 virtual SafeBrowsingProtocolConfig
GetProtocolConfig() const;
105 // Get current enabled status. Must be called on IO thread.
106 bool enabled() const {
107 DCHECK_CURRENTLY_ON(content::BrowserThread::IO
);
111 // Whether the service is enabled by the current set of profiles.
112 bool enabled_by_prefs() const {
113 DCHECK_CURRENTLY_ON(content::BrowserThread::UI
);
114 return enabled_by_prefs_
;
117 safe_browsing::ClientSideDetectionService
*
118 safe_browsing_detection_service() const {
119 DCHECK_CURRENTLY_ON(content::BrowserThread::UI
);
120 return csd_service_
.get();
123 // The DownloadProtectionService is not valid after the SafeBrowsingService
125 safe_browsing::DownloadProtectionService
*
126 download_protection_service() const {
127 DCHECK_CURRENTLY_ON(content::BrowserThread::UI
);
128 return download_service_
.get();
131 net::URLRequestContextGetter
* url_request_context();
133 const scoped_refptr
<SafeBrowsingUIManager
>& ui_manager() const;
135 const scoped_refptr
<SafeBrowsingDatabaseManager
>& database_manager() const;
137 SafeBrowsingProtocolManager
* protocol_manager() const;
139 SafeBrowsingPingManager
* ping_manager() const;
141 // Returns a preference validation delegate that adds incidents to the
142 // incident reporting service for validation failures. Returns NULL if the
143 // service is not applicable for the given profile.
144 scoped_ptr
<TrackedPreferenceValidationDelegate
>
145 CreatePreferenceValidationDelegate(Profile
* profile
) const;
147 #if defined(FULL_SAFE_BROWSING)
148 // Registers |callback| to be run after some delay following process launch.
149 // |callback| will be dropped if the service is not applicable for the
151 void RegisterDelayedAnalysisCallback(
152 const safe_browsing::DelayedAnalysisCallback
& callback
);
155 // Adds |download_manager| to the set monitored by safe browsing.
156 void AddDownloadManager(content::DownloadManager
* download_manager
);
158 // Observes resource requests made by the renderer and reports suspicious
160 void OnResourceRequest(const net::URLRequest
* request
);
162 // Type for subscriptions to SafeBrowsing service state.
163 typedef base::CallbackList
<void(void)>::Subscription StateSubscription
;
165 // Adds a listener for when SafeBrowsing preferences might have changed.
166 // To get the current state, the callback should call enabled_by_prefs().
167 // Should only be called on the UI thread.
168 scoped_ptr
<StateSubscription
> RegisterStateCallback(
169 const base::Callback
<void(void)>& callback
);
172 // Creates the safe browsing service. Need to initialize before using.
173 SafeBrowsingService();
175 ~SafeBrowsingService() override
;
177 virtual SafeBrowsingDatabaseManager
* CreateDatabaseManager();
179 virtual SafeBrowsingUIManager
* CreateUIManager();
181 // Registers all the delayed analysis with the incident reporting service.
182 // This is where you register your process-wide, profile-independent analysis.
183 virtual void RegisterAllDelayedAnalysis();
185 // Return a ptr to DatabaseManager's delegate, or NULL if it doesn't have one.
186 virtual SafeBrowsingProtocolManagerDelegate
* GetProtocolManagerDelegate();
189 friend class SafeBrowsingServiceFactoryImpl
;
190 friend struct content::BrowserThread::DeleteOnThread
<
191 content::BrowserThread::UI
>;
192 friend class base::DeleteHelper
<SafeBrowsingService
>;
193 friend class SafeBrowsingServerTest
;
194 friend class SafeBrowsingServiceTest
;
195 friend class SafeBrowsingURLRequestContextGetter
;
197 void InitURLRequestContextOnIOThread(
198 net::URLRequestContextGetter
* system_url_request_context_getter
);
200 // Destroys the URLRequest and shuts down the provided getter on the
202 void DestroyURLRequestContextOnIOThread(
203 scoped_refptr
<SafeBrowsingURLRequestContextGetter
> context_getter
);
205 // Called to initialize objects that are used on the io_thread. This may be
206 // called multiple times during the life of the SafeBrowsingService.
207 void StartOnIOThread(
208 net::URLRequestContextGetter
* url_request_context_getter
);
210 // Called to stop or shutdown operations on the io_thread. This may be called
211 // multiple times to stop during the life of the SafeBrowsingService. If
212 // shutdown is true, then the operations on the io thread are shutdown
213 // permanently and cannot be restarted.
214 void StopOnIOThread(bool shutdown
);
216 // Start up SafeBrowsing objects. This can be called at browser start, or when
217 // the user checks the "Enable SafeBrowsing" option in the Advanced options
221 // Stops the SafeBrowsingService. This can be called when the safe browsing
222 // preference is disabled. When shutdown is true, operation is permanently
223 // shutdown and cannot be restarted.
224 void Stop(bool shutdown
);
226 // content::NotificationObserver override
227 void Observe(int type
,
228 const content::NotificationSource
& source
,
229 const content::NotificationDetails
& details
) override
;
231 // Starts following the safe browsing preference on |pref_service|.
232 void AddPrefService(PrefService
* pref_service
);
234 // Stop following the safe browsing preference on |pref_service|.
235 void RemovePrefService(PrefService
* pref_service
);
237 // Checks if any profile is currently using the safe browsing service, and
238 // starts or stops the service accordingly.
241 // The factory used to instanciate a SafeBrowsingService object.
242 // Useful for tests, so they can provide their own implementation of
243 // SafeBrowsingService.
244 static SafeBrowsingServiceFactory
* factory_
;
246 // The SafeBrowsingURLRequestContextGetter used to access
247 // |url_request_context_|. Accessed on UI thread.
248 scoped_refptr
<SafeBrowsingURLRequestContextGetter
>
249 url_request_context_getter_
;
251 // The SafeBrowsingURLRequestContext. Accessed on IO thread.
252 scoped_ptr
<net::URLRequestContext
> url_request_context_
;
254 // Handles interaction with SafeBrowsing servers. Accessed on IO thread.
255 SafeBrowsingProtocolManager
* protocol_manager_
;
257 // Provides phishing and malware statistics. Accessed on IO thread.
258 SafeBrowsingPingManager
* ping_manager_
;
260 // Whether the service is running. 'enabled_' is used by SafeBrowsingService
261 // on the IO thread during normal operations.
264 // Whether SafeBrowsing is enabled by the current set of profiles.
265 // Accessed on UI thread.
266 bool enabled_by_prefs_
;
268 // Tracks existing PrefServices, and the safe browsing preference on each.
269 // This is used to determine if any profile is currently using the safe
270 // browsing service, and to start it up or shut it down accordingly.
271 // Accessed on UI thread.
272 std::map
<PrefService
*, PrefChangeRegistrar
*> prefs_map_
;
274 // Used to track creation and destruction of profiles on the UI thread.
275 content::NotificationRegistrar prefs_registrar_
;
277 // Callbacks when SafeBrowsing state might have changed.
278 // Should only be accessed on the UI thread.
279 base::CallbackList
<void(void)> state_callback_list_
;
281 // The ClientSideDetectionService is managed by the SafeBrowsingService,
282 // since its running state and lifecycle depends on SafeBrowsingService's.
283 // Accessed on UI thread.
284 scoped_ptr
<safe_browsing::ClientSideDetectionService
> csd_service_
;
286 // The DownloadProtectionService is managed by the SafeBrowsingService,
287 // since its running state and lifecycle depends on SafeBrowsingService's.
288 // Accessed on UI thread.
289 scoped_ptr
<safe_browsing::DownloadProtectionService
> download_service_
;
291 #if defined(FULL_SAFE_BROWSING)
292 scoped_ptr
<safe_browsing::IncidentReportingService
> incident_service_
;
295 // The UI manager handles showing interstitials. Accessed on both UI and IO
297 scoped_refptr
<SafeBrowsingUIManager
> ui_manager_
;
299 // The database manager handles the database and download logic. Accessed on
300 // both UI and IO thread.
301 scoped_refptr
<SafeBrowsingDatabaseManager
> database_manager_
;
303 #if defined(FULL_SAFE_BROWSING)
304 scoped_ptr
<safe_browsing::OffDomainInclusionDetector
>
305 off_domain_inclusion_detector_
;
307 scoped_ptr
<safe_browsing::ResourceRequestDetector
> resource_request_detector_
;
310 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingService
);
313 // Factory for creating SafeBrowsingService. Useful for tests.
314 class SafeBrowsingServiceFactory
{
316 SafeBrowsingServiceFactory() { }
317 virtual ~SafeBrowsingServiceFactory() { }
318 virtual SafeBrowsingService
* CreateSafeBrowsingService() = 0;
320 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingServiceFactory
);
323 #endif // CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_SERVICE_H_