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

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: git fetch && git pull && gclient sync 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);
65 67
66 // net::URLFetcherDelegate interface. 68 // net::URLFetcherDelegate interface.
67 void OnURLFetchComplete(const net::URLFetcher* source) override; 69 void OnURLFetchComplete(const net::URLFetcher* source) override;
68 70
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: 71 protected:
84 // Constructs a V4UpdateProtocolManager that issues network requests using 72 // Constructs a V4UpdateProtocolManager that issues network requests using
85 // |request_context_getter|. 73 // |request_context_getter|.
86 V4UpdateProtocolManager(net::URLRequestContextGetter* request_context_getter, 74 // Schedules an update to get the hash prefixes for the lists in
87 const V4ProtocolConfig& config); 75 // |current_list_states|, and invoke |callback| when the results
76 // are retrieved. The callback may be invoked synchronously.
77 V4UpdateProtocolManager(
78 net::URLRequestContextGetter* request_context_getter,
79 const V4ProtocolConfig& config,
80 const base::hash_map<UpdateListIdentifier, std::string>&
81 current_list_states,
82 V4UpdateCallback callback);
88 83
89 private: 84 private:
90 FRIEND_TEST_ALL_PREFIXES(V4UpdateProtocolManagerTest, 85 FRIEND_TEST_ALL_PREFIXES(V4UpdateProtocolManagerTest,
91 TestGetUpdatesErrorHandlingNetwork); 86 TestGetUpdatesErrorHandlingNetwork);
92 FRIEND_TEST_ALL_PREFIXES(V4UpdateProtocolManagerTest, 87 FRIEND_TEST_ALL_PREFIXES(V4UpdateProtocolManagerTest,
93 TestGetUpdatesErrorHandlingResponseCode); 88 TestGetUpdatesErrorHandlingResponseCode);
94 FRIEND_TEST_ALL_PREFIXES(V4UpdateProtocolManagerTest, TestGetUpdatesNoError); 89 FRIEND_TEST_ALL_PREFIXES(V4UpdateProtocolManagerTest, TestGetUpdatesNoError);
95 friend class V4UpdateProtocolManagerFactoryImpl; 90 friend class V4UpdateProtocolManagerFactoryImpl;
96 91
97 // The method to generate the URL for the request to be sent to the server. 92 // 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 93 // |request_base64| is the base64 encoded form of an instance of the protobuf
99 // FetchThreatListUpdatesRequest. 94 // FetchThreatListUpdatesRequest.
100 GURL GetUpdateUrl(const std::string& request_base64) const; 95 GURL GetUpdateUrl(const std::string& request_base64) const;
101 96
102 // Fills a FetchThreatListUpdatesRequest protocol buffer for a request. 97 // Fills a FetchThreatListUpdatesRequest protocol buffer for a request.
103 // Returns the serialized and base 64 encoded request as a string. 98 // Returns the serialized and base 64 encoded request as a string.
104 std::string GetUpdateRequest( 99 std::string GetBase64SerializedUpdateRequestProto(
105 const base::hash_set<UpdateListIdentifier>& lists_to_update,
106 const base::hash_map<UpdateListIdentifier, std::string>& 100 const base::hash_map<UpdateListIdentifier, std::string>&
107 current_list_states); 101 current_list_states);
108 102
109 // Parses the base64 encoded response received from the server as a 103 // Parses the base64 encoded response received from the server as a
110 // FetchThreatListUpdatesResponse protobuf and returns each of the 104 // FetchThreatListUpdatesResponse protobuf and returns each of the
111 // ListUpdateResponse protobufs contained in it as a vector. 105 // ListUpdateResponse protobufs contained in it as a vector.
112 // Returns true if parsing is successful, false otherwise. 106 // Returns true if parsing is successful, false otherwise.
113 bool ParseUpdateResponse( 107 bool ParseUpdateResponse(
114 const std::string& data_base64, 108 const std::string& data_base64,
115 std::vector<ListUpdateResponse>* list_update_responses); 109 std::vector<ListUpdateResponse>* list_update_responses);
116 110
117 // Resets the update error counter and multiplier. 111 // Resets the update error counter and multiplier.
118 void ResetUpdateErrors(); 112 void ResetUpdateErrors();
119 113
120 // Updates internal update and backoff state for each update response error, 114 // Updates internal update and backoff state for each update response error,
121 // assuming that the current time is |now|. 115 // assuming that the current time is |now|.
122 void HandleUpdateError(const base::Time& now); 116 void HandleUpdateError(const base::Time& now);
123 117
118 // Generates the URL for the update request and issues the request for the
119 // lists passed to the constructor.
120 void IssueUpdateRequest();
121
122 // Returns whether another update is currently scheduled.
123 bool IsUpdateScheduled() const;
124
125 // Schedule the next update, considering whether we are in backoff.
126 void ScheduleNextUpdate(bool back_off);
127
128 // Schedule the next update, after the given interval.
129 void ScheduleNextUpdateAfterInterval(base::TimeDelta interval);
130
131 // Get the next update interval, considering whether we are in backoff.
132 base::TimeDelta GetNextUpdateInterval(bool back_off);
133
124 // The factory that controls the creation of V4UpdateProtocolManager. 134 // The factory that controls the creation of V4UpdateProtocolManager.
125 // This is used by tests. 135 // This is used by tests.
126 static V4UpdateProtocolManagerFactory* factory_; 136 static V4UpdateProtocolManagerFactory* factory_;
127 137
138 // The last known state of the lists.
139 // At init, this is read from the disk or is empty for no prior state.
140 // Each successful update from the server contains a new state for each
141 // requested list.
142 base::hash_map<UpdateListIdentifier, std::string> current_list_states_;
143
128 // The number of HTTP response errors since the the last successful HTTP 144 // The number of HTTP response errors since the the last successful HTTP
129 // response, used for request backoff timing. 145 // response, used for request backoff timing.
130 size_t update_error_count_; 146 size_t update_error_count_;
131 147
132 // Multiplier for the backoff error after the second. 148 // Multiplier for the backoff error after the second.
133 size_t update_back_off_mult_; 149 size_t update_back_off_mult_;
134 150
135 // The time before which the next update request may not be sent. 151 // The time delta after which the next update request may be sent.
136 // It is set to: 152 // 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 153 // The server can set it by setting the minimum_wait_duration.
138 // the minimum wait time, if the last response was successful. 154 base::TimeDelta next_update_interval_;
139 base::Time next_update_time_;
140 155
141 // The config of the client making Pver4 requests. 156 // The config of the client making Pver4 requests.
142 const V4ProtocolConfig config_; 157 const V4ProtocolConfig config_;
143 158
144 // The context we use to issue network requests. 159 // The context we use to issue network requests.
145 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; 160 scoped_refptr<net::URLRequestContextGetter> request_context_getter_;
146 161
147 // ID for URLFetchers for testing. 162 // ID for URLFetchers for testing.
148 int url_fetcher_id_; 163 int url_fetcher_id_;
149 164
150 // True if there's a request pending.
151 bool update_request_pending_;
152
153 // The callback that's called when GetUpdates completes. 165 // The callback that's called when GetUpdates completes.
154 UpdateCallback callback_; 166 V4UpdateCallback callback_;
155 167
156 // The pending update request. The request must be canceled when the object is 168 // The pending update request. The request must be canceled when the object is
157 // destroyed. 169 // destroyed.
158 scoped_ptr<net::URLFetcher> request_; 170 scoped_ptr<net::URLFetcher> request_;
159 171
172 // Timer to setup the next update request.
173 base::OneShotTimer update_timer_;
174
160 DISALLOW_COPY_AND_ASSIGN(V4UpdateProtocolManager); 175 DISALLOW_COPY_AND_ASSIGN(V4UpdateProtocolManager);
161 }; 176 };
162 177
163 // Interface of a factory to create V4UpdateProtocolManager. Useful for tests. 178 // Interface of a factory to create V4UpdateProtocolManager. Useful for tests.
164 class V4UpdateProtocolManagerFactory { 179 class V4UpdateProtocolManagerFactory {
165 public: 180 public:
166 V4UpdateProtocolManagerFactory() {} 181 V4UpdateProtocolManagerFactory() {}
167 virtual ~V4UpdateProtocolManagerFactory() {} 182 virtual ~V4UpdateProtocolManagerFactory() {}
168 virtual V4UpdateProtocolManager* CreateProtocolManager( 183 virtual scoped_ptr<V4UpdateProtocolManager> CreateProtocolManager(
169 net::URLRequestContextGetter* request_context_getter, 184 net::URLRequestContextGetter* request_context_getter,
170 const V4ProtocolConfig& config) = 0; 185 const V4ProtocolConfig& config,
186 const base::hash_map<UpdateListIdentifier, std::string>&
187 current_list_states,
188 V4UpdateCallback callback) = 0;
171 189
172 private: 190 private:
173 DISALLOW_COPY_AND_ASSIGN(V4UpdateProtocolManagerFactory); 191 DISALLOW_COPY_AND_ASSIGN(V4UpdateProtocolManagerFactory);
174 }; 192 };
175 193
176 } // namespace safe_browsing 194 } // namespace safe_browsing
177 195
178 #endif // COMPONENTS_SAFE_BROWSING_DB_V4_UPDATE_PROTOCOL_MANAGER_H_ 196 #endif // COMPONENTS_SAFE_BROWSING_DB_V4_UPDATE_PROTOCOL_MANAGER_H_
OLDNEW
« no previous file with comments | « components/safe_browsing_db/v4_protocol_manager_util.cc ('k') | components/safe_browsing_db/v4_update_protocol_manager.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698