Index: content/browser/background_fetch/background_fetch_context.cc |
diff --git a/content/browser/background_fetch/background_fetch_context.cc b/content/browser/background_fetch/background_fetch_context.cc |
index 21f3dcd0d3790a440166a6284f8c338c0cd3092e..127ae465cca07ac780c36fe1f121d209d8168031 100644 |
--- a/content/browser/background_fetch/background_fetch_context.cc |
+++ b/content/browser/background_fetch/background_fetch_context.cc |
@@ -4,6 +4,8 @@ |
#include "content/browser/background_fetch/background_fetch_context.h" |
+#include <algorithm> |
+ |
#include "content/browser/background_fetch/background_fetch_job_info.h" |
#include "content/browser/background_fetch/background_fetch_request_info.h" |
#include "content/browser/service_worker/service_worker_context_wrapper.h" |
@@ -31,10 +33,6 @@ BackgroundFetchContext::~BackgroundFetchContext() { |
DCHECK_CURRENTLY_ON(BrowserThread::UI); |
} |
-void BackgroundFetchContext::Init() { |
- DCHECK_CURRENTLY_ON(BrowserThread::UI); |
-} |
- |
void BackgroundFetchContext::Shutdown() { |
DCHECK_CURRENTLY_ON(BrowserThread::UI); |
@@ -48,42 +46,85 @@ void BackgroundFetchContext::ShutdownOnIO() { |
// Call Shutdown on all pending job controllers to give them a chance to flush |
// any status to the DataManager. |
- for (auto& job : job_map_) |
- job.second->Shutdown(); |
+ for (auto& controller : controllers_) |
+ controller->Shutdown(); |
- job_map_.clear(); |
+ controllers_.clear(); |
} |
-void BackgroundFetchContext::CreateRequest( |
- std::unique_ptr<BackgroundFetchJobInfo> job_info, |
- std::vector<BackgroundFetchRequestInfo>& request_infos) { |
+void BackgroundFetchContext::StartFetch( |
+ const BackgroundFetchRegistrationId& registration_id, |
+ const std::vector<ServiceWorkerFetchRequest>& requests, |
+ const BackgroundFetchOptions& options, |
+ const blink::mojom::BackgroundFetchService::FetchCallback& callback) { |
DCHECK_CURRENTLY_ON(BrowserThread::IO); |
- DCHECK_GE(1U, request_infos.size()); |
- |
- // Inform the data manager about the new download. |
- const std::string job_guid = job_info->guid(); |
- std::unique_ptr<BackgroundFetchJobData> job_data = |
- background_fetch_data_manager_.CreateRequest(std::move(job_info), |
- request_infos); |
- |
- // If job_data is null, the DataManager will have logged an error. |
- if (job_data) { |
- // Create a controller which drives the processing of the job. It will use |
- // the JobData to get information about individual requests for the job. |
- job_map_[job_guid] = base::MakeUnique<BackgroundFetchJobController>( |
- job_guid, browser_context_, storage_partition_, std::move(job_data), |
- base::BindOnce(&BackgroundFetchContext::DidCompleteJob, this, |
- job_guid)); |
+ |
+ // Registration IDs must be unique. When the |registration_id| already has an |
+ // active job, it means that the developer is reusing the registration's tag. |
+ if (GetActiveJobForRegistrationId(registration_id)) { |
+ callback.Run(blink::mojom::BackgroundFetchError::DUPLICATED_TAG, |
+ nullptr /* registration */); |
+ return; |
} |
+ |
+ background_fetch_data_manager_.CreateRegistration( |
+ registration_id, requests, options, |
+ base::BindOnce(&BackgroundFetchContext::DidCreateCreateRegistration, this, |
+ registration_id, options, callback)); |
} |
-void BackgroundFetchContext::DidCompleteJob(const std::string& job_guid) { |
- DCHECK(job_map_.find(job_guid) != job_map_.end()); |
+void BackgroundFetchContext::DidCreateCreateRegistration( |
+ const BackgroundFetchRegistrationId& registration_id, |
+ const BackgroundFetchOptions& options, // TODO: kill |
+ const blink::mojom::BackgroundFetchService::FetchCallback& callback, |
+ blink::mojom::BackgroundFetchError error, |
+ std::unique_ptr<BackgroundFetchJobData> job_data) { |
+ if (error != blink::mojom::BackgroundFetchError::NONE) { |
+ callback.Run(error, nullptr /* registration */); |
+ return; |
+ } |
+ |
+ DCHECK(job_data); |
+ |
+ controllers_.emplace_back( |
+ registration_id, browser_context_, storage_partition_, |
+ std::move(job_data), |
+ base::BindOnce(&BackgroundFetchContext::DidCompleteJob, this)); |
- job_map_.erase(job_guid); |
+ BackgroundFetchRegistration registration; |
+ registration.tag = tag; |
+ registration.icons = options.icons; |
+ registration.title = options.title; |
+ registration.total_download_size = options.total_download_size; |
+ |
+ callback.Run(blink::mojom::BackgroundFetchError::NONE, registration); |
+} |
+ |
+BackgroundFetchJobController* |
+BackgroundFetchContext::GetActiveJobForRegistrationId( |
+ const BackgroundFetchRegistrationId& registration_id) const { |
+ for (const auto& controller : controllers_) { |
+ if (controller->registration_id() == registration_id()) |
+ return controller.get(); |
+ } |
+ return nullptr; |
+} |
+ |
+void BackgroundFetchContext::DidCompleteJob( |
+ const BackgroundFetchRegistrationId& registration_id) { |
+ auto iter = std::find_if( |
+ controllers_.begin(), controllers_.end(), |
+ [®istration_id]( |
+ const std::unique_ptr<BackgroundFetchJobController>& controller) { |
+ return controller->registration_id() == registration_id; |
+ }); |
+ |
+ DCHECK(iter != controllers_.end()); |
// TODO(harkness): Once the caller receives the message, inform the |
// DataManager that it can clean up the pending job. |
+ |
+ controllers_.erase(iter); |
} |
} // namespace content |