diff --git a/atom/browser/api/atom_api_web_contents.cc b/atom/browser/api/atom_api_web_contents.cc index 9668c8b3a332..9604274db1f4 100644 --- a/atom/browser/api/atom_api_web_contents.cc +++ b/atom/browser/api/atom_api_web_contents.cc @@ -1433,28 +1433,25 @@ void WebContents::InspectServiceWorker() { } } -void WebContents::HasServiceWorker(const base::Callback& callback) { +void OnServiceWorkerCheckDone(scoped_refptr promise, + content::ServiceWorkerCapability capability) { + promise->Resolve(capability != + content::ServiceWorkerCapability::NO_SERVICE_WORKER); +} + +v8::Local WebContents::HasServiceWorker() { + scoped_refptr promise = new util::Promise(isolate()); auto* context = GetServiceWorkerContext(web_contents()); - if (!context) - return; - - struct WrappedCallback { - base::Callback callback_; - explicit WrappedCallback(const base::Callback& callback) - : callback_(callback) {} - void Run(content::ServiceWorkerCapability capability) { - callback_.Run(capability != - content::ServiceWorkerCapability::NO_SERVICE_WORKER); - delete this; - } - }; - - auto* wrapped_callback = new WrappedCallback(callback); + if (!context) { + promise->RejectWithErrorMessage("Unable to get ServiceWorker context."); + } context->CheckHasServiceWorker( - web_contents()->GetLastCommittedURL(), GURL::EmptyGURL(), - base::BindOnce(&WrappedCallback::Run, - base::Unretained(wrapped_callback))); + web_contents()->GetLastCommittedURL(), + web_contents()->GetLastCommittedURL(), + base::BindOnce(&OnServiceWorkerCheckDone, promise)); + + return promise->GetHandle(); } void WebContents::UnregisterServiceWorker( @@ -1462,7 +1459,6 @@ void WebContents::UnregisterServiceWorker( auto* context = GetServiceWorkerContext(web_contents()); if (!context) return; - context->UnregisterServiceWorker(web_contents()->GetLastCommittedURL(), callback); } diff --git a/atom/browser/api/atom_api_web_contents.h b/atom/browser/api/atom_api_web_contents.h index e3e1989cb179..e938217c2de3 100644 --- a/atom/browser/api/atom_api_web_contents.h +++ b/atom/browser/api/atom_api_web_contents.h @@ -161,7 +161,7 @@ class WebContents : public mate::TrackableObject, void DisableDeviceEmulation(); void InspectElement(int x, int y); void InspectServiceWorker(); - void HasServiceWorker(const base::Callback&); + v8::Local HasServiceWorker(); void UnregisterServiceWorker(const base::Callback&); void SetIgnoreMenuShortcuts(bool ignore); void SetAudioMuted(bool muted); diff --git a/docs/api/promisification.md b/docs/api/promisification.md index 6b6e41d96300..2e11f7ef7dc8 100644 --- a/docs/api/promisification.md +++ b/docs/api/promisification.md @@ -26,7 +26,6 @@ When a majority of affected functions are migrated, this flag will be enabled by - [inAppPurchase.purchaseProduct(productID, quantity, callback)](https://github.com/electron/electron/blob/master/docs/api/in-app-purchase.md#purchaseProduct) - [inAppPurchase.getProducts(productIDs, callback)](https://github.com/electron/electron/blob/master/docs/api/in-app-purchase.md#getProducts) - [netLog.stopLogging([callback])](https://github.com/electron/electron/blob/master/docs/api/net-log.md#stopLogging) -- [protocol.isProtocolHandled(scheme, callback)](https://github.com/electron/electron/blob/master/docs/api/protocol.md#isProtocolHandled) - [ses.getCacheSize(callback)](https://github.com/electron/electron/blob/master/docs/api/session.md#getCacheSize) - [ses.clearCache(callback)](https://github.com/electron/electron/blob/master/docs/api/session.md#clearCache) - [ses.clearStorageData([options, callback])](https://github.com/electron/electron/blob/master/docs/api/session.md#clearStorageData) @@ -36,7 +35,6 @@ When a majority of affected functions are migrated, this flag will be enabled by - [ses.getBlobData(identifier, callback)](https://github.com/electron/electron/blob/master/docs/api/session.md#getBlobData) - [ses.clearAuthCache(options[, callback])](https://github.com/electron/electron/blob/master/docs/api/session.md#clearAuthCache) - [contents.executeJavaScript(code[, userGesture, callback])](https://github.com/electron/electron/blob/master/docs/api/web-contents.md#executeJavaScript) -- [contents.hasServiceWorker(callback)](https://github.com/electron/electron/blob/master/docs/api/web-contents.md#hasServiceWorker) - [contents.unregisterServiceWorker(callback)](https://github.com/electron/electron/blob/master/docs/api/web-contents.md#unregisterServiceWorker) - [contents.print([options], [callback])](https://github.com/electron/electron/blob/master/docs/api/web-contents.md#print) - [contents.printToPDF(options, callback)](https://github.com/electron/electron/blob/master/docs/api/web-contents.md#printToPDF) @@ -59,3 +57,5 @@ When a majority of affected functions are migrated, this flag will be enabled by - [shell.openExternal(url[, options, callback])](https://github.com/electron/electron/blob/master/docs/api/shell.md#openExternal) - [webviewTag.capturePage([rect, ]callback)](https://github.com/electron/electron/blob/master/docs/api/webview-tag.md#capturePage) - [win.capturePage([rect, ]callback)](https://github.com/electron/electron/blob/master/docs/api/browser-window.md#capturePage) +- [desktopCapturer.getSources(options, callback)](https://github.com/electron/electron/blob/master/docs/api/desktop-capturer.md#getSources) +- [contents.hasServiceWorker(callback)](https://github.com/electron/electron/blob/master/docs/api/web-contents.md#hasServiceWorker) diff --git a/docs/api/web-contents.md b/docs/api/web-contents.md index a0d51f63115b..22e0b7564f67 100644 --- a/docs/api/web-contents.md +++ b/docs/api/web-contents.md @@ -1164,6 +1164,12 @@ Captures a snapshot of the page within `rect`. Omitting `rect` will capture the Checks if any ServiceWorker is registered and returns a boolean as response to `callback`. +**[Deprecated Soon](promisification.md)** + +#### `contents.hasServiceWorker()` + +Returns `Promise` - Resolves with a boolean depending on whether or not the current `webContents` has a registered ServiceWorker + #### `contents.unregisterServiceWorker(callback)` * `callback` Function diff --git a/lib/browser/api/web-contents.js b/lib/browser/api/web-contents.js index 87c0c126a5d5..2b8a5c5fce2e 100644 --- a/lib/browser/api/web-contents.js +++ b/lib/browser/api/web-contents.js @@ -375,6 +375,7 @@ WebContents.prototype._init = function () { this.setMaxListeners(0) this.capturePage = deprecate.promisify(this.capturePage) + this.hasServiceWorker = deprecate.promisify(this.hasServiceWorker) // Dispatch IPC messages to the ipc module. this.on('-ipc-message', function (event, internal, channel, args) { diff --git a/spec/api-web-contents-spec.js b/spec/api-web-contents-spec.js index 84730ceb007b..d5c60c9d82ef 100644 --- a/spec/api-web-contents-spec.js +++ b/spec/api-web-contents-spec.js @@ -228,6 +228,23 @@ describe('webContents module', () => { }) }) + describe('ServiceWorker APIs', () => { + it('can successfully register a ServiceWorker', async () => { + await w.loadFile(path.join(fixtures, 'api', 'service-worker', 'service-worker.html')) + const hasSW = await w.webContents.hasServiceWorker() + expect(hasSW).to.be.true() + }) + + it('can successfully register a ServiceWorker (callback)', (done) => { + w.loadFile(path.join(fixtures, 'api', 'service-worker', 'service-worker.html')).then(() => { + w.webContents.hasServiceWorker(hasSW => { + expect(hasSW).to.be.true() + done() + }) + }) + }) + }) + describe('isCurrentlyAudible() API', () => { it('returns whether audio is playing', async () => { const webContents = remote.getCurrentWebContents() diff --git a/spec/fixtures/api/service-worker/service-worker.html b/spec/fixtures/api/service-worker/service-worker.html new file mode 100644 index 000000000000..9b05c23b2eaa --- /dev/null +++ b/spec/fixtures/api/service-worker/service-worker.html @@ -0,0 +1,12 @@ + + + + diff --git a/spec/fixtures/api/service-worker/service-worker.js b/spec/fixtures/api/service-worker/service-worker.js new file mode 100644 index 000000000000..8ae15aa448f8 --- /dev/null +++ b/spec/fixtures/api/service-worker/service-worker.js @@ -0,0 +1,5 @@ +console.log('Service worker startups.') + +self.addEventListener('install', (event) => { + console.log('Service worker installed.') +})