Handle executeJavaScript in JavaScript

This commit is contained in:
Cheng Zhao 2016-01-13 12:11:46 +08:00
parent 5b7d1a9890
commit 55dfddba77
8 changed files with 28 additions and 52 deletions

View file

@ -15,6 +15,8 @@
#include "native_mate/object_template_builder.h"
#include "third_party/WebKit/public/web/WebDocument.h"
#include "third_party/WebKit/public/web/WebLocalFrame.h"
#include "third_party/WebKit/public/web/WebScopedUserGesture.h"
#include "third_party/WebKit/public/web/WebScriptSource.h"
#include "third_party/WebKit/public/web/WebSecurityPolicy.h"
#include "third_party/WebKit/public/web/WebView.h"
@ -120,6 +122,12 @@ void WebFrame::InsertText(const std::string& text) {
web_frame_->insertText(blink::WebString::fromUTF8(text));
}
void WebFrame::ExecuteJavaScript(const base::string16& code, bool by_user) {
scoped_ptr<blink::WebScopedUserGesture> gesture(
by_user ? new blink::WebScopedUserGesture : nullptr);
web_frame_->executeScriptAndReturnValue(blink::WebScriptSource(code));
}
mate::ObjectTemplateBuilder WebFrame::GetObjectTemplateBuilder(
v8::Isolate* isolate) {
return mate::ObjectTemplateBuilder(isolate)
@ -141,7 +149,8 @@ mate::ObjectTemplateBuilder WebFrame::GetObjectTemplateBuilder(
&WebFrame::RegisterURLSchemeAsBypassingCSP)
.SetMethod("registerURLSchemeAsPrivileged",
&WebFrame::RegisterURLSchemeAsPrivileged)
.SetMethod("insertText", &WebFrame::InsertText);
.SetMethod("insertText", &WebFrame::InsertText)
.SetMethod("executeJavaScript", &WebFrame::ExecuteJavaScript);
}
// static

View file

@ -63,6 +63,9 @@ class WebFrame : public mate::Wrappable {
// Editing.
void InsertText(const std::string& text);
// Excecuting scripts.
void ExecuteJavaScript(const base::string16& code, bool by_user);
// mate::Wrappable:
virtual mate::ObjectTemplateBuilder GetObjectTemplateBuilder(
v8::Isolate* isolate);

View file

@ -27,8 +27,6 @@
#include "third_party/WebKit/public/web/WebFrame.h"
#include "third_party/WebKit/public/web/WebLocalFrame.h"
#include "third_party/WebKit/public/web/WebKit.h"
#include "third_party/WebKit/public/web/WebScopedUserGesture.h"
#include "third_party/WebKit/public/web/WebScriptSource.h"
#include "third_party/WebKit/public/web/WebView.h"
#include "ui/base/resource/resource_bundle.h"
#include "native_mate/dictionary.h"
@ -115,8 +113,6 @@ bool AtomRenderViewObserver::OnMessageReceived(const IPC::Message& message) {
bool handled = true;
IPC_BEGIN_MESSAGE_MAP(AtomRenderViewObserver, message)
IPC_MESSAGE_HANDLER(AtomViewMsg_Message, OnBrowserMessage)
IPC_MESSAGE_HANDLER(AtomViewMsg_ExecuteJavaScript,
OnJavaScriptExecuteRequest)
IPC_MESSAGE_UNHANDLED(handled = false)
IPC_END_MESSAGE_MAP()
@ -152,22 +148,4 @@ void AtomRenderViewObserver::OnBrowserMessage(const base::string16& channel,
}
}
void AtomRenderViewObserver::OnJavaScriptExecuteRequest(
const base::string16& code, bool has_user_gesture) {
if (!document_created_)
return;
if (!render_view()->GetWebView())
return;
scoped_ptr<blink::WebScopedUserGesture> gesture(
has_user_gesture ? new blink::WebScopedUserGesture : nullptr);
v8::Isolate* isolate = blink::mainThreadIsolate();
v8::HandleScope handle_scope(isolate);
blink::WebFrame* frame = render_view()->GetWebView()->mainFrame();
frame->executeScriptAndReturnValue(blink::WebScriptSource(code));
}
} // namespace atom

View file

@ -32,8 +32,6 @@ class AtomRenderViewObserver : public content::RenderViewObserver {
void OnBrowserMessage(const base::string16& channel,
const base::ListValue& args);
void OnJavaScriptExecuteRequest(const base::string16& code,
bool has_user_gesture);
// Weak reference to renderer client.
AtomRendererClient* renderer_client_;