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) {
|
||||
auto* context = GetServiceWorkerContext(web_contents());
|
||||
if (!context)
|
||||
return;
|
||||
|
||||
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 !=
|
||||
void OnServiceWorkerCheckDone(scoped_refptr<util::Promise> promise,
|
||||
content::ServiceWorkerCapability capability) {
|
||||
promise->Resolve(capability !=
|
||||
content::ServiceWorkerCapability::NO_SERVICE_WORKER);
|
||||
delete this;
|
||||
}
|
||||
};
|
||||
|
||||
auto* wrapped_callback = new WrappedCallback(callback);
|
||||
v8::Local<v8::Promise> WebContents::HasServiceWorker() {
|
||||
scoped_refptr<util::Promise> promise = new util::Promise(isolate());
|
||||
auto* context = GetServiceWorkerContext(web_contents());
|
||||
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);
|
||||
}
|
||||
|
|
|
@ -161,7 +161,7 @@ class WebContents : public mate::TrackableObject<WebContents>,
|
|||
void DisableDeviceEmulation();
|
||||
void InspectElement(int x, int y);
|
||||
void InspectServiceWorker();
|
||||
void HasServiceWorker(const base::Callback<void(bool)>&);
|
||||
v8::Local<v8::Promise> HasServiceWorker();
|
||||
void UnregisterServiceWorker(const base::Callback<void(bool)>&);
|
||||
void SetIgnoreMenuShortcuts(bool ignore);
|
||||
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.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)
|
||||
|
|
|
@ -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<Boolean>` - Resolves with a boolean depending on whether or not the current `webContents` has a registered ServiceWorker
|
||||
|
||||
#### `contents.unregisterServiceWorker(callback)`
|
||||
|
||||
* `callback` Function
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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()
|
||||
|
|
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