Skip to content

Commit

Permalink
feat: promisify webContents.hasServiceWorker() (#16535)
Browse files Browse the repository at this point in the history
* feat: promisify contents.hasServiceWorker()

* spec: add initial test for hasServiceWorker()
  • Loading branch information
codebytere authored Jan 26, 2019
1 parent 5a35c3a commit d105dcc
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 23 deletions.
36 changes: 16 additions & 20 deletions atom/browser/api/atom_api_web_contents.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1433,36 +1433,32 @@ 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 !=
content::ServiceWorkerCapability::NO_SERVICE_WORKER);
delete this;
}
};
void OnServiceWorkerCheckDone(scoped_refptr<util::Promise> promise,
content::ServiceWorkerCapability capability) {
promise->Resolve(capability !=
content::ServiceWorkerCapability::NO_SERVICE_WORKER);
}

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(
const base::Callback<void(bool)>& callback) {
auto* context = GetServiceWorkerContext(web_contents());
if (!context)
return;

context->UnregisterServiceWorker(web_contents()->GetLastCommittedURL(),
callback);
}
Expand Down
2 changes: 1 addition & 1 deletion atom/browser/api/atom_api_web_contents.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions docs/api/promisification.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
6 changes: 6 additions & 0 deletions docs/api/web-contents.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions lib/browser/api/web-contents.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
17 changes: 17 additions & 0 deletions spec/api-web-contents-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
12 changes: 12 additions & 0 deletions spec/fixtures/api/service-worker/service-worker.html
Original file line number Diff line number Diff line change
@@ -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 changes: 5 additions & 0 deletions spec/fixtures/api/service-worker/service-worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
console.log('Service worker startups.')

self.addEventListener('install', (event) => {
console.log('Service worker installed.')
})

0 comments on commit d105dcc

Please sign in to comment.