feat: promisify webContents.hasServiceWorker() (#16535)
* feat: promisify contents.hasServiceWorker() * spec: add initial test for hasServiceWorker()
This commit is contained in:
parent
5a35c3a279
commit
d105dcc0d3
8 changed files with 60 additions and 23 deletions
|
@ -1433,28 +1433,25 @@ void WebContents::InspectServiceWorker() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void WebContents::HasServiceWorker(const base::Callback<void(bool)>& callback) {
|
void OnServiceWorkerCheckDone(scoped_refptr<util::Promise> promise,
|
||||||
|
content::ServiceWorkerCapability capability) {
|
||||||
|
promise->Resolve(capability !=
|
||||||
|
content::ServiceWorkerCapability::NO_SERVICE_WORKER);
|
||||||
|
}
|
||||||
|
|
||||||
|
v8::Local<v8::Promise> WebContents::HasServiceWorker() {
|
||||||
|
scoped_refptr<util::Promise> promise = new util::Promise(isolate());
|
||||||
auto* context = GetServiceWorkerContext(web_contents());
|
auto* context = GetServiceWorkerContext(web_contents());
|
||||||
if (!context)
|
if (!context) {
|
||||||
return;
|
promise->RejectWithErrorMessage("Unable to get ServiceWorker context.");
|
||||||
|
}
|
||||||
struct WrappedCallback {
|
|
||||||
base::Callback<void(bool)> callback_;
|
|
||||||
explicit WrappedCallback(const base::Callback<void(bool)>& callback)
|
|
||||||
: callback_(callback) {}
|
|
||||||
void Run(content::ServiceWorkerCapability capability) {
|
|
||||||
callback_.Run(capability !=
|
|
||||||
content::ServiceWorkerCapability::NO_SERVICE_WORKER);
|
|
||||||
delete this;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
auto* wrapped_callback = new WrappedCallback(callback);
|
|
||||||
|
|
||||||
context->CheckHasServiceWorker(
|
context->CheckHasServiceWorker(
|
||||||
web_contents()->GetLastCommittedURL(), GURL::EmptyGURL(),
|
web_contents()->GetLastCommittedURL(),
|
||||||
base::BindOnce(&WrappedCallback::Run,
|
web_contents()->GetLastCommittedURL(),
|
||||||
base::Unretained(wrapped_callback)));
|
base::BindOnce(&OnServiceWorkerCheckDone, promise));
|
||||||
|
|
||||||
|
return promise->GetHandle();
|
||||||
}
|
}
|
||||||
|
|
||||||
void WebContents::UnregisterServiceWorker(
|
void WebContents::UnregisterServiceWorker(
|
||||||
|
@ -1462,7 +1459,6 @@ void WebContents::UnregisterServiceWorker(
|
||||||
auto* context = GetServiceWorkerContext(web_contents());
|
auto* context = GetServiceWorkerContext(web_contents());
|
||||||
if (!context)
|
if (!context)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
context->UnregisterServiceWorker(web_contents()->GetLastCommittedURL(),
|
context->UnregisterServiceWorker(web_contents()->GetLastCommittedURL(),
|
||||||
callback);
|
callback);
|
||||||
}
|
}
|
||||||
|
|
|
@ -161,7 +161,7 @@ class WebContents : public mate::TrackableObject<WebContents>,
|
||||||
void DisableDeviceEmulation();
|
void DisableDeviceEmulation();
|
||||||
void InspectElement(int x, int y);
|
void InspectElement(int x, int y);
|
||||||
void InspectServiceWorker();
|
void InspectServiceWorker();
|
||||||
void HasServiceWorker(const base::Callback<void(bool)>&);
|
v8::Local<v8::Promise> HasServiceWorker();
|
||||||
void UnregisterServiceWorker(const base::Callback<void(bool)>&);
|
void UnregisterServiceWorker(const base::Callback<void(bool)>&);
|
||||||
void SetIgnoreMenuShortcuts(bool ignore);
|
void SetIgnoreMenuShortcuts(bool ignore);
|
||||||
void SetAudioMuted(bool muted);
|
void SetAudioMuted(bool muted);
|
||||||
|
|
|
@ -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.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)
|
- [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)
|
- [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.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.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)
|
- [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.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)
|
- [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.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.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.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)
|
- [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)
|
- [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)
|
- [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)
|
- [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)
|
||||||
|
|
|
@ -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
|
Checks if any ServiceWorker is registered and returns a boolean as
|
||||||
response to `callback`.
|
response to `callback`.
|
||||||
|
|
||||||
|
**[Deprecated Soon](promisification.md)**
|
||||||
|
|
||||||
|
#### `contents.hasServiceWorker()`
|
||||||
|
|
||||||
|
Returns `Promise<Boolean>` - Resolves with a boolean depending on whether or not the current `webContents` has a registered ServiceWorker
|
||||||
|
|
||||||
#### `contents.unregisterServiceWorker(callback)`
|
#### `contents.unregisterServiceWorker(callback)`
|
||||||
|
|
||||||
* `callback` Function
|
* `callback` Function
|
||||||
|
|
|
@ -375,6 +375,7 @@ WebContents.prototype._init = function () {
|
||||||
this.setMaxListeners(0)
|
this.setMaxListeners(0)
|
||||||
|
|
||||||
this.capturePage = deprecate.promisify(this.capturePage)
|
this.capturePage = deprecate.promisify(this.capturePage)
|
||||||
|
this.hasServiceWorker = deprecate.promisify(this.hasServiceWorker)
|
||||||
|
|
||||||
// Dispatch IPC messages to the ipc module.
|
// Dispatch IPC messages to the ipc module.
|
||||||
this.on('-ipc-message', function (event, internal, channel, args) {
|
this.on('-ipc-message', function (event, internal, channel, args) {
|
||||||
|
|
|
@ -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', () => {
|
describe('isCurrentlyAudible() API', () => {
|
||||||
it('returns whether audio is playing', async () => {
|
it('returns whether audio is playing', async () => {
|
||||||
const webContents = remote.getCurrentWebContents()
|
const webContents = remote.getCurrentWebContents()
|
||||||
|
|
12
spec/fixtures/api/service-worker/service-worker.html
vendored
Normal file
12
spec/fixtures/api/service-worker/service-worker.html
vendored
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
<html>
|
||||||
|
<body>
|
||||||
|
<script>
|
||||||
|
window.onerror = (err) => console.log(error)
|
||||||
|
|
||||||
|
navigator.serviceWorker.register('service-worker.js', {
|
||||||
|
scope: './'
|
||||||
|
}).then(sw => {
|
||||||
|
console.log("registered")
|
||||||
|
}).catch(err => console.log(error))
|
||||||
|
</script>
|
||||||
|
</body>
|
5
spec/fixtures/api/service-worker/service-worker.js
vendored
Normal file
5
spec/fixtures/api/service-worker/service-worker.js
vendored
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
console.log('Service worker startups.')
|
||||||
|
|
||||||
|
self.addEventListener('install', (event) => {
|
||||||
|
console.log('Service worker installed.')
|
||||||
|
})
|
Loading…
Reference in a new issue