| 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_context.h" | 5 #include "content/browser/background_fetch/background_fetch_context.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_data_manager.h" | 8 #include "content/browser/background_fetch/background_fetch_data_manager.h" |
| 9 #include "content/browser/background_fetch/background_fetch_event_dispatcher.h" | 9 #include "content/browser/background_fetch/background_fetch_event_dispatcher.h" |
| 10 #include "content/browser/background_fetch/background_fetch_job_controller.h" | 10 #include "content/browser/background_fetch/background_fetch_job_controller.h" |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 69 registration_id, requests, options, | 69 registration_id, requests, options, |
| 70 base::BindOnce(&BackgroundFetchContext::DidCreateRegistration, | 70 base::BindOnce(&BackgroundFetchContext::DidCreateRegistration, |
| 71 weak_factory_.GetWeakPtr(), registration_id, options, | 71 weak_factory_.GetWeakPtr(), registration_id, options, |
| 72 std::move(callback))); | 72 std::move(callback))); |
| 73 } | 73 } |
| 74 | 74 |
| 75 void BackgroundFetchContext::DidCreateRegistration( | 75 void BackgroundFetchContext::DidCreateRegistration( |
| 76 const BackgroundFetchRegistrationId& registration_id, | 76 const BackgroundFetchRegistrationId& registration_id, |
| 77 const BackgroundFetchOptions& options, | 77 const BackgroundFetchOptions& options, |
| 78 blink::mojom::BackgroundFetchService::FetchCallback callback, | 78 blink::mojom::BackgroundFetchService::FetchCallback callback, |
| 79 blink::mojom::BackgroundFetchError error, | 79 blink::mojom::BackgroundFetchError error) { |
| 80 std::vector<scoped_refptr<BackgroundFetchRequestInfo>> initial_requests) { | |
| 81 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 80 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 82 | 81 |
| 83 RecordRegistrationCreatedError(error); | 82 RecordRegistrationCreatedError(error); |
| 84 if (error != blink::mojom::BackgroundFetchError::NONE) { | 83 if (error != blink::mojom::BackgroundFetchError::NONE) { |
| 85 std::move(callback).Run(error, base::nullopt /* registration */); | 84 std::move(callback).Run(error, base::nullopt /* registration */); |
| 86 return; | 85 return; |
| 87 } | 86 } |
| 88 | 87 |
| 89 // Create the BackgroundFetchJobController, which will do the actual fetching. | 88 // Create the BackgroundFetchJobController, which will do the actual fetching. |
| 90 CreateController(registration_id, options, std::move(initial_requests)); | 89 CreateController(registration_id, options); |
| 91 | 90 |
| 92 // Create the BackgroundFetchRegistration the renderer process will receive, | 91 // Create the BackgroundFetchRegistration the renderer process will receive, |
| 93 // which enables it to resolve the promise telling the developer it worked. | 92 // which enables it to resolve the promise telling the developer it worked. |
| 94 BackgroundFetchRegistration registration; | 93 BackgroundFetchRegistration registration; |
| 95 registration.tag = registration_id.tag(); | 94 registration.tag = registration_id.tag(); |
| 96 registration.icons = options.icons; | 95 registration.icons = options.icons; |
| 97 registration.title = options.title; | 96 registration.title = options.title; |
| 98 registration.total_download_size = options.total_download_size; | 97 registration.total_download_size = options.total_download_size; |
| 99 | 98 |
| 100 std::move(callback).Run(blink::mojom::BackgroundFetchError::NONE, | 99 std::move(callback).Run(blink::mojom::BackgroundFetchError::NONE, |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 135 if (controller->state() == BackgroundFetchJobController::State::ABORTED || | 134 if (controller->state() == BackgroundFetchJobController::State::ABORTED || |
| 136 controller->state() == BackgroundFetchJobController::State::COMPLETED) { | 135 controller->state() == BackgroundFetchJobController::State::COMPLETED) { |
| 137 return nullptr; | 136 return nullptr; |
| 138 } | 137 } |
| 139 | 138 |
| 140 return controller; | 139 return controller; |
| 141 } | 140 } |
| 142 | 141 |
| 143 void BackgroundFetchContext::CreateController( | 142 void BackgroundFetchContext::CreateController( |
| 144 const BackgroundFetchRegistrationId& registration_id, | 143 const BackgroundFetchRegistrationId& registration_id, |
| 145 const BackgroundFetchOptions& options, | 144 const BackgroundFetchOptions& options) { |
| 146 std::vector<scoped_refptr<BackgroundFetchRequestInfo>> initial_requests) { | |
| 147 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 145 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 148 | 146 |
| 149 net::NetworkTrafficAnnotationTag traffic_annotation = | 147 net::NetworkTrafficAnnotationTag traffic_annotation = |
| 150 net::DefineNetworkTrafficAnnotation("background_fetch_context", R"( | 148 net::DefineNetworkTrafficAnnotation("background_fetch_context", R"( |
| 151 semantics { | 149 semantics { |
| 152 sender: "Background Fetch API" | 150 sender: "Background Fetch API" |
| 153 description: | 151 description: |
| 154 "The Background Fetch API enables developers to upload or download " | 152 "The Background Fetch API enables developers to upload or download " |
| 155 "files on behalf of the user. Such fetches will yield a user " | 153 "files on behalf of the user. Such fetches will yield a user " |
| 156 "visible notification to inform the user of the operation, through " | 154 "visible notification to inform the user of the operation, through " |
| (...skipping 19 matching lines...) Expand all Loading... |
| 176 base::MakeUnique<BackgroundFetchJobController>( | 174 base::MakeUnique<BackgroundFetchJobController>( |
| 177 registration_id, options, data_manager_.get(), browser_context_, | 175 registration_id, options, data_manager_.get(), browser_context_, |
| 178 request_context_getter_, | 176 request_context_getter_, |
| 179 base::BindOnce(&BackgroundFetchContext::DidCompleteJob, | 177 base::BindOnce(&BackgroundFetchContext::DidCompleteJob, |
| 180 weak_factory_.GetWeakPtr()), | 178 weak_factory_.GetWeakPtr()), |
| 181 traffic_annotation); | 179 traffic_annotation); |
| 182 | 180 |
| 183 // TODO(peter): We should actually be able to use Background Fetch in layout | 181 // TODO(peter): We should actually be able to use Background Fetch in layout |
| 184 // tests. That requires a download manager and a request context. | 182 // tests. That requires a download manager and a request context. |
| 185 if (request_context_getter_) { | 183 if (request_context_getter_) { |
| 186 // Start fetching the |initial_requests| immediately. At some point in the | 184 // Start fetching the first few requests immediately. At some point in the |
| 187 // future we may want a more elaborate scheduling mechanism here. | 185 // future we may want a more elaborate scheduling mechanism here. |
| 188 controller->Start(std::move(initial_requests)); | 186 controller->Start(); |
| 189 } | 187 } |
| 190 | 188 |
| 191 active_fetches_.insert( | 189 active_fetches_.insert( |
| 192 std::make_pair(registration_id, std::move(controller))); | 190 std::make_pair(registration_id, std::move(controller))); |
| 193 } | 191 } |
| 194 | 192 |
| 195 void BackgroundFetchContext::DidCompleteJob( | 193 void BackgroundFetchContext::DidCompleteJob( |
| 196 BackgroundFetchJobController* controller) { | 194 BackgroundFetchJobController* controller) { |
| 197 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 195 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 198 | 196 |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 262 | 260 |
| 263 // Delete all persistent information associated with the |registration_id|. | 261 // Delete all persistent information associated with the |registration_id|. |
| 264 data_manager_->DeleteRegistration( | 262 data_manager_->DeleteRegistration( |
| 265 registration_id, base::BindOnce(&RecordRegistrationDeletedError)); | 263 registration_id, base::BindOnce(&RecordRegistrationDeletedError)); |
| 266 | 264 |
| 267 // Delete the local state associated with the |registration_id|. | 265 // Delete the local state associated with the |registration_id|. |
| 268 active_fetches_.erase(registration_id); | 266 active_fetches_.erase(registration_id); |
| 269 } | 267 } |
| 270 | 268 |
| 271 } // namespace content | 269 } // namespace content |
| OLD | NEW |