1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
|
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/views/mus/mus_client.h"
#include "base/bind.h"
#include "base/memory/ptr_util.h"
#include "base/message_loop/message_loop.h"
#include "base/single_thread_task_runner.h"
#include "base/threading/thread.h"
#include "services/service_manager/public/cpp/connector.h"
#include "services/ui/public/cpp/gpu/gpu.h"
#include "services/ui/public/cpp/property_type_converters.h"
#include "services/ui/public/interfaces/constants.mojom.h"
#include "services/ui/public/interfaces/event_matcher.mojom.h"
#include "services/ui/public/interfaces/window_manager.mojom.h"
#include "ui/aura/env.h"
#include "ui/aura/mus/capture_synchronizer.h"
#include "ui/aura/mus/mus_context_factory.h"
#include "ui/aura/mus/property_converter.h"
#include "ui/aura/mus/window_tree_client.h"
#include "ui/aura/mus/window_tree_host_mus.h"
#include "ui/aura/mus/window_tree_host_mus_init_params.h"
#include "ui/aura/window.h"
#include "ui/aura/window_tree_host.h"
#include "ui/views/mus/aura_init.h"
#include "ui/views/mus/clipboard_mus.h"
#include "ui/views/mus/desktop_window_tree_host_mus.h"
#include "ui/views/mus/mus_property_mirror.h"
#include "ui/views/mus/pointer_watcher_event_router.h"
#include "ui/views/mus/screen_mus.h"
#include "ui/views/views_delegate.h"
#include "ui/views/widget/desktop_aura/desktop_native_widget_aura.h"
#include "ui/views/widget/widget_delegate.h"
#include "ui/wm/core/shadow_types.h"
#include "ui/wm/core/wm_state.h"
#if defined(USE_OZONE)
#include "ui/base/cursor/ozone/cursor_data_factory_ozone.h"
#endif
// Widget::InitParams::Type must match that of ui::mojom::WindowType.
#define WINDOW_TYPES_MATCH(NAME) \
static_assert( \
static_cast<int32_t>(views::Widget::InitParams::TYPE_##NAME) == \
static_cast<int32_t>(ui::mojom::WindowType::NAME), \
"Window type constants must match")
WINDOW_TYPES_MATCH(WINDOW);
WINDOW_TYPES_MATCH(PANEL);
WINDOW_TYPES_MATCH(WINDOW_FRAMELESS);
WINDOW_TYPES_MATCH(CONTROL);
WINDOW_TYPES_MATCH(POPUP);
WINDOW_TYPES_MATCH(MENU);
WINDOW_TYPES_MATCH(TOOLTIP);
WINDOW_TYPES_MATCH(BUBBLE);
WINDOW_TYPES_MATCH(DRAG);
// ui::mojom::WindowType::UNKNOWN does not correspond to a value in
// Widget::InitParams::Type.
namespace views {
// static
MusClient* MusClient::instance_ = nullptr;
MusClient::MusClient(service_manager::Connector* connector,
const service_manager::Identity& identity,
scoped_refptr<base::SingleThreadTaskRunner> io_task_runner,
bool create_wm_state,
MusClientTestingState testing_state)
: identity_(identity) {
DCHECK(!instance_);
DCHECK(aura::Env::GetInstance());
instance_ = this;
#if defined(USE_OZONE)
// If we're in a mus client, we aren't going to have all of ozone initialized
// even though we're in an ozone build. All the hard coded USE_OZONE ifdefs
// that handle cursor code expect that there will be a CursorFactoryOzone
// instance. Partially initialize the ozone cursor internals here, like we
// partially initialize other ozone subsystems in
// ChromeBrowserMainExtraPartsViews.
cursor_factory_ozone_ = std::make_unique<ui::CursorDataFactoryOzone>();
#endif
if (!io_task_runner) {
io_thread_ = std::make_unique<base::Thread>("IOThread");
base::Thread::Options thread_options(base::MessageLoop::TYPE_IO, 0);
thread_options.priority = base::ThreadPriority::NORMAL;
CHECK(io_thread_->StartWithOptions(thread_options));
io_task_runner = io_thread_->task_runner();
}
// TODO(msw): Avoid this... use some default value? Allow clients to extend?
property_converter_ = std::make_unique<aura::PropertyConverter>();
property_converter_->RegisterPrimitiveProperty(
::wm::kShadowElevationKey,
ui::mojom::WindowManager::kShadowElevation_Property,
aura::PropertyConverter::CreateAcceptAnyValueCallback());
if (create_wm_state)
wm_state_ = std::make_unique<wm::WMState>();
if (testing_state == MusClientTestingState::CREATE_TESTING_STATE) {
connector->BindInterface(ui::mojom::kServiceName, &server_test_ptr_);
connector->BindInterface(ui::mojom::kServiceName, &event_injector_);
}
window_tree_client_ = aura::WindowTreeClient::CreateForWindowTreeFactory(
connector, this, true, std::move(io_task_runner));
aura::Env::GetInstance()->SetWindowTreeClient(window_tree_client_.get());
pointer_watcher_event_router_ =
std::make_unique<PointerWatcherEventRouter>(window_tree_client_.get());
screen_ = std::make_unique<ScreenMus>(this);
screen_->Init(connector);
std::unique_ptr<ClipboardMus> clipboard = std::make_unique<ClipboardMus>();
clipboard->Init(connector);
ui::Clipboard::SetClipboardForCurrentThread(std::move(clipboard));
ViewsDelegate::GetInstance()->set_native_widget_factory(
base::Bind(&MusClient::CreateNativeWidget, base::Unretained(this)));
ViewsDelegate::GetInstance()->set_desktop_window_tree_host_factory(base::Bind(
&MusClient::CreateDesktopWindowTreeHost, base::Unretained(this)));
}
MusClient::~MusClient() {
// ~WindowTreeClient calls back to us (we're its delegate), destroy it while
// we are still valid.
window_tree_client_.reset();
ui::OSExchangeDataProviderFactory::SetFactory(nullptr);
ui::Clipboard::DestroyClipboardForCurrentThread();
if (ViewsDelegate::GetInstance()) {
ViewsDelegate::GetInstance()->set_native_widget_factory(
ViewsDelegate::NativeWidgetFactory());
ViewsDelegate::GetInstance()->set_desktop_window_tree_host_factory(
ViewsDelegate::DesktopWindowTreeHostFactory());
}
DCHECK_EQ(instance_, this);
instance_ = nullptr;
DCHECK(aura::Env::GetInstance());
}
// static
bool MusClient::ShouldCreateDesktopNativeWidgetAura(
const Widget::InitParams& init_params) {
// TYPE_CONTROL and child widgets require a NativeWidgetAura.
return init_params.type != Widget::InitParams::TYPE_CONTROL &&
!init_params.child;
}
// static
std::map<std::string, std::vector<uint8_t>>
MusClient::ConfigurePropertiesFromParams(
const Widget::InitParams& init_params) {
using PrimitiveType = aura::PropertyConverter::PrimitiveType;
using WindowManager = ui::mojom::WindowManager;
using TransportType = std::vector<uint8_t>;
std::map<std::string, TransportType> properties = init_params.mus_properties;
// Widget::InitParams::Type matches ui::mojom::WindowType.
properties[WindowManager::kWindowType_InitProperty] =
mojo::ConvertTo<TransportType>(static_cast<int32_t>(init_params.type));
properties[WindowManager::kFocusable_InitProperty] =
mojo::ConvertTo<TransportType>(init_params.CanActivate());
properties[WindowManager::kTranslucent_InitProperty] =
mojo::ConvertTo<TransportType>(init_params.opacity ==
Widget::InitParams::TRANSLUCENT_WINDOW);
if (!init_params.bounds.IsEmpty()) {
properties[WindowManager::kBounds_InitProperty] =
mojo::ConvertTo<TransportType>(init_params.bounds);
}
if (!init_params.name.empty()) {
properties[WindowManager::kName_Property] =
mojo::ConvertTo<TransportType>(init_params.name);
}
properties[WindowManager::kAlwaysOnTop_Property] =
mojo::ConvertTo<TransportType>(
static_cast<PrimitiveType>(init_params.keep_on_top));
properties[WindowManager::kRemoveStandardFrame_InitProperty] =
mojo::ConvertTo<TransportType>(init_params.remove_standard_frame);
if (init_params.corner_radius) {
properties[WindowManager::kWindowCornerRadius_Property] =
mojo::ConvertTo<TransportType>(
static_cast<PrimitiveType>(*init_params.corner_radius));
}
if (!Widget::RequiresNonClientView(init_params.type))
return properties;
if (init_params.delegate) {
if (properties.count(WindowManager::kResizeBehavior_Property) == 0) {
properties[WindowManager::kResizeBehavior_Property] =
mojo::ConvertTo<TransportType>(static_cast<PrimitiveType>(
init_params.delegate->GetResizeBehavior()));
}
// TODO(crbug.com/667566): Support additional scales or gfx::Image[Skia].
gfx::ImageSkia app_icon = init_params.delegate->GetWindowAppIcon();
SkBitmap app_bitmap = app_icon.GetRepresentation(1.f).sk_bitmap();
if (!app_bitmap.isNull()) {
properties[WindowManager::kAppIcon_Property] =
mojo::ConvertTo<TransportType>(app_bitmap);
}
// TODO(crbug.com/667566): Support additional scales or gfx::Image[Skia].
gfx::ImageSkia window_icon = init_params.delegate->GetWindowIcon();
SkBitmap window_bitmap = window_icon.GetRepresentation(1.f).sk_bitmap();
if (!window_bitmap.isNull()) {
properties[WindowManager::kWindowIcon_Property] =
mojo::ConvertTo<TransportType>(window_bitmap);
}
}
return properties;
}
NativeWidget* MusClient::CreateNativeWidget(
const Widget::InitParams& init_params,
internal::NativeWidgetDelegate* delegate) {
if (!ShouldCreateDesktopNativeWidgetAura(init_params)) {
// A null return value results in creating NativeWidgetAura.
return nullptr;
}
DesktopNativeWidgetAura* native_widget =
new DesktopNativeWidgetAura(delegate);
if (init_params.desktop_window_tree_host) {
native_widget->SetDesktopWindowTreeHost(
base::WrapUnique(init_params.desktop_window_tree_host));
} else {
native_widget->SetDesktopWindowTreeHost(
CreateDesktopWindowTreeHost(init_params, delegate, native_widget));
}
return native_widget;
}
void MusClient::OnCaptureClientSet(
aura::client::CaptureClient* capture_client) {
pointer_watcher_event_router_->AttachToCaptureClient(capture_client);
window_tree_client_->capture_synchronizer()->AttachToCaptureClient(
capture_client);
}
void MusClient::OnCaptureClientUnset(
aura::client::CaptureClient* capture_client) {
pointer_watcher_event_router_->DetachFromCaptureClient(capture_client);
window_tree_client_->capture_synchronizer()->DetachFromCaptureClient(
capture_client);
}
void MusClient::AddObserver(MusClientObserver* observer) {
observer_list_.AddObserver(observer);
}
void MusClient::RemoveObserver(MusClientObserver* observer) {
observer_list_.RemoveObserver(observer);
}
void MusClient::SetMusPropertyMirror(
std::unique_ptr<MusPropertyMirror> mirror) {
mus_property_mirror_ = std::move(mirror);
}
void MusClient::CloseAllWidgets() {
for (aura::Window* root : window_tree_client_->GetRoots()) {
Widget* widget = Widget::GetWidgetForNativeView(root);
if (widget)
widget->CloseNow();
}
}
ui::mojom::WindowServerTest* MusClient::GetTestingInterface() const {
// This will only be set in tests. CHECK to ensure it doesn't get used
// elsewhere.
CHECK(server_test_ptr_);
return server_test_ptr_.get();
}
ui::mojom::EventInjector* MusClient::GetTestingEventInjector() const {
CHECK(event_injector_);
return event_injector_.get();
}
std::unique_ptr<DesktopWindowTreeHost> MusClient::CreateDesktopWindowTreeHost(
const Widget::InitParams& init_params,
internal::NativeWidgetDelegate* delegate,
DesktopNativeWidgetAura* desktop_native_widget_aura) {
std::map<std::string, std::vector<uint8_t>> mus_properties =
ConfigurePropertiesFromParams(init_params);
aura::WindowTreeHostMusInitParams window_tree_host_init_params =
aura::CreateInitParamsForTopLevel(MusClient::Get()->window_tree_client(),
std::move(mus_properties));
return std::make_unique<DesktopWindowTreeHostMus>(
std::move(window_tree_host_init_params), delegate,
desktop_native_widget_aura);
}
void MusClient::OnEmbed(
std::unique_ptr<aura::WindowTreeHostMus> window_tree_host) {
NOTREACHED();
}
void MusClient::OnLostConnection(aura::WindowTreeClient* client) {}
void MusClient::OnEmbedRootDestroyed(
aura::WindowTreeHostMus* window_tree_host) {
static_cast<DesktopWindowTreeHostMus*>(window_tree_host)
->ServerDestroyedWindow();
}
void MusClient::OnPointerEventObserved(const ui::PointerEvent& event,
aura::Window* target) {
pointer_watcher_event_router_->OnPointerEventObserved(event, target);
}
void MusClient::OnWindowManagerFrameValuesChanged() {
for (auto& observer : observer_list_)
observer.OnWindowManagerFrameValuesChanged();
}
aura::PropertyConverter* MusClient::GetPropertyConverter() {
return property_converter_.get();
}
aura::Window* MusClient::GetWindowAtScreenPoint(const gfx::Point& point) {
for (aura::Window* root : window_tree_client_->GetRoots()) {
aura::WindowTreeHost* window_tree_host = root->GetHost();
if (!window_tree_host)
continue;
// TODO: this likely gets z-order wrong. http://crbug.com/663606.
gfx::Point relative_point(point);
window_tree_host->ConvertScreenInPixelsToDIP(&relative_point);
if (gfx::Rect(root->bounds().size()).Contains(relative_point))
return root->GetEventHandlerForPoint(relative_point);
}
return nullptr;
}
} // namespace views
|