render: executejavascript with option to simulate usergesture

This commit is contained in:
Robo 2015-07-30 14:42:03 +05:30
parent 2ab079dc7d
commit 239d535cac
7 changed files with 40 additions and 8 deletions

View file

@ -572,8 +572,9 @@ void WebContents::InsertCSS(const std::string& css) {
web_contents()->InsertCSS(css); web_contents()->InsertCSS(css);
} }
void WebContents::ExecuteJavaScript(const base::string16& code) { void WebContents::ExecuteJavaScript(const base::string16& code,
web_contents()->GetMainFrame()->ExecuteJavaScript(code); bool has_user_gesture) {
Send(new AtomViewMsg_ExecuteJavaScript(routing_id(), code, has_user_gesture));
} }
void WebContents::OpenDevTools(mate::Arguments* args) { void WebContents::OpenDevTools(mate::Arguments* args) {

View file

@ -65,7 +65,8 @@ class WebContents : public mate::TrackableObject<WebContents>,
void SetUserAgent(const std::string& user_agent); void SetUserAgent(const std::string& user_agent);
std::string GetUserAgent(); std::string GetUserAgent();
void InsertCSS(const std::string& css); void InsertCSS(const std::string& css);
void ExecuteJavaScript(const base::string16& code); void ExecuteJavaScript(const base::string16& code,
bool has_user_gesture);
void OpenDevTools(mate::Arguments* args); void OpenDevTools(mate::Arguments* args);
void CloseDevTools(); void CloseDevTools();
bool IsDevToolsOpened(); bool IsDevToolsOpened();

View file

@ -46,11 +46,11 @@ wrapWebContents = (webContents) ->
# web contents has been loaded. # web contents has been loaded.
webContents.loaded = false webContents.loaded = false
webContents.once 'did-finish-load', -> @loaded = true webContents.once 'did-finish-load', -> @loaded = true
webContents.executeJavaScript = (code) -> webContents.executeJavaScript = (code, hasUserGesture=false) ->
if @loaded if @loaded
@_executeJavaScript code @_executeJavaScript code, hasUserGesture
else else
webContents.once 'did-finish-load', @_executeJavaScript.bind(this, code) webContents.once 'did-finish-load', @_executeJavaScript.bind(this, code, hasUserGesture)
# The navigation controller. # The navigation controller.
controller = new NavigationController(webContents) controller = new NavigationController(webContents)

View file

@ -34,6 +34,10 @@ IPC_MESSAGE_ROUTED2(AtomViewMsg_Message,
base::string16 /* channel */, base::string16 /* channel */,
base::ListValue /* arguments */) base::ListValue /* arguments */)
IPC_MESSAGE_ROUTED2(AtomViewMsg_ExecuteJavaScript,
base::string16 /* code */,
bool /* has user gesture */)
// Sent by the renderer when the draggable regions are updated. // Sent by the renderer when the draggable regions are updated.
IPC_MESSAGE_ROUTED1(AtomViewHostMsg_UpdateDraggableRegions, IPC_MESSAGE_ROUTED1(AtomViewHostMsg_UpdateDraggableRegions,
std::vector<atom::DraggableRegion> /* regions */) std::vector<atom::DraggableRegion> /* regions */)

View file

@ -26,6 +26,8 @@
#include "third_party/WebKit/public/web/WebFrame.h" #include "third_party/WebKit/public/web/WebFrame.h"
#include "third_party/WebKit/public/web/WebLocalFrame.h" #include "third_party/WebKit/public/web/WebLocalFrame.h"
#include "third_party/WebKit/public/web/WebKit.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 "third_party/WebKit/public/web/WebView.h"
#include "ui/base/resource/resource_bundle.h" #include "ui/base/resource/resource_bundle.h"
@ -113,6 +115,8 @@ bool AtomRenderViewObserver::OnMessageReceived(const IPC::Message& message) {
bool handled = true; bool handled = true;
IPC_BEGIN_MESSAGE_MAP(AtomRenderViewObserver, message) IPC_BEGIN_MESSAGE_MAP(AtomRenderViewObserver, message)
IPC_MESSAGE_HANDLER(AtomViewMsg_Message, OnBrowserMessage) IPC_MESSAGE_HANDLER(AtomViewMsg_Message, OnBrowserMessage)
IPC_MESSAGE_HANDLER(AtomViewMsg_ExecuteJavaScript,
OnJavaScriptExecuteRequest)
IPC_MESSAGE_UNHANDLED(handled = false) IPC_MESSAGE_UNHANDLED(handled = false)
IPC_END_MESSAGE_MAP() IPC_END_MESSAGE_MAP()
@ -143,4 +147,23 @@ 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 } // namespace atom

View file

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

View file

@ -905,11 +905,12 @@ Returns a `String` represents the user agent for this page.
Injects CSS into this page. Injects CSS into this page.
### WebContents.executeJavaScript(code) ### WebContents.executeJavaScript(code, userGesture)
* `code` String * `code` String
* `userGesture` Boolean - Default false
Evaluates `code` in page. Evaluates `code` in page. If `userGesture` is set will simulate user gesture.
### WebContents.setAudioMuted(muted) ### WebContents.setAudioMuted(muted)