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 #if defined(SAFE_BROWSING_DB_REMOTE)
93 // Field trial for Android Safe Browsing. This is checked separately in
94 // SafeBrowsingFieldTrial.java for controlling the UI.
95 bool IsAndroidFieldTrialEnabled() const {
96 return is_android_field_trial_enabled_
;
98 #endif // defined(SAFE_BROWSING_DB_REMOTE)
100 // Called on the UI thread to initialize the service.
103 // Called on the main thread to let us know that the io_thread is going away.
106 // Called on UI thread to decide if the download file's sha256 hash
107 // should be calculated for safebrowsing.
108 bool DownloadBinHashNeeded() const;
110 // Create a protocol config struct.
111 virtual SafeBrowsingProtocolConfig
GetProtocolConfig() const;
113 // Get current enabled status. Must be called on IO thread.
114 bool enabled() const {
115 DCHECK_CURRENTLY_ON(content::BrowserThread::IO
);
119 // Whether the service is enabled by the current set of profiles.
120 bool enabled_by_prefs() const {
121 DCHECK_CURRENTLY_ON(content::BrowserThread::UI
);
122 return enabled_by_prefs_
;
125 safe_browsing::ClientSideDetectionService
*
126 safe_browsing_detection_service() const {
127 DCHECK_CURRENTLY_ON(content::BrowserThread::UI
);
128 return csd_service_
.get();
131 // The DownloadProtectionService is not valid after the SafeBrowsingService
133 safe_browsing::DownloadProtectionService
*
134 download_protection_service() const {
135 DCHECK_CURRENTLY_ON(content::BrowserThread::UI
);
136 return download_service_
.get();
139 net::URLRequestContextGetter
* url_request_context();
141 const scoped_refptr
<SafeBrowsingUIManager
>& ui_manager() const;
143 const scoped_refptr
<SafeBrowsingDatabaseManager
>& database_manager() const;
145 SafeBrowsingProtocolManager
* protocol_manager() const;
147 SafeBrowsingPingManager
* ping_manager() const;
149 // Returns a preference validation delegate that adds incidents to the
150 // incident reporting service for validation failures. Returns NULL if the
151 // service is not applicable for the given profile.
152 scoped_ptr
<TrackedPreferenceValidationDelegate
>
153 CreatePreferenceValidationDelegate(Profile
* profile
) const;
155 #if defined(FULL_SAFE_BROWSING)
156 // Registers |callback| to be run after some delay following process launch.
157 // |callback| will be dropped if the service is not applicable for the
159 void RegisterDelayedAnalysisCallback(
160 const safe_browsing::DelayedAnalysisCallback
& callback
);
163 // Adds |download_manager| to the set monitored by safe browsing.
164 void AddDownloadManager(content::DownloadManager
* download_manager
);
166 // Observes resource requests made by the renderer and reports suspicious
168 void OnResourceRequest(const net::URLRequest
* request
);
170 // Type for subscriptions to SafeBrowsing service state.
171 typedef base::CallbackList
<void(void)>::Subscription StateSubscription
;
173 // Adds a listener for when SafeBrowsing preferences might have changed.
174 // To get the current state, the callback should call enabled_by_prefs().
175 // Should only be called on the UI thread.
176 scoped_ptr
<StateSubscription
> RegisterStateCallback(
177 const base::Callback
<void(void)>& callback
);
180 // Creates the safe browsing service. Need to initialize before using.
181 SafeBrowsingService();
183 ~SafeBrowsingService() override
;
185 virtual SafeBrowsingDatabaseManager
* CreateDatabaseManager();
187 virtual SafeBrowsingUIManager
* CreateUIManager();
189 // Registers all the delayed analysis with the incident reporting service.
190 // This is where you register your process-wide, profile-independent analysis.
191 virtual void RegisterAllDelayedAnalysis();
193 // Return a ptr to DatabaseManager's delegate, or NULL if it doesn't have one.
194 virtual SafeBrowsingProtocolManagerDelegate
* GetProtocolManagerDelegate();
197 friend class SafeBrowsingServiceFactoryImpl
;
198 friend struct content::BrowserThread::DeleteOnThread
<
199 content::BrowserThread::UI
>;
200 friend class base::DeleteHelper
<SafeBrowsingService
>;
201 friend class SafeBrowsingServerTest
;
202 friend class SafeBrowsingServiceTest
;
203 friend class SafeBrowsingURLRequestContextGetter
;
205 void InitURLRequestContextOnIOThread(
206 net::URLRequestContextGetter
* system_url_request_context_getter
);
208 // Destroys the URLRequest and shuts down the provided getter on the
210 void DestroyURLRequestContextOnIOThread(
211 scoped_refptr
<SafeBrowsingURLRequestContextGetter
> context_getter
);
213 // Called to initialize objects that are used on the io_thread. This may be
214 // called multiple times during the life of the SafeBrowsingService.
215 void StartOnIOThread(
216 net::URLRequestContextGetter
* url_request_context_getter
);
218 // 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
220 // shutdown is true, then the operations on the io thread are shutdown
221 // permanently and cannot be restarted.
222 void StopOnIOThread(bool shutdown
);
224 // 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
229 // Stops the SafeBrowsingService. This can be called when the safe browsing
230 // preference is disabled. When shutdown is true, operation is permanently
231 // shutdown and cannot be restarted.
232 void Stop(bool shutdown
);
234 // content::NotificationObserver override
235 void Observe(int type
,
236 const content::NotificationSource
& source
,
237 const content::NotificationDetails
& details
) override
;
239 // Starts following the safe browsing preference on |pref_service|.
240 void AddPrefService(PrefService
* pref_service
);
242 // Stop following the safe browsing preference on |pref_service|.
243 void RemovePrefService(PrefService
* pref_service
);
245 // Checks if any profile is currently using the safe browsing service, and
246 // starts or stops the service accordingly.
249 // The factory used to instanciate a SafeBrowsingService object.
250 // Useful for tests, so they can provide their own implementation of
251 // SafeBrowsingService.
252 static SafeBrowsingServiceFactory
* factory_
;
254 // The SafeBrowsingURLRequestContextGetter used to access
255 // |url_request_context_|. Accessed on UI thread.
256 scoped_refptr
<SafeBrowsingURLRequestContextGetter
>
257 url_request_context_getter_
;
259 // The SafeBrowsingURLRequestContext. Accessed on IO thread.
260 scoped_ptr
<net::URLRequestContext
> url_request_context_
;
262 // Handles interaction with SafeBrowsing servers. Accessed on IO thread.
263 SafeBrowsingProtocolManager
* protocol_manager_
;
265 // Provides phishing and malware statistics. Accessed on IO thread.
266 SafeBrowsingPingManager
* ping_manager_
;
268 // Whether the service is running. 'enabled_' is used by SafeBrowsingService
269 // on the IO thread during normal operations.
272 // Whether SafeBrowsing is enabled by the current set of profiles.
273 // Accessed on UI thread.
274 bool enabled_by_prefs_
;
276 #if defined(SAFE_BROWSING_DB_REMOTE)
277 bool is_android_field_trial_enabled_
;
278 #endif // defined(SAFE_BROWSING_DB_REMOTE)
280 // Tracks existing PrefServices, and the safe browsing preference on each.
281 // This is used to determine if any profile is currently using the safe
282 // browsing service, and to start it up or shut it down accordingly.
283 // Accessed on UI thread.
284 std::map
<PrefService
*, PrefChangeRegistrar
*> prefs_map_
;
286 // Used to track creation and destruction of profiles on the UI thread.
287 content::NotificationRegistrar prefs_registrar_
;
289 // Callbacks when SafeBrowsing state might have changed.
290 // Should only be accessed on the UI thread.
291 base::CallbackList
<void(void)> state_callback_list_
;
293 // The ClientSideDetectionService is managed by the SafeBrowsingService,
294 // since its running state and lifecycle depends on SafeBrowsingService's.
295 // Accessed on UI thread.
296 scoped_ptr
<safe_browsing::ClientSideDetectionService
> csd_service_
;
298 // The DownloadProtectionService is managed by the SafeBrowsingService,
299 // since its running state and lifecycle depends on SafeBrowsingService's.
300 // Accessed on UI thread.
301 scoped_ptr
<safe_browsing::DownloadProtectionService
> download_service_
;
303 #if defined(FULL_SAFE_BROWSING)
304 scoped_ptr
<safe_browsing::IncidentReportingService
> incident_service_
;
307 // The UI manager handles showing interstitials. Accessed on both UI and IO
309 scoped_refptr
<SafeBrowsingUIManager
> ui_manager_
;
311 // The database manager handles the database and download logic. Accessed on
312 // both UI and IO thread.
313 scoped_refptr
<SafeBrowsingDatabaseManager
> database_manager_
;
315 #if defined(FULL_SAFE_BROWSING)
316 scoped_ptr
<safe_browsing::OffDomainInclusionDetector
>
317 off_domain_inclusion_detector_
;
319 scoped_ptr
<safe_browsing::ResourceRequestDetector
> resource_request_detector_
;
322 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingService
);
325 // Factory for creating SafeBrowsingService. Useful for tests.
326 class SafeBrowsingServiceFactory
{
328 SafeBrowsingServiceFactory() { }
329 virtual ~SafeBrowsingServiceFactory() { }
330 virtual SafeBrowsingService
* CreateSafeBrowsingService() = 0;
332 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingServiceFactory
);
335 #endif // CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_SERVICE_H_