feat: promisify webContents.hasServiceWorker() (#16535)

* feat: promisify contents.hasServiceWorker()

* spec: add initial test for hasServiceWorker()
This commit is contained in:
Shelley Vohr 2019-01-26 10:23:16 -08:00 committed by GitHub
parent 5a35c3a279
commit d105dcc0d3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 60 additions and 23 deletions

View file

@ -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());
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;
}
};
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);
}

View file

@ -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);