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_data_manager.h" | 5 #include "content/browser/background_fetch/background_fetch_data_manager.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_context.h" | 8 #include "content/browser/background_fetch/background_fetch_context.h" |
9 #include "content/browser/background_fetch/background_fetch_job_response_data.h" | 9 #include "content/browser/background_fetch/background_fetch_job_response_data.h" |
10 #include "content/browser/background_fetch/background_fetch_request_info.h" | 10 #include "content/browser/background_fetch/background_fetch_request_info.h" |
11 #include "content/browser/blob_storage/chrome_blob_storage_context.h" | |
11 #include "content/public/browser/blob_handle.h" | 12 #include "content/public/browser/blob_handle.h" |
12 #include "content/public/browser/browser_context.h" | 13 #include "content/public/browser/browser_context.h" |
13 #include "content/public/browser/download_interrupt_reasons.h" | 14 #include "content/public/browser/download_interrupt_reasons.h" |
14 #include "content/public/browser/download_item.h" | 15 #include "content/public/browser/download_item.h" |
15 | 16 |
16 namespace content { | 17 namespace content { |
17 | 18 |
18 BackgroundFetchDataManager::BackgroundFetchDataManager( | 19 BackgroundFetchDataManager::BackgroundFetchDataManager( |
19 BrowserContext* browser_context) | 20 BrowserContext* browser_context) |
20 : browser_context_(browser_context), weak_ptr_factory_(this) { | 21 : browser_context_(browser_context), weak_ptr_factory_(this) { |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
90 const std::vector<BackgroundFetchRequestInfo>& request_infos = iter->second; | 91 const std::vector<BackgroundFetchRequestInfo>& request_infos = iter->second; |
91 | 92 |
92 DCHECK(request_index <= request_infos.size()); | 93 DCHECK(request_index <= request_infos.size()); |
93 return base::MakeUnique<BackgroundFetchRequestInfo>( | 94 return base::MakeUnique<BackgroundFetchRequestInfo>( |
94 request_infos[request_index]); | 95 request_infos[request_index]); |
95 } | 96 } |
96 | 97 |
97 void BackgroundFetchDataManager::GetJobResponse( | 98 void BackgroundFetchDataManager::GetJobResponse( |
98 const std::string& job_guid, | 99 const std::string& job_guid, |
99 const BackgroundFetchResponseCompleteCallback& callback) { | 100 const BackgroundFetchResponseCompleteCallback& callback) { |
101 BrowserThread::PostTaskAndReplyWithResult( | |
102 BrowserThread::IO, FROM_HERE, | |
Peter Beverloo
2017/03/27 11:05:16
BrowserThread::UI
This class lives on the IO thre
harkness
2017/03/27 12:23:19
According to the comments, only the constructor sh
harkness
2017/03/27 12:35:32
Or maybe the comments are wrong! :)
| |
103 base::Bind(&ChromeBlobStorageContext::GetFor, browser_context_), | |
104 base::Bind(&BackgroundFetchDataManager::DidGetBlobStorageContext, | |
105 weak_ptr_factory_.GetWeakPtr(), job_guid, callback)); | |
106 } | |
107 | |
108 void BackgroundFetchDataManager::DidGetBlobStorageContext( | |
109 const std::string& job_guid, | |
110 const BackgroundFetchResponseCompleteCallback& callback, | |
111 ChromeBlobStorageContext* blob_context) { | |
Peter Beverloo
2017/03/27 11:05:16
DCHECK(blob_context);
harkness
2017/03/27 12:23:19
Done.
| |
100 BackgroundFetchJobInfo* job_info = job_map_[job_guid].get(); | 112 BackgroundFetchJobInfo* job_info = job_map_[job_guid].get(); |
101 DCHECK(job_info); | 113 DCHECK(job_info); |
102 | 114 |
103 // Create a BackgroundFetchJobResponseData object which will aggregate | 115 // Create a BackgroundFetchJobResponseData object which will aggregate |
104 // together the response blobs. | 116 // together the response blobs. |
105 job_info->set_job_response_data( | 117 job_info->set_job_response_data( |
106 base::MakeUnique<BackgroundFetchJobResponseData>(job_info->num_requests(), | 118 base::MakeUnique<BackgroundFetchJobResponseData>(job_info->num_requests(), |
107 callback)); | 119 callback)); |
120 BackgroundFetchJobResponseData* job_response_data = | |
121 job_info->job_response_data(); | |
122 DCHECK(job_response_data); | |
108 | 123 |
109 // Iterate over the requests and create blobs for each response. | 124 // Iterate over the requests and create blobs for each response. |
110 for (size_t request_index = 0; request_index < job_info->num_requests(); | 125 for (size_t request_index = 0; request_index < job_info->num_requests(); |
111 request_index++) { | 126 request_index++) { |
112 // TODO(harkness): This will need to be asynchronous. | 127 // TODO(harkness): This will need to be asynchronous. |
113 std::unique_ptr<BackgroundFetchRequestInfo> request_info = | 128 std::unique_ptr<BackgroundFetchRequestInfo> request_info = |
114 GetRequestInfo(job_guid, request_index); | 129 GetRequestInfo(job_guid, request_index); |
115 | 130 |
116 // TODO(harkness): Only create a blob response if the request was | 131 // TODO(harkness): Only create a blob response if the request was |
117 // successful. Otherwise create an error response. | 132 // successful. Otherwise create an error response. |
118 content::BrowserContext::CreateFileBackedBlob( | 133 std::unique_ptr<BlobHandle> blob_handle = |
119 browser_context_, request_info->file_path(), 0 /* offset */, | 134 blob_context->CreateFileBackedBlob( |
120 request_info->received_bytes(), | 135 request_info->file_path(), 0 /* offset */, |
121 base::Time() /* expected_modification_time */, | 136 request_info->received_bytes(), |
122 base::Bind(&BackgroundFetchDataManager::DidGetRequestResponse, | 137 base::Time() /* expected_modification_time */); |
123 weak_ptr_factory_.GetWeakPtr(), job_guid, request_index)); | 138 |
139 job_response_data->AddResponse(*request_info.get(), std::move(blob_handle)); | |
124 } | 140 } |
125 } | 141 } |
126 | 142 |
127 void BackgroundFetchDataManager::DidGetRequestResponse( | |
128 const std::string& job_guid, | |
129 int request_sequence_number, | |
130 std::unique_ptr<BlobHandle> blob_handle) { | |
131 BackgroundFetchJobInfo* job_info = job_map_[job_guid].get(); | |
132 DCHECK(job_info); | |
133 | |
134 BackgroundFetchJobResponseData* job_response_data = | |
135 job_info->job_response_data(); | |
136 DCHECK(job_response_data); | |
137 | |
138 job_response_data->AddResponse(request_sequence_number, | |
139 std::move(blob_handle)); | |
140 } | |
141 | |
142 bool BackgroundFetchDataManager::UpdateRequestState( | 143 bool BackgroundFetchDataManager::UpdateRequestState( |
143 const std::string& job_guid, | 144 const std::string& job_guid, |
144 const std::string& request_guid, | 145 const std::string& request_guid, |
145 DownloadItem::DownloadState state, | 146 DownloadItem::DownloadState state, |
146 DownloadInterruptReason interrupt_reason) { | 147 DownloadInterruptReason interrupt_reason) { |
147 // Find the request and set the state and the interrupt reason. | 148 // Find the request and set the state and the interrupt reason. |
148 BackgroundFetchJobInfo* job_info = job_map_[job_guid].get(); | 149 BackgroundFetchJobInfo* job_info = job_map_[job_guid].get(); |
149 DCHECK(job_info); | 150 DCHECK(job_info); |
150 BackgroundFetchRequestInfo* request = | 151 BackgroundFetchRequestInfo* request = |
151 job_info->GetActiveRequest(request_guid); | 152 job_info->GetActiveRequest(request_guid); |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
218 const std::string& download_guid) { | 219 const std::string& download_guid) { |
219 BackgroundFetchJobInfo* job_info = job_map_[job_guid].get(); | 220 BackgroundFetchJobInfo* job_info = job_map_[job_guid].get(); |
220 DCHECK(job_info); | 221 DCHECK(job_info); |
221 BackgroundFetchRequestInfo* request = | 222 BackgroundFetchRequestInfo* request = |
222 job_info->GetActiveRequest(request_guid); | 223 job_info->GetActiveRequest(request_guid); |
223 DCHECK(request); | 224 DCHECK(request); |
224 request->set_download_guid(download_guid); | 225 request->set_download_guid(download_guid); |
225 } | 226 } |
226 | 227 |
227 } // namespace content | 228 } // namespace content |
OLD | NEW |