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/files/file_path.h"
16 #include "base/memory/ref_counted.h"
17 #include "base/memory/scoped_ptr.h"
18 #include "base/observer_list.h"
19 #include "base/sequenced_task_runner_helpers.h"
20 #include "chrome/browser/safe_browsing/safe_browsing_util.h"
21 #include "content/public/browser/browser_thread.h"
22 #include "content/public/browser/notification_observer.h"
23 #include "content/public/browser/notification_registrar.h"
25 #if defined(FULL_SAFE_BROWSING)
26 #include "chrome/browser/safe_browsing/incident_reporting/delayed_analysis_callback.h"
29 class PrefChangeRegistrar
;
32 struct SafeBrowsingProtocolConfig
;
33 class SafeBrowsingDatabaseManager
;
34 class SafeBrowsingPingManager
;
35 class SafeBrowsingProtocolManager
;
36 class SafeBrowsingServiceFactory
;
37 class SafeBrowsingUIManager
;
38 class SafeBrowsingURLRequestContextGetter
;
39 class TrackedPreferenceValidationDelegate
;
46 class DownloadManager
;
51 class URLRequestContext
;
52 class URLRequestContextGetter
;
55 namespace safe_browsing
{
56 class ClientSideDetectionService
;
57 class DownloadProtectionService
;
59 #if defined(FULL_SAFE_BROWSING)
60 class IncidentReportingService
;
61 class OffDomainInclusionDetector
;
62 class ResourceRequestDetector
;
66 // Construction needs to happen on the main thread.
67 // The SafeBrowsingService owns both the UI and Database managers which do
68 // the heavylifting of safebrowsing service. Both of these managers stay
69 // alive until SafeBrowsingService is destroyed, however, they are disabled
70 // permanently when Shutdown method is called.
71 class SafeBrowsingService
72 : public base::RefCountedThreadSafe
<
74 content::BrowserThread::DeleteOnUIThread
>,
75 public content::NotificationObserver
{
77 // Makes the passed |factory| the factory used to instanciate
78 // a SafeBrowsingService. Useful for tests.
79 static void RegisterFactory(SafeBrowsingServiceFactory
* factory
) {
83 static base::FilePath
GetCookieFilePathForTesting();
85 static base::FilePath
GetBaseFilename();
87 // Create an instance of the safe browsing service.
88 static SafeBrowsingService
* CreateSafeBrowsingService();
90 // Called on the UI thread to initialize the service.
93 // Called on the main thread to let us know that the io_thread is going away.
96 // Called on UI thread to decide if the download file's sha256 hash
97 // should be calculated for safebrowsing.
98 bool DownloadBinHashNeeded() const;
100 // Create a protocol config struct.
101 virtual SafeBrowsingProtocolConfig
GetProtocolConfig() const;
103 bool enabled() const { return enabled_
; }
105 safe_browsing::ClientSideDetectionService
*
106 safe_browsing_detection_service() const {
107 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI
));
108 return csd_service_
.get();
111 // The DownloadProtectionService is not valid after the SafeBrowsingService
113 safe_browsing::DownloadProtectionService
*
114 download_protection_service() const {
115 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI
));
116 return download_service_
.get();
119 net::URLRequestContextGetter
* url_request_context();
121 const scoped_refptr
<SafeBrowsingUIManager
>& ui_manager() const;
123 const scoped_refptr
<SafeBrowsingDatabaseManager
>& database_manager() const;
125 SafeBrowsingProtocolManager
* protocol_manager() const;
127 SafeBrowsingPingManager
* ping_manager() const;
129 // Returns a preference validation delegate that adds incidents to the
130 // incident reporting service for validation failures. Returns NULL if the
131 // service is not applicable for the given profile.
132 scoped_ptr
<TrackedPreferenceValidationDelegate
>
133 CreatePreferenceValidationDelegate(Profile
* profile
) const;
135 #if defined(FULL_SAFE_BROWSING)
136 // Registers |callback| to be run after some delay following process launch.
137 // |callback| will be dropped if the service is not applicable for the
139 void RegisterDelayedAnalysisCallback(
140 const safe_browsing::DelayedAnalysisCallback
& callback
);
143 // Adds |download_manager| to the set monitored by safe browsing.
144 void AddDownloadManager(content::DownloadManager
* download_manager
);
146 // Observes resource requests made by the renderer and reports suspicious
148 void OnResourceRequest(const net::URLRequest
* request
);
151 // Creates the safe browsing service. Need to initialize before using.
152 SafeBrowsingService();
154 ~SafeBrowsingService() override
;
156 virtual SafeBrowsingDatabaseManager
* CreateDatabaseManager();
158 virtual SafeBrowsingUIManager
* CreateUIManager();
160 // Registers all the delayed analysis with the incident reporting service.
161 // This is where you register your process-wide, profile-independent analysis.
162 virtual void RegisterAllDelayedAnalysis();
165 friend class SafeBrowsingServiceFactoryImpl
;
166 friend struct content::BrowserThread::DeleteOnThread
<
167 content::BrowserThread::UI
>;
168 friend class base::DeleteHelper
<SafeBrowsingService
>;
169 friend class SafeBrowsingServerTest
;
170 friend class SafeBrowsingServiceTest
;
171 friend class SafeBrowsingURLRequestContextGetter
;
173 void InitURLRequestContextOnIOThread(
174 net::URLRequestContextGetter
* system_url_request_context_getter
);
176 void DestroyURLRequestContextOnIOThread();
178 // Called to initialize objects that are used on the io_thread. This may be
179 // called multiple times during the life of the SafeBrowsingService.
180 void StartOnIOThread(
181 net::URLRequestContextGetter
* url_request_context_getter
);
183 // Called to stop or shutdown operations on the io_thread. This may be called
184 // multiple times to stop during the life of the SafeBrowsingService. If
185 // shutdown is true, then the operations on the io thread are shutdown
186 // permanently and cannot be restarted.
187 void StopOnIOThread(bool shutdown
);
189 // Start up SafeBrowsing objects. This can be called at browser start, or when
190 // the user checks the "Enable SafeBrowsing" option in the Advanced options
194 // Stops the SafeBrowsingService. This can be called when the safe browsing
195 // preference is disabled. When shutdown is true, operation is permanently
196 // shutdown and cannot be restarted.
197 void Stop(bool shutdown
);
199 // content::NotificationObserver override
200 void Observe(int type
,
201 const content::NotificationSource
& source
,
202 const content::NotificationDetails
& details
) override
;
204 // Starts following the safe browsing preference on |pref_service|.
205 void AddPrefService(PrefService
* pref_service
);
207 // Stop following the safe browsing preference on |pref_service|.
208 void RemovePrefService(PrefService
* pref_service
);
210 // Checks if any profile is currently using the safe browsing service, and
211 // starts or stops the service accordingly.
214 // The factory used to instanciate a SafeBrowsingService object.
215 // Useful for tests, so they can provide their own implementation of
216 // SafeBrowsingService.
217 static SafeBrowsingServiceFactory
* factory_
;
219 // The SafeBrowsingURLRequestContextGetter used to access
220 // |url_request_context_|. Accessed on UI thread.
221 scoped_refptr
<net::URLRequestContextGetter
>
222 url_request_context_getter_
;
224 // The SafeBrowsingURLRequestContext. Accessed on IO thread.
225 scoped_ptr
<net::URLRequestContext
> url_request_context_
;
227 // Handles interaction with SafeBrowsing servers. Accessed on IO thread.
228 SafeBrowsingProtocolManager
* protocol_manager_
;
230 // Provides phishing and malware statistics. Accessed on IO thread.
231 SafeBrowsingPingManager
* ping_manager_
;
233 // Whether the service is running. 'enabled_' is used by SafeBrowsingService
234 // on the IO thread during normal operations.
237 // Tracks existing PrefServices, and the safe browsing preference on each.
238 // This is used to determine if any profile is currently using the safe
239 // browsing service, and to start it up or shut it down accordingly.
240 // Accessed on UI thread.
241 std::map
<PrefService
*, PrefChangeRegistrar
*> prefs_map_
;
243 // Used to track creation and destruction of profiles on the UI thread.
244 content::NotificationRegistrar prefs_registrar_
;
246 // The ClientSideDetectionService is managed by the SafeBrowsingService,
247 // since its running state and lifecycle depends on SafeBrowsingService's.
248 // Accessed on UI thread.
249 scoped_ptr
<safe_browsing::ClientSideDetectionService
> csd_service_
;
251 // The DownloadProtectionService is managed by the SafeBrowsingService,
252 // since its running state and lifecycle depends on SafeBrowsingService's.
253 // Accessed on UI thread.
254 scoped_ptr
<safe_browsing::DownloadProtectionService
> download_service_
;
256 #if defined(FULL_SAFE_BROWSING)
257 scoped_ptr
<safe_browsing::IncidentReportingService
> incident_service_
;
260 // The UI manager handles showing interstitials. Accessed on both UI and IO
262 scoped_refptr
<SafeBrowsingUIManager
> ui_manager_
;
264 // The database manager handles the database and download logic. Accessed on
265 // both UI and IO thread.
266 scoped_refptr
<SafeBrowsingDatabaseManager
> database_manager_
;
268 #if defined(FULL_SAFE_BROWSING)
269 scoped_ptr
<safe_browsing::OffDomainInclusionDetector
>
270 off_domain_inclusion_detector_
;
272 scoped_ptr
<safe_browsing::ResourceRequestDetector
> resource_request_detector_
;
275 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingService
);
278 // Factory for creating SafeBrowsingService. Useful for tests.
279 class SafeBrowsingServiceFactory
{
281 SafeBrowsingServiceFactory() { }
282 virtual ~SafeBrowsingServiceFactory() { }
283 virtual SafeBrowsingService
* CreateSafeBrowsingService() = 0;
285 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingServiceFactory
);
288 #endif // CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_SERVICE_H_