OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_SAFE_BROWSING_PROTOCOL_MANAGER_H_ | 5 #ifndef CHROME_BROWSER_SAFE_BROWSING_PROTOCOL_MANAGER_H_ |
6 #define CHROME_BROWSER_SAFE_BROWSING_PROTOCOL_MANAGER_H_ | 6 #define CHROME_BROWSER_SAFE_BROWSING_PROTOCOL_MANAGER_H_ |
7 | 7 |
8 // A class that implements Chrome's interface with the SafeBrowsing protocol. | 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 // |
9 // The SafeBrowsingProtocolManager handles formatting and making requests of, | 12 // The SafeBrowsingProtocolManager handles formatting and making requests of, |
10 // and handling responses from, Google's SafeBrowsing servers. This class uses | 13 // and handling responses from, Google's SafeBrowsing servers. This class uses |
11 // The SafeBrowsingProtocolParser class to do the actual parsing. | 14 // The SafeBrowsingProtocolParser class to do the actual parsing. |
12 | 15 |
13 #include <deque> | 16 #include <deque> |
14 #include <set> | 17 #include <set> |
15 #include <string> | 18 #include <string> |
16 #include <vector> | 19 #include <vector> |
17 | 20 |
18 #include "base/gtest_prod_util.h" | 21 #include "base/gtest_prod_util.h" |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
95 // Scheduled update callback. | 98 // Scheduled update callback. |
96 void GetNextUpdate(); | 99 void GetNextUpdate(); |
97 | 100 |
98 // Called by the SafeBrowsingService when our request for a list of all chunks | 101 // Called by the SafeBrowsingService when our request for a list of all chunks |
99 // for each list is done. If database_error is true, that means the protocol | 102 // for each list is done. If database_error is true, that means the protocol |
100 // manager shouldn't fetch updates since they can't be written to disk. It | 103 // manager shouldn't fetch updates since they can't be written to disk. It |
101 // should try again later to open the database. | 104 // should try again later to open the database. |
102 void OnGetChunksComplete(const std::vector<SBListChunkRanges>& list, | 105 void OnGetChunksComplete(const std::vector<SBListChunkRanges>& list, |
103 bool database_error); | 106 bool database_error); |
104 | 107 |
105 // Called after the chunks that were parsed were inserted in the database. | |
106 void OnChunkInserted(); | |
107 | |
108 // The last time we received an update. | 108 // The last time we received an update. |
109 base::Time last_update() const { return last_update_; } | 109 base::Time last_update() const { return last_update_; } |
110 | 110 |
111 // Setter for additional_query_. To make sure the additional_query_ won't | 111 // Setter for additional_query_. To make sure the additional_query_ won't |
112 // be changed in the middle of an update, caller (e.g.: SafeBrowsingService) | 112 // be changed in the middle of an update, caller (e.g.: SafeBrowsingService) |
113 // should call this after callbacks triggered in UpdateFinished() or before | 113 // should call this after callbacks triggered in UpdateFinished() or before |
114 // IssueUpdateRequest(). | 114 // IssueUpdateRequest(). |
115 void set_additional_query(const std::string& query) { | 115 void set_additional_query(const std::string& query) { |
116 additional_query_ = query; | 116 additional_query_ = query; |
117 } | 117 } |
(...skipping 27 matching lines...) Expand all Loading... |
145 // Memory space for histograms is determined by the max. ALWAYS | 145 // Memory space for histograms is determined by the max. ALWAYS |
146 // ADD NEW VALUES BEFORE THIS ONE. | 146 // ADD NEW VALUES BEFORE THIS ONE. |
147 GET_HASH_RESULT_MAX | 147 GET_HASH_RESULT_MAX |
148 }; | 148 }; |
149 | 149 |
150 // Record a GetHash result. |is_download| indicates if the get | 150 // Record a GetHash result. |is_download| indicates if the get |
151 // hash is triggered by download related lookup. | 151 // hash is triggered by download related lookup. |
152 static void RecordGetHashResult(bool is_download, | 152 static void RecordGetHashResult(bool is_download, |
153 ResultType result_type); | 153 ResultType result_type); |
154 | 154 |
| 155 // Returns whether another update is currently scheduled. |
| 156 bool IsUpdateScheduled() const; |
| 157 |
155 protected: | 158 protected: |
156 // Constructs a SafeBrowsingProtocolManager for |delegate| that issues | 159 // Constructs a SafeBrowsingProtocolManager for |delegate| that issues |
157 // network requests using |request_context_getter|. | 160 // network requests using |request_context_getter|. |
158 SafeBrowsingProtocolManager( | 161 SafeBrowsingProtocolManager( |
159 SafeBrowsingProtocolManagerDelegate* delegate, | 162 SafeBrowsingProtocolManagerDelegate* delegate, |
160 net::URLRequestContextGetter* request_context_getter, | 163 net::URLRequestContextGetter* request_context_getter, |
161 const SafeBrowsingProtocolConfig& config); | 164 const SafeBrowsingProtocolConfig& config); |
162 | 165 |
163 private: | 166 private: |
164 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingProtocolManagerTest, TestBackOffTimes); | 167 FRIEND_TEST_ALL_PREFIXES(SafeBrowsingProtocolManagerTest, TestBackOffTimes); |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
225 // SafeBrowsingService with the new content. Returns 'true' on successful | 228 // SafeBrowsingService with the new content. Returns 'true' on successful |
226 // parse, 'false' on error. | 229 // parse, 'false' on error. |
227 bool HandleServiceResponse(const GURL& url, const char* data, int length); | 230 bool HandleServiceResponse(const GURL& url, const char* data, int length); |
228 | 231 |
229 // Updates internal state for each GetHash response error, assuming that the | 232 // Updates internal state for each GetHash response error, assuming that the |
230 // current time is |now|. | 233 // current time is |now|. |
231 void HandleGetHashError(const base::Time& now); | 234 void HandleGetHashError(const base::Time& now); |
232 | 235 |
233 // Helper function for update completion. | 236 // Helper function for update completion. |
234 void UpdateFinished(bool success); | 237 void UpdateFinished(bool success); |
| 238 void UpdateFinished(bool success, bool back_off); |
235 | 239 |
236 // A callback that runs if we timeout waiting for a response to an update | 240 // A callback that runs if we timeout waiting for a response to an update |
237 // request. We use this to properly set our update state. | 241 // request. We use this to properly set our update state. |
238 void UpdateResponseTimeout(); | 242 void UpdateResponseTimeout(); |
239 | 243 |
| 244 // Called after the chunks are added to the database. |
| 245 void OnAddChunksComplete(); |
| 246 |
240 private: | 247 private: |
241 // Map of GetHash requests to parameters which created it. | 248 // Map of GetHash requests to parameters which created it. |
242 struct FullHashDetails { | 249 struct FullHashDetails { |
243 FullHashDetails(); | 250 FullHashDetails(); |
244 FullHashDetails(FullHashCallback callback, bool is_download); | 251 FullHashDetails(FullHashCallback callback, bool is_download); |
245 ~FullHashDetails(); | 252 ~FullHashDetails(); |
246 | 253 |
247 FullHashCallback callback; | 254 FullHashCallback callback; |
248 bool is_download; | 255 bool is_download; |
249 }; | 256 }; |
(...skipping 26 matching lines...) Expand all Loading... |
276 float back_off_fuzz_; | 283 float back_off_fuzz_; |
277 | 284 |
278 // The list for which we are make a request. | 285 // The list for which we are make a request. |
279 std::string list_name_; | 286 std::string list_name_; |
280 | 287 |
281 // For managing the next earliest time to query the SafeBrowsing servers for | 288 // For managing the next earliest time to query the SafeBrowsing servers for |
282 // updates. | 289 // updates. |
283 base::TimeDelta next_update_interval_; | 290 base::TimeDelta next_update_interval_; |
284 base::OneShotTimer<SafeBrowsingProtocolManager> update_timer_; | 291 base::OneShotTimer<SafeBrowsingProtocolManager> update_timer_; |
285 | 292 |
| 293 // timeout_timer_ is used to interrupt update requests which are taking |
| 294 // too long. |
| 295 base::OneShotTimer<SafeBrowsingProtocolManager> timeout_timer_; |
| 296 |
286 // All chunk requests that need to be made. | 297 // All chunk requests that need to be made. |
287 std::deque<ChunkUrl> chunk_request_urls_; | 298 std::deque<ChunkUrl> chunk_request_urls_; |
288 | 299 |
289 HashRequests hash_requests_; | 300 HashRequests hash_requests_; |
290 | 301 |
291 // The next scheduled update has special behavior for the first 2 requests. | 302 // The next scheduled update has special behavior for the first 2 requests. |
292 enum UpdateRequestState { | 303 enum UpdateRequestState { |
293 FIRST_REQUEST = 0, | 304 FIRST_REQUEST = 0, |
294 SECOND_REQUEST, | 305 SECOND_REQUEST, |
295 NORMAL_REQUEST | 306 NORMAL_REQUEST |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
349 const SafeBrowsingProtocolConfig& config) = 0; | 360 const SafeBrowsingProtocolConfig& config) = 0; |
350 private: | 361 private: |
351 DISALLOW_COPY_AND_ASSIGN(SBProtocolManagerFactory); | 362 DISALLOW_COPY_AND_ASSIGN(SBProtocolManagerFactory); |
352 }; | 363 }; |
353 | 364 |
354 // Delegate interface for the SafeBrowsingProtocolManager. | 365 // Delegate interface for the SafeBrowsingProtocolManager. |
355 class SafeBrowsingProtocolManagerDelegate { | 366 class SafeBrowsingProtocolManagerDelegate { |
356 public: | 367 public: |
357 typedef base::Callback<void(const std::vector<SBListChunkRanges>&, bool)> | 368 typedef base::Callback<void(const std::vector<SBListChunkRanges>&, bool)> |
358 GetChunksCallback; | 369 GetChunksCallback; |
| 370 typedef base::Callback<void(void)> AddChunksCallback; |
359 | 371 |
360 virtual ~SafeBrowsingProtocolManagerDelegate(); | 372 virtual ~SafeBrowsingProtocolManagerDelegate(); |
361 | 373 |
362 // |UpdateStarted()| is called just before the SafeBrowsing update protocol | 374 // |UpdateStarted()| is called just before the SafeBrowsing update protocol |
363 // has begun. | 375 // has begun. |
364 virtual void UpdateStarted() = 0; | 376 virtual void UpdateStarted() = 0; |
365 | 377 |
366 // |UpdateFinished()| is called just after the SafeBrowsing update protocol | 378 // |UpdateFinished()| is called just after the SafeBrowsing update protocol |
367 // has completed. | 379 // has completed. |
368 virtual void UpdateFinished(bool success) = 0; | 380 virtual void UpdateFinished(bool success) = 0; |
369 | 381 |
370 // Wipe out the local database. The SafeBrowsing server can request this. | 382 // Wipe out the local database. The SafeBrowsing server can request this. |
371 virtual void ResetDatabase() = 0; | 383 virtual void ResetDatabase() = 0; |
372 | 384 |
373 // Retrieve all the local database chunks, and invoke |callback| with the | 385 // Retrieve all the local database chunks, and invoke |callback| with the |
374 // results. The SafeBrowsingProtocolManagerDelegate must only invoke the | 386 // results. The SafeBrowsingProtocolManagerDelegate must only invoke the |
375 // callback if the SafeBrowsingProtocolManager is still alive. Only one call | 387 // callback if the SafeBrowsingProtocolManager is still alive. Only one call |
376 // may be made to GetChunks at a time. | 388 // may be made to GetChunks at a time. |
377 virtual void GetChunks(GetChunksCallback callback) = 0; | 389 virtual void GetChunks(GetChunksCallback callback) = 0; |
378 | 390 |
379 // Add new chunks to the database. | 391 // Add new chunks to the database. Invokes |callback| when complete, but must |
380 virtual void AddChunks(const std::string& list, SBChunkList* chunks) = 0; | 392 // call at a later time. |
| 393 virtual void AddChunks(const std::string& list, SBChunkList* chunks, |
| 394 AddChunksCallback callback) = 0; |
381 | 395 |
382 // Delete chunks from the database. | 396 // Delete chunks from the database. |
383 virtual void DeleteChunks( | 397 virtual void DeleteChunks( |
384 std::vector<SBChunkDelete>* delete_chunks) = 0; | 398 std::vector<SBChunkDelete>* delete_chunks) = 0; |
385 }; | 399 }; |
386 | 400 |
387 #endif // CHROME_BROWSER_SAFE_BROWSING_PROTOCOL_MANAGER_H_ | 401 #endif // CHROME_BROWSER_SAFE_BROWSING_PROTOCOL_MANAGER_H_ |
OLD | NEW |