OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 COMPONENTS_SAFE_BROWSING_DB_V4_UPDATE_PROTOCOL_MANAGER_H_ |
| 6 #define COMPONENTS_SAFE_BROWSING_DB_V4_UPDATE_PROTOCOL_MANAGER_H_ |
| 7 |
| 8 // A class that implements Chrome's interface with the SafeBrowsing V4 update |
| 9 // protocol. |
| 10 // |
| 11 // The V4UpdateProtocolManager handles formatting and making requests of, and |
| 12 // handling responses from, Google's SafeBrowsing servers. The purpose of this |
| 13 // class is to get hash prefixes from the SB server for the given set of lists. |
| 14 |
| 15 #include <string> |
| 16 #include <vector> |
| 17 |
| 18 #include "base/gtest_prod_util.h" |
| 19 #include "base/macros.h" |
| 20 #include "base/memory/scoped_ptr.h" |
| 21 #include "base/threading/non_thread_safe.h" |
| 22 #include "base/time/time.h" |
| 23 #include "base/timer/timer.h" |
| 24 #include "components/safe_browsing_db/safebrowsing.pb.h" |
| 25 #include "components/safe_browsing_db/util.h" |
| 26 #include "components/safe_browsing_db/v4_protocol_manager_util.h" |
| 27 #include "net/url_request/url_fetcher_delegate.h" |
| 28 #include "url/gurl.h" |
| 29 |
| 30 namespace net { |
| 31 class URLFetcher; |
| 32 class URLRequestContextGetter; |
| 33 } // namespace net |
| 34 |
| 35 namespace safe_browsing { |
| 36 |
| 37 class V4UpdateProtocolManagerFactory; |
| 38 |
| 39 class V4UpdateProtocolManager : public net::URLFetcherDelegate, |
| 40 public base::NonThreadSafe { |
| 41 public: |
| 42 typedef FetchThreatListUpdatesRequest::ListUpdateRequest ListUpdateRequest; |
| 43 typedef FetchThreatListUpdatesResponse::ListUpdateResponse ListUpdateResponse; |
| 44 |
| 45 // UpdateCallback is invoked when GetUpdates completes. |
| 46 // Parameters: |
| 47 // - The vector of update response protobufs received from the server for |
| 48 // each list type. |
| 49 // The caller can then use this vector to re-build the current_list_states. |
| 50 typedef base::Callback<void(const std::vector<ListUpdateResponse>&)> |
| 51 UpdateCallback; |
| 52 |
| 53 ~V4UpdateProtocolManager() override; |
| 54 |
| 55 // Makes the passed |factory| the factory used to instantiate |
| 56 // a V4UpdateProtocolManager. Useful for tests. |
| 57 static void RegisterFactory(V4UpdateProtocolManagerFactory* factory) { |
| 58 factory_ = factory; |
| 59 } |
| 60 |
| 61 // Create an instance of the safe browsing v4 protocol manager. |
| 62 static V4UpdateProtocolManager* Create( |
| 63 net::URLRequestContextGetter* request_context_getter, |
| 64 const V4ProtocolConfig& config); |
| 65 |
| 66 // net::URLFetcherDelegate interface. |
| 67 void OnURLFetchComplete(const net::URLFetcher* source) override; |
| 68 |
| 69 // Retrieve the hash prefix update, and invoke the callback argument when the |
| 70 // results are retrieved. The callback may be invoked synchronously. |
| 71 // Parameters: |
| 72 // - The set of lists to fetch the updates for. |
| 73 // - The last known state for each of the known lists. |
| 74 // It is valid to have one or more lists in lists_to_update set that have no |
| 75 // corresponding value in the current_list_states map. This corresponds to the |
| 76 // initial state for those lists. |
| 77 virtual void GetUpdates( |
| 78 const base::hash_set<UpdateListIdentifier>& lists_to_update, |
| 79 const base::hash_map<UpdateListIdentifier, std::string>& |
| 80 current_list_states, |
| 81 UpdateCallback callback); |
| 82 |
| 83 protected: |
| 84 // Constructs a V4UpdateProtocolManager that issues network requests using |
| 85 // |request_context_getter|. |
| 86 V4UpdateProtocolManager(net::URLRequestContextGetter* request_context_getter, |
| 87 const V4ProtocolConfig& config); |
| 88 |
| 89 private: |
| 90 FRIEND_TEST_ALL_PREFIXES(V4UpdateProtocolManagerTest, |
| 91 TestGetUpdatesErrorHandlingNetwork); |
| 92 FRIEND_TEST_ALL_PREFIXES(V4UpdateProtocolManagerTest, |
| 93 TestGetUpdatesErrorHandlingResponseCode); |
| 94 FRIEND_TEST_ALL_PREFIXES(V4UpdateProtocolManagerTest, TestGetUpdatesNoError); |
| 95 friend class V4UpdateProtocolManagerFactoryImpl; |
| 96 |
| 97 // The method to generate the URL for the request to be sent to the server. |
| 98 // |request_base64| is the base64 encoded form of an instance of the protobuf |
| 99 // FetchThreatListUpdatesRequest. |
| 100 GURL GetUpdateUrl(const std::string& request_base64) const; |
| 101 |
| 102 // Fills a FetchThreatListUpdatesRequest protocol buffer for a request. |
| 103 // Returns the serialized and base 64 encoded request as a string. |
| 104 std::string GetUpdateRequest( |
| 105 const base::hash_set<UpdateListIdentifier>& lists_to_update, |
| 106 const base::hash_map<UpdateListIdentifier, std::string>& |
| 107 current_list_states); |
| 108 |
| 109 // Parses the base64 encoded response received from the server as a |
| 110 // FetchThreatListUpdatesResponse protobuf and returns each of the |
| 111 // ListUpdateResponse protobufs contained in it as a vector. |
| 112 // Returns true if parsing is successful, false otherwise. |
| 113 bool ParseUpdateResponse( |
| 114 const std::string& data_base64, |
| 115 std::vector<ListUpdateResponse>* list_update_responses); |
| 116 |
| 117 // Resets the update error counter and multiplier. |
| 118 void ResetUpdateErrors(); |
| 119 |
| 120 // Updates internal update and backoff state for each update response error, |
| 121 // assuming that the current time is |now|. |
| 122 void HandleUpdateError(const base::Time& now); |
| 123 |
| 124 // The factory that controls the creation of V4UpdateProtocolManager. |
| 125 // This is used by tests. |
| 126 static V4UpdateProtocolManagerFactory* factory_; |
| 127 |
| 128 // The number of HTTP response errors since the the last successful HTTP |
| 129 // response, used for request backoff timing. |
| 130 size_t update_error_count_; |
| 131 |
| 132 // Multiplier for the backoff error after the second. |
| 133 size_t update_back_off_mult_; |
| 134 |
| 135 // The time before which the next update request may not be sent. |
| 136 // It is set to: |
| 137 // the backoff time, if the last response was an error, or |
| 138 // the minimum wait time, if the last response was successful. |
| 139 base::Time next_update_time_; |
| 140 |
| 141 // The config of the client making Pver4 requests. |
| 142 const V4ProtocolConfig config_; |
| 143 |
| 144 // The context we use to issue network requests. |
| 145 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; |
| 146 |
| 147 // ID for URLFetchers for testing. |
| 148 int url_fetcher_id_; |
| 149 |
| 150 // True if there's a request pending. |
| 151 bool update_request_pending_; |
| 152 |
| 153 // The callback that's called when GetUpdates completes. |
| 154 UpdateCallback callback_; |
| 155 |
| 156 // The pending update request. The request must be canceled when the object is |
| 157 // destroyed. |
| 158 scoped_ptr<net::URLFetcher> request_; |
| 159 |
| 160 DISALLOW_COPY_AND_ASSIGN(V4UpdateProtocolManager); |
| 161 }; |
| 162 |
| 163 // Interface of a factory to create V4UpdateProtocolManager. Useful for tests. |
| 164 class V4UpdateProtocolManagerFactory { |
| 165 public: |
| 166 V4UpdateProtocolManagerFactory() {} |
| 167 virtual ~V4UpdateProtocolManagerFactory() {} |
| 168 virtual V4UpdateProtocolManager* CreateProtocolManager( |
| 169 net::URLRequestContextGetter* request_context_getter, |
| 170 const V4ProtocolConfig& config) = 0; |
| 171 |
| 172 private: |
| 173 DISALLOW_COPY_AND_ASSIGN(V4UpdateProtocolManagerFactory); |
| 174 }; |
| 175 |
| 176 } // namespace safe_browsing |
| 177 |
| 178 #endif // COMPONENTS_SAFE_BROWSING_DB_V4_UPDATE_PROTOCOL_MANAGER_H_ |
OLD | NEW |