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/incident_reporting/delayed_analysis_callback.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 class PrefChangeRegistrar
;
29 struct SafeBrowsingProtocolConfig
;
30 class SafeBrowsingDatabaseManager
;
31 class SafeBrowsingPingManager
;
32 class SafeBrowsingProtocolManager
;
33 class SafeBrowsingServiceFactory
;
34 class SafeBrowsingUIManager
;
35 class SafeBrowsingURLRequestContextGetter
;
36 class TrackedPreferenceValidationDelegate
;
43 class URLRequestContext
;
44 class URLRequestContextGetter
;
47 namespace safe_browsing
{
48 class ClientSideDetectionService
;
49 class DownloadProtectionService
;
50 class IncidentReportingService
;
53 // Construction needs to happen on the main thread.
54 // The SafeBrowsingService owns both the UI and Database managers which do
55 // the heavylifting of safebrowsing service. Both of these managers stay
56 // alive until SafeBrowsingService is destroyed, however, they are disabled
57 // permanently when Shutdown method is called.
58 class SafeBrowsingService
59 : public base::RefCountedThreadSafe
<
61 content::BrowserThread::DeleteOnUIThread
>,
62 public content::NotificationObserver
{
64 // Makes the passed |factory| the factory used to instanciate
65 // a SafeBrowsingService. Useful for tests.
66 static void RegisterFactory(SafeBrowsingServiceFactory
* factory
) {
70 static base::FilePath
GetCookieFilePathForTesting();
72 static base::FilePath
GetBaseFilename();
74 // Create an instance of the safe browsing service.
75 static SafeBrowsingService
* CreateSafeBrowsingService();
77 #if defined(OS_ANDROID) && defined(FULL_SAFE_BROWSING)
78 // Return whether the user is in mobile safe browsing
79 // field trial enabled group.
80 static bool IsEnabledByFieldTrial();
83 // Called on the UI thread to initialize the service.
86 // Called on the main thread to let us know that the io_thread is going away.
89 // Called on UI thread to decide if the download file's sha256 hash
90 // should be calculated for safebrowsing.
91 bool DownloadBinHashNeeded() const;
93 // Create a protocol config struct.
94 virtual SafeBrowsingProtocolConfig
GetProtocolConfig() const;
96 bool enabled() const { return enabled_
; }
98 safe_browsing::ClientSideDetectionService
*
99 safe_browsing_detection_service() const {
100 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI
));
101 return csd_service_
.get();
104 // The DownloadProtectionService is not valid after the SafeBrowsingService
106 safe_browsing::DownloadProtectionService
*
107 download_protection_service() const {
108 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI
));
109 return download_service_
.get();
112 net::URLRequestContextGetter
* url_request_context();
114 const scoped_refptr
<SafeBrowsingUIManager
>& ui_manager() const;
116 const scoped_refptr
<SafeBrowsingDatabaseManager
>& database_manager() const;
118 SafeBrowsingProtocolManager
* protocol_manager() const;
120 SafeBrowsingPingManager
* ping_manager() const;
122 // Returns a preference validation delegate that adds incidents to the
123 // incident reporting service for validation failures. Returns NULL if the
124 // service is not applicable for the given profile.
125 scoped_ptr
<TrackedPreferenceValidationDelegate
>
126 CreatePreferenceValidationDelegate(Profile
* profile
) const;
128 // Registers |callback| to be run after some delay following process launch.
129 // |callback| will be dropped if the service is not applicable for the
131 void RegisterDelayedAnalysisCallback(
132 const safe_browsing::DelayedAnalysisCallback
& callback
);
135 // Creates the safe browsing service. Need to initialize before using.
136 SafeBrowsingService();
138 virtual ~SafeBrowsingService();
140 virtual SafeBrowsingDatabaseManager
* CreateDatabaseManager();
142 virtual SafeBrowsingUIManager
* CreateUIManager();
144 // Registers all the delayed analysis with the incident reporting service.
145 // This is where you register your process-wide, profile-independent analysis.
146 virtual void RegisterAllDelayedAnalysis();
149 friend class SafeBrowsingServiceFactoryImpl
;
150 friend struct content::BrowserThread::DeleteOnThread
<
151 content::BrowserThread::UI
>;
152 friend class base::DeleteHelper
<SafeBrowsingService
>;
153 friend class SafeBrowsingServerTest
;
154 friend class SafeBrowsingServiceTest
;
155 friend class SafeBrowsingURLRequestContextGetter
;
157 void InitURLRequestContextOnIOThread(
158 net::URLRequestContextGetter
* system_url_request_context_getter
);
160 void DestroyURLRequestContextOnIOThread();
162 // Called to initialize objects that are used on the io_thread. This may be
163 // called multiple times during the life of the SafeBrowsingService.
164 void StartOnIOThread(
165 net::URLRequestContextGetter
* url_request_context_getter
);
167 // Called to stop or shutdown operations on the io_thread. This may be called
168 // multiple times to stop during the life of the SafeBrowsingService. If
169 // shutdown is true, then the operations on the io thread are shutdown
170 // permanently and cannot be restarted.
171 void StopOnIOThread(bool shutdown
);
173 // Start up SafeBrowsing objects. This can be called at browser start, or when
174 // the user checks the "Enable SafeBrowsing" option in the Advanced options
178 // Stops the SafeBrowsingService. This can be called when the safe browsing
179 // preference is disabled. When shutdown is true, operation is permanently
180 // shutdown and cannot be restarted.
181 void Stop(bool shutdown
);
183 // content::NotificationObserver override
184 virtual void Observe(int type
,
185 const content::NotificationSource
& source
,
186 const content::NotificationDetails
& details
) OVERRIDE
;
188 // Starts following the safe browsing preference on |pref_service|.
189 void AddPrefService(PrefService
* pref_service
);
191 // Stop following the safe browsing preference on |pref_service|.
192 void RemovePrefService(PrefService
* pref_service
);
194 // Checks if any profile is currently using the safe browsing service, and
195 // starts or stops the service accordingly.
198 // The factory used to instanciate a SafeBrowsingService object.
199 // Useful for tests, so they can provide their own implementation of
200 // SafeBrowsingService.
201 static SafeBrowsingServiceFactory
* factory_
;
203 // The SafeBrowsingURLRequestContextGetter used to access
204 // |url_request_context_|. Accessed on UI thread.
205 scoped_refptr
<net::URLRequestContextGetter
>
206 url_request_context_getter_
;
208 // The SafeBrowsingURLRequestContext. Accessed on IO thread.
209 scoped_ptr
<net::URLRequestContext
> url_request_context_
;
211 // Handles interaction with SafeBrowsing servers. Accessed on IO thread.
212 SafeBrowsingProtocolManager
* protocol_manager_
;
214 // Provides phishing and malware statistics. Accessed on IO thread.
215 SafeBrowsingPingManager
* ping_manager_
;
217 // Whether the service is running. 'enabled_' is used by SafeBrowsingService
218 // on the IO thread during normal operations.
221 // Tracks existing PrefServices, and the safe browsing preference on each.
222 // This is used to determine if any profile is currently using the safe
223 // browsing service, and to start it up or shut it down accordingly.
224 // Accessed on UI thread.
225 std::map
<PrefService
*, PrefChangeRegistrar
*> prefs_map_
;
227 // Used to track creation and destruction of profiles on the UI thread.
228 content::NotificationRegistrar prefs_registrar_
;
230 // The ClientSideDetectionService is managed by the SafeBrowsingService,
231 // since its running state and lifecycle depends on SafeBrowsingService's.
232 // Accessed on UI thread.
233 scoped_ptr
<safe_browsing::ClientSideDetectionService
> csd_service_
;
235 // The DownloadProtectionService is managed by the SafeBrowsingService,
236 // since its running state and lifecycle depends on SafeBrowsingService's.
237 // Accessed on UI thread.
238 scoped_ptr
<safe_browsing::DownloadProtectionService
> download_service_
;
240 scoped_ptr
<safe_browsing::IncidentReportingService
> incident_service_
;
242 // The UI manager handles showing interstitials. Accessed on both UI and IO
244 scoped_refptr
<SafeBrowsingUIManager
> ui_manager_
;
246 // The database manager handles the database and download logic. Accessed on
247 // both UI and IO thread.
248 scoped_refptr
<SafeBrowsingDatabaseManager
> database_manager_
;
250 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingService
);
253 // Factory for creating SafeBrowsingService. Useful for tests.
254 class SafeBrowsingServiceFactory
{
256 SafeBrowsingServiceFactory() { }
257 virtual ~SafeBrowsingServiceFactory() { }
258 virtual SafeBrowsingService
* CreateSafeBrowsingService() = 0;
260 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingServiceFactory
);
263 #endif // CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_SERVICE_H_