Merge pull request #2383 from deepak1556/render_script_execution_patch
render: executejavascript with option to create usergesture context
This commit is contained in:
		
				commit
				
					
						d455232eb1
					
				
			
		
					 10 changed files with 62 additions and 10 deletions
				
			
		| 
						 | 
				
			
			@ -572,8 +572,9 @@ void WebContents::InsertCSS(const std::string& css) {
 | 
			
		|||
  web_contents()->InsertCSS(css);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void WebContents::ExecuteJavaScript(const base::string16& code) {
 | 
			
		||||
  web_contents()->GetMainFrame()->ExecuteJavaScript(code);
 | 
			
		||||
void WebContents::ExecuteJavaScript(const base::string16& code,
 | 
			
		||||
                                    bool has_user_gesture) {
 | 
			
		||||
  Send(new AtomViewMsg_ExecuteJavaScript(routing_id(), code, has_user_gesture));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void WebContents::OpenDevTools(mate::Arguments* args) {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -65,7 +65,8 @@ class WebContents : public mate::TrackableObject<WebContents>,
 | 
			
		|||
  void SetUserAgent(const std::string& user_agent);
 | 
			
		||||
  std::string GetUserAgent();
 | 
			
		||||
  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 CloseDevTools();
 | 
			
		||||
  bool IsDevToolsOpened();
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -46,11 +46,11 @@ wrapWebContents = (webContents) ->
 | 
			
		|||
  # web contents has been loaded.
 | 
			
		||||
  webContents.loaded = false
 | 
			
		||||
  webContents.once 'did-finish-load', -> @loaded = true
 | 
			
		||||
  webContents.executeJavaScript = (code) ->
 | 
			
		||||
  webContents.executeJavaScript = (code, hasUserGesture=false) ->
 | 
			
		||||
    if @loaded
 | 
			
		||||
      @_executeJavaScript code
 | 
			
		||||
      @_executeJavaScript code, hasUserGesture
 | 
			
		||||
    else
 | 
			
		||||
      webContents.once 'did-finish-load', @_executeJavaScript.bind(this, code)
 | 
			
		||||
      webContents.once 'did-finish-load', @_executeJavaScript.bind(this, code, hasUserGesture)
 | 
			
		||||
 | 
			
		||||
  # The navigation controller.
 | 
			
		||||
  controller = new NavigationController(webContents)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -34,6 +34,10 @@ IPC_MESSAGE_ROUTED2(AtomViewMsg_Message,
 | 
			
		|||
                    base::string16 /* channel */,
 | 
			
		||||
                    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.
 | 
			
		||||
IPC_MESSAGE_ROUTED1(AtomViewHostMsg_UpdateDraggableRegions,
 | 
			
		||||
                    std::vector<atom::DraggableRegion> /* regions */)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -26,6 +26,8 @@
 | 
			
		|||
#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"
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -113,6 +115,8 @@ 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()
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -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
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -32,6 +32,8 @@ 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_;
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -905,11 +905,14 @@ Returns a `String` represents the user agent for this page.
 | 
			
		|||
 | 
			
		||||
Injects CSS into this page.
 | 
			
		||||
 | 
			
		||||
### WebContents.executeJavaScript(code)
 | 
			
		||||
### WebContents.executeJavaScript(code, userGesture)
 | 
			
		||||
 | 
			
		||||
* `code` String
 | 
			
		||||
* `userGesture` Boolean - Default false
 | 
			
		||||
 | 
			
		||||
Evaluates `code` in page.
 | 
			
		||||
Evaluates `code` in page. If `userGesture` is set will create user gesture context,
 | 
			
		||||
HTML api like `requestFullScreen` which require user action can take advantage
 | 
			
		||||
of this option for automation.
 | 
			
		||||
 | 
			
		||||
### WebContents.setAudioMuted(muted)
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -225,11 +225,14 @@ Returns a `String` represents the user agent for guest page.
 | 
			
		|||
 | 
			
		||||
Injects CSS into guest page.
 | 
			
		||||
 | 
			
		||||
### `<webview>`.executeJavaScript(code)
 | 
			
		||||
### `<webview>`.executeJavaScript(code, userGesture)
 | 
			
		||||
 | 
			
		||||
* `code` String
 | 
			
		||||
* `userGesture` Boolean - Default false
 | 
			
		||||
 | 
			
		||||
Evaluates `code` in guest page.
 | 
			
		||||
Evaluates `code` in page. If `userGesture` is set will create user gesture context,
 | 
			
		||||
HTML api like `requestFullScreen` which require user action can take advantage
 | 
			
		||||
of this option for automation.
 | 
			
		||||
 | 
			
		||||
### `<webview>`.openDevTools()
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										1
									
								
								spec/fixtures/pages/fullscreen.html
									
										
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								spec/fixtures/pages/fullscreen.html
									
										
									
									
										vendored
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1 @@
 | 
			
		|||
<video></video>
 | 
			
		||||
| 
						 | 
				
			
			@ -261,3 +261,17 @@ describe '<webview> tag', ->
 | 
			
		|||
          done()
 | 
			
		||||
        webview.src = "file://#{fixtures}/pages/dom-ready.html?port=#{port}"
 | 
			
		||||
        document.body.appendChild webview
 | 
			
		||||
 | 
			
		||||
  describe 'executeJavaScript', ->
 | 
			
		||||
    it 'should support user gesture', (done) ->
 | 
			
		||||
      listener = (e) ->
 | 
			
		||||
        webview.removeEventListener 'enter-html-full-screen', listener
 | 
			
		||||
        done()
 | 
			
		||||
      listener2 = (e) ->
 | 
			
		||||
        jsScript = 'document.getElementsByTagName("video")[0].webkitRequestFullScreen()'
 | 
			
		||||
        webview.executeJavaScript jsScript, true
 | 
			
		||||
        webview.removeEventListener 'did-finish-load', listener2
 | 
			
		||||
      webview.addEventListener 'enter-html-full-screen', listener
 | 
			
		||||
      webview.addEventListener 'did-finish-load', listener2
 | 
			
		||||
      webview.src = "file://#{fixtures}/pages/fullscreen.html"
 | 
			
		||||
      document.body.appendChild webview
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue