Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / safe_browsing / protocol_manager.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 #ifndef CHROME_BROWSER_SAFE_BROWSING_PROTOCOL_MANAGER_H_
6 #define CHROME_BROWSER_SAFE_BROWSING_PROTOCOL_MANAGER_H_
7
8 // A class that implements Chrome's interface with the SafeBrowsing protocol.
9 // See https://developers.google.com/safe-browsing/developers_guide_v2 for
10 // protocol details.
11 //
12 // The SafeBrowsingProtocolManager handles formatting and making requests of,
13 // and handling responses from, Google's SafeBrowsing servers. This class uses
14 // The SafeBrowsingProtocolParser class to do the actual parsing.
15
16 #include <deque>
17 #include <set>
18 #include <string>
19 #include <vector>
20
21 #include "base/containers/hash_tables.h"
22 #include "base/gtest_prod_util.h"
23 #include "base/memory/scoped_ptr.h"
24 #include "base/threading/non_thread_safe.h"
25 #include "base/time/time.h"
26 #include "base/timer/timer.h"
27 #include "chrome/browser/safe_browsing/chunk_range.h"
28 #include "chrome/browser/safe_browsing/protocol_manager_helper.h"
29 #include "chrome/browser/safe_browsing/protocol_parser.h"
30 #include "chrome/browser/safe_browsing/safe_browsing_util.h"
31 #include "net/url_request/url_fetcher_delegate.h"
32 #include "url/gurl.h"
33
34 namespace net {
35 class URLFetcher;
36 class URLRequestContextGetter;
37 }  // namespace net
38
39 class SBProtocolManagerFactory;
40 class SafeBrowsingProtocolManagerDelegate;
41
42 class SafeBrowsingProtocolManager : public net::URLFetcherDelegate,
43                                     public base::NonThreadSafe {
44  public:
45   // FullHashCallback is invoked when GetFullHash completes.
46   // Parameters:
47   //   - The vector of full hash results. If empty, indicates that there
48   //     were no matches, and that the resource is safe.
49   //   - The cache lifetime of the result. A lifetime of 0 indicates the results
50   //     should not be cached.
51   typedef base::Callback<void(const std::vector<SBFullHashResult>&,
52                               const base::TimeDelta&)> FullHashCallback;
53
54   ~SafeBrowsingProtocolManager() override;
55
56   // Makes the passed |factory| the factory used to instantiate
57   // a SafeBrowsingService. Useful for tests.
58   static void RegisterFactory(SBProtocolManagerFactory* factory) {
59     factory_ = factory;
60   }
61
62   // Create an instance of the safe browsing protocol manager.
63   static SafeBrowsingProtocolManager* Create(
64       SafeBrowsingProtocolManagerDelegate* delegate,
65       net::URLRequestContextGetter* request_context_getter,
66       const SafeBrowsingProtocolConfig& config);
67
68   // Sets up the update schedule and internal state for making periodic requests
69   // of the Safebrowsing servers.
70   virtual void Initialize();
71
72   // net::URLFetcherDelegate interface.
73   void OnURLFetchComplete(const net::URLFetcher* source) override;
74
75   // Retrieve the full hash for a set of prefixes, and invoke the callback
76   // argument when the results are retrieved. The callback may be invoked
77   // synchronously.
78   virtual void GetFullHash(const std::vector<SBPrefix>& prefixes,
79                            FullHashCallback callback,
80                            bool is_download);
81
82   // Forces the start of next update after |interval| time.
83   void ForceScheduleNextUpdate(base::TimeDelta interval);
84
85   // Scheduled update callback.
86   void GetNextUpdate();
87
88   // Called by the SafeBrowsingService when our request for a list of all chunks
89   // for each list is done.  If database_error is true, that means the protocol
90   // manager shouldn't fetch updates since they can't be written to disk.  It
91   // should try again later to open the database.
92   void OnGetChunksComplete(const std::vector<SBListChunkRanges>& list,
93                            bool database_error);
94
95   // The last time we received an update.
96   base::Time last_update() const { return last_update_; }
97
98   // Setter for additional_query_. To make sure the additional_query_ won't
99   // be changed in the middle of an update, caller (e.g.: SafeBrowsingService)
100   // should call this after callbacks triggered in UpdateFinished() or before
101   // IssueUpdateRequest().
102   void set_additional_query(const std::string& query) {
103     additional_query_ = query;
104   }
105   const std::string& additional_query() const {
106     return additional_query_;
107   }
108
109   // Enumerate failures for histogramming purposes.  DO NOT CHANGE THE
110   // ORDERING OF THESE VALUES.
111   enum ResultType {
112     // 200 response code means that the server recognized the hash
113     // prefix, while 204 is an empty response indicating that the
114     // server did not recognize it.
115     GET_HASH_STATUS_200,
116     GET_HASH_STATUS_204,
117
118     // Subset of successful responses which returned no full hashes.
119     // This includes the STATUS_204 case, and the *_ERROR cases.
120     GET_HASH_FULL_HASH_EMPTY,
121
122     // Subset of successful responses for which one or more of the
123     // full hashes matched (should lead to an interstitial).
124     GET_HASH_FULL_HASH_HIT,
125
126     // Subset of successful responses which weren't empty and have no
127     // matches.  It means that there was a prefix collision which was
128     // cleared up by the full hashes.
129     GET_HASH_FULL_HASH_MISS,
130
131     // Subset of successful responses where the response body wasn't parsable.
132     GET_HASH_PARSE_ERROR,
133
134     // Gethash request failed (network error).
135     GET_HASH_NETWORK_ERROR,
136
137     // Gethash request returned HTTP result code other than 200 or 204.
138     GET_HASH_HTTP_ERROR,
139
140     // Gethash attempted during error backoff, no request sent.
141     GET_HASH_BACKOFF_ERROR,
142
143     // Memory space for histograms is determined by the max.  ALWAYS
144     // ADD NEW VALUES BEFORE THIS ONE.
145     GET_HASH_RESULT_MAX
146   };
147
148   // Record a GetHash result. |is_download| indicates if the get
149   // hash is triggered by download related lookup.
150   static void RecordGetHashResult(bool is_download,
151                                   ResultType result_type);
152
153   // Returns whether another update is currently scheduled.
154   bool IsUpdateScheduled() const;
155
156   // Called when app changes status of foreground or background.
157   void SetAppInForeground(bool foreground) {
158     app_in_foreground_ = foreground;
159   }
160
161  protected:
162   // Constructs a SafeBrowsingProtocolManager for |delegate| that issues
163   // network requests using |request_context_getter|.
164   SafeBrowsingProtocolManager(
165       SafeBrowsingProtocolManagerDelegate* delegate,
166       net::URLRequestContextGetter* request_context_getter,
167       const SafeBrowsingProtocolConfig& config);
168
169  private:
170   FRIEND_TEST_ALL_PREFIXES(SafeBrowsingProtocolManagerTest, TestBackOffTimes);
171   FRIEND_TEST_ALL_PREFIXES(SafeBrowsingProtocolManagerTest, TestChunkStrings);
172   FRIEND_TEST_ALL_PREFIXES(SafeBrowsingProtocolManagerTest, TestGetHashUrl);
173   FRIEND_TEST_ALL_PREFIXES(SafeBrowsingProtocolManagerTest,
174                            TestGetHashBackOffTimes);
175   FRIEND_TEST_ALL_PREFIXES(SafeBrowsingProtocolManagerTest, TestNextChunkUrl);
176   FRIEND_TEST_ALL_PREFIXES(SafeBrowsingProtocolManagerTest, TestUpdateUrl);
177   friend class SafeBrowsingServerTest;
178   friend class SBProtocolManagerFactoryImpl;
179
180   // Internal API for fetching information from the SafeBrowsing servers. The
181   // GetHash requests are higher priority since they can block user requests
182   // so are handled separately.
183   enum SafeBrowsingRequestType {
184     NO_REQUEST = 0,     // No requests in progress
185     UPDATE_REQUEST,     // Request for redirect URLs
186     BACKUP_UPDATE_REQUEST, // Request for redirect URLs to a backup URL.
187     CHUNK_REQUEST,      // Request for a specific chunk
188   };
189
190   // Which type of backup update request is being used.
191   enum BackupUpdateReason {
192     BACKUP_UPDATE_REASON_CONNECT,
193     BACKUP_UPDATE_REASON_HTTP,
194     BACKUP_UPDATE_REASON_NETWORK,
195     BACKUP_UPDATE_REASON_MAX,
196   };
197
198   // Generates Update URL for querying about the latest set of chunk updates.
199   GURL UpdateUrl() const;
200
201   // Generates backup Update URL for querying about the latest set of chunk
202   // updates. |url_prefix| is the base prefix to use.
203   GURL BackupUpdateUrl(BackupUpdateReason reason) const;
204
205   // Generates GetHash request URL for retrieving full hashes.
206   GURL GetHashUrl() const;
207   // Generates URL for reporting safe browsing hits for UMA users.
208
209   // Composes a ChunkUrl based on input string.
210   GURL NextChunkUrl(const std::string& input) const;
211
212   // Returns the time for the next update request. If |back_off| is true,
213   // the time returned will increment an error count and return the appriate
214   // next time (see ScheduleNextUpdate below).
215   base::TimeDelta GetNextUpdateInterval(bool back_off);
216
217   // Worker function for calculating GetHash and Update backoff times (in
218   // seconds). |multiplier| is doubled for each consecutive error between the
219   // 2nd and 5th, and |error_count| is incremented with each call.
220   base::TimeDelta GetNextBackOffInterval(size_t* error_count,
221                                          size_t* multiplier) const;
222
223   // Manages our update with the next allowable update time. If 'back_off_' is
224   // true, we must decrease the frequency of requests of the SafeBrowsing
225   // service according to section 5 of the protocol specification.
226   // When disable_auto_update_ is set, ScheduleNextUpdate will do nothing.
227   // ForceScheduleNextUpdate has to be called to trigger the update.
228   void ScheduleNextUpdate(bool back_off);
229
230   // Sends a request for a list of chunks we should download to the SafeBrowsing
231   // servers. In order to format this request, we need to send all the chunk
232   // numbers for each list that we have to the server. Getting the chunk numbers
233   // requires a database query (run on the database thread), and the request
234   // is sent upon completion of that query in OnGetChunksComplete.
235   void IssueUpdateRequest();
236
237   // Sends a backup request for a list of chunks to download, when the primary
238   // update request failed. |reason| specifies why the backup is needed. Unlike
239   // the primary IssueUpdateRequest, this does not need to hit the local
240   // SafeBrowsing database since the existing chunk numbers are remembered from
241   // the primary update request. Returns whether the backup request was issued -
242   // this may be false in cases where there is not a prefix specified.
243   bool IssueBackupUpdateRequest(BackupUpdateReason reason);
244
245   // Sends a request for a chunk to the SafeBrowsing servers.
246   void IssueChunkRequest();
247
248   // Runs the protocol parser on received data and update the
249   // SafeBrowsingService with the new content. Returns 'true' on successful
250   // parse, 'false' on error.
251   bool HandleServiceResponse(const GURL& url, const char* data, size_t length);
252
253   // Updates internal state for each GetHash response error, assuming that the
254   // current time is |now|.
255   void HandleGetHashError(const base::Time& now);
256
257   // Helper function for update completion.
258   void UpdateFinished(bool success);
259   void UpdateFinished(bool success, bool back_off);
260
261   // A callback that runs if we timeout waiting for a response to an update
262   // request. We use this to properly set our update state.
263   void UpdateResponseTimeout();
264
265   // Called after the chunks are added to the database.
266   void OnAddChunksComplete();
267
268  private:
269   // Map of GetHash requests to parameters which created it.
270   struct FullHashDetails {
271     FullHashDetails();
272     FullHashDetails(FullHashCallback callback, bool is_download);
273     ~FullHashDetails();
274
275     FullHashCallback callback;
276     bool is_download;
277   };
278   typedef base::hash_map<const net::URLFetcher*, FullHashDetails> HashRequests;
279
280   // The factory that controls the creation of SafeBrowsingProtocolManager.
281   // This is used by tests.
282   static SBProtocolManagerFactory* factory_;
283
284   // Our delegate.
285   SafeBrowsingProtocolManagerDelegate* delegate_;
286
287   // Current active request (in case we need to cancel) for updates or chunks
288   // from the SafeBrowsing service. We can only have one of these outstanding
289   // at any given time unlike GetHash requests, which are tracked separately.
290   scoped_ptr<net::URLFetcher> request_;
291
292   // The kind of request that is currently in progress.
293   SafeBrowsingRequestType request_type_;
294
295   // The number of HTTP response errors, used for request backoff timing.
296   size_t update_error_count_;
297   size_t gethash_error_count_;
298
299   // Multipliers which double (max == 8) for each error after the second.
300   size_t update_back_off_mult_;
301   size_t gethash_back_off_mult_;
302
303   // Multiplier between 0 and 1 to spread clients over an interval.
304   float back_off_fuzz_;
305
306   // The list for which we are make a request.
307   std::string list_name_;
308
309   // For managing the next earliest time to query the SafeBrowsing servers for
310   // updates.
311   base::TimeDelta next_update_interval_;
312   base::OneShotTimer<SafeBrowsingProtocolManager> update_timer_;
313
314   // timeout_timer_ is used to interrupt update requests which are taking
315   // too long.
316   base::OneShotTimer<SafeBrowsingProtocolManager> timeout_timer_;
317
318   // All chunk requests that need to be made.
319   std::deque<ChunkUrl> chunk_request_urls_;
320
321   HashRequests hash_requests_;
322
323   // The next scheduled update has special behavior for the first 2 requests.
324   enum UpdateRequestState {
325     FIRST_REQUEST = 0,
326     SECOND_REQUEST,
327     NORMAL_REQUEST
328   };
329   UpdateRequestState update_state_;
330
331   // True if the service has been given an add/sub chunk but it hasn't been
332   // added to the database yet.
333   bool chunk_pending_to_write_;
334
335   // The last time we successfully received an update.
336   base::Time last_update_;
337
338   // While in GetHash backoff, we can't make another GetHash until this time.
339   base::Time next_gethash_time_;
340
341   // Current product version sent in each request.
342   std::string version_;
343
344   // Used for measuring chunk request latency.
345   base::Time chunk_request_start_;
346
347   // Tracks the size of each update (in bytes).
348   size_t update_size_;
349
350   // The safe browsing client name sent in each request.
351   std::string client_name_;
352
353   // A string that is appended to the end of URLs for download, gethash,
354   // safebrowsing hits and chunk update requests.
355   std::string additional_query_;
356
357   // The context we use to issue network requests.
358   scoped_refptr<net::URLRequestContextGetter> request_context_getter_;
359
360   // URL prefix where browser fetches safebrowsing chunk updates, and hashes.
361   std::string url_prefix_;
362
363   // Backup URL prefixes for updates.
364   std::string backup_url_prefixes_[BACKUP_UPDATE_REASON_MAX];
365
366   // The current reason why the backup update request is happening.
367   BackupUpdateReason backup_update_reason_;
368
369   // Data to POST when doing an update.
370   std::string update_list_data_;
371
372   // When true, protocol manager will not start an update unless
373   // ForceScheduleNextUpdate() is called. This is set for testing purpose.
374   bool disable_auto_update_;
375
376 #if defined(OS_ANDROID)
377   // When true, protocol_manager will not check network connection
378   // type when scheduling next update. This is set for testing purpose.
379   bool disable_connection_check_;
380 #endif
381
382   // ID for URLFetchers for testing.
383   int url_fetcher_id_;
384
385   // Whether the app is in foreground or background.
386   bool app_in_foreground_;
387
388   DISALLOW_COPY_AND_ASSIGN(SafeBrowsingProtocolManager);
389 };
390
391 // Interface of a factory to create ProtocolManager.  Useful for tests.
392 class SBProtocolManagerFactory {
393  public:
394   SBProtocolManagerFactory() {}
395   virtual ~SBProtocolManagerFactory() {}
396   virtual SafeBrowsingProtocolManager* CreateProtocolManager(
397       SafeBrowsingProtocolManagerDelegate* delegate,
398       net::URLRequestContextGetter* request_context_getter,
399       const SafeBrowsingProtocolConfig& config) = 0;
400  private:
401   DISALLOW_COPY_AND_ASSIGN(SBProtocolManagerFactory);
402 };
403
404 // Delegate interface for the SafeBrowsingProtocolManager.
405 class SafeBrowsingProtocolManagerDelegate {
406  public:
407   typedef base::Callback<void(const std::vector<SBListChunkRanges>&, bool)>
408       GetChunksCallback;
409   typedef base::Callback<void(void)> AddChunksCallback;
410
411   virtual ~SafeBrowsingProtocolManagerDelegate();
412
413   // |UpdateStarted()| is called just before the SafeBrowsing update protocol
414   // has begun.
415   virtual void UpdateStarted() = 0;
416
417   // |UpdateFinished()| is called just after the SafeBrowsing update protocol
418   // has completed.
419   virtual void UpdateFinished(bool success) = 0;
420
421   // Wipe out the local database. The SafeBrowsing server can request this.
422   virtual void ResetDatabase() = 0;
423
424   // Retrieve all the local database chunks, and invoke |callback| with the
425   // results. The SafeBrowsingProtocolManagerDelegate must only invoke the
426   // callback if the SafeBrowsingProtocolManager is still alive. Only one call
427   // may be made to GetChunks at a time.
428   virtual void GetChunks(GetChunksCallback callback) = 0;
429
430   // Add new chunks to the database. Invokes |callback| when complete, but must
431   // call at a later time.
432   virtual void AddChunks(const std::string& list,
433                          scoped_ptr<ScopedVector<SBChunkData> > chunks,
434                          AddChunksCallback callback) = 0;
435
436   // Delete chunks from the database.
437   virtual void DeleteChunks(
438       scoped_ptr<std::vector<SBChunkDelete> > chunk_deletes) = 0;
439 };
440
441 #endif  // CHROME_BROWSER_SAFE_BROWSING_PROTOCOL_MANAGER_H_