forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ref count to service workers for extension API.
We need a way to keep a service worker alive 1) during extension function's request->response roundtrip completes. 2) when an event is about to be dispatched to a (stopped) service worker. This CL shows a way to do chromium#1. chromium#2 can follow later. The CL adds plumbing to expose functions to increment/decrement ref counting to an ServiceWorkerVersion. This is done by adding a way to add "external requests" to a ServiceWorkerVersion: when a worker has external requests pending, it will be considered to be in working state, i.e ServiceWorkerVersion::HasWork() will return true. The public interface is exposed through ServiceWorkerContext. And the interface methods expect a GUID/nonce for each such requests from service worker renderer: ServiceWorkerContext::StartingExternalRequest() and ServiceWorkerContext::FinishedExternalRequest() Extension APIs that are expected to be long running aren't handled in this CL. For example: an extension API showing a dialog to user that waits for user action. BUG=602442 Test=There's no easy way to test it without tweaking the code, I've used the following steps to make sure that we keep SW alive when an extension API is in-flight: Change the stop worker idle timeout and worker timeout to sth small, e.g. 3s. Call an extension function that runs for 7s (> 3s + 3s). Without the CL, the extension function's callback won't be called because the worker would shut down after 6s. The added test ServiceWorkerTest.WorkerRefCount tests this at a bit lower level: by checking ref count (= count of external requests for a ServiceWorkerVersion). Review-Url: https://codereview.chromium.org/2166523003 Cr-Commit-Position: refs/heads/master@{#425824}
- Loading branch information
lazyboy
authored and
Commit bot
committed
Oct 18, 2016
1 parent
44a5f92
commit 4c82177
Showing
41 changed files
with
572 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
chrome/test/data/extensions/api_test/service_worker/api_worker_ref_count/manifest.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"name": "Tests that worker is kept alive during extension API call.", | ||
"version": "1.0", | ||
"manifest_version": 2 | ||
} |
1 change: 1 addition & 0 deletions
1
chrome/test/data/extensions/api_test/service_worker/api_worker_ref_count/page.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<script src="page.js"></script> |
33 changes: 33 additions & 0 deletions
33
chrome/test/data/extensions/api_test/service_worker/api_worker_ref_count/page.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Copyright 2016 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. | ||
|
||
var worker = null; | ||
var FAILURE_MESSAGE = 'FAILURE'; | ||
|
||
window.runServiceWorker = function() { | ||
navigator.serviceWorker.register('sw.js').then(function() { | ||
return navigator.serviceWorker.ready; | ||
}).then(function(registration) { | ||
worker = registration.active; | ||
chrome.test.sendMessage('WORKER STARTED'); | ||
}).catch(function(err) { | ||
chrome.test.sendMessage(FAILURE_MESSAGE); | ||
}); | ||
}; | ||
|
||
window.testSendMessage = function() { | ||
if (worker == null) { | ||
chrome.test.sendMessage(FAILURE_MESSAGE); | ||
return; | ||
} | ||
var channel = new MessageChannel(); | ||
channel.port1.onmessage = function(e) { | ||
if (e.data == 'Worker reply: Hello world') { | ||
chrome.test.sendMessage('SUCCESS'); | ||
} else { | ||
chrome.test.sendMessage(FAILURE_MESSAGE); | ||
} | ||
}; | ||
worker.postMessage('sendMessageTest', [channel.port2]); | ||
}; |
20 changes: 20 additions & 0 deletions
20
chrome/test/data/extensions/api_test/service_worker/api_worker_ref_count/sw.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Copyright 2016 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. | ||
|
||
self.onmessage = function(e) { | ||
var fail = function() { | ||
e.ports[0].postMessage('FAILURE'); | ||
}; | ||
if (e.data == 'sendMessageTest') { | ||
try { | ||
chrome.test.sendMessage('CHECK_REF_COUNT', function(reply) { | ||
e.ports[0].postMessage('Worker reply: ' + reply); | ||
}); | ||
} catch (e) { | ||
fail(); | ||
} | ||
} else { | ||
fail(); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.