Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / safe_browsing / safe_browsing_service.h
blobc7e5c6b2c9f8522e54c38d25efacd1198f72e5ee
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.
4 //
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_
11 #include <map>
12 #include <string>
14 #include "base/callback.h"
15 #include "base/callback_list.h"
16 #include "base/files/file_path.h"
17 #include "base/memory/ref_counted.h"
18 #include "base/memory/scoped_ptr.h"
19 #include "base/observer_list.h"
20 #include "base/sequenced_task_runner_helpers.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 #if defined(FULL_SAFE_BROWSING)
27 #include "chrome/browser/safe_browsing/incident_reporting/delayed_analysis_callback.h"
28 #endif
30 class PrefChangeRegistrar;
31 class PrefService;
32 class Profile;
33 struct SafeBrowsingProtocolConfig;
34 class SafeBrowsingDatabaseManager;
35 class SafeBrowsingPingManager;
36 class SafeBrowsingProtocolManager;
37 class SafeBrowsingProtocolManagerDelegate;
38 class SafeBrowsingServiceFactory;
39 class SafeBrowsingUIManager;
40 class SafeBrowsingURLRequestContextGetter;
41 class TrackedPreferenceValidationDelegate;
43 namespace base {
44 class Thread;
47 namespace content {
48 class DownloadManager;
51 namespace net {
52 class URLRequest;
53 class URLRequestContext;
54 class URLRequestContextGetter;
57 namespace safe_browsing {
58 class ClientSideDetectionService;
59 class DownloadProtectionService;
61 #if defined(FULL_SAFE_BROWSING)
62 class IncidentReportingService;
63 class OffDomainInclusionDetector;
64 class ResourceRequestDetector;
65 #endif
66 } // namespace safe_browsing
68 // Construction needs to happen on the main thread.
69 // The SafeBrowsingService owns both the UI and Database managers which do
70 // the heavylifting of safebrowsing service. Both of these managers stay
71 // alive until SafeBrowsingService is destroyed, however, they are disabled
72 // permanently when Shutdown method is called.
73 class SafeBrowsingService
74 : public base::RefCountedThreadSafe<
75 SafeBrowsingService,
76 content::BrowserThread::DeleteOnUIThread>,
77 public content::NotificationObserver {
78 public:
79 enum ResourceTypesToCheck {
80 CHECK_ALL_RESOURCE_TYPES,
81 CHECK_ONLY_DANGEROUS_TYPES,
84 // Makes the passed |factory| the factory used to instanciate
85 // a SafeBrowsingService. Useful for tests.
86 static void RegisterFactory(SafeBrowsingServiceFactory* factory) {
87 factory_ = factory;
90 static base::FilePath GetCookieFilePathForTesting();
92 static base::FilePath GetBaseFilename();
94 // Create an instance of the safe browsing service.
95 static SafeBrowsingService* CreateSafeBrowsingService();
97 #if defined(SAFE_BROWSING_DB_REMOTE)
98 // Field trial for Android Safe Browsing. This is checked separately in
99 // SafeBrowsingFieldTrial.java for controlling the UI.
100 bool IsAndroidFieldTrialEnabled() const {
101 return is_android_field_trial_enabled_;
104 // Should we check all types, or just the dangerous ones?
105 // We can flip this with a field trial if a non-dangerous type
106 // starts getting exploited.
107 ResourceTypesToCheck GetResourceTypesToCheck() const {
108 return resource_types_to_check_;
110 #endif // defined(SAFE_BROWSING_DB_REMOTE)
112 // Called on the UI thread to initialize the service.
113 void Initialize();
115 // Called on the main thread to let us know that the io_thread is going away.
116 void ShutDown();
118 // Called on UI thread to decide if the download file's sha256 hash
119 // should be calculated for safebrowsing.
120 bool DownloadBinHashNeeded() const;
122 // Create a protocol config struct.
123 virtual SafeBrowsingProtocolConfig GetProtocolConfig() const;
125 // Get current enabled status. Must be called on IO thread.
126 bool enabled() const {
127 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
128 return enabled_;
131 // Whether the service is enabled by the current set of profiles.
132 bool enabled_by_prefs() const {
133 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
134 return enabled_by_prefs_;
137 safe_browsing::ClientSideDetectionService*
138 safe_browsing_detection_service() const {
139 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
140 return csd_service_.get();
143 // The DownloadProtectionService is not valid after the SafeBrowsingService
144 // is destroyed.
145 safe_browsing::DownloadProtectionService*
146 download_protection_service() const {
147 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
148 return download_service_.get();
151 net::URLRequestContextGetter* url_request_context();
153 const scoped_refptr<SafeBrowsingUIManager>& ui_manager() const;
155 const scoped_refptr<SafeBrowsingDatabaseManager>& database_manager() const;
157 SafeBrowsingProtocolManager* protocol_manager() const;
159 SafeBrowsingPingManager* ping_manager() const;
161 // Returns a preference validation delegate that adds incidents to the
162 // incident reporting service for validation failures. Returns NULL if the
163 // service is not applicable for the given profile.
164 scoped_ptr<TrackedPreferenceValidationDelegate>
165 CreatePreferenceValidationDelegate(Profile* profile) const;
167 #if defined(FULL_SAFE_BROWSING)
168 // Registers |callback| to be run after some delay following process launch.
169 // |callback| will be dropped if the service is not applicable for the
170 // process.
171 void RegisterDelayedAnalysisCallback(
172 const safe_browsing::DelayedAnalysisCallback& callback);
173 #endif
175 // Adds |download_manager| to the set monitored by safe browsing.
176 void AddDownloadManager(content::DownloadManager* download_manager);
178 // Observes resource requests made by the renderer and reports suspicious
179 // activity.
180 void OnResourceRequest(const net::URLRequest* request);
182 // Type for subscriptions to SafeBrowsing service state.
183 typedef base::CallbackList<void(void)>::Subscription StateSubscription;
185 // Adds a listener for when SafeBrowsing preferences might have changed.
186 // To get the current state, the callback should call enabled_by_prefs().
187 // Should only be called on the UI thread.
188 scoped_ptr<StateSubscription> RegisterStateCallback(
189 const base::Callback<void(void)>& callback);
191 protected:
192 // Creates the safe browsing service. Need to initialize before using.
193 SafeBrowsingService();
195 ~SafeBrowsingService() override;
197 virtual SafeBrowsingDatabaseManager* CreateDatabaseManager();
199 virtual SafeBrowsingUIManager* CreateUIManager();
201 // Registers all the delayed analysis with the incident reporting service.
202 // This is where you register your process-wide, profile-independent analysis.
203 virtual void RegisterAllDelayedAnalysis();
205 // Return a ptr to DatabaseManager's delegate, or NULL if it doesn't have one.
206 virtual SafeBrowsingProtocolManagerDelegate* GetProtocolManagerDelegate();
208 private:
209 friend class SafeBrowsingServiceFactoryImpl;
210 friend struct content::BrowserThread::DeleteOnThread<
211 content::BrowserThread::UI>;
212 friend class base::DeleteHelper<SafeBrowsingService>;
213 friend class SafeBrowsingServerTest;
214 friend class SafeBrowsingServiceTest;
215 friend class SafeBrowsingURLRequestContextGetter;
217 void InitURLRequestContextOnIOThread(
218 net::URLRequestContextGetter* system_url_request_context_getter);
220 // Destroys the URLRequest and shuts down the provided getter on the
221 // IO thread.
222 void DestroyURLRequestContextOnIOThread(
223 scoped_refptr<SafeBrowsingURLRequestContextGetter> context_getter);
225 // Called to initialize objects that are used on the io_thread. This may be
226 // called multiple times during the life of the SafeBrowsingService.
227 void StartOnIOThread(
228 net::URLRequestContextGetter* url_request_context_getter);
230 // Called to stop or shutdown operations on the io_thread. This may be called
231 // multiple times to stop during the life of the SafeBrowsingService. If
232 // shutdown is true, then the operations on the io thread are shutdown
233 // permanently and cannot be restarted.
234 void StopOnIOThread(bool shutdown);
236 // Start up SafeBrowsing objects. This can be called at browser start, or when
237 // the user checks the "Enable SafeBrowsing" option in the Advanced options
238 // UI.
239 void Start();
241 // Stops the SafeBrowsingService. This can be called when the safe browsing
242 // preference is disabled. When shutdown is true, operation is permanently
243 // shutdown and cannot be restarted.
244 void Stop(bool shutdown);
246 // content::NotificationObserver override
247 void Observe(int type,
248 const content::NotificationSource& source,
249 const content::NotificationDetails& details) override;
251 // Starts following the safe browsing preference on |pref_service|.
252 void AddPrefService(PrefService* pref_service);
254 // Stop following the safe browsing preference on |pref_service|.
255 void RemovePrefService(PrefService* pref_service);
257 // Checks if any profile is currently using the safe browsing service, and
258 // starts or stops the service accordingly.
259 void RefreshState();
261 // The factory used to instanciate a SafeBrowsingService object.
262 // Useful for tests, so they can provide their own implementation of
263 // SafeBrowsingService.
264 static SafeBrowsingServiceFactory* factory_;
266 // The SafeBrowsingURLRequestContextGetter used to access
267 // |url_request_context_|. Accessed on UI thread.
268 scoped_refptr<SafeBrowsingURLRequestContextGetter>
269 url_request_context_getter_;
271 // The SafeBrowsingURLRequestContext. Accessed on IO thread.
272 scoped_ptr<net::URLRequestContext> url_request_context_;
274 // Handles interaction with SafeBrowsing servers. Accessed on IO thread.
275 SafeBrowsingProtocolManager* protocol_manager_;
277 // Provides phishing and malware statistics. Accessed on IO thread.
278 SafeBrowsingPingManager* ping_manager_;
280 // Whether the service is running. 'enabled_' is used by SafeBrowsingService
281 // on the IO thread during normal operations.
282 bool enabled_;
284 // Whether SafeBrowsing is enabled by the current set of profiles.
285 // Accessed on UI thread.
286 bool enabled_by_prefs_;
288 #if defined(SAFE_BROWSING_DB_REMOTE)
289 bool is_android_field_trial_enabled_;
290 ResourceTypesToCheck resource_types_to_check_;
291 #endif // defined(SAFE_BROWSING_DB_REMOTE)
293 // Tracks existing PrefServices, and the safe browsing preference on each.
294 // This is used to determine if any profile is currently using the safe
295 // browsing service, and to start it up or shut it down accordingly.
296 // Accessed on UI thread.
297 std::map<PrefService*, PrefChangeRegistrar*> prefs_map_;
299 // Used to track creation and destruction of profiles on the UI thread.
300 content::NotificationRegistrar prefs_registrar_;
302 // Callbacks when SafeBrowsing state might have changed.
303 // Should only be accessed on the UI thread.
304 base::CallbackList<void(void)> state_callback_list_;
306 // The ClientSideDetectionService is managed by the SafeBrowsingService,
307 // since its running state and lifecycle depends on SafeBrowsingService's.
308 // Accessed on UI thread.
309 scoped_ptr<safe_browsing::ClientSideDetectionService> csd_service_;
311 // The DownloadProtectionService is managed by the SafeBrowsingService,
312 // since its running state and lifecycle depends on SafeBrowsingService's.
313 // Accessed on UI thread.
314 scoped_ptr<safe_browsing::DownloadProtectionService> download_service_;
316 #if defined(FULL_SAFE_BROWSING)
317 scoped_ptr<safe_browsing::IncidentReportingService> incident_service_;
318 #endif
320 // The UI manager handles showing interstitials. Accessed on both UI and IO
321 // thread.
322 scoped_refptr<SafeBrowsingUIManager> ui_manager_;
324 // The database manager handles the database and download logic. Accessed on
325 // both UI and IO thread.
326 scoped_refptr<SafeBrowsingDatabaseManager> database_manager_;
328 #if defined(FULL_SAFE_BROWSING)
329 scoped_ptr<safe_browsing::OffDomainInclusionDetector>
330 off_domain_inclusion_detector_;
332 scoped_ptr<safe_browsing::ResourceRequestDetector> resource_request_detector_;
333 #endif
335 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingService);
338 // Factory for creating SafeBrowsingService. Useful for tests.
339 class SafeBrowsingServiceFactory {
340 public:
341 SafeBrowsingServiceFactory() { }
342 virtual ~SafeBrowsingServiceFactory() { }
343 virtual SafeBrowsingService* CreateSafeBrowsingService() = 0;
344 private:
345 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingServiceFactory);
348 #endif // CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_SERVICE_H_