Merge pull request #1535 from deepak1556/contents_patch

webContents: adding serviceworker helper utilities
This commit is contained in:
Cheng Zhao 2015-05-01 14:56:35 +08:00
commit 9300859983
3 changed files with 60 additions and 0 deletions

View file

@ -28,8 +28,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"
@ -54,6 +57,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)
@ -565,6 +584,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())
@ -602,6 +642,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(

View file

@ -64,6 +64,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();

View file

@ -852,6 +852,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