blob: fe31c6c8a728815786ce86c59cec3514439c4b84 [file] [log] [blame]
oshima55ef9212015-04-27 23:27:031// Copyright 2015 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.
4
5#include "ash/host/ash_window_tree_host_unified.h"
dchengcbf0d9d2015-12-27 22:49:236
Mitsuru Oshima04b54d02017-10-09 14:22:457#include <memory>
dchengcbf0d9d2015-12-27 22:49:238#include <utility>
9
Ahmed Fakhry4f8e3722017-10-31 21:01:5810#include "ash/host/ash_window_tree_host_mirroring_delegate.h"
oshima55ef9212015-04-27 23:27:0311#include "ash/host/root_window_transformer.h"
12#include "base/logging.h"
Ganesh Borle0113f1b2018-03-01 08:44:0213#include "base/stl_util.h"
oshima55ef9212015-04-27 23:27:0314#include "ui/aura/window.h"
15#include "ui/aura/window_event_dispatcher.h"
16#include "ui/aura/window_targeter.h"
17#include "ui/compositor/compositor.h"
oshima55ef9212015-04-27 23:27:0318#include "ui/gfx/geometry/insets.h"
sadrul4f923692016-02-05 05:21:2419#include "ui/platform_window/stub/stub_window.h"
oshima55ef9212015-04-27 23:27:0320
21namespace ash {
22
23class UnifiedEventTargeter : public aura::WindowTargeter {
24 public:
Ahmed Fakhry4f8e3722017-10-31 21:01:5825 UnifiedEventTargeter(aura::Window* src_root,
26 aura::Window* dst_root,
27 AshWindowTreeHostMirroringDelegate* delegate)
28 : src_root_(src_root), dst_root_(dst_root), delegate_(delegate) {
29 DCHECK(delegate);
30 }
oshima55ef9212015-04-27 23:27:0331
32 ui::EventTarget* FindTargetForEvent(ui::EventTarget* root,
33 ui::Event* event) override {
34 if (root == src_root_ && !event->target()) {
Ahmed Fakhry4f8e3722017-10-31 21:01:5835 delegate_->SetCurrentEventTargeterSourceHost(src_root_->GetHost());
36
oshima55ef9212015-04-27 23:27:0337 if (event->IsLocatedEvent()) {
38 ui::LocatedEvent* located_event = static_cast<ui::LocatedEvent*>(event);
39 located_event->ConvertLocationToTarget(
40 static_cast<aura::Window*>(nullptr), dst_root_);
oshima55ef9212015-04-27 23:27:0341 }
42 ignore_result(
penghuang17134c6c2017-03-23 00:01:0743 dst_root_->GetHost()->event_sink()->OnEventFromSource(event));
Ahmed Fakhry4f8e3722017-10-31 21:01:5844
45 // Reset the source host.
46 delegate_->SetCurrentEventTargeterSourceHost(nullptr);
47
oshima55ef9212015-04-27 23:27:0348 return nullptr;
49 } else {
oshimaf8b27692015-04-30 07:23:0050 NOTREACHED() << "event type:" << event->type();
oshima55ef9212015-04-27 23:27:0351 return aura::WindowTargeter::FindTargetForEvent(root, event);
52 }
53 }
54
Ahmed Fakhry4f8e3722017-10-31 21:01:5855 private:
oshima55ef9212015-04-27 23:27:0356 aura::Window* src_root_;
57 aura::Window* dst_root_;
Ahmed Fakhry4f8e3722017-10-31 21:01:5858 AshWindowTreeHostMirroringDelegate* delegate_; // Not owned.
oshima55ef9212015-04-27 23:27:0359
60 DISALLOW_COPY_AND_ASSIGN(UnifiedEventTargeter);
61};
62
63AshWindowTreeHostUnified::AshWindowTreeHostUnified(
Ahmed Fakhry4f8e3722017-10-31 21:01:5864 const gfx::Rect& initial_bounds,
65 AshWindowTreeHostMirroringDelegate* delegate)
66 : AshWindowTreeHostPlatform(), delegate_(delegate) {
67 DCHECK(delegate);
Maksim Sisov08b20ec12019-11-25 13:20:5868 std::unique_ptr<ui::PlatformWindow> window(new ui::StubWindow(this));
sadrul4f923692016-02-05 05:21:2469 window->SetBounds(initial_bounds);
70 SetPlatformWindow(std::move(window));
oshima55ef9212015-04-27 23:27:0371}
72
73AshWindowTreeHostUnified::~AshWindowTreeHostUnified() {
oshimac2f69582015-05-20 22:04:5474 for (auto* ash_host : mirroring_hosts_)
75 ash_host->AsWindowTreeHost()->window()->RemoveObserver(this);
oshima55ef9212015-04-27 23:27:0376}
77
78void AshWindowTreeHostUnified::PrepareForShutdown() {
sadrul4f923692016-02-05 05:21:2479 AshWindowTreeHostPlatform::PrepareForShutdown();
oshimab8fb0b72015-04-28 18:25:1080
vmpstreb900a12016-06-29 02:22:2881 for (auto* host : mirroring_hosts_)
oshima55ef9212015-04-27 23:27:0382 host->PrepareForShutdown();
83}
84
85void AshWindowTreeHostUnified::RegisterMirroringHost(
86 AshWindowTreeHost* mirroring_ash_host) {
87 aura::Window* src_root = mirroring_ash_host->AsWindowTreeHost()->window();
88 src_root->SetEventTargeter(
Ahmed Fakhry4f8e3722017-10-31 21:01:5889 std::make_unique<UnifiedEventTargeter>(src_root, window(), delegate_));
Jan Wilken Dörrie58b0c4b2019-06-06 18:44:3490 DCHECK(!base::Contains(mirroring_hosts_, mirroring_ash_host));
oshima55ef9212015-04-27 23:27:0391 mirroring_hosts_.push_back(mirroring_ash_host);
92 mirroring_ash_host->AsWindowTreeHost()->window()->AddObserver(this);
93}
94
oshima55ef9212015-04-27 23:27:0395void AshWindowTreeHostUnified::SetCursorNative(gfx::NativeCursor cursor) {
vmpstreb900a12016-06-29 02:22:2896 for (auto* host : mirroring_hosts_)
oshima55ef9212015-04-27 23:27:0397 host->AsWindowTreeHost()->SetCursor(cursor);
98}
99
oshima55ef9212015-04-27 23:27:03100void AshWindowTreeHostUnified::OnCursorVisibilityChangedNative(bool show) {
vmpstreb900a12016-06-29 02:22:28101 for (auto* host : mirroring_hosts_)
oshima55ef9212015-04-27 23:27:03102 host->AsWindowTreeHost()->OnCursorVisibilityChanged(show);
103}
104
sadrul4f923692016-02-05 05:21:24105void AshWindowTreeHostUnified::OnBoundsChanged(const gfx::Rect& bounds) {
106 if (platform_window())
riajiang8077b132016-11-29 23:35:36107 OnHostResizedInPixels(bounds.size());
sadrul4f923692016-02-05 05:21:24108}
109
oshima55ef9212015-04-27 23:27:03110void AshWindowTreeHostUnified::OnWindowDestroying(aura::Window* window) {
111 auto iter =
112 std::find_if(mirroring_hosts_.begin(), mirroring_hosts_.end(),
113 [window](AshWindowTreeHost* ash_host) {
114 return ash_host->AsWindowTreeHost()->window() == window;
115 });
116 DCHECK(iter != mirroring_hosts_.end());
117 window->RemoveObserver(this);
118 mirroring_hosts_.erase(iter);
119}
120
oshima55ef9212015-04-27 23:27:03121} // namespace ash