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 class PrefChangeRegistrar
;
27 struct SafeBrowsingProtocolConfig
;
28 class SafeBrowsingDatabaseManager
;
29 class SafeBrowsingPingManager
;
30 class SafeBrowsingProtocolManager
;
31 class SafeBrowsingServiceFactory
;
32 class SafeBrowsingUIManager
;
33 class SafeBrowsingURLRequestContextGetter
;
40 class URLRequestContext
;
41 class URLRequestContextGetter
;
44 namespace safe_browsing
{
45 class ClientSideDetectionService
;
46 class DownloadProtectionService
;
49 // Construction needs to happen on the main thread.
50 // The SafeBrowsingService owns both the UI and Database managers which do
51 // the heavylifting of safebrowsing service. Both of these managers stay
52 // alive until SafeBrowsingService is destroyed, however, they are disabled
53 // permanently when Shutdown method is called.
54 class SafeBrowsingService
55 : public base::RefCountedThreadSafe
<
57 content::BrowserThread::DeleteOnUIThread
>,
58 public content::NotificationObserver
{
60 // Makes the passed |factory| the factory used to instanciate
61 // a SafeBrowsingService. Useful for tests.
62 static void RegisterFactory(SafeBrowsingServiceFactory
* factory
) {
66 static base::FilePath
GetCookieFilePathForTesting();
68 static base::FilePath
GetBaseFilename();
70 // Create an instance of the safe browsing service.
71 static SafeBrowsingService
* CreateSafeBrowsingService();
73 // Called on the UI thread to initialize the service.
76 // Called on the main thread to let us know that the io_thread is going away.
79 // Called on UI thread to decide if the download file's sha256 hash
80 // should be calculated for safebrowsing.
81 bool DownloadBinHashNeeded() const;
83 // Create a protocol config struct.
84 SafeBrowsingProtocolConfig
GetProtocolConfig() const;
86 bool enabled() const { return enabled_
; }
88 safe_browsing::ClientSideDetectionService
*
89 safe_browsing_detection_service() const {
90 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI
));
91 return csd_service_
.get();
94 // The DownloadProtectionService is not valid after the SafeBrowsingService
96 safe_browsing::DownloadProtectionService
*
97 download_protection_service() const {
98 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI
));
99 return download_service_
.get();
102 net::URLRequestContextGetter
* url_request_context();
104 const scoped_refptr
<SafeBrowsingUIManager
>& ui_manager() const;
106 const scoped_refptr
<SafeBrowsingDatabaseManager
>& database_manager() const;
108 SafeBrowsingProtocolManager
* protocol_manager() const;
110 SafeBrowsingPingManager
* ping_manager() const;
113 // Creates the safe browsing service. Need to initialize before using.
114 SafeBrowsingService();
116 virtual ~SafeBrowsingService();
118 virtual SafeBrowsingDatabaseManager
* CreateDatabaseManager();
120 virtual SafeBrowsingUIManager
* CreateUIManager();
123 friend class SafeBrowsingServiceFactoryImpl
;
124 friend struct content::BrowserThread::DeleteOnThread
<
125 content::BrowserThread::UI
>;
126 friend class base::DeleteHelper
<SafeBrowsingService
>;
127 friend class SafeBrowsingServerTest
;
128 friend class SafeBrowsingServiceTest
;
129 friend class SafeBrowsingURLRequestContextGetter
;
131 void InitURLRequestContextOnIOThread(
132 net::URLRequestContextGetter
* system_url_request_context_getter
);
134 void DestroyURLRequestContextOnIOThread();
136 // Called to initialize objects that are used on the io_thread. This may be
137 // called multiple times during the life of the SafeBrowsingService.
138 void StartOnIOThread(
139 net::URLRequestContextGetter
* url_request_context_getter
);
141 // Called to stop or shutdown operations on the io_thread. This may be called
142 // multiple times to stop during the life of the SafeBrowsingService. If
143 // shutdown is true, then the operations on the io thread are shutdown
144 // permanently and cannot be restarted.
145 void StopOnIOThread(bool shutdown
);
147 // Start up SafeBrowsing objects. This can be called at browser start, or when
148 // the user checks the "Enable SafeBrowsing" option in the Advanced options
152 // Stops the SafeBrowsingService. This can be called when the safe browsing
153 // preference is disabled. When shutdown is true, operation is permanently
154 // shutdown and cannot be restarted.
155 void Stop(bool shutdown
);
157 // content::NotificationObserver override
158 virtual void Observe(int type
,
159 const content::NotificationSource
& source
,
160 const content::NotificationDetails
& details
) OVERRIDE
;
162 // Starts following the safe browsing preference on |pref_service|.
163 void AddPrefService(PrefService
* pref_service
);
165 // Stop following the safe browsing preference on |pref_service|.
166 void RemovePrefService(PrefService
* pref_service
);
168 // Checks if any profile is currently using the safe browsing service, and
169 // starts or stops the service accordingly.
172 // The factory used to instanciate a SafeBrowsingService object.
173 // Useful for tests, so they can provide their own implementation of
174 // SafeBrowsingService.
175 static SafeBrowsingServiceFactory
* factory_
;
177 // The SafeBrowsingURLRequestContextGetter used to access
178 // |url_request_context_|. Accessed on UI thread.
179 scoped_refptr
<net::URLRequestContextGetter
>
180 url_request_context_getter_
;
182 // The SafeBrowsingURLRequestContext. Accessed on IO thread.
183 scoped_ptr
<net::URLRequestContext
> url_request_context_
;
185 // Handles interaction with SafeBrowsing servers. Accessed on IO thread.
186 SafeBrowsingProtocolManager
* protocol_manager_
;
188 // Provides phishing and malware statistics. Accessed on IO thread.
189 SafeBrowsingPingManager
* ping_manager_
;
191 // Whether the service is running. 'enabled_' is used by SafeBrowsingService
192 // on the IO thread during normal operations.
195 // Tracks existing PrefServices, and the safe browsing preference on each.
196 // This is used to determine if any profile is currently using the safe
197 // browsing service, and to start it up or shut it down accordingly.
198 // Accessed on UI thread.
199 std::map
<PrefService
*, PrefChangeRegistrar
*> prefs_map_
;
201 // Used to track creation and destruction of profiles on the UI thread.
202 content::NotificationRegistrar prefs_registrar_
;
204 // The ClientSideDetectionService is managed by the SafeBrowsingService,
205 // since its running state and lifecycle depends on SafeBrowsingService's.
206 // Accessed on UI thread.
207 scoped_ptr
<safe_browsing::ClientSideDetectionService
> csd_service_
;
209 // The DownloadProtectionService is managed by the SafeBrowsingService,
210 // since its running state and lifecycle depends on SafeBrowsingService's.
211 // Accessed on UI thread.
212 scoped_ptr
<safe_browsing::DownloadProtectionService
> download_service_
;
214 // The UI manager handles showing interstitials. Accessed on both UI and IO
216 scoped_refptr
<SafeBrowsingUIManager
> ui_manager_
;
218 // The database manager handles the database and download logic. Accessed on
219 // both UI and IO thread.
220 scoped_refptr
<SafeBrowsingDatabaseManager
> database_manager_
;
222 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingService
);
225 // Factory for creating SafeBrowsingService. Useful for tests.
226 class SafeBrowsingServiceFactory
{
228 SafeBrowsingServiceFactory() { }
229 virtual ~SafeBrowsingServiceFactory() { }
230 virtual SafeBrowsingService
* CreateSafeBrowsingService() = 0;
232 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingServiceFactory
);
235 #endif // CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_SERVICE_H_