• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2020 The Chromium Embedded Framework Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "libcef/browser/native/browser_platform_delegate_native_aura.h"
6 
7 #include "content/browser/renderer_host/render_widget_host_view_aura.h"
8 #include "content/public/browser/render_view_host.h"
9 #include "content/public/browser/render_widget_host.h"
10 #include "ui/events/blink/blink_event_util.h"
11 #include "ui/events/blink/web_input_event.h"
12 #include "ui/gfx/geometry/vector2d.h"
13 
CefBrowserPlatformDelegateNativeAura(const CefWindowInfo & window_info,SkColor background_color)14 CefBrowserPlatformDelegateNativeAura::CefBrowserPlatformDelegateNativeAura(
15     const CefWindowInfo& window_info,
16     SkColor background_color)
17     : CefBrowserPlatformDelegateNative(window_info, background_color) {}
18 
SendKeyEvent(const CefKeyEvent & event)19 void CefBrowserPlatformDelegateNativeAura::SendKeyEvent(
20     const CefKeyEvent& event) {
21   auto view = GetHostView();
22   if (!view)
23     return;
24 
25   ui::KeyEvent ui_event = TranslateUiKeyEvent(event);
26   view->OnKeyEvent(&ui_event);
27 }
28 
SendMouseClickEvent(const CefMouseEvent & event,CefBrowserHost::MouseButtonType type,bool mouseUp,int clickCount)29 void CefBrowserPlatformDelegateNativeAura::SendMouseClickEvent(
30     const CefMouseEvent& event,
31     CefBrowserHost::MouseButtonType type,
32     bool mouseUp,
33     int clickCount) {
34   auto view = GetHostView();
35   if (!view)
36     return;
37 
38   ui::MouseEvent ui_event =
39       TranslateUiClickEvent(event, type, mouseUp, clickCount);
40   view->OnMouseEvent(&ui_event);
41 }
42 
SendMouseMoveEvent(const CefMouseEvent & event,bool mouseLeave)43 void CefBrowserPlatformDelegateNativeAura::SendMouseMoveEvent(
44     const CefMouseEvent& event,
45     bool mouseLeave) {
46   auto view = GetHostView();
47   if (!view)
48     return;
49 
50   ui::MouseEvent ui_event = TranslateUiMoveEvent(event, mouseLeave);
51   view->OnMouseEvent(&ui_event);
52 }
53 
SendMouseWheelEvent(const CefMouseEvent & event,int deltaX,int deltaY)54 void CefBrowserPlatformDelegateNativeAura::SendMouseWheelEvent(
55     const CefMouseEvent& event,
56     int deltaX,
57     int deltaY) {
58   auto view = GetHostView();
59   if (!view)
60     return;
61 
62   ui::MouseWheelEvent ui_event = TranslateUiWheelEvent(event, deltaX, deltaY);
63   view->OnMouseEvent(&ui_event);
64 }
65 
SendTouchEvent(const CefTouchEvent & event)66 void CefBrowserPlatformDelegateNativeAura::SendTouchEvent(
67     const CefTouchEvent& event) {
68   NOTIMPLEMENTED();
69 }
70 
71 content::NativeWebKeyboardEvent
TranslateWebKeyEvent(const CefKeyEvent & key_event) const72 CefBrowserPlatformDelegateNativeAura::TranslateWebKeyEvent(
73     const CefKeyEvent& key_event) const {
74   return content::NativeWebKeyboardEvent(TranslateUiKeyEvent(key_event));
75 }
76 
77 blink::WebMouseEvent
TranslateWebClickEvent(const CefMouseEvent & mouse_event,CefBrowserHost::MouseButtonType type,bool mouseUp,int clickCount) const78 CefBrowserPlatformDelegateNativeAura::TranslateWebClickEvent(
79     const CefMouseEvent& mouse_event,
80     CefBrowserHost::MouseButtonType type,
81     bool mouseUp,
82     int clickCount) const {
83   return ui::MakeWebMouseEvent(
84       TranslateUiClickEvent(mouse_event, type, mouseUp, clickCount));
85 }
86 
87 blink::WebMouseEvent
TranslateWebMoveEvent(const CefMouseEvent & mouse_event,bool mouseLeave) const88 CefBrowserPlatformDelegateNativeAura::TranslateWebMoveEvent(
89     const CefMouseEvent& mouse_event,
90     bool mouseLeave) const {
91   return ui::MakeWebMouseEvent(TranslateUiMoveEvent(mouse_event, mouseLeave));
92 }
93 
94 blink::WebMouseWheelEvent
TranslateWebWheelEvent(const CefMouseEvent & mouse_event,int deltaX,int deltaY) const95 CefBrowserPlatformDelegateNativeAura::TranslateWebWheelEvent(
96     const CefMouseEvent& mouse_event,
97     int deltaX,
98     int deltaY) const {
99   return ui::MakeWebMouseWheelEvent(
100       TranslateUiWheelEvent(mouse_event, deltaX, deltaY));
101 }
102 
TranslateUiClickEvent(const CefMouseEvent & mouse_event,CefBrowserHost::MouseButtonType type,bool mouseUp,int clickCount) const103 ui::MouseEvent CefBrowserPlatformDelegateNativeAura::TranslateUiClickEvent(
104     const CefMouseEvent& mouse_event,
105     CefBrowserHost::MouseButtonType type,
106     bool mouseUp,
107     int clickCount) const {
108   DCHECK_GE(clickCount, 1);
109 
110   ui::EventType event_type =
111       mouseUp ? ui::ET_MOUSE_RELEASED : ui::ET_MOUSE_PRESSED;
112   gfx::PointF location(mouse_event.x, mouse_event.y);
113   gfx::PointF root_location(
114       GetScreenPoint(gfx::Point(mouse_event.x, mouse_event.y)));
115   base::TimeTicks time_stamp = GetEventTimeStamp();
116   int flags = TranslateUiEventModifiers(mouse_event.modifiers);
117 
118   int changed_button_flags = 0;
119   switch (type) {
120     case MBT_LEFT:
121       changed_button_flags |= ui::EF_LEFT_MOUSE_BUTTON;
122       break;
123     case MBT_MIDDLE:
124       changed_button_flags |= ui::EF_MIDDLE_MOUSE_BUTTON;
125       break;
126     case MBT_RIGHT:
127       changed_button_flags |= ui::EF_RIGHT_MOUSE_BUTTON;
128       break;
129     default:
130       NOTREACHED();
131   }
132 
133   ui::MouseEvent result(event_type, location, root_location, time_stamp, flags,
134                         changed_button_flags);
135   result.SetClickCount(clickCount);
136   return result;
137 }
138 
TranslateUiMoveEvent(const CefMouseEvent & mouse_event,bool mouseLeave) const139 ui::MouseEvent CefBrowserPlatformDelegateNativeAura::TranslateUiMoveEvent(
140     const CefMouseEvent& mouse_event,
141     bool mouseLeave) const {
142   ui::EventType event_type =
143       mouseLeave ? ui::ET_MOUSE_EXITED : ui::ET_MOUSE_MOVED;
144   gfx::PointF location(mouse_event.x, mouse_event.y);
145   gfx::PointF root_location(
146       GetScreenPoint(gfx::Point(mouse_event.x, mouse_event.y)));
147   base::TimeTicks time_stamp = GetEventTimeStamp();
148   int flags = TranslateUiEventModifiers(mouse_event.modifiers);
149 
150   int changed_button_flags = 0;
151   if (!mouseLeave) {
152     changed_button_flags = TranslateUiChangedButtonFlags(mouse_event.modifiers);
153   }
154 
155   return ui::MouseEvent(event_type, location, root_location, time_stamp, flags,
156                         changed_button_flags);
157 }
158 
TranslateUiWheelEvent(const CefMouseEvent & mouse_event,int deltaX,int deltaY) const159 ui::MouseWheelEvent CefBrowserPlatformDelegateNativeAura::TranslateUiWheelEvent(
160     const CefMouseEvent& mouse_event,
161     int deltaX,
162     int deltaY) const {
163   gfx::Vector2d offset(GetUiWheelEventOffset(deltaX, deltaY));
164   DCHECK(!offset.IsZero());
165 
166   gfx::PointF location(mouse_event.x, mouse_event.y);
167   gfx::PointF root_location(
168       GetScreenPoint(gfx::Point(mouse_event.x, mouse_event.y)));
169   base::TimeTicks time_stamp = GetEventTimeStamp();
170   int flags = TranslateUiEventModifiers(mouse_event.modifiers);
171   int changed_button_flags =
172       TranslateUiChangedButtonFlags(mouse_event.modifiers);
173 
174   return ui::MouseWheelEvent(offset, location, root_location, time_stamp,
175                              (ui::EF_PRECISION_SCROLLING_DELTA | flags),
176                              changed_button_flags);
177 }
178 
GetUiWheelEventOffset(int deltaX,int deltaY) const179 gfx::Vector2d CefBrowserPlatformDelegateNativeAura::GetUiWheelEventOffset(
180     int deltaX,
181     int deltaY) const {
182   return gfx::Vector2d(deltaX, deltaY);
183 }
184 
185 // static
GetEventTimeStamp()186 base::TimeTicks CefBrowserPlatformDelegateNativeAura::GetEventTimeStamp() {
187   return base::TimeTicks::Now();
188 }
189 
190 // static
TranslateUiEventModifiers(uint32 cef_modifiers)191 int CefBrowserPlatformDelegateNativeAura::TranslateUiEventModifiers(
192     uint32 cef_modifiers) {
193   int result = 0;
194   // Set modifiers based on key state.
195   if (cef_modifiers & EVENTFLAG_CAPS_LOCK_ON)
196     result |= ui::EF_CAPS_LOCK_ON;
197   if (cef_modifiers & EVENTFLAG_SHIFT_DOWN)
198     result |= ui::EF_SHIFT_DOWN;
199   if (cef_modifiers & EVENTFLAG_CONTROL_DOWN)
200     result |= ui::EF_CONTROL_DOWN;
201   if (cef_modifiers & EVENTFLAG_ALT_DOWN)
202     result |= ui::EF_ALT_DOWN;
203   if (cef_modifiers & EVENTFLAG_LEFT_MOUSE_BUTTON)
204     result |= ui::EF_LEFT_MOUSE_BUTTON;
205   if (cef_modifiers & EVENTFLAG_MIDDLE_MOUSE_BUTTON)
206     result |= ui::EF_MIDDLE_MOUSE_BUTTON;
207   if (cef_modifiers & EVENTFLAG_RIGHT_MOUSE_BUTTON)
208     result |= ui::EF_RIGHT_MOUSE_BUTTON;
209   if (cef_modifiers & EVENTFLAG_COMMAND_DOWN)
210     result |= ui::EF_COMMAND_DOWN;
211   if (cef_modifiers & EVENTFLAG_NUM_LOCK_ON)
212     result |= ui::EF_NUM_LOCK_ON;
213   if (cef_modifiers & EVENTFLAG_IS_KEY_PAD)
214     result |= ui::EF_IS_EXTENDED_KEY;
215   if (cef_modifiers & EVENTFLAG_ALTGR_DOWN)
216     result |= ui::EF_ALTGR_DOWN;
217   if (cef_modifiers & EVENTFLAG_IS_REPEAT)
218     result |= ui::EF_IS_REPEAT;
219   return result;
220 }
221 
222 // static
TranslateUiChangedButtonFlags(uint32 cef_modifiers)223 int CefBrowserPlatformDelegateNativeAura::TranslateUiChangedButtonFlags(
224     uint32 cef_modifiers) {
225   int result = 0;
226   if (cef_modifiers & EVENTFLAG_LEFT_MOUSE_BUTTON)
227     result |= ui::EF_LEFT_MOUSE_BUTTON;
228   else if (cef_modifiers & EVENTFLAG_MIDDLE_MOUSE_BUTTON)
229     result |= ui::EF_MIDDLE_MOUSE_BUTTON;
230   else if (cef_modifiers & EVENTFLAG_RIGHT_MOUSE_BUTTON)
231     result |= ui::EF_RIGHT_MOUSE_BUTTON;
232   return result;
233 }
234 
235 content::RenderWidgetHostViewAura*
GetHostView() const236 CefBrowserPlatformDelegateNativeAura::GetHostView() const {
237   if (!web_contents_)
238     return nullptr;
239   return static_cast<content::RenderWidgetHostViewAura*>(
240       web_contents_->GetRenderWidgetHostView());
241 }
242