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

Side by Side Diff: content/browser/background_fetch/background_fetch_job_data.cc

Issue 2753583002: Add the JobComplete callback and error/interrupt information (Closed)
Patch Set: Converted callback to OnceClosure and fixed nits. Created 3 years, 9 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 2017 The Chromium Authors. All rights reserved. 1 // Copyright 2017 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 "content/browser/background_fetch/background_fetch_job_data.h" 5 #include "content/browser/background_fetch/background_fetch_job_data.h"
6 6
7 #include "base/memory/ptr_util.h" 7 #include "base/memory/ptr_util.h"
8 #include "content/browser/background_fetch/background_fetch_job_info.h" 8 #include "content/browser/background_fetch/background_fetch_job_info.h"
9 9
10 namespace content { 10 namespace content {
11 11
12 BackgroundFetchJobData::BackgroundFetchJobData( 12 BackgroundFetchJobData::BackgroundFetchJobData(
13 BackgroundFetchRequestInfos& request_infos) 13 BackgroundFetchRequestInfos& request_infos)
14 : request_infos_(request_infos) {} 14 : request_infos_(request_infos) {}
15 15
16 BackgroundFetchJobData::~BackgroundFetchJobData() {} 16 BackgroundFetchJobData::~BackgroundFetchJobData() {}
17 17
18 // TODO(harkness): Provide more detail about status and where the returned data 18 bool BackgroundFetchJobData::UpdateBackgroundFetchRequestState(
19 // is now available. 19 const std::string& fetch_guid,
20 bool BackgroundFetchJobData::BackgroundFetchRequestInfoComplete( 20 DownloadItem::DownloadState state,
21 const std::string& fetch_guid) { 21 DownloadInterruptReason interrupt_reason) {
22 // Make sure that the request was expected to be in-progress. 22 // Make sure that the request was expected to be in-progress.
23 auto index_iter = request_info_index_.find(fetch_guid); 23 auto index_iter = request_info_index_.find(fetch_guid);
24 DCHECK(index_iter != request_info_index_.end()); 24 DCHECK(index_iter != request_info_index_.end());
25 DCHECK_EQ(fetch_guid, request_infos_[index_iter->second].guid()); 25 DCHECK_EQ(fetch_guid, request_infos_[index_iter->second].guid());
26 26
27 // Set the request as complete and delete it from the in-progress index. 27 // Set the state of the request and the interrupt reason.
28 request_infos_[index_iter->second].set_complete(true); 28 request_infos_[index_iter->second].set_state(state);
29 request_info_index_.erase(index_iter); 29 request_infos_[index_iter->second].set_interrupt_reason(interrupt_reason);
30
31 // If the new state is complete or cancelled, remove the in-progress request.
32 if ((state == DownloadItem::DownloadState::COMPLETE) ||
33 (state == DownloadItem::DownloadState::CANCELLED)) {
Peter Beverloo 2017/03/16 12:37:10 Could we have a switch() w/o a default: state to p
harkness 2017/03/17 11:14:11 Done.
34 request_info_index_.erase(index_iter);
35 }
30 36
31 // Return a boolean indicating whether there are more requests to be 37 // Return a boolean indicating whether there are more requests to be
32 // processed. 38 // processed.
33 return next_request_info_ != request_infos_.size(); 39 return next_request_info_ != request_infos_.size();
34 } 40 }
35 41
36 const BackgroundFetchRequestInfo& 42 const BackgroundFetchRequestInfo&
37 BackgroundFetchJobData::GetNextBackgroundFetchRequestInfo() { 43 BackgroundFetchJobData::GetNextBackgroundFetchRequestInfo() {
38 DCHECK(next_request_info_ != request_infos_.size()); 44 DCHECK(next_request_info_ != request_infos_.size());
39 45
40 const BackgroundFetchRequestInfo& next_request = 46 const BackgroundFetchRequestInfo& next_request =
41 request_infos_[next_request_info_]; 47 request_infos_[next_request_info_];
42 DCHECK(!next_request.complete()); 48 DCHECK_EQ(next_request.state(),
49 DownloadItem::DownloadState::MAX_DOWNLOAD_STATE);
43 request_info_index_[next_request.guid()] = next_request_info_++; 50 request_info_index_[next_request.guid()] = next_request_info_++;
44 51
45 return next_request; 52 return next_request;
46 } 53 }
47 54
48 bool BackgroundFetchJobData::IsComplete() const { 55 bool BackgroundFetchJobData::IsComplete() const {
49 return ((next_request_info_ == request_infos_.size()) && 56 return ((next_request_info_ == request_infos_.size()) &&
50 request_info_index_.empty()); 57 request_info_index_.empty());
51 } 58 }
52 59
53 bool BackgroundFetchJobData::HasRequestsRemaining() const { 60 bool BackgroundFetchJobData::HasRequestsRemaining() const {
54 return next_request_info_ != request_infos_.size(); 61 return next_request_info_ != request_infos_.size();
55 } 62 }
56 63
57 void BackgroundFetchJobData::SetRequestDownloadGuid( 64 void BackgroundFetchJobData::SetRequestDownloadGuid(
58 const std::string& request_guid, 65 const std::string& request_guid,
59 const std::string& download_guid) { 66 const std::string& download_guid) {
60 auto index_iter = request_info_index_.find(request_guid); 67 auto index_iter = request_info_index_.find(request_guid);
61 DCHECK(index_iter != request_info_index_.end()); 68 DCHECK(index_iter != request_info_index_.end());
62 request_infos_[index_iter->second].set_download_guid(download_guid); 69 request_infos_[index_iter->second].set_download_guid(download_guid);
63 } 70 }
64 } // namespace content 71 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698