ben | 294d04b | 2015-10-15 01:23:33 | [diff] [blame] | 1 | // 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 | |
sky | b044635 | 2015-11-18 00:43:56 | [diff] [blame] | 5 | #include "mash/wm/window_manager_application.h" |
ben | 294d04b | 2015-10-15 01:23:33 | [diff] [blame] | 6 | |
dcheng | e4860045 | 2015-12-28 02:24:50 | [diff] [blame] | 7 | #include <utility> |
avi | 703dbbf | 2015-12-22 01:46:28 | [diff] [blame] | 8 | |
sadrul | 3b1906f4 | 2015-12-02 23:50:44 | [diff] [blame] | 9 | #include "base/bind.h" |
mostynb | 1aaf506 | 2016-04-04 17:56:27 | [diff] [blame] | 10 | #include "base/memory/ptr_util.h" |
sadrul | 99b70b38 | 2015-11-24 21:07:17 | [diff] [blame] | 11 | #include "components/mus/public/cpp/event_matcher.h" |
fsamuel | 68afcf45 | 2015-10-16 00:12:45 | [diff] [blame] | 12 | #include "components/mus/public/cpp/window.h" |
sky | 7a35dfa | 2016-02-01 21:37:28 | [diff] [blame] | 13 | #include "components/mus/public/interfaces/window_manager_factory.mojom.h" |
sadrul | 6690404 | 2015-12-03 05:25:03 | [diff] [blame] | 14 | #include "mash/wm/accelerator_registrar_impl.h" |
sky | 7a35dfa | 2016-02-01 21:37:28 | [diff] [blame] | 15 | #include "mash/wm/root_window_controller.h" |
sky | ffa8df2 | 2016-02-02 05:47:34 | [diff] [blame] | 16 | #include "mash/wm/root_windows_observer.h" |
msw | 85273d0c | 2016-01-14 05:05:17 | [diff] [blame] | 17 | #include "mash/wm/user_window_controller_impl.h" |
moshayedi | d41e5ab | 2016-03-18 00:08:15 | [diff] [blame] | 18 | #include "mojo/converters/input_events/input_events_type_converters.h" |
ben | f709a309 | 2016-04-12 22:38:22 | [diff] [blame] | 19 | #include "services/shell/public/cpp/connection.h" |
| 20 | #include "services/shell/public/cpp/connector.h" |
ben | 7095d1a | 2016-04-13 06:14:58 | [diff] [blame^] | 21 | #include "services/tracing/public/cpp/tracing_impl.h" |
moshayedi | d41e5ab | 2016-03-18 00:08:15 | [diff] [blame] | 22 | #include "ui/events/event.h" |
sky | 56961cd | 2015-11-05 03:47:28 | [diff] [blame] | 23 | #include "ui/mojo/init/ui_init.h" |
| 24 | #include "ui/views/mus/aura_init.h" |
| 25 | #include "ui/views/mus/display_converter.h" |
ben | 294d04b | 2015-10-15 01:23:33 | [diff] [blame] | 26 | |
sky | 89c6eb07 | 2015-11-25 20:12:27 | [diff] [blame] | 27 | namespace mash { |
| 28 | namespace wm { |
sadrul | 99b70b38 | 2015-11-24 21:07:17 | [diff] [blame] | 29 | |
ben | 294d04b | 2015-10-15 01:23:33 | [diff] [blame] | 30 | WindowManagerApplication::WindowManagerApplication() |
ben | e4a2bc4 | 2016-02-26 07:30:25 | [diff] [blame] | 31 | : connector_(nullptr), window_manager_factory_binding_(this) {} |
sadrul | 99b70b38 | 2015-11-24 21:07:17 | [diff] [blame] | 32 | |
sky | 7a35dfa | 2016-02-01 21:37:28 | [diff] [blame] | 33 | WindowManagerApplication::~WindowManagerApplication() { |
sky | ffa8df2 | 2016-02-02 05:47:34 | [diff] [blame] | 34 | // 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 | |
sky | 7a35dfa | 2016-02-01 21:37:28 | [diff] [blame] | 41 | std::set<RootWindowController*> controllers(root_controllers_); |
sky | ffa8df2 | 2016-02-02 05:47:34 | [diff] [blame] | 42 | for (RootWindowController* controller : controllers) |
| 43 | controller->Destroy(); |
| 44 | } |
| 45 | |
| 46 | std::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; |
sky | cf496a47 | 2015-10-23 21:08:47 | [diff] [blame] | 53 | } |
| 54 | |
sky | 7a35dfa | 2016-02-01 21:37:28 | [diff] [blame] | 55 | void 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()))); |
ben | e4a2bc4 | 2016-02-26 07:30:25 | [diff] [blame] | 62 | aura_init_.reset(new views::AuraInit(connector_, "mash_wm_resources.pak")); |
ben | c6f7263b | 2015-10-27 18:17:33 | [diff] [blame] | 63 | } |
| 64 | |
sky | 7a35dfa | 2016-02-01 21:37:28 | [diff] [blame] | 65 | void 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(); |
sky | ffa8df2 | 2016-02-02 05:47:34 | [diff] [blame] | 73 | |
| 74 | FOR_EACH_OBSERVER(RootWindowsObserver, root_windows_observers_, |
| 75 | OnRootWindowControllerAdded(root_controller)); |
ben | fe6381bc | 2015-12-02 16:07:23 | [diff] [blame] | 76 | } |
| 77 | |
sky | 7a35dfa | 2016-02-01 21:37:28 | [diff] [blame] | 78 | void WindowManagerApplication::OnRootWindowDestroyed( |
| 79 | RootWindowController* root_controller) { |
| 80 | root_controllers_.erase(root_controller); |
| 81 | user_window_controller_.reset(nullptr); |
| 82 | } |
| 83 | |
| 84 | void WindowManagerApplication::OnAccelerator(uint32_t id, |
moshayedi | d41e5ab | 2016-03-18 00:08:15 | [diff] [blame] | 85 | const ui::Event& event) { |
sky | 7a35dfa | 2016-02-01 21:37:28 | [diff] [blame] | 86 | for (auto* registrar : accelerator_registrars_) { |
| 87 | if (registrar->OwnsAccelerator(id)) { |
moshayedi | d41e5ab | 2016-03-18 00:08:15 | [diff] [blame] | 88 | registrar->ProcessAccelerator(id, mus::mojom::Event::From(event)); |
sky | 7a35dfa | 2016-02-01 21:37:28 | [diff] [blame] | 89 | break; |
| 90 | } |
| 91 | } |
sadrul | 99b70b38 | 2015-11-24 21:07:17 | [diff] [blame] | 92 | } |
| 93 | |
sky | ffa8df2 | 2016-02-02 05:47:34 | [diff] [blame] | 94 | void WindowManagerApplication::AddRootWindowsObserver( |
| 95 | RootWindowsObserver* observer) { |
| 96 | root_windows_observers_.AddObserver(observer); |
| 97 | } |
| 98 | |
| 99 | void WindowManagerApplication::RemoveRootWindowsObserver( |
| 100 | RootWindowsObserver* observer) { |
| 101 | root_windows_observers_.RemoveObserver(observer); |
| 102 | } |
| 103 | |
sadrul | 6690404 | 2015-12-03 05:25:03 | [diff] [blame] | 104 | void WindowManagerApplication::OnAcceleratorRegistrarDestroyed( |
| 105 | AcceleratorRegistrarImpl* registrar) { |
| 106 | accelerator_registrars_.erase(registrar); |
| 107 | } |
| 108 | |
ben | e4a2bc4 | 2016-02-26 07:30:25 | [diff] [blame] | 109 | void WindowManagerApplication::Initialize(mojo::Connector* connector, |
ben | 9a8e15f6 | 2016-03-08 05:17:48 | [diff] [blame] | 110 | const mojo::Identity& identity, |
ben | cccfe2a | 2016-03-05 16:54:14 | [diff] [blame] | 111 | uint32_t id) { |
ben | e4a2bc4 | 2016-02-26 07:30:25 | [diff] [blame] | 112 | connector_ = connector; |
ben | 9a8e15f6 | 2016-03-08 05:17:48 | [diff] [blame] | 113 | tracing_.Initialize(connector, identity.name()); |
sky | 7a35dfa | 2016-02-01 21:37:28 | [diff] [blame] | 114 | |
| 115 | mus::mojom::WindowManagerFactoryServicePtr wm_factory_service; |
ben | e4a2bc4 | 2016-02-26 07:30:25 | [diff] [blame] | 116 | connector_->ConnectToInterface("mojo:mus", &wm_factory_service); |
sky | 7a35dfa | 2016-02-01 21:37:28 | [diff] [blame] | 117 | wm_factory_service->SetWindowManagerFactory( |
| 118 | window_manager_factory_binding_.CreateInterfacePtrAndBind()); |
| 119 | |
msw | 85273d0c | 2016-01-14 05:05:17 | [diff] [blame] | 120 | user_window_controller_.reset(new UserWindowControllerImpl()); |
ben | 294d04b | 2015-10-15 01:23:33 | [diff] [blame] | 121 | } |
| 122 | |
ben | dbcf6eb | 2016-02-07 15:50:04 | [diff] [blame] | 123 | bool WindowManagerApplication::AcceptConnection(mojo::Connection* connection) { |
ben | 123d058 | 2016-02-09 06:57:04 | [diff] [blame] | 124 | connection->AddInterface<mash::wm::mojom::UserWindowController>(this); |
| 125 | connection->AddInterface<mus::mojom::AcceleratorRegistrar>(this); |
ben | c740d5b | 2016-03-18 04:54:27 | [diff] [blame] | 126 | if (connection->GetRemoteIdentity().name() == "mojo:mash_session") |
| 127 | connection->GetInterface(&session_); |
ben | 294d04b | 2015-10-15 01:23:33 | [diff] [blame] | 128 | return true; |
| 129 | } |
| 130 | |
ben | 294d04b | 2015-10-15 01:23:33 | [diff] [blame] | 131 | void WindowManagerApplication::Create( |
ben | dbcf6eb | 2016-02-07 15:50:04 | [diff] [blame] | 132 | mojo::Connection* connection, |
msw | 85273d0c | 2016-01-14 05:05:17 | [diff] [blame] | 133 | mojo::InterfaceRequest<mash::wm::mojom::UserWindowController> request) { |
sky | 7a35dfa | 2016-02-01 21:37:28 | [diff] [blame] | 134 | if (!root_controllers_.empty() && (*root_controllers_.begin())->root()) { |
msw | 85273d0c | 2016-01-14 05:05:17 | [diff] [blame] | 135 | user_window_controller_binding_.AddBinding(user_window_controller_.get(), |
| 136 | std::move(request)); |
| 137 | } else { |
mostynb | 1aaf506 | 2016-04-04 17:56:27 | [diff] [blame] | 138 | user_window_controller_requests_.push_back(base::WrapUnique( |
msw | 85273d0c | 2016-01-14 05:05:17 | [diff] [blame] | 139 | new mojo::InterfaceRequest<mash::wm::mojom::UserWindowController>( |
| 140 | std::move(request)))); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | void WindowManagerApplication::Create( |
ben | dbcf6eb | 2016-02-07 15:50:04 | [diff] [blame] | 145 | mojo::Connection* connection, |
sadrul | 6690404 | 2015-12-03 05:25:03 | [diff] [blame] | 146 | 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( |
sky | ffa8df2 | 2016-02-02 05:47:34 | [diff] [blame] | 157 | this, ++accelerator_registrar_count, std::move(request), |
sadrul | 6690404 | 2015-12-03 05:25:03 | [diff] [blame] | 158 | base::Bind(&WindowManagerApplication::OnAcceleratorRegistrarDestroyed, |
| 159 | base::Unretained(this)))); |
| 160 | } |
| 161 | |
sky | 7a35dfa | 2016-02-01 21:37:28 | [diff] [blame] | 162 | void 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))); |
ben | 294d04b | 2015-10-15 01:23:33 | [diff] [blame] | 167 | } |
sky | 89c6eb07 | 2015-11-25 20:12:27 | [diff] [blame] | 168 | |
| 169 | } // namespace wm |
| 170 | } // namespace mash |