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 #include "components/safe_browsing_db/v4_update_protocol_manager.h" | 5 #include "components/safe_browsing_db/v4_update_protocol_manager.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/base64.h" | 9 #include "base/base64url.h" |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
11 #include "base/metrics/histogram_macros.h" | 11 #include "base/metrics/histogram_macros.h" |
12 #include "base/rand_util.h" | 12 #include "base/rand_util.h" |
13 #include "base/timer/timer.h" | 13 #include "base/timer/timer.h" |
14 #include "components/safe_browsing_db/safebrowsing.pb.h" | 14 #include "components/safe_browsing_db/safebrowsing.pb.h" |
15 #include "net/base/load_flags.h" | 15 #include "net/base/load_flags.h" |
16 #include "net/http/http_response_headers.h" | 16 #include "net/http/http_response_headers.h" |
17 #include "net/http/http_status_code.h" | 17 #include "net/http/http_status_code.h" |
18 #include "net/url_request/url_fetcher.h" | 18 #include "net/url_request/url_fetcher.h" |
19 #include "net/url_request/url_request_context_getter.h" | 19 #include "net/url_request/url_request_context_getter.h" |
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
184 base::TimeDelta interval) { | 184 base::TimeDelta interval) { |
185 DCHECK(CalledOnValidThread()); | 185 DCHECK(CalledOnValidThread()); |
186 DCHECK(interval >= base::TimeDelta()); | 186 DCHECK(interval >= base::TimeDelta()); |
187 | 187 |
188 // Unschedule any current timer. | 188 // Unschedule any current timer. |
189 update_timer_.Stop(); | 189 update_timer_.Stop(); |
190 update_timer_.Start(FROM_HERE, interval, this, | 190 update_timer_.Start(FROM_HERE, interval, this, |
191 &V4UpdateProtocolManager::IssueUpdateRequest); | 191 &V4UpdateProtocolManager::IssueUpdateRequest); |
192 } | 192 } |
193 | 193 |
194 // static | 194 // static |
Nathan Parker
2016/06/22 17:24:05
nit: I feel like the longer name doesn't add much
vakh (use Gerrit instead)
2016/06/22 20:57:02
Done.
| |
195 std::string V4UpdateProtocolManager::GetBase64SerializedUpdateRequestProto( | 195 std::string V4UpdateProtocolManager::GetWebBase64SerializedUpdateRequestProto( |
196 const StoreStateMap* store_state_map) { | 196 const StoreStateMap* store_state_map) { |
197 // Build the request. Client info and client states are not added to the | 197 // Build the request. Client info and client states are not added to the |
198 // request protocol buffer. Client info is passed as params in the url. | 198 // request protocol buffer. Client info is passed as params in the url. |
199 FetchThreatListUpdatesRequest request; | 199 FetchThreatListUpdatesRequest request; |
200 for (const auto& entry : *store_state_map) { | 200 for (const auto& entry : *store_state_map) { |
201 const auto& list_to_update = entry.first; | 201 const auto& list_to_update = entry.first; |
202 const auto& state = entry.second; | 202 const auto& state = entry.second; |
203 ListUpdateRequest* list_update_request = request.add_list_update_requests(); | 203 ListUpdateRequest* list_update_request = request.add_list_update_requests(); |
204 list_update_request->set_platform_type(list_to_update.platform_type); | 204 list_update_request->set_platform_type(list_to_update.platform_type); |
205 list_update_request->set_threat_entry_type( | 205 list_update_request->set_threat_entry_type( |
206 list_to_update.threat_entry_type); | 206 list_to_update.threat_entry_type); |
207 list_update_request->set_threat_type(list_to_update.threat_type); | 207 list_update_request->set_threat_type(list_to_update.threat_type); |
208 | 208 |
209 if (!state.empty()) { | 209 if (!state.empty()) { |
210 list_update_request->set_state(state); | 210 list_update_request->set_state(state); |
211 } | 211 } |
212 } | 212 } |
213 | 213 |
214 // Serialize and Base64 encode. | 214 // Serialize and Base64 encode. |
215 std::string req_data, req_base64; | 215 std::string req_data, req_base64; |
216 request.SerializeToString(&req_data); | 216 request.SerializeToString(&req_data); |
217 base::Base64Encode(req_data, &req_base64); | 217 base::Base64UrlEncode(req_data, base::Base64UrlEncodePolicy::INCLUDE_PADDING, |
Nathan Parker
2016/06/22 17:24:05
Does this affect any test? Should it?
vakh (use Gerrit instead)
2016/06/22 20:57:03
Done.
Added a test for the '-' and for the '_' cas
| |
218 | 218 &req_base64); |
219 return req_base64; | 219 return req_base64; |
220 } | 220 } |
221 | 221 |
222 bool V4UpdateProtocolManager::ParseUpdateResponse( | 222 bool V4UpdateProtocolManager::ParseUpdateResponse( |
223 const std::string& data, | 223 const std::string& data, |
224 std::vector<ListUpdateResponse>* list_update_responses) { | 224 std::vector<ListUpdateResponse>* list_update_responses) { |
225 FetchThreatListUpdatesResponse response; | 225 FetchThreatListUpdatesResponse response; |
226 | 226 |
227 if (!response.ParseFromString(data)) { | 227 if (!response.ParseFromString(data)) { |
228 RecordParseUpdateResult(PARSE_FROM_STRING_ERROR); | 228 RecordParseUpdateResult(PARSE_FROM_STRING_ERROR); |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
263 void V4UpdateProtocolManager::IssueUpdateRequest() { | 263 void V4UpdateProtocolManager::IssueUpdateRequest() { |
264 DCHECK(CalledOnValidThread()); | 264 DCHECK(CalledOnValidThread()); |
265 | 265 |
266 // If an update request is already pending, record and return silently. | 266 // If an update request is already pending, record and return silently. |
267 if (request_.get()) { | 267 if (request_.get()) { |
268 RecordUpdateResult(V4OperationResult::ALREADY_PENDING_ERROR); | 268 RecordUpdateResult(V4OperationResult::ALREADY_PENDING_ERROR); |
269 return; | 269 return; |
270 } | 270 } |
271 | 271 |
272 std::string req_base64 = | 272 std::string req_base64 = |
273 GetBase64SerializedUpdateRequestProto(store_state_map_.get()); | 273 GetWebBase64SerializedUpdateRequestProto(store_state_map_.get()); |
274 GURL update_url; | 274 GURL update_url; |
275 net::HttpRequestHeaders headers; | 275 net::HttpRequestHeaders headers; |
276 GetUpdateUrlAndHeaders(req_base64, &update_url, &headers); | 276 GetUpdateUrlAndHeaders(req_base64, &update_url, &headers); |
277 | 277 |
278 std::unique_ptr<net::URLFetcher> fetcher = net::URLFetcher::Create( | 278 std::unique_ptr<net::URLFetcher> fetcher = net::URLFetcher::Create( |
279 url_fetcher_id_++, update_url, net::URLFetcher::GET, this); | 279 url_fetcher_id_++, update_url, net::URLFetcher::GET, this); |
280 fetcher->SetExtraRequestHeaders(headers.ToString()); | 280 fetcher->SetExtraRequestHeaders(headers.ToString()); |
281 | 281 |
282 request_.reset(fetcher.release()); | 282 request_.reset(fetcher.release()); |
283 | 283 |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
341 | 341 |
342 void V4UpdateProtocolManager::GetUpdateUrlAndHeaders( | 342 void V4UpdateProtocolManager::GetUpdateUrlAndHeaders( |
343 const std::string& req_base64, | 343 const std::string& req_base64, |
344 GURL* gurl, | 344 GURL* gurl, |
345 net::HttpRequestHeaders* headers) const { | 345 net::HttpRequestHeaders* headers) const { |
346 V4ProtocolManagerUtil::GetRequestUrlAndHeaders( | 346 V4ProtocolManagerUtil::GetRequestUrlAndHeaders( |
347 req_base64, "threatListUpdates:fetch", config_, gurl, headers); | 347 req_base64, "threatListUpdates:fetch", config_, gurl, headers); |
348 } | 348 } |
349 | 349 |
350 } // namespace safe_browsing | 350 } // namespace safe_browsing |
OLD | NEW |