Chromium Code Reviews| 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(); |
| 74 | 69 |
| 70 // Tell the update manager where to get the latest state of the stores. | |
| 71 void SetStoreStateMap(const StoreStateMap* store_state_map); | |
| 72 | |
| 75 protected: | 73 protected: |
| 76 // Constructs a V4UpdateProtocolManager that issues network requests using | 74 // Constructs a V4UpdateProtocolManager that issues network requests using |
| 77 // |request_context_getter|. | 75 // |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 | 76 // SafeBrowsing lists, and invoke |callback| when the results are retrieved. |
| 79 // |current_list_states|, and invoke |callback| when the results | 77 // The callback may be invoked synchronously. |
| 80 // are retrieved. The callback may be invoked synchronously. | |
| 81 V4UpdateProtocolManager( | 78 V4UpdateProtocolManager( |
| 82 net::URLRequestContextGetter* request_context_getter, | 79 net::URLRequestContextGetter* request_context_getter, |
| 83 const V4ProtocolConfig& config, | 80 const V4ProtocolConfig& config, |
| 84 const base::hash_map<UpdateListIdentifier, std::string>& | |
| 85 current_list_states, | |
| 86 V4UpdateCallback callback); | 81 V4UpdateCallback callback); |
| 87 | 82 |
| 88 private: | 83 private: |
| 89 FRIEND_TEST_ALL_PREFIXES(V4UpdateProtocolManagerTest, | 84 FRIEND_TEST_ALL_PREFIXES(V4UpdateProtocolManagerTest, |
| 90 TestGetUpdatesErrorHandlingNetwork); | 85 TestGetUpdatesErrorHandlingNetwork); |
| 91 FRIEND_TEST_ALL_PREFIXES(V4UpdateProtocolManagerTest, | 86 FRIEND_TEST_ALL_PREFIXES(V4UpdateProtocolManagerTest, |
| 92 TestGetUpdatesErrorHandlingResponseCode); | 87 TestGetUpdatesErrorHandlingResponseCode); |
| 93 FRIEND_TEST_ALL_PREFIXES(V4UpdateProtocolManagerTest, TestGetUpdatesNoError); | 88 FRIEND_TEST_ALL_PREFIXES(V4UpdateProtocolManagerTest, TestGetUpdatesNoError); |
| 94 FRIEND_TEST_ALL_PREFIXES(V4UpdateProtocolManagerTest, | 89 FRIEND_TEST_ALL_PREFIXES(V4UpdateProtocolManagerTest, |
| 95 TestGetUpdatesWithOneBackoff); | 90 TestGetUpdatesWithOneBackoff); |
| 96 friend class V4UpdateProtocolManagerFactoryImpl; | 91 friend class V4UpdateProtocolManagerFactoryImpl; |
| 97 | 92 |
| 98 // The method to populate |gurl| with the URL to be sent to the server. | 93 // 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 | 94 // |request_base64| is the base64 encoded form of an instance of the protobuf |
| 100 // FetchThreatListUpdatesRequest. Also sets the appropriate header values for | 95 // FetchThreatListUpdatesRequest. Also sets the appropriate header values for |
| 101 // sending PVer4 requests in |headers|. | 96 // sending PVer4 requests in |headers|. |
| 102 void GetUpdateUrlAndHeaders(const std::string& request_base64, | 97 void GetUpdateUrlAndHeaders(const std::string& request_base64, |
| 103 GURL* gurl, | 98 GURL* gurl, |
| 104 net::HttpRequestHeaders* headers) const; | 99 net::HttpRequestHeaders* headers) const; |
| 105 | 100 |
| 106 // Fills a FetchThreatListUpdatesRequest protocol buffer for a request. | 101 // Fills a FetchThreatListUpdatesRequest protocol buffer for a request. |
| 107 // Returns the serialized and base 64 encoded request as a string. | 102 // Returns the serialized and base 64 encoded request as a string. |
| 108 std::string GetBase64SerializedUpdateRequestProto( | 103 static std::string GetBase64SerializedUpdateRequestProto( |
| 109 const base::hash_map<UpdateListIdentifier, std::string>& | 104 const StoreStateMap* store_state_map); |
| 110 current_list_states); | |
| 111 | 105 |
| 112 // Parses the base64 encoded response received from the server as a | 106 // Parses the base64 encoded response received from the server as a |
| 113 // FetchThreatListUpdatesResponse protobuf and returns each of the | 107 // FetchThreatListUpdatesResponse protobuf and returns each of the |
| 114 // ListUpdateResponse protobufs contained in it as a vector. | 108 // ListUpdateResponse protobufs contained in it as a vector. |
| 115 // Returns true if parsing is successful, false otherwise. | 109 // Returns true if parsing is successful, false otherwise. |
| 116 bool ParseUpdateResponse( | 110 bool ParseUpdateResponse( |
| 117 const std::string& data_base64, | 111 const std::string& data_base64, |
| 118 std::vector<ListUpdateResponse>* list_update_responses); | 112 std::vector<ListUpdateResponse>* list_update_responses); |
| 119 | 113 |
| 120 // Resets the update error counter and multiplier. | 114 // Resets the update error counter and multiplier. |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 138 void ScheduleNextUpdateAfterInterval(base::TimeDelta interval); | 132 void ScheduleNextUpdateAfterInterval(base::TimeDelta interval); |
| 139 | 133 |
| 140 // Get the next update interval, considering whether we are in backoff. | 134 // Get the next update interval, considering whether we are in backoff. |
| 141 base::TimeDelta GetNextUpdateInterval(bool back_off); | 135 base::TimeDelta GetNextUpdateInterval(bool back_off); |
| 142 | 136 |
| 143 // The factory that controls the creation of V4UpdateProtocolManager. | 137 // The factory that controls the creation of V4UpdateProtocolManager. |
| 144 // This is used by tests. | 138 // This is used by tests. |
| 145 static V4UpdateProtocolManagerFactory* factory_; | 139 static V4UpdateProtocolManagerFactory* factory_; |
| 146 | 140 |
| 147 // The last known state of the lists. | 141 // The last known state of the lists. |
| 148 // At init, this is read from the disk or is empty for no prior state. | 142 // Updated after every successful update of the database. |
| 149 // Each successful update from the server contains a new state for each | 143 const StoreStateMap* store_state_map_; |
|
Scott Hess - ex-Googler
2016/06/17 22:53:43
Having the instance retain a pointer to a map owne
vakh (use Gerrit instead)
2016/06/21 00:29:36
Done.
| |
| 150 // requested list. | |
| 151 base::hash_map<UpdateListIdentifier, std::string> current_list_states_; | |
| 152 | 144 |
| 153 // The number of HTTP response errors since the the last successful HTTP | 145 // The number of HTTP response errors since the the last successful HTTP |
| 154 // response, used for request backoff timing. | 146 // response, used for request backoff timing. |
| 155 size_t update_error_count_; | 147 size_t update_error_count_; |
| 156 | 148 |
| 157 // Multiplier for the backoff error after the second. | 149 // Multiplier for the backoff error after the second. |
| 158 size_t update_back_off_mult_; | 150 size_t update_back_off_mult_; |
| 159 | 151 |
| 160 // The time delta after which the next update request may be sent. | 152 // 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. | 153 // It is set to a random interval between 60 and 300 seconds at start. |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 187 }; | 179 }; |
| 188 | 180 |
| 189 // Interface of a factory to create V4UpdateProtocolManager. Useful for tests. | 181 // Interface of a factory to create V4UpdateProtocolManager. Useful for tests. |
| 190 class V4UpdateProtocolManagerFactory { | 182 class V4UpdateProtocolManagerFactory { |
| 191 public: | 183 public: |
| 192 V4UpdateProtocolManagerFactory() {} | 184 V4UpdateProtocolManagerFactory() {} |
| 193 virtual ~V4UpdateProtocolManagerFactory() {} | 185 virtual ~V4UpdateProtocolManagerFactory() {} |
| 194 virtual std::unique_ptr<V4UpdateProtocolManager> CreateProtocolManager( | 186 virtual std::unique_ptr<V4UpdateProtocolManager> CreateProtocolManager( |
| 195 net::URLRequestContextGetter* request_context_getter, | 187 net::URLRequestContextGetter* request_context_getter, |
| 196 const V4ProtocolConfig& config, | 188 const V4ProtocolConfig& config, |
| 197 const base::hash_map<UpdateListIdentifier, std::string>& | |
| 198 current_list_states, | |
| 199 V4UpdateCallback callback) = 0; | 189 V4UpdateCallback callback) = 0; |
| 200 | 190 |
| 201 private: | 191 private: |
| 202 DISALLOW_COPY_AND_ASSIGN(V4UpdateProtocolManagerFactory); | 192 DISALLOW_COPY_AND_ASSIGN(V4UpdateProtocolManagerFactory); |
| 203 }; | 193 }; |
| 204 | 194 |
| 205 } // namespace safe_browsing | 195 } // namespace safe_browsing |
| 206 | 196 |
| 207 #endif // COMPONENTS_SAFE_BROWSING_DB_V4_UPDATE_PROTOCOL_MANAGER_H_ | 197 #endif // COMPONENTS_SAFE_BROWSING_DB_V4_UPDATE_PROTOCOL_MANAGER_H_ |
| OLD | NEW |