]> khamidou — GitHub mirrors - electron.git/commitdiff
webContents: adding serviceworker helper utilities
authordeepak1556 <hop2deep@gmail.com>
Thu, 30 Apr 2015 13:15:19 +0000 (18:45 +0530)
committerdeepak1556 <hop2deep@gmail.com>
Fri, 1 May 2015 06:32:57 +0000 (12:02 +0530)
atom/browser/api/atom_api_web_contents.cc
atom/browser/api/atom_api_web_contents.h
docs/api/browser-window.md

index 0400e9457e37fade9a511554da1a06c6a050c1dd..00a8d1a786f0eecaffaa8ae33f435d18e8ca85f0 100644 (file)
 #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<void(bool)>& callback) {
+  auto context = GetServiceWorkerContext(web_contents());
+  if (!context)
+    return;
+
+  context->CheckHasServiceWorker(web_contents()->GetLastCommittedURL(),
+                                 GURL::EmptyGURL(),
+                                 callback);
+}
+
+void WebContents::UnregisterServiceWorker(
+    const base::Callback<void(bool)>& 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(
index 371c338708cc1fa98965e9b5bbcb0cc14c99d0e3..d6c3e360ce728304d8f5faccf548fbed00c550b0 100644 (file)
@@ -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(bool)>&);
+  void UnregisterServiceWorker(const base::Callback<void(bool)>&);
 
   // Editing commands.
   void Undo();
index e4cec40f47cf5e43ffc84fe6ab42a8112433df3a..84985391871f8af0f8c1af7d76ac7f400e37a091 100644 (file)
@@ -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