[Media Router] Add integration tests and e2e tests for media router and presentation...
[chromium-blink-merge.git] / components / view_manager / native_viewport / platform_viewport_common.cc
blob7656c420253266265b33c5fe9b9aa4085d712c88
1 // Copyright 2013 The Chromium 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.
5 #include "components/view_manager/native_viewport/platform_viewport.h"
7 #include "base/command_line.h"
8 #include "base/message_loop/message_loop.h"
9 #include "components/view_manager/native_viewport/platform_viewport_headless.h"
10 #include "components/view_manager/public/interfaces/view_manager.mojom.h"
11 #include "mojo/converters/geometry/geometry_type_converters.h"
12 #include "mojo/converters/input_events/input_events_type_converters.h"
13 #include "mojo/converters/input_events/mojo_extended_key_event_data.h"
14 #include "ui/events/event.h"
15 #include "ui/events/event_utils.h"
16 #include "ui/events/platform/platform_event_dispatcher.h"
17 #include "ui/gfx/geometry/rect.h"
18 #include "ui/platform_window/platform_window.h"
19 #include "ui/platform_window/platform_window_delegate.h"
21 #if defined(OS_WIN)
22 #include "ui/platform_window/win/win_window.h"
23 #elif defined(USE_X11)
24 #include "ui/platform_window/x11/x11_window.h"
25 #elif defined(OS_ANDROID)
26 #include "ui/platform_window/android/platform_window_android.h"
27 #endif
29 namespace native_viewport {
30 namespace {
32 float ConvertUIWheelValueToMojoValue(int offset) {
33 // Mojo's event type takes a value between -1 and 1. Normalize by allowing
34 // up to 20 of ui's offset. This is a bit arbitrary.
35 return std::max(
36 -1.0f, std::min(1.0f, static_cast<float>(offset) /
37 (20 * static_cast<float>(
38 ui::MouseWheelEvent::kWheelDelta))));
40 } // namespace
42 class PlatformViewportCommon : public PlatformViewport,
43 public ui::PlatformWindowDelegate {
44 public:
45 explicit PlatformViewportCommon(Delegate* delegate) : delegate_(delegate) {
48 ~PlatformViewportCommon() override {
49 // Destroy the platform-window while |this| is still alive.
50 platform_window_.reset();
53 private:
54 // Overridden from PlatformViewport:
55 void Init(const gfx::Rect& bounds) override {
56 CHECK(!platform_window_);
58 metrics_ = mojo::ViewportMetrics::New();
59 metrics_->size_in_pixels = mojo::Size::From(bounds.size());
61 #if defined(OS_WIN)
62 platform_window_.reset(new ui::WinWindow(this, bounds));
63 #elif defined(USE_X11)
64 platform_window_.reset(new ui::X11Window(this));
65 #elif defined(OS_ANDROID)
66 platform_window_.reset(new ui::PlatformWindowAndroid(this));
67 #endif
68 platform_window_->SetBounds(bounds);
71 void Show() override { platform_window_->Show(); }
73 void Hide() override { platform_window_->Hide(); }
75 void Close() override { platform_window_->Close(); }
77 gfx::Size GetSize() override {
78 return metrics_->size_in_pixels.To<gfx::Size>();
81 void SetBounds(const gfx::Rect& bounds) override {
82 platform_window_->SetBounds(bounds);
85 // ui::PlatformWindowDelegate:
86 void OnBoundsChanged(const gfx::Rect& new_bounds) override {
87 delegate_->OnMetricsChanged(new_bounds.size(),
88 metrics_->device_pixel_ratio);
91 void OnDamageRect(const gfx::Rect& damaged_region) override {}
93 void DispatchEvent(ui::Event* event) override {
94 mojo::EventPtr mojo_event(mojo::Event::From(*event));
95 if (event->IsMouseWheelEvent()) {
96 // Mojo's event type has a different meaning for wheel events. Convert
97 // between the two.
98 ui::MouseWheelEvent* wheel_event =
99 static_cast<ui::MouseWheelEvent*>(event);
100 DCHECK(mojo_event->pointer_data);
101 mojo_event->pointer_data->horizontal_wheel =
102 ConvertUIWheelValueToMojoValue(wheel_event->x_offset());
103 mojo_event->pointer_data->horizontal_wheel =
104 ConvertUIWheelValueToMojoValue(wheel_event->y_offset());
106 delegate_->OnEvent(mojo_event.Pass());
108 switch (event->type()) {
109 case ui::ET_MOUSE_PRESSED:
110 case ui::ET_TOUCH_PRESSED:
111 platform_window_->SetCapture();
112 break;
113 case ui::ET_MOUSE_RELEASED:
114 case ui::ET_TOUCH_RELEASED:
115 platform_window_->ReleaseCapture();
116 break;
117 default:
118 break;
121 #if defined(USE_X11)
122 // We want to emulate the WM_CHAR generation behaviour of Windows.
124 // On Linux, we've previously inserted characters by having
125 // InputMethodAuraLinux take all key down events and send a character event
126 // to the TextInputClient. This causes a mismatch in code that has to be
127 // shared between Windows and Linux, including blink code. Now that we're
128 // trying to have one way of doing things, we need to standardize on and
129 // emulate Windows character events.
131 // This is equivalent to what we're doing in the current Linux port, but
132 // done once instead of done multiple times in different places.
133 if (event->type() == ui::ET_KEY_PRESSED) {
134 ui::KeyEvent* key_press_event = static_cast<ui::KeyEvent*>(event);
135 ui::KeyEvent char_event(key_press_event->GetCharacter(),
136 key_press_event->key_code(),
137 key_press_event->flags());
139 DCHECK_EQ(key_press_event->GetCharacter(), char_event.GetCharacter());
140 DCHECK_EQ(key_press_event->key_code(), char_event.key_code());
141 DCHECK_EQ(key_press_event->flags(), char_event.flags());
143 char_event.SetExtendedKeyEventData(
144 make_scoped_ptr(new mojo::MojoExtendedKeyEventData(
145 key_press_event->GetLocatedWindowsKeyboardCode(),
146 key_press_event->GetText(),
147 key_press_event->GetUnmodifiedText())));
148 char_event.set_platform_keycode(key_press_event->platform_keycode());
150 delegate_->OnEvent(mojo::Event::From(char_event));
152 #endif
155 void OnCloseRequest() override { platform_window_->Close(); }
157 void OnClosed() override { delegate_->OnDestroyed(); }
159 void OnWindowStateChanged(ui::PlatformWindowState state) override {}
161 void OnLostCapture() override {}
163 void OnAcceleratedWidgetAvailable(gfx::AcceleratedWidget widget,
164 float device_pixel_ratio) override {
165 metrics_->device_pixel_ratio = device_pixel_ratio;
166 delegate_->OnAcceleratedWidgetAvailable(widget,
167 metrics_->device_pixel_ratio);
170 void OnActivationChanged(bool active) override {}
172 scoped_ptr<ui::PlatformWindow> platform_window_;
173 Delegate* delegate_;
174 mojo::ViewportMetricsPtr metrics_;
176 DISALLOW_COPY_AND_ASSIGN(PlatformViewportCommon);
179 // static
180 scoped_ptr<PlatformViewport> PlatformViewport::Create(Delegate* delegate,
181 bool headless) {
182 if (headless)
183 return PlatformViewportHeadless::Create(delegate);
184 return make_scoped_ptr(new PlatformViewportCommon(delegate));
187 } // namespace native_viewport