| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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 COMPONENTS_SAFE_BROWSING_DB_V4_UPDATE_PROTOCOL_MANAGER_H_ | 5 #ifndef COMPONENTS_SAFE_BROWSING_DB_V4_UPDATE_PROTOCOL_MANAGER_H_ |
| 6 #define COMPONENTS_SAFE_BROWSING_DB_V4_UPDATE_PROTOCOL_MANAGER_H_ | 6 #define COMPONENTS_SAFE_BROWSING_DB_V4_UPDATE_PROTOCOL_MANAGER_H_ |
| 7 | 7 |
| 8 // A class that implements Chrome's interface with the SafeBrowsing V4 update | 8 // A class that implements Chrome's interface with the SafeBrowsing V4 update |
| 9 // protocol. | 9 // protocol. |
| 10 // | 10 // |
| (...skipping 19 matching lines...) Expand all Loading... |
| 30 | 30 |
| 31 namespace net { | 31 namespace net { |
| 32 class URLFetcher; | 32 class URLFetcher; |
| 33 class URLRequestContextGetter; | 33 class URLRequestContextGetter; |
| 34 } // namespace net | 34 } // namespace net |
| 35 | 35 |
| 36 namespace safe_browsing { | 36 namespace safe_browsing { |
| 37 | 37 |
| 38 class V4UpdateProtocolManagerFactory; | 38 class V4UpdateProtocolManagerFactory; |
| 39 | 39 |
| 40 typedef FetchThreatListUpdatesRequest::ListUpdateRequest ListUpdateRequest; | |
| 41 typedef FetchThreatListUpdatesResponse::ListUpdateResponse ListUpdateResponse; | |
| 42 | |
| 43 // V4UpdateCallback is invoked when a scheduled update completes. | 40 // V4UpdateCallback is invoked when a scheduled update completes. |
| 44 // Parameters: | 41 // Parameters: |
| 45 // - The vector of update response protobufs received from the server for | 42 // - The vector of update response protobufs received from the server for |
| 46 // each list type. | 43 // each list type. |
| 47 typedef base::Callback<void(const std::vector<ListUpdateResponse>&)> | 44 typedef base::Callback<void(const std::vector<ListUpdateResponse>&)> |
| 48 V4UpdateCallback; | 45 V4UpdateCallback; |
| 49 | 46 |
| 50 class V4UpdateProtocolManager : public net::URLFetcherDelegate, | 47 class V4UpdateProtocolManager : public net::URLFetcherDelegate, |
| 51 public base::NonThreadSafe { | 48 public base::NonThreadSafe { |
| 52 public: | 49 public: |
| 53 ~V4UpdateProtocolManager() override; | 50 ~V4UpdateProtocolManager() override; |
| 54 | 51 |
| 55 // Makes the passed |factory| the factory used to instantiate | 52 // Makes the passed |factory| the factory used to instantiate |
| 56 // a V4UpdateProtocolManager. Useful for tests. | 53 // a V4UpdateProtocolManager. Useful for tests. |
| 57 static void RegisterFactory(V4UpdateProtocolManagerFactory* factory) { | 54 static void RegisterFactory(V4UpdateProtocolManagerFactory* factory) { |
| 58 factory_ = factory; | 55 factory_ = factory; |
| 59 } | 56 } |
| 60 | 57 |
| 61 // Create an instance of the safe browsing v4 protocol manager. | 58 // Create an instance of the safe browsing v4 protocol manager. |
| 62 static std::unique_ptr<V4UpdateProtocolManager> Create( | 59 static std::unique_ptr<V4UpdateProtocolManager> Create( |
| 63 net::URLRequestContextGetter* request_context_getter, | 60 net::URLRequestContextGetter* request_context_getter, |
| 64 const V4ProtocolConfig& config, | 61 const V4ProtocolConfig& config, |
| 65 const base::hash_map<UpdateListIdentifier, std::string>& | |
| 66 current_list_states, | |
| 67 V4UpdateCallback callback); | 62 V4UpdateCallback callback); |
| 68 | 63 |
| 69 // net::URLFetcherDelegate interface. | 64 // net::URLFetcherDelegate interface. |
| 70 void OnURLFetchComplete(const net::URLFetcher* source) override; | 65 void OnURLFetchComplete(const net::URLFetcher* source) override; |
| 71 | 66 |
| 72 // Schedule the next update without backoff. | 67 // Schedule the next update without backoff. |
| 73 void ScheduleNextUpdate(); | 68 void ScheduleNextUpdate(std::unique_ptr<StoreStateMap> store_state_map); |
| 74 | 69 |
| 75 protected: | 70 protected: |
| 76 // Constructs a V4UpdateProtocolManager that issues network requests using | 71 // Constructs a V4UpdateProtocolManager that issues network requests using |
| 77 // |request_context_getter|. | 72 // |request_context_getter|. It schedules updates to get the hash prefixes for |
| 78 // Schedules an update to get the hash prefixes for the lists in | 73 // SafeBrowsing lists, and invoke |callback| when the results are retrieved. |
| 79 // |current_list_states|, and invoke |callback| when the results | 74 // The callback may be invoked synchronously. |
| 80 // are retrieved. The callback may be invoked synchronously. | |
| 81 V4UpdateProtocolManager( | 75 V4UpdateProtocolManager( |
| 82 net::URLRequestContextGetter* request_context_getter, | 76 net::URLRequestContextGetter* request_context_getter, |
| 83 const V4ProtocolConfig& config, | 77 const V4ProtocolConfig& config, |
| 84 const base::hash_map<UpdateListIdentifier, std::string>& | |
| 85 current_list_states, | |
| 86 V4UpdateCallback callback); | 78 V4UpdateCallback callback); |
| 87 | 79 |
| 88 private: | 80 private: |
| 89 FRIEND_TEST_ALL_PREFIXES(V4UpdateProtocolManagerTest, | 81 FRIEND_TEST_ALL_PREFIXES(V4UpdateProtocolManagerTest, |
| 90 TestGetUpdatesErrorHandlingNetwork); | 82 TestGetUpdatesErrorHandlingNetwork); |
| 91 FRIEND_TEST_ALL_PREFIXES(V4UpdateProtocolManagerTest, | 83 FRIEND_TEST_ALL_PREFIXES(V4UpdateProtocolManagerTest, |
| 92 TestGetUpdatesErrorHandlingResponseCode); | 84 TestGetUpdatesErrorHandlingResponseCode); |
| 93 FRIEND_TEST_ALL_PREFIXES(V4UpdateProtocolManagerTest, TestGetUpdatesNoError); | 85 FRIEND_TEST_ALL_PREFIXES(V4UpdateProtocolManagerTest, TestGetUpdatesNoError); |
| 94 FRIEND_TEST_ALL_PREFIXES(V4UpdateProtocolManagerTest, | 86 FRIEND_TEST_ALL_PREFIXES(V4UpdateProtocolManagerTest, |
| 95 TestGetUpdatesWithOneBackoff); | 87 TestGetUpdatesWithOneBackoff); |
| 96 friend class V4UpdateProtocolManagerFactoryImpl; | 88 friend class V4UpdateProtocolManagerFactoryImpl; |
| 97 | 89 |
| 98 // The method to populate |gurl| with the URL to be sent to the server. | 90 // The method to populate |gurl| with the URL to be sent to the server. |
| 99 // |request_base64| is the base64 encoded form of an instance of the protobuf | 91 // |request_base64| is the base64 encoded form of an instance of the protobuf |
| 100 // FetchThreatListUpdatesRequest. Also sets the appropriate header values for | 92 // FetchThreatListUpdatesRequest. Also sets the appropriate header values for |
| 101 // sending PVer4 requests in |headers|. | 93 // sending PVer4 requests in |headers|. |
| 102 void GetUpdateUrlAndHeaders(const std::string& request_base64, | 94 void GetUpdateUrlAndHeaders(const std::string& request_base64, |
| 103 GURL* gurl, | 95 GURL* gurl, |
| 104 net::HttpRequestHeaders* headers) const; | 96 net::HttpRequestHeaders* headers) const; |
| 105 | 97 |
| 106 // Fills a FetchThreatListUpdatesRequest protocol buffer for a request. | 98 // Fills a FetchThreatListUpdatesRequest protocol buffer for a request. |
| 107 // Returns the serialized and base 64 encoded request as a string. | 99 // Returns the serialized and base 64 encoded request as a string. |
| 108 std::string GetBase64SerializedUpdateRequestProto( | 100 static std::string GetBase64SerializedUpdateRequestProto( |
| 109 const base::hash_map<UpdateListIdentifier, std::string>& | 101 const StoreStateMap& store_state_map); |
| 110 current_list_states); | |
| 111 | 102 |
| 112 // Parses the base64 encoded response received from the server as a | 103 // Parses the base64 encoded response received from the server as a |
| 113 // FetchThreatListUpdatesResponse protobuf and returns each of the | 104 // FetchThreatListUpdatesResponse protobuf and returns each of the |
| 114 // ListUpdateResponse protobufs contained in it as a vector. | 105 // ListUpdateResponse protobufs contained in it as a vector. |
| 115 // Returns true if parsing is successful, false otherwise. | 106 // Returns true if parsing is successful, false otherwise. |
| 116 bool ParseUpdateResponse( | 107 bool ParseUpdateResponse( |
| 117 const std::string& data_base64, | 108 const std::string& data_base64, |
| 118 std::vector<ListUpdateResponse>* list_update_responses); | 109 std::vector<ListUpdateResponse>* list_update_responses); |
| 119 | 110 |
| 120 // Resets the update error counter and multiplier. | 111 // Resets the update error counter and multiplier. |
| (...skipping 17 matching lines...) Expand all Loading... |
| 138 void ScheduleNextUpdateAfterInterval(base::TimeDelta interval); | 129 void ScheduleNextUpdateAfterInterval(base::TimeDelta interval); |
| 139 | 130 |
| 140 // Get the next update interval, considering whether we are in backoff. | 131 // Get the next update interval, considering whether we are in backoff. |
| 141 base::TimeDelta GetNextUpdateInterval(bool back_off); | 132 base::TimeDelta GetNextUpdateInterval(bool back_off); |
| 142 | 133 |
| 143 // The factory that controls the creation of V4UpdateProtocolManager. | 134 // The factory that controls the creation of V4UpdateProtocolManager. |
| 144 // This is used by tests. | 135 // This is used by tests. |
| 145 static V4UpdateProtocolManagerFactory* factory_; | 136 static V4UpdateProtocolManagerFactory* factory_; |
| 146 | 137 |
| 147 // The last known state of the lists. | 138 // The last known state of the lists. |
| 148 // At init, this is read from the disk or is empty for no prior state. | 139 // Updated after every successful update of the database. |
| 149 // Each successful update from the server contains a new state for each | 140 std::unique_ptr<StoreStateMap> store_state_map_; |
| 150 // requested list. | |
| 151 base::hash_map<UpdateListIdentifier, std::string> current_list_states_; | |
| 152 | 141 |
| 153 // The number of HTTP response errors since the the last successful HTTP | 142 // The number of HTTP response errors since the the last successful HTTP |
| 154 // response, used for request backoff timing. | 143 // response, used for request backoff timing. |
| 155 size_t update_error_count_; | 144 size_t update_error_count_; |
| 156 | 145 |
| 157 // Multiplier for the backoff error after the second. | 146 // Multiplier for the backoff error after the second. |
| 158 size_t update_back_off_mult_; | 147 size_t update_back_off_mult_; |
| 159 | 148 |
| 160 // The time delta after which the next update request may be sent. | 149 // The time delta after which the next update request may be sent. |
| 161 // It is set to a random interval between 60 and 300 seconds at start. | 150 // It is set to a random interval between 60 and 300 seconds at start. |
| (...skipping 25 matching lines...) Expand all Loading... |
| 187 }; | 176 }; |
| 188 | 177 |
| 189 // Interface of a factory to create V4UpdateProtocolManager. Useful for tests. | 178 // Interface of a factory to create V4UpdateProtocolManager. Useful for tests. |
| 190 class V4UpdateProtocolManagerFactory { | 179 class V4UpdateProtocolManagerFactory { |
| 191 public: | 180 public: |
| 192 V4UpdateProtocolManagerFactory() {} | 181 V4UpdateProtocolManagerFactory() {} |
| 193 virtual ~V4UpdateProtocolManagerFactory() {} | 182 virtual ~V4UpdateProtocolManagerFactory() {} |
| 194 virtual std::unique_ptr<V4UpdateProtocolManager> CreateProtocolManager( | 183 virtual std::unique_ptr<V4UpdateProtocolManager> CreateProtocolManager( |
| 195 net::URLRequestContextGetter* request_context_getter, | 184 net::URLRequestContextGetter* request_context_getter, |
| 196 const V4ProtocolConfig& config, | 185 const V4ProtocolConfig& config, |
| 197 const base::hash_map<UpdateListIdentifier, std::string>& | |
| 198 current_list_states, | |
| 199 V4UpdateCallback callback) = 0; | 186 V4UpdateCallback callback) = 0; |
| 200 | 187 |
| 201 private: | 188 private: |
| 202 DISALLOW_COPY_AND_ASSIGN(V4UpdateProtocolManagerFactory); | 189 DISALLOW_COPY_AND_ASSIGN(V4UpdateProtocolManagerFactory); |
| 203 }; | 190 }; |
| 204 | 191 |
| 205 } // namespace safe_browsing | 192 } // namespace safe_browsing |
| 206 | 193 |
| 207 #endif // COMPONENTS_SAFE_BROWSING_DB_V4_UPDATE_PROTOCOL_MANAGER_H_ | 194 #endif // COMPONENTS_SAFE_BROWSING_DB_V4_UPDATE_PROTOCOL_MANAGER_H_ |
| OLD | NEW |