From 42863e4700a19acd57ed425e91c4054fae40f8e6 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Fri, 18 Sep 2015 14:20:31 +0800 Subject: [PATCH] Move SendInputEvent to WebContents --- atom/browser/api/atom_api_web_contents.cc | 40 +++++++++++++++++++++++ atom/browser/api/atom_api_web_contents.h | 5 ++- atom/browser/api/atom_api_window.cc | 31 ------------------ atom/browser/api/atom_api_window.h | 1 - atom/browser/native_window.cc | 36 -------------------- atom/browser/native_window.h | 10 ------ 6 files changed, 44 insertions(+), 79 deletions(-) diff --git a/atom/browser/api/atom_api_web_contents.cc b/atom/browser/api/atom_api_web_contents.cc index d4cb6cd227d5..43b20a3d7135 100644 --- a/atom/browser/api/atom_api_web_contents.cc +++ b/atom/browser/api/atom_api_web_contents.cc @@ -30,12 +30,14 @@ #include "chrome/browser/printing/print_preview_message_handler.h" #include "content/common/view_messages.h" #include "content/public/browser/favicon_status.h" +#include "content/public/browser/native_web_keyboard_event.h" #include "content/public/browser/navigation_details.h" #include "content/public/browser/navigation_entry.h" #include "content/public/browser/plugin_service.h" #include "content/public/browser/render_frame_host.h" #include "content/public/browser/render_process_host.h" #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" @@ -46,6 +48,7 @@ #include "net/http/http_response_headers.h" #include "net/url_request/static_http_user_agent_settings.h" #include "net/url_request/url_request_context.h" +#include "third_party/WebKit/public/web/WebInputEvent.h" #include "atom/common/node_includes.h" @@ -813,6 +816,42 @@ bool WebContents::SendIPCMessage(const base::string16& channel, return Send(new AtomViewMsg_Message(routing_id(), channel, args)); } +void WebContents::SendInputEvent(v8::Isolate* isolate, + v8::Local input_event) { + const auto view = web_contents()->GetRenderWidgetHostView(); + if (!view) + return; + const auto host = view->GetRenderWidgetHost(); + if (!host) + return; + + int type = mate::GetWebInputEventType(isolate, input_event); + if (blink::WebInputEvent::isMouseEventType(type)) { + blink::WebMouseEvent mouse_event; + if (mate::ConvertFromV8(isolate, input_event, &mouse_event)) { + host->ForwardMouseEvent(mouse_event); + return; + } + } else if (blink::WebInputEvent::isKeyboardEventType(type)) { + content::NativeWebKeyboardEvent keyboard_event;; + if (mate::ConvertFromV8(isolate, input_event, &keyboard_event)) { + host->ForwardKeyboardEvent(keyboard_event); + return; + } + } else if (type == blink::WebInputEvent::MouseWheel) { + blink::WebMouseWheelEvent mouse_wheel_event; + if (mate::ConvertFromV8(isolate, input_event, &mouse_wheel_event)) { + host->ForwardWheelEvent(mouse_wheel_event); + return; + } + } + + isolate->ThrowException(v8::Exception::Error(mate::StringToV8( + isolate, "Invalid event type"))); +} + + + void WebContents::SetSize(const SetSizeParams& params) { if (guest_delegate_) guest_delegate_->SetSize(params); @@ -875,6 +914,7 @@ mate::ObjectTemplateBuilder WebContents::GetObjectTemplateBuilder( .SetMethod("focus", &WebContents::Focus) .SetMethod("tabTraverse", &WebContents::TabTraverse) .SetMethod("_send", &WebContents::SendIPCMessage, true) + .SetMethod("sendInputEvent", &WebContents::SendInputEvent) .SetMethod("setSize", &WebContents::SetSize) .SetMethod("setAllowTransparency", &WebContents::SetAllowTransparency) .SetMethod("isGuest", &WebContents::IsGuest) diff --git a/atom/browser/api/atom_api_web_contents.h b/atom/browser/api/atom_api_web_contents.h index 0ce47237e494..6dc266053255 100644 --- a/atom/browser/api/atom_api_web_contents.h +++ b/atom/browser/api/atom_api_web_contents.h @@ -114,10 +114,13 @@ class WebContents : public mate::TrackableObject, void Focus(); void TabTraverse(bool reverse); - // Sending messages to browser. + // Send messages to browser. bool SendIPCMessage(const base::string16& channel, const base::ListValue& args); + // Send WebInputEvent to the page. + void SendInputEvent(v8::Isolate* isolate, v8::Local input_event); + // Methods for creating . void SetSize(const SetSizeParams& params); void SetAllowTransparency(bool allow); diff --git a/atom/browser/api/atom_api_window.cc b/atom/browser/api/atom_api_window.cc index db7b1d5ca1fc..985b6a75438a 100644 --- a/atom/browser/api/atom_api_window.cc +++ b/atom/browser/api/atom_api_window.cc @@ -8,18 +8,15 @@ #include "atom/browser/api/atom_api_web_contents.h" #include "atom/browser/browser.h" #include "atom/browser/native_window.h" -#include "atom/common/native_mate_converters/blink_converter.h" #include "atom/common/native_mate_converters/callback.h" #include "atom/common/native_mate_converters/gfx_converter.h" #include "atom/common/native_mate_converters/gurl_converter.h" #include "atom/common/native_mate_converters/image_converter.h" #include "atom/common/native_mate_converters/string16_converter.h" #include "atom/common/options_switches.h" -#include "content/public/browser/native_web_keyboard_event.h" #include "content/public/browser/render_process_host.h" #include "native_mate/constructor.h" #include "native_mate/dictionary.h" -#include "third_party/WebKit/public/web/WebInputEvent.h" #include "ui/gfx/geometry/rect.h" #if defined(OS_WIN) @@ -539,33 +536,6 @@ bool Window::IsVisibleOnAllWorkspaces() { return window_->IsVisibleOnAllWorkspaces(); } -void Window::SendInputEvent(v8::Isolate* isolate, - v8::Local input_event) { - int type = mate::GetWebInputEventType(isolate, input_event); - if (blink::WebInputEvent::isMouseEventType(type)) { - blink::WebMouseEvent mouse_event; - if (mate::ConvertFromV8(isolate, input_event, &mouse_event)) { - window_->SendInputEvent(mouse_event); - return; - } - } else if (blink::WebInputEvent::isKeyboardEventType(type)) { - content::NativeWebKeyboardEvent keyboard_event;; - if (mate::ConvertFromV8(isolate, input_event, &keyboard_event)) { - window_->SendInputEvent(keyboard_event); - return; - } - } else if (type == blink::WebInputEvent::MouseWheel) { - blink::WebMouseWheelEvent mouse_wheel_event; - if (mate::ConvertFromV8(isolate, input_event, &mouse_wheel_event)) { - window_->SendInputEvent(mouse_wheel_event); - return; - } - } - - isolate->ThrowException(v8::Exception::Error(mate::StringToV8( - isolate, "Invalid event type"))); -} - void Window::BeginFrameSubscription() { window_->SetFrameSubscription(true); } @@ -658,7 +628,6 @@ void Window::BuildPrototype(v8::Isolate* isolate, &Window::SetVisibleOnAllWorkspaces) .SetMethod("isVisibleOnAllWorkspaces", &Window::IsVisibleOnAllWorkspaces) - .SetMethod("sendInputEvent", &Window::SendInputEvent) .SetMethod("beginFrameSubscription", &Window::BeginFrameSubscription) .SetMethod("endFrameSubscription", &Window::EndFrameSubscription) #if defined(OS_MACOSX) diff --git a/atom/browser/api/atom_api_window.h b/atom/browser/api/atom_api_window.h index 507bbb43849c..83322c16cb93 100644 --- a/atom/browser/api/atom_api_window.h +++ b/atom/browser/api/atom_api_window.h @@ -142,7 +142,6 @@ class Window : public mate::TrackableObject, bool IsMenuBarVisible(); void SetAspectRatio(double aspect_ratio, mate::Arguments* args); - void SendInputEvent(v8::Isolate* isolate, v8::Local input_event); void BeginFrameSubscription(); void EndFrameSubscription(); diff --git a/atom/browser/native_window.cc b/atom/browser/native_window.cc index 809545255498..eee53e352efe 100644 --- a/atom/browser/native_window.cc +++ b/atom/browser/native_window.cc @@ -40,10 +40,6 @@ #include "ui/gfx/screen.h" #include "ui/gl/gpu_switching_manager.h" -using content::NavigationEntry; -using content::RenderWidgetHostView; -using content::RenderWidgetHost; - DEFINE_WEB_CONTENTS_USER_DATA_KEY(atom::NativeWindowRelay); namespace atom { @@ -302,38 +298,6 @@ void NativeWindow::SetFrameSubscription(bool isOffscreen) { } } -void NativeWindow::SendInputEvent(const blink::WebMouseEvent& mouse_event) { - const auto view = web_contents()->GetRenderWidgetHostView(); - if (!view) - return; - const auto host = view->GetRenderWidgetHost(); - if (!host) - return; - host->ForwardMouseEvent(mouse_event); -} - -void NativeWindow::SendInputEvent( - const blink::WebMouseWheelEvent& mouse_wheel_event) { - const auto view = web_contents()->GetRenderWidgetHostView(); - if (!view) - return; - const auto host = view->GetRenderWidgetHost(); - if (!host) - return; - host->ForwardWheelEvent(mouse_wheel_event); -} - -void NativeWindow::SendInputEvent( - const content::NativeWebKeyboardEvent& keyboard_event) { - const auto view = web_contents()->GetRenderWidgetHostView(); - if (!view) - return; - const auto host = view->GetRenderWidgetHost(); - if (!host) - return; - host->ForwardKeyboardEvent(keyboard_event); -} - void NativeWindow::RequestToClosePage() { bool prevent_default = false; FOR_EACH_OBSERVER(NativeWindowObserver, diff --git a/atom/browser/native_window.h b/atom/browser/native_window.h index 3344cfb65c32..a593fdc3c2b2 100644 --- a/atom/browser/native_window.h +++ b/atom/browser/native_window.h @@ -26,11 +26,6 @@ class SkRegion; -namespace blink { -class WebMouseEvent; -class WebMouseWheelEvent; -} - namespace brightray { class InspectableWebContents; } @@ -179,11 +174,6 @@ class NativeWindow : public content::WebContentsObserver, // Subscribe to the frame updates. void SetFrameSubscription(bool isOffscreen); - // Send the WebInputEvent to the page. - void SendInputEvent(const blink::WebMouseEvent& mouse_event); - void SendInputEvent(const blink::WebMouseWheelEvent& mouse_wheel_event); - void SendInputEvent(const content::NativeWebKeyboardEvent& keyboard_event); - base::WeakPtr GetWeakPtr() { return weak_factory_.GetWeakPtr(); }