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.
5 #ifndef CHROME_BROWSER_SAFE_BROWSING_PROTOCOL_MANAGER_H_
6 #define CHROME_BROWSER_SAFE_BROWSING_PROTOCOL_MANAGER_H_
8 // A class that implements Chrome's interface with the SafeBrowsing protocol.
9 // See https://developers.google.com/safe-browsing/developers_guide_v2 for
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.
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"
36 class URLRequestContextGetter
;
39 class SBProtocolManagerFactory
;
40 class SafeBrowsingProtocolManagerDelegate
;
42 class SafeBrowsingProtocolManager
: public net::URLFetcherDelegate
,
43 public base::NonThreadSafe
{
45 // FullHashCallback is invoked when GetFullHash completes.
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
;
54 ~SafeBrowsingProtocolManager() override
;
56 // Makes the passed |factory| the factory used to instantiate
57 // a SafeBrowsingService. Useful for tests.
58 static void RegisterFactory(SBProtocolManagerFactory
* factory
) {
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
);
68 // Sets up the update schedule and internal state for making periodic requests
69 // of the Safebrowsing servers.
70 virtual void Initialize();
72 // net::URLFetcherDelegate interface.
73 void OnURLFetchComplete(const net::URLFetcher
* source
) override
;
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
78 virtual void GetFullHash(const std::vector
<SBPrefix
>& prefixes
,
79 FullHashCallback callback
,
81 bool is_extended_reporting
);
83 // Forces the start of next update after |interval| time.
84 void ForceScheduleNextUpdate(base::TimeDelta interval
);
86 // Scheduled update callback.
89 // Called by the SafeBrowsingService when our request for a list of all chunks
90 // for each list is done. If database_error is true, that means the protocol
91 // manager shouldn't fetch updates since they can't be written to disk. It
92 // should try again later to open the database.
93 void OnGetChunksComplete(const std::vector
<SBListChunkRanges
>& list
,
95 bool is_extended_reporting
);
97 // The last time we received an update.
98 base::Time
last_update() const { return last_update_
; }
100 // Setter for additional_query_. To make sure the additional_query_ won't
101 // be changed in the middle of an update, caller (e.g.: SafeBrowsingService)
102 // should call this after callbacks triggered in UpdateFinished() or before
103 // IssueUpdateRequest().
104 void set_additional_query(const std::string
& query
) {
105 additional_query_
= query
;
107 const std::string
& additional_query() const {
108 return additional_query_
;
111 // Enumerate failures for histogramming purposes. DO NOT CHANGE THE
112 // ORDERING OF THESE VALUES.
114 // 200 response code means that the server recognized the hash
115 // prefix, while 204 is an empty response indicating that the
116 // server did not recognize it.
120 // Subset of successful responses which returned no full hashes.
121 // This includes the STATUS_204 case, and the *_ERROR cases.
122 GET_HASH_FULL_HASH_EMPTY
,
124 // Subset of successful responses for which one or more of the
125 // full hashes matched (should lead to an interstitial).
126 GET_HASH_FULL_HASH_HIT
,
128 // Subset of successful responses which weren't empty and have no
129 // matches. It means that there was a prefix collision which was
130 // cleared up by the full hashes.
131 GET_HASH_FULL_HASH_MISS
,
133 // Subset of successful responses where the response body wasn't parsable.
134 GET_HASH_PARSE_ERROR
,
136 // Gethash request failed (network error).
137 GET_HASH_NETWORK_ERROR
,
139 // Gethash request returned HTTP result code other than 200 or 204.
142 // Gethash attempted during error backoff, no request sent.
143 GET_HASH_BACKOFF_ERROR
,
145 // Memory space for histograms is determined by the max. ALWAYS
146 // ADD NEW VALUES BEFORE THIS ONE.
150 // Record a GetHash result. |is_download| indicates if the get
151 // hash is triggered by download related lookup.
152 static void RecordGetHashResult(bool is_download
,
153 ResultType result_type
);
155 // Returns whether another update is currently scheduled.
156 bool IsUpdateScheduled() const;
158 // Called when app changes status of foreground or background.
159 void SetAppInForeground(bool foreground
) {
160 app_in_foreground_
= foreground
;
164 // Constructs a SafeBrowsingProtocolManager for |delegate| that issues
165 // network requests using |request_context_getter|.
166 SafeBrowsingProtocolManager(
167 SafeBrowsingProtocolManagerDelegate
* delegate
,
168 net::URLRequestContextGetter
* request_context_getter
,
169 const SafeBrowsingProtocolConfig
& config
);
172 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingProtocolManagerTest
, TestBackOffTimes
);
173 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingProtocolManagerTest
, TestChunkStrings
);
174 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingProtocolManagerTest
, TestGetHashUrl
);
175 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingProtocolManagerTest
,
176 TestGetHashBackOffTimes
);
177 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingProtocolManagerTest
, TestNextChunkUrl
);
178 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingProtocolManagerTest
, TestUpdateUrl
);
179 friend class SafeBrowsingServerTest
;
180 friend class SBProtocolManagerFactoryImpl
;
182 // Internal API for fetching information from the SafeBrowsing servers. The
183 // GetHash requests are higher priority since they can block user requests
184 // so are handled separately.
185 enum SafeBrowsingRequestType
{
186 NO_REQUEST
= 0, // No requests in progress
187 UPDATE_REQUEST
, // Request for redirect URLs
188 BACKUP_UPDATE_REQUEST
, // Request for redirect URLs to a backup URL.
189 CHUNK_REQUEST
, // Request for a specific chunk
192 // Which type of backup update request is being used.
193 enum BackupUpdateReason
{
194 BACKUP_UPDATE_REASON_CONNECT
,
195 BACKUP_UPDATE_REASON_HTTP
,
196 BACKUP_UPDATE_REASON_NETWORK
,
197 BACKUP_UPDATE_REASON_MAX
,
200 // Generates Update URL for querying about the latest set of chunk updates.
201 GURL
UpdateUrl(bool is_extended_reporting
) const;
203 // Generates backup Update URL for querying about the latest set of chunk
204 // updates. |url_prefix| is the base prefix to use.
205 GURL
BackupUpdateUrl(BackupUpdateReason reason
) const;
207 // Generates GetHash request URL for retrieving full hashes.
208 GURL
GetHashUrl(bool is_extended_reporting
) const;
209 // Generates URL for reporting safe browsing hits for UMA users.
211 // Composes a ChunkUrl based on input string.
212 GURL
NextChunkUrl(const std::string
& input
) const;
214 // Returns the time for the next update request. If |back_off| is true,
215 // the time returned will increment an error count and return the appriate
216 // next time (see ScheduleNextUpdate below).
217 base::TimeDelta
GetNextUpdateInterval(bool back_off
);
219 // Worker function for calculating GetHash and Update backoff times (in
220 // seconds). |multiplier| is doubled for each consecutive error between the
221 // 2nd and 5th, and |error_count| is incremented with each call.
222 base::TimeDelta
GetNextBackOffInterval(size_t* error_count
,
223 size_t* multiplier
) const;
225 // Manages our update with the next allowable update time. If 'back_off_' is
226 // true, we must decrease the frequency of requests of the SafeBrowsing
227 // service according to section 5 of the protocol specification.
228 // When disable_auto_update_ is set, ScheduleNextUpdate will do nothing.
229 // ForceScheduleNextUpdate has to be called to trigger the update.
230 void ScheduleNextUpdate(bool back_off
);
232 // Sends a request for a list of chunks we should download to the SafeBrowsing
233 // servers. In order to format this request, we need to send all the chunk
234 // numbers for each list that we have to the server. Getting the chunk numbers
235 // requires a database query (run on the database thread), and the request
236 // is sent upon completion of that query in OnGetChunksComplete.
237 void IssueUpdateRequest();
239 // Sends a backup request for a list of chunks to download, when the primary
240 // update request failed. |reason| specifies why the backup is needed. Unlike
241 // the primary IssueUpdateRequest, this does not need to hit the local
242 // SafeBrowsing database since the existing chunk numbers are remembered from
243 // the primary update request. Returns whether the backup request was issued -
244 // this may be false in cases where there is not a prefix specified.
245 bool IssueBackupUpdateRequest(BackupUpdateReason reason
);
247 // Sends a request for a chunk to the SafeBrowsing servers.
248 void IssueChunkRequest();
250 // Runs the protocol parser on received data and update the
251 // SafeBrowsingService with the new content. Returns 'true' on successful
252 // parse, 'false' on error.
253 bool HandleServiceResponse(const GURL
& url
, const char* data
, size_t length
);
255 // Updates internal state for each GetHash response error, assuming that the
256 // current time is |now|.
257 void HandleGetHashError(const base::Time
& now
);
259 // Helper function for update completion.
260 void UpdateFinished(bool success
);
261 void UpdateFinished(bool success
, bool back_off
);
263 // A callback that runs if we timeout waiting for a response to an update
264 // request. We use this to properly set our update state.
265 void UpdateResponseTimeout();
267 // Called after the chunks are added to the database.
268 void OnAddChunksComplete();
271 // Map of GetHash requests to parameters which created it.
272 struct FullHashDetails
{
274 FullHashDetails(FullHashCallback callback
, bool is_download
);
277 FullHashCallback callback
;
280 typedef base::hash_map
<const net::URLFetcher
*, FullHashDetails
> HashRequests
;
282 // The factory that controls the creation of SafeBrowsingProtocolManager.
283 // This is used by tests.
284 static SBProtocolManagerFactory
* factory_
;
287 SafeBrowsingProtocolManagerDelegate
* delegate_
;
289 // Current active request (in case we need to cancel) for updates or chunks
290 // from the SafeBrowsing service. We can only have one of these outstanding
291 // at any given time unlike GetHash requests, which are tracked separately.
292 scoped_ptr
<net::URLFetcher
> request_
;
294 // The kind of request that is currently in progress.
295 SafeBrowsingRequestType request_type_
;
297 // The number of HTTP response errors, used for request backoff timing.
298 size_t update_error_count_
;
299 size_t gethash_error_count_
;
301 // Multipliers which double (max == 8) for each error after the second.
302 size_t update_back_off_mult_
;
303 size_t gethash_back_off_mult_
;
305 // Multiplier between 0 and 1 to spread clients over an interval.
306 float back_off_fuzz_
;
308 // The list for which we are make a request.
309 std::string list_name_
;
311 // For managing the next earliest time to query the SafeBrowsing servers for
313 base::TimeDelta next_update_interval_
;
314 base::OneShotTimer
<SafeBrowsingProtocolManager
> update_timer_
;
316 // timeout_timer_ is used to interrupt update requests which are taking
318 base::OneShotTimer
<SafeBrowsingProtocolManager
> timeout_timer_
;
320 // All chunk requests that need to be made.
321 std::deque
<ChunkUrl
> chunk_request_urls_
;
323 HashRequests hash_requests_
;
325 // The next scheduled update has special behavior for the first 2 requests.
326 enum UpdateRequestState
{
331 UpdateRequestState update_state_
;
333 // True if the service has been given an add/sub chunk but it hasn't been
334 // added to the database yet.
335 bool chunk_pending_to_write_
;
337 // The last time we successfully received an update.
338 base::Time last_update_
;
340 // While in GetHash backoff, we can't make another GetHash until this time.
341 base::Time next_gethash_time_
;
343 // Current product version sent in each request.
344 std::string version_
;
346 // Used for measuring chunk request latency.
347 base::Time chunk_request_start_
;
349 // Tracks the size of each update (in bytes).
352 // The safe browsing client name sent in each request.
353 std::string client_name_
;
355 // A string that is appended to the end of URLs for download, gethash,
356 // safebrowsing hits and chunk update requests.
357 std::string additional_query_
;
359 // The context we use to issue network requests.
360 scoped_refptr
<net::URLRequestContextGetter
> request_context_getter_
;
362 // URL prefix where browser fetches safebrowsing chunk updates, and hashes.
363 std::string url_prefix_
;
365 // Backup URL prefixes for updates.
366 std::string backup_url_prefixes_
[BACKUP_UPDATE_REASON_MAX
];
368 // The current reason why the backup update request is happening.
369 BackupUpdateReason backup_update_reason_
;
371 // Data to POST when doing an update.
372 std::string update_list_data_
;
374 // When true, protocol manager will not start an update unless
375 // ForceScheduleNextUpdate() is called. This is set for testing purpose.
376 bool disable_auto_update_
;
378 #if defined(OS_ANDROID)
379 // When true, protocol_manager will not check network connection
380 // type when scheduling next update. This is set for testing purpose.
381 bool disable_connection_check_
;
384 // ID for URLFetchers for testing.
387 // Whether the app is in foreground or background.
388 bool app_in_foreground_
;
390 DISALLOW_COPY_AND_ASSIGN(SafeBrowsingProtocolManager
);
393 // Interface of a factory to create ProtocolManager. Useful for tests.
394 class SBProtocolManagerFactory
{
396 SBProtocolManagerFactory() {}
397 virtual ~SBProtocolManagerFactory() {}
398 virtual SafeBrowsingProtocolManager
* CreateProtocolManager(
399 SafeBrowsingProtocolManagerDelegate
* delegate
,
400 net::URLRequestContextGetter
* request_context_getter
,
401 const SafeBrowsingProtocolConfig
& config
) = 0;
403 DISALLOW_COPY_AND_ASSIGN(SBProtocolManagerFactory
);
406 // Delegate interface for the SafeBrowsingProtocolManager.
407 class SafeBrowsingProtocolManagerDelegate
{
409 typedef base::Callback
<void(
410 const std::vector
<SBListChunkRanges
>&, /* List of chunks */
411 bool, /* database_error */
412 bool /* is_extended_reporting */
413 )> GetChunksCallback
;
414 typedef base::Callback
<void(void)> AddChunksCallback
;
416 virtual ~SafeBrowsingProtocolManagerDelegate();
418 // |UpdateStarted()| is called just before the SafeBrowsing update protocol
420 virtual void UpdateStarted() = 0;
422 // |UpdateFinished()| is called just after the SafeBrowsing update protocol
424 virtual void UpdateFinished(bool success
) = 0;
426 // Wipe out the local database. The SafeBrowsing server can request this.
427 virtual void ResetDatabase() = 0;
429 // Retrieve all the local database chunks, and invoke |callback| with the
430 // results. The SafeBrowsingProtocolManagerDelegate must only invoke the
431 // callback if the SafeBrowsingProtocolManager is still alive. Only one call
432 // may be made to GetChunks at a time.
433 virtual void GetChunks(GetChunksCallback callback
) = 0;
435 // Add new chunks to the database. Invokes |callback| when complete, but must
436 // call at a later time.
437 virtual void AddChunks(const std::string
& list
,
438 scoped_ptr
<ScopedVector
<SBChunkData
> > chunks
,
439 AddChunksCallback callback
) = 0;
441 // Delete chunks from the database.
442 virtual void DeleteChunks(
443 scoped_ptr
<std::vector
<SBChunkDelete
> > chunk_deletes
) = 0;
446 #endif // CHROME_BROWSER_SAFE_BROWSING_PROTOCOL_MANAGER_H_