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