From d76bd4a10394c77a2131f39fc952f981d7b75470 Mon Sep 17 00:00:00 2001 From: deepak1556 Date: Thu, 30 Apr 2015 18:45:19 +0530 Subject: [PATCH] webContents: adding serviceworker helper utilities --- atom/browser/api/atom_api_web_contents.cc | 43 +++++++++++++++++++++++ atom/browser/api/atom_api_web_contents.h | 2 ++ docs/api/browser-window.md | 15 ++++++++ 3 files changed, 60 insertions(+) diff --git a/atom/browser/api/atom_api_web_contents.cc b/atom/browser/api/atom_api_web_contents.cc index 0400e9457e3..00a8d1a786f 100644 --- a/atom/browser/api/atom_api_web_contents.cc +++ b/atom/browser/api/atom_api_web_contents.cc @@ -27,8 +27,11 @@ #include "content/public/browser/render_view_host.h" #include "content/public/browser/render_widget_host_view.h" #include "content/public/browser/resource_request_details.h" +#include "content/public/browser/service_worker_context.h" +#include "content/public/browser/storage_partition.h" #include "content/public/browser/site_instance.h" #include "content/public/browser/web_contents.h" +#include "native_mate/callback.h" #include "native_mate/dictionary.h" #include "native_mate/object_template_builder.h" @@ -53,6 +56,22 @@ NativeWindow* GetWindowFromGuest(const content::WebContents* guest) { return nullptr; } +content::ServiceWorkerContext* GetServiceWorkerContext( + const content::WebContents* web_contents) { + auto context = web_contents->GetBrowserContext(); + auto site_instance = web_contents->GetSiteInstance(); + if (!context || !site_instance) + return nullptr; + + content::StoragePartition* storage_partition = + content::BrowserContext::GetStoragePartition( + context, site_instance); + + DCHECK(storage_partition); + + return storage_partition->GetServiceWorkerContext(); +} + } // namespace WebContents::WebContents(content::WebContents* web_contents) @@ -548,6 +567,27 @@ void WebContents::SetAllowTransparency(bool allow) { } } +void WebContents::HasServiceWorker( + const base::Callback& callback) { + auto context = GetServiceWorkerContext(web_contents()); + if (!context) + return; + + context->CheckHasServiceWorker(web_contents()->GetLastCommittedURL(), + GURL::EmptyGURL(), + callback); +} + +void WebContents::UnregisterServiceWorker( + const base::Callback& callback) { + auto context = GetServiceWorkerContext(web_contents()); + if (!context) + return; + + context->UnregisterServiceWorker(web_contents()->GetLastCommittedURL(), + callback); +} + mate::ObjectTemplateBuilder WebContents::GetObjectTemplateBuilder( v8::Isolate* isolate) { if (template_.IsEmpty()) @@ -585,6 +625,9 @@ mate::ObjectTemplateBuilder WebContents::GetObjectTemplateBuilder( .SetMethod("setAutoSize", &WebContents::SetAutoSize) .SetMethod("setAllowTransparency", &WebContents::SetAllowTransparency) .SetMethod("isGuest", &WebContents::is_guest) + .SetMethod("hasServiceWorker", &WebContents::HasServiceWorker) + .SetMethod("unregisterServiceWorker", + &WebContents::UnregisterServiceWorker) .Build()); return mate::ObjectTemplateBuilder( diff --git a/atom/browser/api/atom_api_web_contents.h b/atom/browser/api/atom_api_web_contents.h index 371c338708c..d6c3e360ce7 100644 --- a/atom/browser/api/atom_api_web_contents.h +++ b/atom/browser/api/atom_api_web_contents.h @@ -63,6 +63,8 @@ class WebContents : public mate::EventEmitter, void CloseDevTools(); bool IsDevToolsOpened(); void InspectElement(int x, int y); + void HasServiceWorker(const base::Callback&); + void UnregisterServiceWorker(const base::Callback&); // Editing commands. void Undo(); diff --git a/docs/api/browser-window.md b/docs/api/browser-window.md index e4cec40f47c..84985391871 100644 --- a/docs/api/browser-window.md +++ b/docs/api/browser-window.md @@ -846,6 +846,21 @@ Executes editing command `replace` in page. Executes editing command `replaceMisspelling` in page. +### WebContents.hasServiceWorker(callback) + +* `callback` Function + +Checks if any serviceworker is registered and returns boolean as +response to `callback`. + +### WebContents.unregisterServiceWorker(callback) + +* `callback` Function + +Unregisters any serviceworker if present and returns boolean as +response to `callback` when the JS promise is fullfilled or false +when the JS promise is rejected. + ### WebContents.send(channel[, args...]) * `channel` String