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 DownloadManager;
47 class URLRequestContext;
48 class URLRequestContextGetter;
51 namespace safe_browsing {
52 class ClientSideDetectionService;
53 class DownloadProtectionService;
54 class IncidentReportingService;
57 // Construction needs to happen on the main thread.
58 // The SafeBrowsingService owns both the UI and Database managers which do
59 // the heavylifting of safebrowsing service. Both of these managers stay
60 // alive until SafeBrowsingService is destroyed, however, they are disabled
61 // permanently when Shutdown method is called.
62 class SafeBrowsingService
63 : public base::RefCountedThreadSafe<
65 content::BrowserThread::DeleteOnUIThread>,
66 public content::NotificationObserver {
68 // Makes the passed |factory| the factory used to instanciate
69 // a SafeBrowsingService. Useful for tests.
70 static void RegisterFactory(SafeBrowsingServiceFactory* factory) {
74 static base::FilePath GetCookieFilePathForTesting();
76 static base::FilePath GetBaseFilename();
78 // Create an instance of the safe browsing service.
79 static SafeBrowsingService* CreateSafeBrowsingService();
81 #if defined(OS_ANDROID) && defined(FULL_SAFE_BROWSING)
82 // Return whether the user is in mobile safe browsing
83 // field trial enabled group.
84 static bool IsEnabledByFieldTrial();
87 // Called on the UI thread to initialize the service.
90 // Called on the main thread to let us know that the io_thread is going away.
93 // Called on UI thread to decide if the download file's sha256 hash
94 // should be calculated for safebrowsing.
95 bool DownloadBinHashNeeded() const;
97 // Create a protocol config struct.
98 virtual SafeBrowsingProtocolConfig GetProtocolConfig() const;
100 bool enabled() const { return enabled_; }
102 safe_browsing::ClientSideDetectionService*
103 safe_browsing_detection_service() const {
104 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
105 return csd_service_.get();
108 // The DownloadProtectionService is not valid after the SafeBrowsingService
110 safe_browsing::DownloadProtectionService*
111 download_protection_service() const {
112 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
113 return download_service_.get();
116 net::URLRequestContextGetter* url_request_context();
118 const scoped_refptr<SafeBrowsingUIManager>& ui_manager() const;
120 const scoped_refptr<SafeBrowsingDatabaseManager>& database_manager() const;
122 SafeBrowsingProtocolManager* protocol_manager() const;
124 SafeBrowsingPingManager* ping_manager() const;
126 // Returns a preference validation delegate that adds incidents to the
127 // incident reporting service for validation failures. Returns NULL if the
128 // service is not applicable for the given profile.
129 scoped_ptr<TrackedPreferenceValidationDelegate>
130 CreatePreferenceValidationDelegate(Profile* profile) const;
132 // Registers |callback| to be run after some delay following process launch.
133 // |callback| will be dropped if the service is not applicable for the
135 void RegisterDelayedAnalysisCallback(
136 const safe_browsing::DelayedAnalysisCallback& callback);
138 // Adds |download_manager| to the set monitored by safe browsing.
139 void AddDownloadManager(content::DownloadManager* download_manager);
142 // Creates the safe browsing service. Need to initialize before using.
143 SafeBrowsingService();
145 ~SafeBrowsingService() override;
147 virtual SafeBrowsingDatabaseManager* CreateDatabaseManager();
149 virtual SafeBrowsingUIManager* CreateUIManager();
151 // Registers all the delayed analysis with the incident reporting service.
152 // This is where you register your process-wide, profile-independent analysis.
153 virtual void RegisterAllDelayedAnalysis();
156 friend class SafeBrowsingServiceFactoryImpl;
157 friend struct content::BrowserThread::DeleteOnThread<
158 content::BrowserThread::UI>;
159 friend class base::DeleteHelper<SafeBrowsingService>;
160 friend class SafeBrowsingServerTest;
161 friend class SafeBrowsingServiceTest;
162 friend class SafeBrowsingURLRequestContextGetter;
164 void InitURLRequestContextOnIOThread(
165 net::URLRequestContextGetter* system_url_request_context_getter);
167 void DestroyURLRequestContextOnIOThread();
169 // Called to initialize objects that are used on the io_thread. This may be
170 // called multiple times during the life of the SafeBrowsingService.
171 void StartOnIOThread(
172 net::URLRequestContextGetter* url_request_context_getter);
174 // Called to stop or shutdown operations on the io_thread. This may be called
175 // multiple times to stop during the life of the SafeBrowsingService. If
176 // shutdown is true, then the operations on the io thread are shutdown
177 // permanently and cannot be restarted.
178 void StopOnIOThread(bool shutdown);
180 // Start up SafeBrowsing objects. This can be called at browser start, or when
181 // the user checks the "Enable SafeBrowsing" option in the Advanced options
185 // Stops the SafeBrowsingService. This can be called when the safe browsing
186 // preference is disabled. When shutdown is true, operation is permanently
187 // shutdown and cannot be restarted.
188 void Stop(bool shutdown);
190 // content::NotificationObserver override
191 void Observe(int type,
192 const content::NotificationSource& source,
193 const content::NotificationDetails& details) override;
195 // Starts following the safe browsing preference on |pref_service|.
196 void AddPrefService(PrefService* pref_service);
198 // Stop following the safe browsing preference on |pref_service|.
199 void RemovePrefService(PrefService* pref_service);
201 // Checks if any profile is currently using the safe browsing service, and
202 // starts or stops the service accordingly.
205 // The factory used to instanciate a SafeBrowsingService object.
206 // Useful for tests, so they can provide their own implementation of
207 // SafeBrowsingService.
208 static SafeBrowsingServiceFactory* factory_;
210 // The SafeBrowsingURLRequestContextGetter used to access
211 // |url_request_context_|. Accessed on UI thread.
212 scoped_refptr<net::URLRequestContextGetter>
213 url_request_context_getter_;
215 // The SafeBrowsingURLRequestContext. Accessed on IO thread.
216 scoped_ptr<net::URLRequestContext> url_request_context_;
218 // Handles interaction with SafeBrowsing servers. Accessed on IO thread.
219 SafeBrowsingProtocolManager* protocol_manager_;
221 // Provides phishing and malware statistics. Accessed on IO thread.
222 SafeBrowsingPingManager* ping_manager_;
224 // Whether the service is running. 'enabled_' is used by SafeBrowsingService
225 // on the IO thread during normal operations.
228 // Tracks existing PrefServices, and the safe browsing preference on each.
229 // This is used to determine if any profile is currently using the safe
230 // browsing service, and to start it up or shut it down accordingly.
231 // Accessed on UI thread.
232 std::map<PrefService*, PrefChangeRegistrar*> prefs_map_;
234 // Used to track creation and destruction of profiles on the UI thread.
235 content::NotificationRegistrar prefs_registrar_;
237 // The ClientSideDetectionService is managed by the SafeBrowsingService,
238 // since its running state and lifecycle depends on SafeBrowsingService's.
239 // Accessed on UI thread.
240 scoped_ptr<safe_browsing::ClientSideDetectionService> csd_service_;
242 // The DownloadProtectionService is managed by the SafeBrowsingService,
243 // since its running state and lifecycle depends on SafeBrowsingService's.
244 // Accessed on UI thread.
245 scoped_ptr<safe_browsing::DownloadProtectionService> download_service_;
247 scoped_ptr<safe_browsing::IncidentReportingService> incident_service_;
249 // The UI manager handles showing interstitials. Accessed on both UI and IO
251 scoped_refptr<SafeBrowsingUIManager> ui_manager_;
253 // The database manager handles the database and download logic. Accessed on
254 // both UI and IO thread.
255 scoped_refptr<SafeBrowsingDatabaseManager> database_manager_;
257 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingService);
260 // Factory for creating SafeBrowsingService. Useful for tests.
261 class SafeBrowsingServiceFactory {
263 SafeBrowsingServiceFactory() { }
264 virtual ~SafeBrowsingServiceFactory() { }
265 virtual SafeBrowsingService* CreateSafeBrowsingService() = 0;
267 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingServiceFactory);
270 #endif // CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_SERVICE_H_