OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "content/browser/background_fetch/background_fetch_job_data.h" | |
6 | |
7 #include "base/memory/ptr_util.h" | |
8 #include "content/browser/background_fetch/background_fetch_job_info.h" | |
9 | |
10 namespace content { | |
11 | |
12 BackgroundFetchJobData::BackgroundFetchJobData( | |
13 BackgroundFetchRequestInfos& request_infos) | |
14 : request_infos_(request_infos) {} | |
15 | |
16 BackgroundFetchJobData::~BackgroundFetchJobData() {} | |
17 | |
18 bool BackgroundFetchJobData::UpdateBackgroundFetchRequestState( | |
19 const std::string& fetch_guid, | |
20 DownloadItem::DownloadState state, | |
21 DownloadInterruptReason interrupt_reason) { | |
22 // Make sure that the request was expected to be in-progress. | |
23 auto index_iter = request_info_index_.find(fetch_guid); | |
24 DCHECK(index_iter != request_info_index_.end()); | |
25 DCHECK_EQ(fetch_guid, request_infos_[index_iter->second].guid()); | |
26 | |
27 // Set the state of the request and the interrupt reason. | |
28 request_infos_[index_iter->second].set_state(state); | |
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 } | |
41 | |
42 // Return a boolean indicating whether there are more requests to be | |
43 // processed. | |
44 return next_request_info_ != request_infos_.size(); | |
45 } | |
46 | |
47 const BackgroundFetchRequestInfo& | |
48 BackgroundFetchJobData::GetNextBackgroundFetchRequestInfo() { | |
49 DCHECK(next_request_info_ != request_infos_.size()); | |
50 | |
51 const BackgroundFetchRequestInfo& next_request = | |
52 request_infos_[next_request_info_]; | |
53 DCHECK_EQ(next_request.state(), | |
54 DownloadItem::DownloadState::MAX_DOWNLOAD_STATE); | |
55 request_info_index_[next_request.guid()] = next_request_info_++; | |
56 | |
57 return next_request; | |
58 } | |
59 | |
60 bool BackgroundFetchJobData::IsComplete() const { | |
61 return ((next_request_info_ == request_infos_.size()) && | |
62 request_info_index_.empty()); | |
63 } | |
64 | |
65 bool BackgroundFetchJobData::HasRequestsRemaining() const { | |
66 return next_request_info_ != request_infos_.size(); | |
67 } | |
68 | |
69 void BackgroundFetchJobData::SetRequestDownloadGuid( | |
70 const std::string& request_guid, | |
71 const std::string& download_guid) { | |
72 auto index_iter = request_info_index_.find(request_guid); | |
73 DCHECK(index_iter != request_info_index_.end()); | |
74 request_infos_[index_iter->second].set_download_guid(download_guid); | |
75 } | |
76 } // namespace content | |
OLD | NEW |