Commit 75819ff2 authored by Matt Falkenhagen's avatar Matt Falkenhagen Committed by Commit Bot

service worker: Don't crash when selecting Offline mode in DevTools.

When ServiceWorkerOnUI is enabled, methods must be called on the UI
thread.

This adds a test that would crash when run_web_tests.py is run with
--additional-driver-flag="--enable-features=ServiceWorkerOnUI" before
the fix.

Bug: 1030543
Change-Id: Ibdd7cbac3fd92900dc78d538237f5f8b1a3a96f6
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1980252Reviewed-by: default avatarHiroki Nakagawa <nhiroki@chromium.org>
Commit-Queue: Matt Falkenhagen <falken@chromium.org>
Cr-Commit-Position: refs/heads/master@{#727144}
parent 634b8e01
...@@ -751,27 +751,28 @@ class BackgroundSyncRestorer { ...@@ -751,27 +751,28 @@ class BackgroundSyncRestorer {
static_cast<StoragePartitionImpl*>(storage_partition_) static_cast<StoragePartitionImpl*>(storage_partition_)
->GetBackgroundSyncContext(); ->GetBackgroundSyncContext();
if (offline) { if (offline) {
base::PostTask( RunOrPostTaskOnThread(
FROM_HERE, {BrowserThread::IO}, FROM_HERE, ServiceWorkerContext::GetCoreThreadId(),
base::BindOnce( base::BindOnce(
&SetServiceWorkerOfflineOnIO, sync_context, &SetServiceWorkerOfflineOnServiceWorkerCoreThread, sync_context,
base::RetainedRef(static_cast<ServiceWorkerContextWrapper*>( base::WrapRefCounted(static_cast<ServiceWorkerContextWrapper*>(
storage_partition_->GetServiceWorkerContext())), storage_partition_->GetServiceWorkerContext())),
service_worker_host->version_id(), service_worker_host->version_id(),
offline_sw_registration_id_.get())); offline_sw_registration_id_.get()));
} else { } else {
base::PostTask(FROM_HERE, {BrowserThread::IO}, RunOrPostTaskOnThread(
base::BindOnce(&SetServiceWorkerOnlineOnIO, sync_context, FROM_HERE, ServiceWorkerContext::GetCoreThreadId(),
offline_sw_registration_id_.get())); base::BindOnce(&SetServiceWorkerOnlineOnServiceWorkerCoreThread,
sync_context, offline_sw_registration_id_.get()));
} }
} }
static void SetServiceWorkerOfflineOnIO( static void SetServiceWorkerOfflineOnServiceWorkerCoreThread(
scoped_refptr<BackgroundSyncContextImpl> sync_context, scoped_refptr<BackgroundSyncContextImpl> sync_context,
scoped_refptr<ServiceWorkerContextWrapper> swcontext, scoped_refptr<ServiceWorkerContextWrapper> swcontext,
int64_t version_id, int64_t version_id,
int64_t* offline_sw_registration_id) { int64_t* offline_sw_registration_id) {
DCHECK_CURRENTLY_ON(BrowserThread::IO); DCHECK_CURRENTLY_ON(ServiceWorkerContext::GetCoreThreadId());
ServiceWorkerVersion* version = swcontext.get()->GetLiveVersion(version_id); ServiceWorkerVersion* version = swcontext.get()->GetLiveVersion(version_id);
if (!version) if (!version)
return; return;
...@@ -783,10 +784,10 @@ class BackgroundSyncRestorer { ...@@ -783,10 +784,10 @@ class BackgroundSyncRestorer {
registration_id, true); registration_id, true);
} }
static void SetServiceWorkerOnlineOnIO( static void SetServiceWorkerOnlineOnServiceWorkerCoreThread(
scoped_refptr<BackgroundSyncContextImpl> sync_context, scoped_refptr<BackgroundSyncContextImpl> sync_context,
int64_t* offline_sw_registration_id) { int64_t* offline_sw_registration_id) {
DCHECK_CURRENTLY_ON(BrowserThread::IO); DCHECK_CURRENTLY_ON(ServiceWorkerContext::GetCoreThreadId());
if (*offline_sw_registration_id == if (*offline_sw_registration_id ==
blink::mojom::kInvalidServiceWorkerRegistrationId) { blink::mojom::kInvalidServiceWorkerRegistrationId) {
return; return;
......
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
(async function() {
TestRunner.addResult(`Tests "Offline" checkbox does not crash. crbug.com/746220\n`);
await TestRunner.loadModule('application_test_runner');
// Note: every test that uses a storage API must manually clean-up state from previous tests.
await ApplicationTestRunner.resetState();
const scriptURL = 'resources/service-worker-empty.js';
const scope = 'resources/offline';
// Register a service worker.
await ApplicationTestRunner.registerServiceWorker(scriptURL, scope);
await ApplicationTestRunner.waitForActivated(scope);
// Switch offline mode on.
const oldNetwork = SDK.multitargetNetworkManager.networkConditions();
SDK.multitargetNetworkManager.setNetworkConditions(SDK.NetworkManager.OfflineConditions);
// Switch offline mode off.
SDK.multitargetNetworkManager.setNetworkConditions(oldNetwork);
// The test passes if it doesn't crash.
TestRunner.completeTest();
})();
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