Commit e205bc26 authored by Mugdha Lakhani's avatar Mugdha Lakhani Committed by Commit Bot

[Background Fetch] Respond to service worker database deletion.

by abandoning all ongoing fetches for the storage partition.

Bug: 841385
Change-Id: I6fc990304272832fe0e8d993281a0aa06d083e75
Reviewed-on: https://chromium-review.googlesource.com/1087065
Commit-Queue: Mugdha Lakhani <nator@chromium.org>
Reviewed-by: default avatarPeter Beverloo <peter@chromium.org>
Cr-Commit-Position: refs/heads/master@{#566039}
parent 8f5c116d
......@@ -183,10 +183,8 @@ void BackgroundFetchContext::UpdateUI(
title, std::move(callback)));
}
void BackgroundFetchContext::OnRegistrationDeleted(
int64_t service_worker_registration_id,
const GURL& pattern) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
void BackgroundFetchContext::AbandonFetches(
int64_t service_worker_registration_id) {
// Abandon all active fetches associated with this service worker.
// BackgroundFetchJobController::Abort() will eventually lead to deletion of
// the controller from job_controllers, hence we can't use a range based
......@@ -195,9 +193,12 @@ void BackgroundFetchContext::OnRegistrationDeleted(
/* no_increment */) {
auto saved_iter = iter;
iter++;
if (saved_iter->second->registration_id()
.service_worker_registration_id() ==
service_worker_registration_id) {
if (service_worker_registration_id ==
blink::mojom::kInvalidServiceWorkerRegistrationId ||
saved_iter->second->registration_id()
.service_worker_registration_id() ==
service_worker_registration_id) {
DCHECK(saved_iter->second);
saved_iter->second->Abort(
BackgroundFetchReasonToAbort::SERVICE_WORKER_UNAVAILABLE);
}
......@@ -205,8 +206,10 @@ void BackgroundFetchContext::OnRegistrationDeleted(
for (auto iter = fetch_callbacks_.begin(); iter != fetch_callbacks_.end();
/* no increment */) {
if (iter->first.service_worker_registration_id() ==
service_worker_registration_id) {
if (service_worker_registration_id ==
blink::mojom::kInvalidServiceWorkerRegistrationId ||
iter->first.service_worker_registration_id() ==
service_worker_registration_id) {
DCHECK(iter->second);
std::move(iter->second)
.Run(blink::mojom::BackgroundFetchError::SERVICE_WORKER_UNAVAILABLE,
......@@ -217,8 +220,16 @@ void BackgroundFetchContext::OnRegistrationDeleted(
}
}
void BackgroundFetchContext::OnRegistrationDeleted(
int64_t service_worker_registration_id,
const GURL& pattern) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
AbandonFetches(service_worker_registration_id);
}
void BackgroundFetchContext::OnStorageWiped() {
// TODO(nator): abort all active fetches for storage partition.
DCHECK_CURRENTLY_ON(BrowserThread::IO);
AbandonFetches(blink::mojom::kInvalidServiceWorkerRegistrationId);
}
void BackgroundFetchContext::DidUpdateStoredUI(
......
......@@ -205,6 +205,11 @@ class CONTENT_EXPORT BackgroundFetchContext
void SetDataManagerForTesting(
std::unique_ptr<BackgroundFetchDataManager> data_manager);
// Helper method to abandon ongoing fetches for a given service worker.
// Abandons all of them if |service_worker_registration_id| is set to
// blink::mojom::kInvalidServiceWorkerRegistrationId.
void AbandonFetches(int64_t service_worker_registration_id);
// |this| is owned, indirectly, by the BrowserContext.
BrowserContext* browser_context_;
......
......@@ -297,9 +297,6 @@ TEST_F(BackgroundFetchJobControllerTest, Abort) {
controller->StartRequest(requests[0]);
controller->Abort(BackgroundFetchReasonToAbort::CANCELLED_FROM_UI);
// Tell the delegate to abort the job as well so it doesn't send completed
// messages to the JobController.
delegate_->Abort(registration_id.unique_id());
base::RunLoop().RunUntilIdle();
......@@ -357,9 +354,28 @@ TEST_F(BackgroundFetchJobControllerTest, ServiceWorkerRegistrationDeleted) {
context_->OnRegistrationDeleted(kExampleServiceWorkerRegistrationId,
GURL("https://example.com/funny_cat.png"));
// Tell the delegate to abort the job as well so it doesn't send completed
// messages to the JobController.
delegate_->Abort(registration_id.unique_id());
base::RunLoop().RunUntilIdle();
EXPECT_EQ(JobCompletionStatus::kAborted,
request_manager_.GetCompletionStatus(registration_id));
}
TEST_F(BackgroundFetchJobControllerTest, ServiceWorkerDatabaseDeleted) {
BackgroundFetchRegistrationId registration_id;
auto requests = CreateRegistrationForRequests(
&registration_id, {{GURL("https://example.com/funny_cat.png"), "GET"}},
true /* auto_complete_requests */);
EXPECT_EQ(JobCompletionStatus::kRunning,
request_manager_.GetCompletionStatus(registration_id));
std::unique_ptr<BackgroundFetchJobController> controller =
CreateJobController(registration_id, requests.size());
AddControllerToContextMap(registration_id.unique_id(), std::move(controller));
context_->OnStorageWiped();
base::RunLoop().RunUntilIdle();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment