8000 feat: promisify webContents.hasServiceWorker() by codebytere · Pull Request #16535 · electron/electron · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat: promisify webContents.hasServiceWorker() #16535

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
< 6D47 tr data-hunk="8e1f6633e836ddcb8149e3c429735cad9de682d0caa65f4e696aaf78dac14e51" class="show-top-border">
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. => 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