Roll src/third_party/WebKit 8b42d1d:744641d (svn 186770:186771)
[chromium-blink-merge.git] / chrome / browser / safe_browsing / safe_browsing_service.h
blob6230e7ac60746245ecf5dc1fc8cda5456c85d7f3
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/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 #if defined(FULL_SAFE_BROWSING)
26 #include "chrome/browser/safe_browsing/incident_reporting/delayed_analysis_callback.h"
27 #endif
29 class PrefChangeRegistrar;
30 class PrefService;
31 class Profile;
32 struct SafeBrowsingProtocolConfig;
33 class SafeBrowsingDatabaseManager;
34 class SafeBrowsingPingManager;
35 class SafeBrowsingProtocolManager;
36 class SafeBrowsingServiceFactory;
37 class SafeBrowsingUIManager;
38 class SafeBrowsingURLRequestContextGetter;
39 class TrackedPreferenceValidationDelegate;
41 namespace base {
42 class Thread;
45 namespace content {
46 class DownloadManager;
49 namespace net {
50 class URLRequest;
51 class URLRequestContext;
52 class URLRequestContextGetter;
55 namespace safe_browsing {
56 class ClientSideDetectionService;
57 class DownloadProtectionService;
59 #if defined(FULL_SAFE_BROWSING)
60 class IncidentReportingService;
61 class OffDomainInclusionDetector;
62 #endif
65 // Construction needs to happen on the main thread.
66 // The SafeBrowsingService owns both the UI and Database managers which do
67 // the heavylifting of safebrowsing service. Both of these managers stay
68 // alive until SafeBrowsingService is destroyed, however, they are disabled
69 // permanently when Shutdown method is called.
70 class SafeBrowsingService
71 : public base::RefCountedThreadSafe<
72 SafeBrowsingService,
73 content::BrowserThread::DeleteOnUIThread>,
74 public content::NotificationObserver {
75 public:
76 // Makes the passed |factory| the factory used to instanciate
77 // a SafeBrowsingService. Useful for tests.
78 static void RegisterFactory(SafeBrowsingServiceFactory* factory) {
79 factory_ = factory;
82 static base::FilePath GetCookieFilePathForTesting();
84 static base::FilePath GetBaseFilename();
86 // Create an instance of the safe browsing service.
87 static SafeBrowsingService* CreateSafeBrowsingService();
89 // Called on the UI thread to initialize the service.
90 void Initialize();
92 // Called on the main thread to let us know that the io_thread is going away.
93 void ShutDown();
95 // Called on UI thread to decide if the download file's sha256 hash
96 // should be calculated for safebrowsing.
97 bool DownloadBinHashNeeded() const;
99 // Create a protocol config struct.
100 virtual SafeBrowsingProtocolConfig GetProtocolConfig() const;
102 bool enabled() const { return enabled_; }
104 safe_browsing::ClientSideDetectionService*
105 safe_browsing_detection_service() const {
106 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
107 return csd_service_.get();
110 // The DownloadProtectionService is not valid after the SafeBrowsingService
111 // is destroyed.
112 safe_browsing::DownloadProtectionService*
113 download_protection_service() const {
114 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
115 return download_service_.get();
118 net::URLRequestContextGetter* url_request_context();
120 const scoped_refptr<SafeBrowsingUIManager>& ui_manager() const;
122 const scoped_refptr<SafeBrowsingDatabaseManager>& database_manager() const;
124 SafeBrowsingProtocolManager* protocol_manager() const;
126 SafeBrowsingPingManager* ping_manager() const;
128 // Returns a preference validation delegate that adds incidents to the
129 // incident reporting service for validation failures. Returns NULL if the
130 // service is not applicable for the given profile.
131 scoped_ptr<TrackedPreferenceValidationDelegate>
132 CreatePreferenceValidationDelegate(Profile* profile) const;
134 #if defined(FULL_SAFE_BROWSING)
135 // Registers |callback| to be run after some delay following process launch.
136 // |callback| will be dropped if the service is not applicable for the
137 // process.
138 void RegisterDelayedAnalysisCallback(
139 const safe_browsing::DelayedAnalysisCallback& callback);
140 #endif
142 // Adds |download_manager| to the set monitored by safe browsing.
143 void AddDownloadManager(content::DownloadManager* download_manager);
145 // Observes resource requests made by the renderer and reports suspicious
146 // activity.
147 void OnResourceRequest(const net::URLRequest* request);
149 protected:
150 // Creates the safe browsing service. Need to initialize before using.
151 SafeBrowsingService();
153 ~SafeBrowsingService() override;
155 virtual SafeBrowsingDatabaseManager* CreateDatabaseManager();
157 virtual SafeBrowsingUIManager* CreateUIManager();
159 // Registers all the delayed analysis with the incident reporting service.
160 // This is where you register your process-wide, profile-independent analysis.
161 virtual void RegisterAllDelayedAnalysis();
163 private:
164 friend class SafeBrowsingServiceFactoryImpl;
165 friend struct content::BrowserThread::DeleteOnThread<
166 content::BrowserThread::UI>;
167 friend class base::DeleteHelper<SafeBrowsingService>;
168 friend class SafeBrowsingServerTest;
169 friend class SafeBrowsingServiceTest;
170 friend class SafeBrowsingURLRequestContextGetter;
172 void InitURLRequestContextOnIOThread(
173 net::URLRequestContextGetter* system_url_request_context_getter);
175 void DestroyURLRequestContextOnIOThread();
177 // Called to initialize objects that are used on the io_thread. This may be
178 // called multiple times during the life of the SafeBrowsingService.
179 void StartOnIOThread(
180 net::URLRequestContextGetter* url_request_context_getter);
182 // Called to stop or shutdown operations on the io_thread. This may be called
183 // multiple times to stop during the life of the SafeBrowsingService. If
184 // shutdown is true, then the operations on the io thread are shutdown
185 // permanently and cannot be restarted.
186 void StopOnIOThread(bool shutdown);
188 // Start up SafeBrowsing objects. This can be called at browser start, or when
189 // the user checks the "Enable SafeBrowsing" option in the Advanced options
190 // UI.
191 void Start();
193 // Stops the SafeBrowsingService. This can be called when the safe browsing
194 // preference is disabled. When shutdown is true, operation is permanently
195 // shutdown and cannot be restarted.
196 void Stop(bool shutdown);
198 // content::NotificationObserver override
199 void Observe(int type,
200 const content::NotificationSource& source,
201 const content::NotificationDetails& details) override;
203 // Starts following the safe browsing preference on |pref_service|.
204 void AddPrefService(PrefService* pref_service);
206 // Stop following the safe browsing preference on |pref_service|.
207 void RemovePrefService(PrefService* pref_service);
209 // Checks if any profile is currently using the safe browsing service, and
210 // starts or stops the service accordingly.
211 void RefreshState();
213 // The factory used to instanciate a SafeBrowsingService object.
214 // Useful for tests, so they can provide their own implementation of
215 // SafeBrowsingService.
216 static SafeBrowsingServiceFactory* factory_;
218 // The SafeBrowsingURLRequestContextGetter used to access
219 // |url_request_context_|. Accessed on UI thread.
220 scoped_refptr<net::URLRequestContextGetter>
221 url_request_context_getter_;
223 // The SafeBrowsingURLRequestContext. Accessed on IO thread.
224 scoped_ptr<net::URLRequestContext> url_request_context_;
226 // Handles interaction with SafeBrowsing servers. Accessed on IO thread.
227 SafeBrowsingProtocolManager* protocol_manager_;
229 // Provides phishing and malware statistics. Accessed on IO thread.
230 SafeBrowsingPingManager* ping_manager_;
232 // Whether the service is running. 'enabled_' is used by SafeBrowsingService
233 // on the IO thread during normal operations.
234 bool enabled_;
236 // Tracks existing PrefServices, and the safe browsing preference on each.
237 // This is used to determine if any profile is currently using the safe
238 // browsing service, and to start it up or shut it down accordingly.
239 // Accessed on UI thread.
240 std::map<PrefService*, PrefChangeRegistrar*> prefs_map_;
242 // Used to track creation and destruction of profiles on the UI thread.
243 content::NotificationRegistrar prefs_registrar_;
245 // The ClientSideDetectionService is managed by the SafeBrowsingService,
246 // since its running state and lifecycle depends on SafeBrowsingService's.
247 // Accessed on UI thread.
248 scoped_ptr<safe_browsing::ClientSideDetectionService> csd_service_;
250 // The DownloadProtectionService is managed by the SafeBrowsingService,
251 // since its running state and lifecycle depends on SafeBrowsingService's.
252 // Accessed on UI thread.
253 scoped_ptr<safe_browsing::DownloadProtectionService> download_service_;
255 #if defined(FULL_SAFE_BROWSING)
256 scoped_ptr<safe_browsing::IncidentReportingService> incident_service_;
257 #endif
259 // The UI manager handles showing interstitials. Accessed on both UI and IO
260 // thread.
261 scoped_refptr<SafeBrowsingUIManager> ui_manager_;
263 // The database manager handles the database and download logic. Accessed on
264 // both UI and IO thread.
265 scoped_refptr<SafeBrowsingDatabaseManager> database_manager_;
267 #if defined(FULL_SAFE_BROWSING)
268 scoped_ptr<safe_browsing::OffDomainInclusionDetector>
269 off_domain_inclusion_detector_;
270 #endif
272 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingService);
275 // Factory for creating SafeBrowsingService. Useful for tests.
276 class SafeBrowsingServiceFactory {
277 public:
278 SafeBrowsingServiceFactory() { }
279 virtual ~SafeBrowsingServiceFactory() { }
280 virtual SafeBrowsingService* CreateSafeBrowsingService() = 0;
281 private:
282 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingServiceFactory);
285 #endif // CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_SERVICE_H_