Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(342)

Unified Diff: content/browser/background_fetch/background_fetch_context.cc

Issue 2774343002: Hook up BackgroundFetchServiceImpl::Fetch() to start a fetch (Closed)
Patch Set: rebase Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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 ff91c2a376b459ed1dc2776857723c5e177a3a69..5506585dfe2f993d31be576d4f4ef9297179a3a1 100644
--- a/content/browser/background_fetch/background_fetch_context.cc
+++ b/content/browser/background_fetch/background_fetch_context.cc
@@ -4,40 +4,34 @@
#include "content/browser/background_fetch/background_fetch_context.h"
-#include "content/browser/background_fetch/background_fetch_job_info.h"
-#include "content/browser/background_fetch/background_fetch_request_info.h"
+#include "base/memory/ptr_util.h"
+#include "content/browser/background_fetch/background_fetch_data_manager.h"
+#include "content/browser/background_fetch/background_fetch_job_controller.h"
+#include "content/browser/background_fetch/background_fetch_registration_id.h"
#include "content/browser/service_worker/service_worker_context_wrapper.h"
+#include "content/browser/storage_partition_impl.h"
#include "content/public/browser/browser_context.h"
-#include "content/public/browser/download_manager.h"
-#include "content/public/browser/storage_partition.h"
namespace content {
BackgroundFetchContext::BackgroundFetchContext(
BrowserContext* browser_context,
- StoragePartition* storage_partition,
+ StoragePartitionImpl* storage_partition,
const scoped_refptr<ServiceWorkerContextWrapper>& service_worker_context)
: browser_context_(browser_context),
storage_partition_(storage_partition),
service_worker_context_(service_worker_context),
- background_fetch_data_manager_(browser_context) {
+ background_fetch_data_manager_(
+ base::MakeUnique<BackgroundFetchDataManager>(browser_context)) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
- // TODO(harkness): BackgroundFetchContext should have
- // ServiceWorkerContextObserver as a parent class and should register as an
- // observer here.
}
BackgroundFetchContext::~BackgroundFetchContext() {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
}
-void BackgroundFetchContext::Init() {
- DCHECK_CURRENTLY_ON(BrowserThread::UI);
-}
-
void BackgroundFetchContext::Shutdown() {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
-
BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE,
base::Bind(&BackgroundFetchContext::ShutdownOnIO, this));
@@ -45,41 +39,66 @@ void BackgroundFetchContext::Shutdown() {
void BackgroundFetchContext::ShutdownOnIO() {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
+ active_fetches_.clear();
+}
- // 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();
+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);
+ background_fetch_data_manager_->CreateRegistration(
+ registration_id, requests, options,
+ base::BindOnce(&BackgroundFetchContext::DidCreateRegistration, this,
+ registration_id, options, callback));
+}
- job_map_.clear();
+void BackgroundFetchContext::DidCreateRegistration(
+ const BackgroundFetchRegistrationId& registration_id,
+ const BackgroundFetchOptions& options,
+ const blink::mojom::BackgroundFetchService::FetchCallback& callback,
+ blink::mojom::BackgroundFetchError error) {
+ if (error != blink::mojom::BackgroundFetchError::NONE) {
+ callback.Run(error, base::nullopt /* registration */);
+ return;
+ }
+
+ // Create the BackgroundFetchJobController, which will do the actual fetching.
+ CreateController(registration_id, options);
+
+ // Create the BackgroundFetchRegistration the renderer process will receive,
+ // which enables it to resolve the promise telling the developer it worked.
+ BackgroundFetchRegistration registration;
+ registration.tag = registration_id.tag();
+ registration.icons = options.icons;
+ registration.title = options.title;
+ registration.total_download_size = options.total_download_size;
+
+ callback.Run(blink::mojom::BackgroundFetchError::NONE, registration);
}
-void BackgroundFetchContext::CreateRequest(
- std::unique_ptr<BackgroundFetchJobInfo> job_info,
- std::vector<std::unique_ptr<BackgroundFetchRequestInfo>> request_infos) {
- 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();
- background_fetch_data_manager_.CreateRequest(std::move(job_info),
- std::move(request_infos));
-
- // Create a controller which drives the processing of the job. It will use
- // the DataManager to get information about individual requests for the job.
- job_map_[job_guid] = base::MakeUnique<BackgroundFetchJobController>(
- job_guid, browser_context_, storage_partition_,
- &background_fetch_data_manager_,
- base::BindOnce(&BackgroundFetchContext::DidCompleteJob, this, job_guid));
+void BackgroundFetchContext::CreateController(
+ const BackgroundFetchRegistrationId& registration_id,
+ const BackgroundFetchOptions& options) {
+ std::unique_ptr<BackgroundFetchJobController> controller =
+ base::MakeUnique<BackgroundFetchJobController>(
+ registration_id, browser_context_, storage_partition_,
+ background_fetch_data_manager_.get(),
+ base::BindOnce(&BackgroundFetchContext::DidFinishFetch, this));
+
+ active_fetches_.insert(
+ std::make_pair(registration_id, std::move(controller)));
}
-void BackgroundFetchContext::DidCompleteJob(const std::string& job_guid) {
- DCHECK(job_map_.find(job_guid) != job_map_.end());
+void BackgroundFetchContext::DidFinishFetch(
+ const BackgroundFetchRegistrationId& registration_id) {
+ DCHECK_GT(active_fetches_.count(registration_id), 0u);
- job_map_.erase(job_guid);
+ // TODO(peter): Dispatch the `backgroundfetched` or the `backgroundfetchfail`
+ // event to the Service Worker.
- // TODO(harkness): Once the caller receives the message, inform the
- // DataManager that it can clean up the pending job.
+ active_fetches_.erase(registration_id);
}
} // namespace content

Powered by Google App Engine
This is Rietveld 408576698