diff options
author | kalman <kalman@chromium.org> | 2015-09-18 10:21:58 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-09-18 17:22:38 +0000 |
commit | 6f984aed2ba46212d23d28a0f4ca075d142b6de4 (patch) | |
tree | 6f1de36c3e8702afbe0ae5236e32aa82eb004192 /extensions/renderer/resources | |
parent | 7653d3b301093051d2ac6cf805f13aeb65d511fa (diff) | |
download | chromium_src-6f984aed2ba46212d23d28a0f4ca075d142b6de4.zip chromium_src-6f984aed2ba46212d23d28a0f4ca075d142b6de4.tar.gz chromium_src-6f984aed2ba46212d23d28a0f4ca075d142b6de4.tar.bz2 |
Finish the implementation of chrome.runtime.getBackgroundClient() for extension service workers.
Until now it assumed that the extension's background page was open. Now it uses
WakeEventPage IPC call to wake the event page if it's asleep. This required
making WakeEventPage thread safe, to be able to be called on a service worker
thread, then when the response comes from the browser, call back on that same
thread.
It was also incorrectly called chrome.getBackgroundClient() rather than
chrome.runtime.getBackgroundClient().
I also did a rewrite of the extension service worker tests to be better
reusable, and easier to add more tests. It was hard to test a closed event page
in the current framework. Then I added more tests.
BUG=501569, 532720
R=rdevlin.cronin@chromium.org
Review URL: https://codereview.chromium.org/1344243003
Cr-Commit-Position: refs/heads/master@{#349700}
Diffstat (limited to 'extensions/renderer/resources')
-rw-r--r-- | extensions/renderer/resources/service_worker_bindings.js | 69 |
1 files changed, 55 insertions, 14 deletions
diff --git a/extensions/renderer/resources/service_worker_bindings.js b/extensions/renderer/resources/service_worker_bindings.js index bc8cfd6b..532f51d 100644 --- a/extensions/renderer/resources/service_worker_bindings.js +++ b/extensions/renderer/resources/service_worker_bindings.js @@ -2,25 +2,66 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -(function(backgroundUrl) { +// This function is returned to DidInitializeServiceWorkerContextOnWorkerThread +// then executed, passing in dependencies as function arguments. +// +// |backgroundUrl| is the URL of the extension's background page. +// |wakeEventPage| is a function that wakes up the current extension's event +// page, then runs its callback on completion or failure. +// |logging| is an object equivalent to a subset of base/debug/logging.h, with +// CHECK/DCHECK/etc. +(function(backgroundUrl, wakeEventPage, logging) { 'use strict'; - self.chrome = self.chrome || {}; + self.chrome.runtime = self.chrome.runtime || {}; - self.chrome.getBackgroundClient = function() { return new Promise( - function(resolve, reject) { - self.clients.matchAll({ + // Returns a Promise that resolves to the background page's client, or null + // if there is no background client. + function findBackgroundClient() { + return self.clients.matchAll({ includeUncontrolled: true, type: 'window' - }).then(function(clients) { - for (let client of clients) { - if (client.url == backgroundUrl) { - resolve(client); - return; - } - } - reject("BackgroundClient ('" + backgroundUrl + "') does not exist.") - }) + }).then(function(clients) { + return clients.find(function(client) { + return client.url == backgroundUrl; + }); + }); + } + + // Returns a Promise wrapper around wakeEventPage, that resolves on success, + // or rejects on failure. + function makeWakeEventPagePromise() { + return new Promise(function(resolve, reject) { + wakeEventPage(function(success) { + if (success) + resolve(); + else + reject('Failed to start background client "' + backgroundUrl + '"'); + }); }); } + + // The chrome.runtime.getBackgroundClient function is documented in + // runtime.json. It returns a Promise that resolves to the background page's + // client, or is rejected if there is no background client or if the + // background client failed to wake. + self.chrome.runtime.getBackgroundClient = function() { + return findBackgroundClient().then(function(client) { + if (client) { + // Background client is already awake, or it was persistent. + return client; + } + + // Event page needs to be woken. + return makeWakeEventPagePromise().then(function() { + return findBackgroundClient(); + }).then(function(client) { + if (!client) { + return Promise.reject( + 'Background client "' + backgroundUrl + '" not found'); + } + return client; + }); + }); + }; }); |