OLD | NEW |
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 switch (state) { |
| 33 case DownloadItem::DownloadState::COMPLETE: |
| 34 case DownloadItem::DownloadState::CANCELLED: |
| 35 request_info_index_.erase(index_iter); |
| 36 case DownloadItem::DownloadState::IN_PROGRESS: |
| 37 case DownloadItem::DownloadState::INTERRUPTED: |
| 38 case DownloadItem::DownloadState::MAX_DOWNLOAD_STATE: |
| 39 break; |
| 40 } |
30 | 41 |
31 // Return a boolean indicating whether there are more requests to be | 42 // Return a boolean indicating whether there are more requests to be |
32 // processed. | 43 // processed. |
33 return next_request_info_ != request_infos_.size(); | 44 return next_request_info_ != request_infos_.size(); |
34 } | 45 } |
35 | 46 |
36 const BackgroundFetchRequestInfo& | 47 const BackgroundFetchRequestInfo& |
37 BackgroundFetchJobData::GetNextBackgroundFetchRequestInfo() { | 48 BackgroundFetchJobData::GetNextBackgroundFetchRequestInfo() { |
38 DCHECK(next_request_info_ != request_infos_.size()); | 49 DCHECK(next_request_info_ != request_infos_.size()); |
39 | 50 |
40 const BackgroundFetchRequestInfo& next_request = | 51 const BackgroundFetchRequestInfo& next_request = |
41 request_infos_[next_request_info_]; | 52 request_infos_[next_request_info_]; |
42 DCHECK(!next_request.complete()); | 53 DCHECK_EQ(next_request.state(), |
| 54 DownloadItem::DownloadState::MAX_DOWNLOAD_STATE); |
43 request_info_index_[next_request.guid()] = next_request_info_++; | 55 request_info_index_[next_request.guid()] = next_request_info_++; |
44 | 56 |
45 return next_request; | 57 return next_request; |
46 } | 58 } |
47 | 59 |
48 bool BackgroundFetchJobData::IsComplete() const { | 60 bool BackgroundFetchJobData::IsComplete() const { |
49 return ((next_request_info_ == request_infos_.size()) && | 61 return ((next_request_info_ == request_infos_.size()) && |
50 request_info_index_.empty()); | 62 request_info_index_.empty()); |
51 } | 63 } |
52 | 64 |
53 bool BackgroundFetchJobData::HasRequestsRemaining() const { | 65 bool BackgroundFetchJobData::HasRequestsRemaining() const { |
54 return next_request_info_ != request_infos_.size(); | 66 return next_request_info_ != request_infos_.size(); |
55 } | 67 } |
56 | 68 |
57 void BackgroundFetchJobData::SetRequestDownloadGuid( | 69 void BackgroundFetchJobData::SetRequestDownloadGuid( |
58 const std::string& request_guid, | 70 const std::string& request_guid, |
59 const std::string& download_guid) { | 71 const std::string& download_guid) { |
60 auto index_iter = request_info_index_.find(request_guid); | 72 auto index_iter = request_info_index_.find(request_guid); |
61 DCHECK(index_iter != request_info_index_.end()); | 73 DCHECK(index_iter != request_info_index_.end()); |
62 request_infos_[index_iter->second].set_download_guid(download_guid); | 74 request_infos_[index_iter->second].set_download_guid(download_guid); |
63 } | 75 } |
64 } // namespace content | 76 } // namespace content |
OLD | NEW |