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 class SafeBrowsingDatabaseManager
;
28 class SafeBrowsingPingManager
;
29 class SafeBrowsingProtocolManager
;
30 class SafeBrowsingServiceFactory
;
31 class SafeBrowsingUIManager
;
32 class SafeBrowsingURLRequestContextGetter
;
39 class URLRequestContext
;
40 class URLRequestContextGetter
;
43 namespace safe_browsing
{
44 class ClientSideDetectionService
;
45 class DownloadProtectionService
;
48 // Construction needs to happen on the main thread.
49 // The SafeBrowsingService owns both the UI and Database managers which do
50 // the heavylifting of safebrowsing service. Both of these managers stay
51 // alive until SafeBrowsingService is destroyed, however, they are disabled
52 // permanently when Shutdown method is called.
53 class SafeBrowsingService
54 : public base::RefCountedThreadSafe
<
56 content::BrowserThread::DeleteOnUIThread
>,
57 public content::NotificationObserver
{
59 // Makes the passed |factory| the factory used to instanciate
60 // a SafeBrowsingService. Useful for tests.
61 static void RegisterFactory(SafeBrowsingServiceFactory
* factory
) {
65 static base::FilePath
GetCookieFilePathForTesting();
67 static base::FilePath
GetBaseFilename();
69 // Create an instance of the safe browsing service.
70 static SafeBrowsingService
* CreateSafeBrowsingService();
72 // Called on the UI thread to initialize the service.
75 // Called on the main thread to let us know that the io_thread is going away.
78 // Called on UI thread to decide if the download file's sha256 hash
79 // should be calculated for safebrowsing.
80 bool DownloadBinHashNeeded() const;
82 bool enabled() const { return enabled_
; }
84 safe_browsing::ClientSideDetectionService
*
85 safe_browsing_detection_service() const {
86 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI
));
87 return csd_service_
.get();
90 // The DownloadProtectionService is not valid after the SafeBrowsingService
92 safe_browsing::DownloadProtectionService
*
93 download_protection_service() const {
94 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI
));
95 return download_service_
.get();
98 net::URLRequestContextGetter
* url_request_context();
100 const scoped_refptr
<SafeBrowsingUIManager
>& ui_manager() const;
102 const scoped_refptr
<SafeBrowsingDatabaseManager
>& database_manager() const;
104 SafeBrowsingProtocolManager
* protocol_manager() const;
106 SafeBrowsingPingManager
* ping_manager() const;
109 // Creates the safe browsing service. Need to initialize before using.
110 SafeBrowsingService();
112 virtual ~SafeBrowsingService();
114 virtual SafeBrowsingDatabaseManager
* CreateDatabaseManager();
116 virtual SafeBrowsingUIManager
* CreateUIManager();
119 friend class SafeBrowsingServiceFactoryImpl
;
120 friend struct content::BrowserThread::DeleteOnThread
<
121 content::BrowserThread::UI
>;
122 friend class base::DeleteHelper
<SafeBrowsingService
>;
123 friend class SafeBrowsingServerTest
;
124 friend class SafeBrowsingServiceTest
;
125 friend class SafeBrowsingURLRequestContextGetter
;
127 void InitURLRequestContextOnIOThread(
128 net::URLRequestContextGetter
* system_url_request_context_getter
);
130 void DestroyURLRequestContextOnIOThread();
132 // Called to initialize objects that are used on the io_thread. This may be
133 // called multiple times during the life of the SafeBrowsingService.
134 void StartOnIOThread(
135 net::URLRequestContextGetter
* url_request_context_getter
);
137 // Called to stop or shutdown operations on the io_thread. This may be called
138 // multiple times to stop during the life of the SafeBrowsingService. If
139 // shutdown is true, then the operations on the io thread are shutdown
140 // permanently and cannot be restarted.
141 void StopOnIOThread(bool shutdown
);
143 // Start up SafeBrowsing objects. This can be called at browser start, or when
144 // the user checks the "Enable SafeBrowsing" option in the Advanced options
148 // Stops the SafeBrowsingService. This can be called when the safe browsing
149 // preference is disabled. When shutdown is true, operation is permanently
150 // shutdown and cannot be restarted.
151 void Stop(bool shutdown
);
153 // content::NotificationObserver override
154 virtual void Observe(int type
,
155 const content::NotificationSource
& source
,
156 const content::NotificationDetails
& details
) OVERRIDE
;
158 // Starts following the safe browsing preference on |pref_service|.
159 void AddPrefService(PrefService
* pref_service
);
161 // Stop following the safe browsing preference on |pref_service|.
162 void RemovePrefService(PrefService
* pref_service
);
164 // Checks if any profile is currently using the safe browsing service, and
165 // starts or stops the service accordingly.
168 // The factory used to instanciate a SafeBrowsingService object.
169 // Useful for tests, so they can provide their own implementation of
170 // SafeBrowsingService.
171 static SafeBrowsingServiceFactory
* factory_
;
173 // The SafeBrowsingURLRequestContextGetter used to access
174 // |url_request_context_|. Accessed on UI thread.
175 scoped_refptr
<net::URLRequestContextGetter
>
176 url_request_context_getter_
;
178 // The SafeBrowsingURLRequestContext. Accessed on IO thread.
179 scoped_ptr
<net::URLRequestContext
> url_request_context_
;
181 // Handles interaction with SafeBrowsing servers. Accessed on IO thread.
182 SafeBrowsingProtocolManager
* protocol_manager_
;
184 // Provides phishing and malware statistics. Accessed on IO thread.
185 SafeBrowsingPingManager
* ping_manager_
;
187 // Whether the service is running. 'enabled_' is used by SafeBrowsingService
188 // on the IO thread during normal operations.
191 // Tracks existing PrefServices, and the safe browsing preference on each.
192 // This is used to determine if any profile is currently using the safe
193 // browsing service, and to start it up or shut it down accordingly.
194 // Accessed on UI thread.
195 std::map
<PrefService
*, PrefChangeRegistrar
*> prefs_map_
;
197 // Used to track creation and destruction of profiles on the UI thread.
198 content::NotificationRegistrar prefs_registrar_
;
200 // The ClientSideDetectionService is managed by the SafeBrowsingService,
201 // since its running state and lifecycle depends on SafeBrowsingService's.
202 // Accessed on UI thread.
203 scoped_ptr
<safe_browsing::ClientSideDetectionService
> csd_service_
;
205 // The DownloadProtectionService is managed by the SafeBrowsingService,
206 // since its running state and lifecycle depends on SafeBrowsingService's.
207 // Accessed on UI thread.
208 scoped_ptr
<safe_browsing::DownloadProtectionService
> download_service_
;
210 // The UI manager handles showing interstitials. Accessed on both UI and IO
212 scoped_refptr
<SafeBrowsingUIManager
> ui_manager_
;
214 // The database manager handles the database and download logic. Accessed on
215 // both UI and IO thread.
216 scoped_refptr
<SafeBrowsingDatabaseManager
> database_manager_
;
218 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingService
);
221 // Factory for creating SafeBrowsingService. Useful for tests.
222 class SafeBrowsingServiceFactory
{
224 SafeBrowsingServiceFactory() { }
225 virtual ~SafeBrowsingServiceFactory() { }
226 virtual SafeBrowsingService
* CreateSafeBrowsingService() = 0;
228 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingServiceFactory
);
231 #endif // CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_SERVICE_H_