Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(16)

Side by Side Diff: components/safe_browsing_db/v4_update_protocol_manager.h

Issue 1848973004: Makes V4UpdateProtocolManager auto-schedule update fetching (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@v4_01_
Patch Set: formatting and removed ClearCurrentListStates from test Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 18 matching lines...) Expand all
29 29
30 namespace net { 30 namespace net {
31 class URLFetcher; 31 class URLFetcher;
32 class URLRequestContextGetter; 32 class URLRequestContextGetter;
33 } // namespace net 33 } // namespace net
34 34
35 namespace safe_browsing { 35 namespace safe_browsing {
36 36
37 class V4UpdateProtocolManagerFactory; 37 class V4UpdateProtocolManagerFactory;
38 38
39 typedef FetchThreatListUpdatesRequest::ListUpdateRequest ListUpdateRequest;
40 typedef FetchThreatListUpdatesResponse::ListUpdateResponse ListUpdateResponse;
41
42 // V4UpdateCallback is invoked when a scheduled update completes.
43 // Parameters:
44 // - The vector of update response protobufs received from the server for
45 // each list type.
46 typedef base::Callback<void(const std::vector<ListUpdateResponse>&)>
47 V4UpdateCallback;
48
39 class V4UpdateProtocolManager : public net::URLFetcherDelegate, 49 class V4UpdateProtocolManager : public net::URLFetcherDelegate,
40 public base::NonThreadSafe { 50 public base::NonThreadSafe {
41 public: 51 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; 52 ~V4UpdateProtocolManager() override;
54 53
55 // Makes the passed |factory| the factory used to instantiate 54 // Makes the passed |factory| the factory used to instantiate
56 // a V4UpdateProtocolManager. Useful for tests. 55 // a V4UpdateProtocolManager. Useful for tests.
57 static void RegisterFactory(V4UpdateProtocolManagerFactory* factory) { 56 static void RegisterFactory(V4UpdateProtocolManagerFactory* factory) {
58 factory_ = factory; 57 factory_ = factory;
59 } 58 }
60 59
61 // Create an instance of the safe browsing v4 protocol manager. 60 // Create an instance of the safe browsing v4 protocol manager.
62 static V4UpdateProtocolManager* Create( 61 static scoped_ptr<V4UpdateProtocolManager> Create(
63 net::URLRequestContextGetter* request_context_getter, 62 net::URLRequestContextGetter* request_context_getter,
64 const V4ProtocolConfig& config); 63 const V4ProtocolConfig& config,
64 const base::hash_map<UpdateListIdentifier, std::string>&
65 current_list_states,
66 V4UpdateCallback callback,
67 bool is_headless);
65 68
66 // net::URLFetcherDelegate interface. 69 // net::URLFetcherDelegate interface.
67 void OnURLFetchComplete(const net::URLFetcher* source) override; 70 void OnURLFetchComplete(const net::URLFetcher* source) override;
68 71
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: 72 protected:
84 // Constructs a V4UpdateProtocolManager that issues network requests using 73 // Constructs a V4UpdateProtocolManager that issues network requests using
85 // |request_context_getter|. 74 // |request_context_getter|.
86 V4UpdateProtocolManager(net::URLRequestContextGetter* request_context_getter, 75 // Schedules an update to get the hash prefixes for the lists in
87 const V4ProtocolConfig& config); 76 // |current_list_states|, and invoke |callback| when the results
77 // are retrieved. The callback may be invoked synchronously.
78 V4UpdateProtocolManager(
79 net::URLRequestContextGetter* request_context_getter,
80 const V4ProtocolConfig& config,
81 const base::hash_map<UpdateListIdentifier, std::string>&
82 current_list_states,
83 V4UpdateCallback callback,
84 bool is_headless);
88 85
89 private: 86 private:
90 FRIEND_TEST_ALL_PREFIXES(V4UpdateProtocolManagerTest, 87 FRIEND_TEST_ALL_PREFIXES(V4UpdateProtocolManagerTest,
91 TestGetUpdatesErrorHandlingNetwork); 88 TestGetUpdatesErrorHandlingNetwork);
92 FRIEND_TEST_ALL_PREFIXES(V4UpdateProtocolManagerTest, 89 FRIEND_TEST_ALL_PREFIXES(V4UpdateProtocolManagerTest,
93 TestGetUpdatesErrorHandlingResponseCode); 90 TestGetUpdatesErrorHandlingResponseCode);
94 FRIEND_TEST_ALL_PREFIXES(V4UpdateProtocolManagerTest, TestGetUpdatesNoError); 91 FRIEND_TEST_ALL_PREFIXES(V4UpdateProtocolManagerTest, TestGetUpdatesNoError);
95 friend class V4UpdateProtocolManagerFactoryImpl; 92 friend class V4UpdateProtocolManagerFactoryImpl;
96 93
97 // The method to generate the URL for the request to be sent to the server. 94 // 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 95 // |request_base64| is the base64 encoded form of an instance of the protobuf
99 // FetchThreatListUpdatesRequest. 96 // FetchThreatListUpdatesRequest.
100 GURL GetUpdateUrl(const std::string& request_base64) const; 97 GURL GetUpdateUrl(const std::string& request_base64) const;
101 98
102 // Fills a FetchThreatListUpdatesRequest protocol buffer for a request. 99 // Fills a FetchThreatListUpdatesRequest protocol buffer for a request.
103 // Returns the serialized and base 64 encoded request as a string. 100 // Returns the serialized and base 64 encoded request as a string.
104 std::string GetUpdateRequest( 101 std::string GetUpdateRequest(
105 const base::hash_set<UpdateListIdentifier>& lists_to_update,
106 const base::hash_map<UpdateListIdentifier, std::string>& 102 const base::hash_map<UpdateListIdentifier, std::string>&
107 current_list_states); 103 current_list_states);
108 104
109 // Parses the base64 encoded response received from the server as a 105 // Parses the base64 encoded response received from the server as a
110 // FetchThreatListUpdatesResponse protobuf and returns each of the 106 // FetchThreatListUpdatesResponse protobuf and returns each of the
111 // ListUpdateResponse protobufs contained in it as a vector. 107 // ListUpdateResponse protobufs contained in it as a vector.
112 // Returns true if parsing is successful, false otherwise. 108 // Returns true if parsing is successful, false otherwise.
113 bool ParseUpdateResponse( 109 bool ParseUpdateResponse(
114 const std::string& data_base64, 110 const std::string& data_base64,
115 std::vector<ListUpdateResponse>* list_update_responses); 111 std::vector<ListUpdateResponse>* list_update_responses);
116 112
117 // Resets the update error counter and multiplier. 113 // Resets the update error counter and multiplier.
118 void ResetUpdateErrors(); 114 void ResetUpdateErrors();
119 115
120 // Updates internal update and backoff state for each update response error, 116 // Updates internal update and backoff state for each update response error,
121 // assuming that the current time is |now|. 117 // assuming that the current time is |now|.
122 void HandleUpdateError(const base::Time& now); 118 void HandleUpdateError(const base::Time& now);
123 119
120 // Schedules update callback, if one isn't in progress already.
121 void GetNextUpdate();
Nathan Parker 2016/04/01 01:04:52 ScheduleNextUpdate?
vakh (use Gerrit instead) 2016/04/01 02:23:02 Removed it entirely and scheduling IssueUpdateRequ
122
123 // Generates the URL for the update request and issues the request for the
124 // lists passed to the constructor.
125 void IssueUpdateRequest();
126
127 // Returns whether another update is currently scheduled.
128 bool IsUpdateScheduled() const;
129
130 // Schedule the next update, considering whether we are in backoff.
131 void ScheduleNextUpdate(bool back_off);
132
133 // Schedule the next update, after the given interval.
134 void ScheduleNextUpdateAfterInterval(base::TimeDelta interval);
135
136 // Get the next update interval, considering whether we are in backoff.
137 base::TimeDelta GetNextUpdateInterval(bool back_off);
138
124 // The factory that controls the creation of V4UpdateProtocolManager. 139 // The factory that controls the creation of V4UpdateProtocolManager.
125 // This is used by tests. 140 // This is used by tests.
126 static V4UpdateProtocolManagerFactory* factory_; 141 static V4UpdateProtocolManagerFactory* factory_;
127 142
143 // The last known state of the lists.
144 // At init, this is read from the disk or is empty for no prior state.
145 // Each successful update from the server contains a new state for each
146 // requested list.
147 base::hash_map<UpdateListIdentifier, std::string> current_list_states_;
148
128 // The number of HTTP response errors since the the last successful HTTP 149 // The number of HTTP response errors since the the last successful HTTP
129 // response, used for request backoff timing. 150 // response, used for request backoff timing.
130 size_t update_error_count_; 151 size_t update_error_count_;
131 152
132 // Multiplier for the backoff error after the second. 153 // Multiplier for the backoff error after the second.
133 size_t update_back_off_mult_; 154 size_t update_back_off_mult_;
134 155
135 // The time before which the next update request may not be sent. 156 // The time delta after which the next update request may be sent.
136 // It is set to: 157 // It is set to a random interval between 60 and 300 seconds at start.
137 // the backoff time, if the last response was an error, or 158 // The server can set it by setting the minimum_wait_duration.
138 // the minimum wait time, if the last response was successful. 159 base::TimeDelta next_update_interval_;
Nathan Parker 2016/04/01 01:04:52 Is this the time after the _next_ update?
vakh (use Gerrit instead) 2016/04/01 02:23:02 The minimum time between the current update reques
139 base::Time next_update_time_;
140 160
141 // The config of the client making Pver4 requests. 161 // The config of the client making Pver4 requests.
142 const V4ProtocolConfig config_; 162 const V4ProtocolConfig config_;
143 163
144 // The context we use to issue network requests. 164 // The context we use to issue network requests.
145 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; 165 scoped_refptr<net::URLRequestContextGetter> request_context_getter_;
146 166
147 // ID for URLFetchers for testing. 167 // ID for URLFetchers for testing.
148 int url_fetcher_id_; 168 int url_fetcher_id_;
149 169
150 // True if there's a request pending.
151 bool update_request_pending_;
152
153 // The callback that's called when GetUpdates completes. 170 // The callback that's called when GetUpdates completes.
154 UpdateCallback callback_; 171 V4UpdateCallback callback_;
155 172
156 // The pending update request. The request must be canceled when the object is 173 // The pending update request. The request must be canceled when the object is
157 // destroyed. 174 // destroyed.
158 scoped_ptr<net::URLFetcher> request_; 175 scoped_ptr<net::URLFetcher> request_;
159 176
177 // Timer to setup the next update request.
178 base::OneShotTimer update_timer_;
179
160 DISALLOW_COPY_AND_ASSIGN(V4UpdateProtocolManager); 180 DISALLOW_COPY_AND_ASSIGN(V4UpdateProtocolManager);
161 }; 181 };
162 182
163 // Interface of a factory to create V4UpdateProtocolManager. Useful for tests. 183 // Interface of a factory to create V4UpdateProtocolManager. Useful for tests.
164 class V4UpdateProtocolManagerFactory { 184 class V4UpdateProtocolManagerFactory {
165 public: 185 public:
166 V4UpdateProtocolManagerFactory() {} 186 V4UpdateProtocolManagerFactory() {}
167 virtual ~V4UpdateProtocolManagerFactory() {} 187 virtual ~V4UpdateProtocolManagerFactory() {}
168 virtual V4UpdateProtocolManager* CreateProtocolManager( 188 virtual scoped_ptr<V4UpdateProtocolManager> CreateProtocolManager(
169 net::URLRequestContextGetter* request_context_getter, 189 net::URLRequestContextGetter* request_context_getter,
170 const V4ProtocolConfig& config) = 0; 190 const V4ProtocolConfig& config,
191 const base::hash_map<UpdateListIdentifier, std::string>&
192 current_list_states,
193 V4UpdateCallback callback,
194 bool is_headless) = 0;
171 195
172 private: 196 private:
173 DISALLOW_COPY_AND_ASSIGN(V4UpdateProtocolManagerFactory); 197 DISALLOW_COPY_AND_ASSIGN(V4UpdateProtocolManagerFactory);
174 }; 198 };
175 199
176 } // namespace safe_browsing 200 } // namespace safe_browsing
177 201
178 #endif // COMPONENTS_SAFE_BROWSING_DB_V4_UPDATE_PROTOCOL_MANAGER_H_ 202 #endif // COMPONENTS_SAFE_BROWSING_DB_V4_UPDATE_PROTOCOL_MANAGER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698