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