blob: cf50eba774572764cc3af15a140dab8b7ebd8ac9 [file] [log] [blame]
ben294d04b2015-10-15 01:23:331// 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
skyb0446352015-11-18 00:43:565#include "mash/wm/window_manager_application.h"
ben294d04b2015-10-15 01:23:336
dchenge48600452015-12-28 02:24:507#include <utility>
avi703dbbf2015-12-22 01:46:288
sadrul3b1906f42015-12-02 23:50:449#include "base/bind.h"
mostynb1aaf5062016-04-04 17:56:2710#include "base/memory/ptr_util.h"
sadrul99b70b382015-11-24 21:07:1711#include "components/mus/public/cpp/event_matcher.h"
fsamuel68afcf452015-10-16 00:12:4512#include "components/mus/public/cpp/window.h"
sky7a35dfa2016-02-01 21:37:2813#include "components/mus/public/interfaces/window_manager_factory.mojom.h"
sadrul66904042015-12-03 05:25:0314#include "mash/wm/accelerator_registrar_impl.h"
sky7a35dfa2016-02-01 21:37:2815#include "mash/wm/root_window_controller.h"
skyffa8df22016-02-02 05:47:3416#include "mash/wm/root_windows_observer.h"
msw85273d0c2016-01-14 05:05:1717#include "mash/wm/user_window_controller_impl.h"
moshayedid41e5ab2016-03-18 00:08:1518#include "mojo/converters/input_events/input_events_type_converters.h"
benf709a3092016-04-12 22:38:2219#include "services/shell/public/cpp/connection.h"
20#include "services/shell/public/cpp/connector.h"
ben7095d1a2016-04-13 06:14:5821#include "services/tracing/public/cpp/tracing_impl.h"
moshayedid41e5ab2016-03-18 00:08:1522#include "ui/events/event.h"
sky56961cd2015-11-05 03:47:2823#include "ui/mojo/init/ui_init.h"
24#include "ui/views/mus/aura_init.h"
25#include "ui/views/mus/display_converter.h"
ben294d04b2015-10-15 01:23:3326
sky89c6eb072015-11-25 20:12:2727namespace mash {
28namespace wm {
sadrul99b70b382015-11-24 21:07:1729
ben294d04b2015-10-15 01:23:3330WindowManagerApplication::WindowManagerApplication()
bene4a2bc42016-02-26 07:30:2531 : connector_(nullptr), window_manager_factory_binding_(this) {}
sadrul99b70b382015-11-24 21:07:1732
sky7a35dfa2016-02-01 21:37:2833WindowManagerApplication::~WindowManagerApplication() {
skyffa8df22016-02-02 05:47:3434 // AcceleratorRegistrarImpl removes an observer in its destructor. Destroy
35 // it early on.
36 std::set<AcceleratorRegistrarImpl*> accelerator_registrars(
37 accelerator_registrars_);
38 for (AcceleratorRegistrarImpl* registrar : accelerator_registrars)
39 registrar->Destroy();
40
sky7a35dfa2016-02-01 21:37:2841 std::set<RootWindowController*> controllers(root_controllers_);
skyffa8df22016-02-02 05:47:3442 for (RootWindowController* controller : controllers)
43 controller->Destroy();
44}
45
46std::set<RootWindowController*> WindowManagerApplication::GetRootControllers() {
47 std::set<RootWindowController*> root_controllers;
48 for (RootWindowController* controller : root_controllers_) {
49 if (controller->root())
50 root_controllers.insert(controller);
51 }
52 return root_controllers;
skycf496a472015-10-23 21:08:4753}
54
sky7a35dfa2016-02-01 21:37:2855void WindowManagerApplication::OnRootWindowControllerGotRoot(
56 RootWindowController* root_controller) {
57 if (ui_init_.get())
58 return;
59
60 ui_init_.reset(new ui::mojo::UIInit(
61 views::GetDisplaysFromWindow(root_controller->root())));
bene4a2bc42016-02-26 07:30:2562 aura_init_.reset(new views::AuraInit(connector_, "mash_wm_resources.pak"));
benc6f7263b2015-10-27 18:17:3363}
64
sky7a35dfa2016-02-01 21:37:2865void WindowManagerApplication::OnRootWindowControllerDoneInit(
66 RootWindowController* root_controller) {
67 // TODO(msw): figure out if this should be per display, or global.
68 user_window_controller_->Initialize(root_controller);
69 for (auto& request : user_window_controller_requests_)
70 user_window_controller_binding_.AddBinding(user_window_controller_.get(),
71 std::move(*request));
72 user_window_controller_requests_.clear();
skyffa8df22016-02-02 05:47:3473
74 FOR_EACH_OBSERVER(RootWindowsObserver, root_windows_observers_,
75 OnRootWindowControllerAdded(root_controller));
benfe6381bc2015-12-02 16:07:2376}
77
sky7a35dfa2016-02-01 21:37:2878void WindowManagerApplication::OnRootWindowDestroyed(
79 RootWindowController* root_controller) {
80 root_controllers_.erase(root_controller);
81 user_window_controller_.reset(nullptr);
82}
83
84void WindowManagerApplication::OnAccelerator(uint32_t id,
moshayedid41e5ab2016-03-18 00:08:1585 const ui::Event& event) {
sky7a35dfa2016-02-01 21:37:2886 for (auto* registrar : accelerator_registrars_) {
87 if (registrar->OwnsAccelerator(id)) {
moshayedid41e5ab2016-03-18 00:08:1588 registrar->ProcessAccelerator(id, mus::mojom::Event::From(event));
sky7a35dfa2016-02-01 21:37:2889 break;
90 }
91 }
sadrul99b70b382015-11-24 21:07:1792}
93
skyffa8df22016-02-02 05:47:3494void WindowManagerApplication::AddRootWindowsObserver(
95 RootWindowsObserver* observer) {
96 root_windows_observers_.AddObserver(observer);
97}
98
99void WindowManagerApplication::RemoveRootWindowsObserver(
100 RootWindowsObserver* observer) {
101 root_windows_observers_.RemoveObserver(observer);
102}
103
sadrul66904042015-12-03 05:25:03104void WindowManagerApplication::OnAcceleratorRegistrarDestroyed(
105 AcceleratorRegistrarImpl* registrar) {
106 accelerator_registrars_.erase(registrar);
107}
108
bene4a2bc42016-02-26 07:30:25109void WindowManagerApplication::Initialize(mojo::Connector* connector,
ben9a8e15f62016-03-08 05:17:48110 const mojo::Identity& identity,
bencccfe2a2016-03-05 16:54:14111 uint32_t id) {
bene4a2bc42016-02-26 07:30:25112 connector_ = connector;
ben9a8e15f62016-03-08 05:17:48113 tracing_.Initialize(connector, identity.name());
sky7a35dfa2016-02-01 21:37:28114
115 mus::mojom::WindowManagerFactoryServicePtr wm_factory_service;
bene4a2bc42016-02-26 07:30:25116 connector_->ConnectToInterface("mojo:mus", &wm_factory_service);
sky7a35dfa2016-02-01 21:37:28117 wm_factory_service->SetWindowManagerFactory(
118 window_manager_factory_binding_.CreateInterfacePtrAndBind());
119
msw85273d0c2016-01-14 05:05:17120 user_window_controller_.reset(new UserWindowControllerImpl());
ben294d04b2015-10-15 01:23:33121}
122
bendbcf6eb2016-02-07 15:50:04123bool WindowManagerApplication::AcceptConnection(mojo::Connection* connection) {
ben123d0582016-02-09 06:57:04124 connection->AddInterface<mash::wm::mojom::UserWindowController>(this);
125 connection->AddInterface<mus::mojom::AcceleratorRegistrar>(this);
benc740d5b2016-03-18 04:54:27126 if (connection->GetRemoteIdentity().name() == "mojo:mash_session")
127 connection->GetInterface(&session_);
ben294d04b2015-10-15 01:23:33128 return true;
129}
130
ben294d04b2015-10-15 01:23:33131void WindowManagerApplication::Create(
bendbcf6eb2016-02-07 15:50:04132 mojo::Connection* connection,
msw85273d0c2016-01-14 05:05:17133 mojo::InterfaceRequest<mash::wm::mojom::UserWindowController> request) {
sky7a35dfa2016-02-01 21:37:28134 if (!root_controllers_.empty() && (*root_controllers_.begin())->root()) {
msw85273d0c2016-01-14 05:05:17135 user_window_controller_binding_.AddBinding(user_window_controller_.get(),
136 std::move(request));
137 } else {
mostynb1aaf5062016-04-04 17:56:27138 user_window_controller_requests_.push_back(base::WrapUnique(
msw85273d0c2016-01-14 05:05:17139 new mojo::InterfaceRequest<mash::wm::mojom::UserWindowController>(
140 std::move(request))));
141 }
142}
143
144void WindowManagerApplication::Create(
bendbcf6eb2016-02-07 15:50:04145 mojo::Connection* connection,
sadrul66904042015-12-03 05:25:03146 mojo::InterfaceRequest<mus::mojom::AcceleratorRegistrar> request) {
147 static int accelerator_registrar_count = 0;
148 if (accelerator_registrar_count == std::numeric_limits<int>::max()) {
149 // Restart from zero if we have reached the limit. It is technically
150 // possible to end up with multiple active registrars with the same
151 // namespace, but it is highly unlikely. In the event that multiple
152 // registrars have the same namespace, this new registrar will be unable to
153 // install accelerators.
154 accelerator_registrar_count = 0;
155 }
156 accelerator_registrars_.insert(new AcceleratorRegistrarImpl(
skyffa8df22016-02-02 05:47:34157 this, ++accelerator_registrar_count, std::move(request),
sadrul66904042015-12-03 05:25:03158 base::Bind(&WindowManagerApplication::OnAcceleratorRegistrarDestroyed,
159 base::Unretained(this))));
160}
161
sky7a35dfa2016-02-01 21:37:28162void WindowManagerApplication::CreateWindowManager(
163 mus::mojom::DisplayPtr display,
164 mojo::InterfaceRequest<mus::mojom::WindowTreeClient> client_request) {
165 root_controllers_.insert(RootWindowController::CreateFromDisplay(
166 this, std::move(display), std::move(client_request)));
ben294d04b2015-10-15 01:23:33167}
sky89c6eb072015-11-25 20:12:27168
169} // namespace wm
170} // namespace mash