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

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

Issue 2786783002: Dispatch a bare Service Worker event for a finished Background Fetch (Closed)
Patch Set: 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 4f5b2e7f5bb4907b80bd488913302c84ba1203f4..a3d3261a02e3242c3a392d8d2c0ff9261ce5ae1e 100644
--- a/content/browser/background_fetch/background_fetch_context.cc
+++ b/content/browser/background_fetch/background_fetch_context.cc
@@ -6,24 +6,43 @@
#include "base/memory/ptr_util.h"
#include "content/browser/background_fetch/background_fetch_data_manager.h"
+#include "content/browser/background_fetch/background_fetch_event_dispatcher.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/blob_handle.h"
#include "content/public/browser/browser_context.h"
#include "net/url_request/url_request_context_getter.h"
#include "url/origin.h"
namespace content {
+namespace {
+
+// Records the |error| status issued by the DataManager after it was requested
+// to create and store a new Background Fetch registration.
+void RecordRegistrationCreatedError(blink::mojom::BackgroundFetchError error) {
+ // TODO(peter): Add UMA.
+}
+
+// Records the |error| status issued by the DataManager after the storage
+// associated with a registration has been completely deleted.
+void RecordRegistrationDeletedError(blink::mojom::BackgroundFetchError error) {
+ // TODO(peter): Add UMA.
+}
+
+} // namespace
+
BackgroundFetchContext::BackgroundFetchContext(
BrowserContext* browser_context,
StoragePartitionImpl* storage_partition,
- const scoped_refptr<ServiceWorkerContextWrapper>& service_worker_context)
+ scoped_refptr<ServiceWorkerContextWrapper> service_worker_context)
: browser_context_(browser_context),
- service_worker_context_(service_worker_context),
- background_fetch_data_manager_(
- base::MakeUnique<BackgroundFetchDataManager>(browser_context)) {
+ data_manager_(
+ base::MakeUnique<BackgroundFetchDataManager>(browser_context)),
+ event_dispatcher_(base::MakeUnique<BackgroundFetchEventDispatcher>(
+ std::move(service_worker_context))) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
download_manager_ = BrowserContext::GetDownloadManager(browser_context_);
request_context_ =
@@ -52,7 +71,7 @@ void BackgroundFetchContext::StartFetch(
const BackgroundFetchOptions& options,
const blink::mojom::BackgroundFetchService::FetchCallback& callback) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
- background_fetch_data_manager_->CreateRegistration(
+ data_manager_->CreateRegistration(
registration_id, requests, options,
base::BindOnce(&BackgroundFetchContext::DidCreateRegistration, this,
registration_id, options, callback));
@@ -64,6 +83,7 @@ void BackgroundFetchContext::DidCreateRegistration(
const blink::mojom::BackgroundFetchService::FetchCallback& callback,
blink::mojom::BackgroundFetchError error,
std::vector<BackgroundFetchRequestInfo> initial_requests) {
+ RecordRegistrationCreatedError(error);
if (error != blink::mojom::BackgroundFetchError::NONE) {
callback.Run(error, base::nullopt /* registration */);
return;
@@ -126,8 +146,8 @@ void BackgroundFetchContext::CreateController(
std::vector<BackgroundFetchRequestInfo> initial_requests) {
std::unique_ptr<BackgroundFetchJobController> controller =
base::MakeUnique<BackgroundFetchJobController>(
- registration_id, options, background_fetch_data_manager_.get(),
- download_manager_, request_context_,
+ registration_id, options, data_manager_.get(), download_manager_,
+ request_context_,
base::BindOnce(&BackgroundFetchContext::DidCompleteJob, this));
// TODO(peter): We should actually be able to use Background Fetch in layout
@@ -149,11 +169,49 @@ void BackgroundFetchContext::DidCompleteJob(
DCHECK_GT(active_fetches_.count(registration_id), 0u);
- if (controller->state() == BackgroundFetchJobController::State::COMPLETED) {
- // TODO(peter): Dispatch the `backgroundfetched` or `backgroundfetchfail`
- // event to the Service Worker to inform the developer.
+ // The `backgroundfetched` and/or `backgroundfetchfail` event will only be
+ // invoked for Background Fetch jobs which have been completed.
+ if (controller->state() != BackgroundFetchJobController::State::COMPLETED) {
harkness 2017/03/29 22:12:33 We need to call the aborted event if that's the st
Peter Beverloo 2017/03/29 22:20:28 As I understand it, `backgroundfetchabort` is for
Peter Beverloo 2017/03/30 10:19:37 https://github.com/WICG/background-fetch#reacting-
harkness 2017/03/30 14:15:28 Acknowledged.
+ DeleteRegistration(registration_id);
+ return;
}
+ // Get the sequence of settled fetches from the data manager.
+ data_manager_->GetSettledFetchesForRegistration(
+ registration_id,
+ base::BindOnce(&BackgroundFetchContext::DidGetSettledFetches, this,
+ registration_id));
+}
+
+void BackgroundFetchContext::DidGetSettledFetches(
+ const BackgroundFetchRegistrationId& registration_id,
+ blink::mojom::BackgroundFetchError error,
+ std::vector<BackgroundFetchSettledFetch> settled_fetches,
+ std::vector<std::unique_ptr<BlobHandle>> blob_handles) {
+ if (error != blink::mojom::BackgroundFetchError::NONE) {
+ DeleteRegistration(registration_id);
+ return;
+ }
+
+ // TODO(peter): Distinguish between the `backgroundfetched` and
+ // `backgroundfetchfail` events based on the status code of all fetches. We
+ // don't populate that field yet, so always assume it's successful for now.
+
+ event_dispatcher_->DispatchBackgroundFetchedEvent(
+ registration_id, std::move(settled_fetches),
+ base::Bind(&BackgroundFetchContext::DeleteRegistration, this,
+ registration_id));
+}
+
+void BackgroundFetchContext::DeleteRegistration(
+ const BackgroundFetchRegistrationId& registration_id) {
+ DCHECK_GT(active_fetches_.count(registration_id), 0u);
+
+ // Delete all persistent information associated with the |registration_id|.
+ data_manager_->DeleteRegistration(
+ registration_id, base::BindOnce(&RecordRegistrationDeletedError));
+
+ // Delete the local state associated with the |registration_id|.
active_fetches_.erase(registration_id);
}

Powered by Google App Engine
This is Rietveld 408576698