Upstream version 11.40.277.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / safe_browsing / safe_browsing_service.h
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.
7
8 #ifndef CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_SERVICE_H_
9 #define CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_SERVICE_H_
10
11 #include <map>
12 #include <string>
13
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"
25
26 class PrefChangeRegistrar;
27 class PrefService;
28 class Profile;
29 struct SafeBrowsingProtocolConfig;
30 class SafeBrowsingDatabaseManager;
31 class SafeBrowsingPingManager;
32 class SafeBrowsingProtocolManager;
33 class SafeBrowsingServiceFactory;
34 class SafeBrowsingUIManager;
35 class SafeBrowsingURLRequestContextGetter;
36 class TrackedPreferenceValidationDelegate;
37
38 namespace base {
39 class Thread;
40 }
41
42 namespace content {
43 class DownloadManager;
44 }
45
46 namespace net {
47 class URLRequestContext;
48 class URLRequestContextGetter;
49 }
50
51 namespace safe_browsing {
52 class ClientSideDetectionService;
53 class DownloadProtectionService;
54 class IncidentReportingService;
55 }
56
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<
64           SafeBrowsingService,
65           content::BrowserThread::DeleteOnUIThread>,
66       public content::NotificationObserver {
67  public:
68   // Makes the passed |factory| the factory used to instanciate
69   // a SafeBrowsingService. Useful for tests.
70   static void RegisterFactory(SafeBrowsingServiceFactory* factory) {
71     factory_ = factory;
72   }
73
74   static base::FilePath GetCookieFilePathForTesting();
75
76   static base::FilePath GetBaseFilename();
77
78   // Create an instance of the safe browsing service.
79   static SafeBrowsingService* CreateSafeBrowsingService();
80
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();
85 #endif
86
87   // Called on the UI thread to initialize the service.
88   void Initialize();
89
90   // Called on the main thread to let us know that the io_thread is going away.
91   void ShutDown();
92
93   // Called on UI thread to decide if the download file's sha256 hash
94   // should be calculated for safebrowsing.
95   bool DownloadBinHashNeeded() const;
96
97   // Create a protocol config struct.
98   virtual SafeBrowsingProtocolConfig GetProtocolConfig() const;
99
100   bool enabled() const { return enabled_; }
101
102   safe_browsing::ClientSideDetectionService*
103       safe_browsing_detection_service() const {
104     DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
105     return csd_service_.get();
106   }
107
108   // The DownloadProtectionService is not valid after the SafeBrowsingService
109   // is destroyed.
110   safe_browsing::DownloadProtectionService*
111       download_protection_service() const {
112     DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
113     return download_service_.get();
114   }
115
116   net::URLRequestContextGetter* url_request_context();
117
118   const scoped_refptr<SafeBrowsingUIManager>& ui_manager() const;
119
120   const scoped_refptr<SafeBrowsingDatabaseManager>& database_manager() const;
121
122   SafeBrowsingProtocolManager* protocol_manager() const;
123
124   SafeBrowsingPingManager* ping_manager() const;
125
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;
131
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
134   // process.
135   void RegisterDelayedAnalysisCallback(
136       const safe_browsing::DelayedAnalysisCallback& callback);
137
138   // Adds |download_manager| to the set monitored by safe browsing.
139   void AddDownloadManager(content::DownloadManager* download_manager);
140
141  protected:
142   // Creates the safe browsing service.  Need to initialize before using.
143   SafeBrowsingService();
144
145   ~SafeBrowsingService() override;
146
147   virtual SafeBrowsingDatabaseManager* CreateDatabaseManager();
148
149   virtual SafeBrowsingUIManager* CreateUIManager();
150
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();
154
155  private:
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;
163
164   void InitURLRequestContextOnIOThread(
165       net::URLRequestContextGetter* system_url_request_context_getter);
166
167   void DestroyURLRequestContextOnIOThread();
168
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);
173
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);
179
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
182   // UI.
183   void Start();
184
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);
189
190   // content::NotificationObserver override
191   void Observe(int type,
192                const content::NotificationSource& source,
193                const content::NotificationDetails& details) override;
194
195   // Starts following the safe browsing preference on |pref_service|.
196   void AddPrefService(PrefService* pref_service);
197
198   // Stop following the safe browsing preference on |pref_service|.
199   void RemovePrefService(PrefService* pref_service);
200
201   // Checks if any profile is currently using the safe browsing service, and
202   // starts or stops the service accordingly.
203   void RefreshState();
204
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_;
209
210   // The SafeBrowsingURLRequestContextGetter used to access
211   // |url_request_context_|. Accessed on UI thread.
212   scoped_refptr<net::URLRequestContextGetter>
213       url_request_context_getter_;
214
215   // The SafeBrowsingURLRequestContext. Accessed on IO thread.
216   scoped_ptr<net::URLRequestContext> url_request_context_;
217
218   // Handles interaction with SafeBrowsing servers. Accessed on IO thread.
219   SafeBrowsingProtocolManager* protocol_manager_;
220
221   // Provides phishing and malware statistics. Accessed on IO thread.
222   SafeBrowsingPingManager* ping_manager_;
223
224   // Whether the service is running. 'enabled_' is used by SafeBrowsingService
225   // on the IO thread during normal operations.
226   bool enabled_;
227
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_;
233
234   // Used to track creation and destruction of profiles on the UI thread.
235   content::NotificationRegistrar prefs_registrar_;
236
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_;
241
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_;
246
247   scoped_ptr<safe_browsing::IncidentReportingService> incident_service_;
248
249   // The UI manager handles showing interstitials.  Accessed on both UI and IO
250   // thread.
251   scoped_refptr<SafeBrowsingUIManager> ui_manager_;
252
253   // The database manager handles the database and download logic.  Accessed on
254   // both UI and IO thread.
255   scoped_refptr<SafeBrowsingDatabaseManager> database_manager_;
256
257   DISALLOW_COPY_AND_ASSIGN(SafeBrowsingService);
258 };
259
260 // Factory for creating SafeBrowsingService.  Useful for tests.
261 class SafeBrowsingServiceFactory {
262  public:
263   SafeBrowsingServiceFactory() { }
264   virtual ~SafeBrowsingServiceFactory() { }
265   virtual SafeBrowsingService* CreateSafeBrowsingService() = 0;
266  private:
267   DISALLOW_COPY_AND_ASSIGN(SafeBrowsingServiceFactory);
268 };
269
270 #endif  // CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_SERVICE_H_