James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 1 | // Copyright 2014 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 | |
James Robinson | 1581cd6 | 2014-12-02 10:52:00 -0800 | [diff] [blame] | 5 | #include "services/window_manager/window_manager_app.h" |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 6 | |
| 7 | #include "base/message_loop/message_loop.h" |
| 8 | #include "base/stl_util.h" |
John Abd-El-Malek | ed8d84d | 2014-10-23 14:27:08 -0700 | [diff] [blame] | 9 | #include "mojo/converters/geometry/geometry_type_converters.h" |
James Robinson | e2ac7e8 | 2014-10-15 13:21:59 -0700 | [diff] [blame] | 10 | #include "mojo/converters/input_events/input_events_type_converters.h" |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 11 | #include "mojo/public/cpp/application/application_connection.h" |
| 12 | #include "mojo/public/cpp/application/application_impl.h" |
James Robinson | baf71d3 | 2014-10-08 13:00:20 -0700 | [diff] [blame] | 13 | #include "mojo/public/interfaces/application/shell.mojom.h" |
Colin Blundell | c4edbc7 | 2014-12-10 14:16:27 +0100 | [diff] [blame] | 14 | #include "mojo/services/view_manager/public/cpp/view.h" |
| 15 | #include "mojo/services/view_manager/public/cpp/view_manager.h" |
Elliot Glaysher | f88a2fb | 2014-12-16 11:09:36 -0800 | [diff] [blame] | 16 | #include "services/window_manager/capture_controller.h" |
James Robinson | 1581cd6 | 2014-12-02 10:52:00 -0800 | [diff] [blame] | 17 | #include "services/window_manager/focus_controller.h" |
| 18 | #include "services/window_manager/focus_rules.h" |
Elliot Glaysher | ad08bd0 | 2015-01-20 12:18:28 -0800 | [diff] [blame] | 19 | #include "services/window_manager/hit_test.h" |
James Robinson | 1581cd6 | 2014-12-02 10:52:00 -0800 | [diff] [blame] | 20 | #include "services/window_manager/view_event_dispatcher.h" |
| 21 | #include "services/window_manager/view_target.h" |
| 22 | #include "services/window_manager/view_targeter.h" |
| 23 | #include "services/window_manager/window_manager_delegate.h" |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 24 | |
James Robinson | 489c8f1 | 2014-12-08 12:08:55 -0800 | [diff] [blame] | 25 | using mojo::ApplicationConnection; |
| 26 | using mojo::Id; |
| 27 | using mojo::ServiceProvider; |
| 28 | using mojo::View; |
| 29 | using mojo::WindowManager; |
| 30 | |
| 31 | namespace window_manager { |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 32 | |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 33 | namespace { |
| 34 | |
Elliot Glaysher | 28b50b9 | 2014-11-10 17:06:49 -0800 | [diff] [blame] | 35 | Id GetIdForView(View* view) { |
| 36 | return view ? view->id() : 0; |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | } // namespace |
| 40 | |
Scott Violet | 5d674ef | 2014-10-22 15:52:54 -0700 | [diff] [blame] | 41 | // Used for calls to Embed() that occur before we've connected to the |
| 42 | // ViewManager. |
| 43 | struct WindowManagerApp::PendingEmbed { |
James Robinson | 489c8f1 | 2014-12-08 12:08:55 -0800 | [diff] [blame] | 44 | mojo::String url; |
James Robinson | 3b67967 | 2015-01-21 18:36:01 -0800 | [diff] [blame] | 45 | mojo::InterfaceRequest<ServiceProvider> services; |
| 46 | mojo::ServiceProviderPtr exposed_services; |
Scott Violet | 5d674ef | 2014-10-22 15:52:54 -0700 | [diff] [blame] | 47 | }; |
| 48 | |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 49 | //////////////////////////////////////////////////////////////////////////////// |
| 50 | // WindowManagerApp, public: |
| 51 | |
| 52 | WindowManagerApp::WindowManagerApp( |
| 53 | ViewManagerDelegate* view_manager_delegate, |
| 54 | WindowManagerDelegate* window_manager_delegate) |
James Robinson | baf71d3 | 2014-10-08 13:00:20 -0700 | [diff] [blame] | 55 | : shell_(nullptr), |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 56 | wrapped_view_manager_delegate_(view_manager_delegate), |
James Robinson | e2ac7e8 | 2014-10-15 13:21:59 -0700 | [diff] [blame] | 57 | window_manager_delegate_(window_manager_delegate), |
Michael Wasserman | bae1794 | 2015-01-14 20:33:10 -0800 | [diff] [blame] | 58 | root_(nullptr) { |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Eric Seidel | 3f1157b | 2014-11-05 13:09:08 -0800 | [diff] [blame] | 61 | WindowManagerApp::~WindowManagerApp() { |
Michael Wasserman | 93d013c | 2015-01-14 20:22:36 -0800 | [diff] [blame] | 62 | // TODO(msw|sky): Should this destructor explicitly delete the ViewManager? |
| 63 | mojo::ViewManager* cached_view_manager = view_manager(); |
| 64 | for (RegisteredViewIdSet::const_iterator it = registered_view_id_set_.begin(); |
| 65 | cached_view_manager && it != registered_view_id_set_.end(); ++it) { |
| 66 | View* view = cached_view_manager->GetViewById(*it); |
| 67 | if (view && view == root_) |
| 68 | root_ = nullptr; |
| 69 | if (view) |
| 70 | view->RemoveObserver(this); |
| 71 | } |
| 72 | registered_view_id_set_.clear(); |
| 73 | DCHECK(!root_); |
| 74 | |
Eric Seidel | 3f1157b | 2014-11-05 13:09:08 -0800 | [diff] [blame] | 75 | STLDeleteElements(&connections_); |
| 76 | } |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 77 | |
Scott Violet | 37e9aea | 2014-10-31 15:06:04 -0700 | [diff] [blame] | 78 | void WindowManagerApp::AddConnection(WindowManagerImpl* connection) { |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 79 | DCHECK(connections_.find(connection) == connections_.end()); |
| 80 | connections_.insert(connection); |
| 81 | } |
| 82 | |
Scott Violet | 37e9aea | 2014-10-31 15:06:04 -0700 | [diff] [blame] | 83 | void WindowManagerApp::RemoveConnection(WindowManagerImpl* connection) { |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 84 | DCHECK(connections_.find(connection) != connections_.end()); |
| 85 | connections_.erase(connection); |
| 86 | } |
| 87 | |
Scott Violet | 695598e | 2015-02-07 08:36:14 -0800 | [diff] [blame] | 88 | bool WindowManagerApp::SetCapture(Id view_id) { |
Scott Violet | 92643b5 | 2014-12-18 14:35:42 -0800 | [diff] [blame] | 89 | View* view = view_manager()->GetViewById(view_id); |
Scott Violet | 6edabbf | 2015-02-25 16:02:50 -0800 | [diff] [blame] | 90 | return view && SetCaptureImpl(view); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 91 | } |
| 92 | |
Scott Violet | 695598e | 2015-02-07 08:36:14 -0800 | [diff] [blame] | 93 | bool WindowManagerApp::FocusWindow(Id view_id) { |
Scott Violet | 92643b5 | 2014-12-18 14:35:42 -0800 | [diff] [blame] | 94 | View* view = view_manager()->GetViewById(view_id); |
Scott Violet | 6edabbf | 2015-02-25 16:02:50 -0800 | [diff] [blame] | 95 | return view && FocusWindowImpl(view); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Scott Violet | 695598e | 2015-02-07 08:36:14 -0800 | [diff] [blame] | 98 | bool WindowManagerApp::ActivateWindow(Id view_id) { |
Scott Violet | 92643b5 | 2014-12-18 14:35:42 -0800 | [diff] [blame] | 99 | View* view = view_manager()->GetViewById(view_id); |
Scott Violet | 6edabbf | 2015-02-25 16:02:50 -0800 | [diff] [blame] | 100 | return view && ActivateWindowImpl(view); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | bool WindowManagerApp::IsReady() const { |
Scott Violet | 92643b5 | 2014-12-18 14:35:42 -0800 | [diff] [blame] | 104 | return root_; |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 105 | } |
| 106 | |
James Robinson | 489c8f1 | 2014-12-08 12:08:55 -0800 | [diff] [blame] | 107 | void WindowManagerApp::InitFocus(scoped_ptr<FocusRules> rules) { |
Elliot Glaysher | f88a2fb | 2014-12-16 11:09:36 -0800 | [diff] [blame] | 108 | DCHECK(root_); |
| 109 | |
James Robinson | 489c8f1 | 2014-12-08 12:08:55 -0800 | [diff] [blame] | 110 | focus_controller_.reset(new FocusController(rules.Pass())); |
Elliot Glaysher | 28b50b9 | 2014-11-10 17:06:49 -0800 | [diff] [blame] | 111 | focus_controller_->AddObserver(this); |
Elliot Glaysher | e28eebf | 2014-11-19 10:39:24 -0800 | [diff] [blame] | 112 | SetFocusController(root_, focus_controller_.get()); |
Elliot Glaysher | f88a2fb | 2014-12-16 11:09:36 -0800 | [diff] [blame] | 113 | |
| 114 | capture_controller_.reset(new CaptureController); |
| 115 | capture_controller_->AddObserver(this); |
| 116 | SetCaptureController(root_, capture_controller_.get()); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Scott Violet | 5d674ef | 2014-10-22 15:52:54 -0700 | [diff] [blame] | 119 | void WindowManagerApp::Embed( |
James Robinson | 489c8f1 | 2014-12-08 12:08:55 -0800 | [diff] [blame] | 120 | const mojo::String& url, |
James Robinson | 3b67967 | 2015-01-21 18:36:01 -0800 | [diff] [blame] | 121 | mojo::InterfaceRequest<mojo::ServiceProvider> services, |
| 122 | mojo::ServiceProviderPtr exposed_services) { |
Scott Violet | 92643b5 | 2014-12-18 14:35:42 -0800 | [diff] [blame] | 123 | if (view_manager()) { |
James Robinson | 3b67967 | 2015-01-21 18:36:01 -0800 | [diff] [blame] | 124 | window_manager_delegate_->Embed(url, services.Pass(), |
| 125 | exposed_services.Pass()); |
Scott Violet | 5d674ef | 2014-10-22 15:52:54 -0700 | [diff] [blame] | 126 | return; |
| 127 | } |
| 128 | scoped_ptr<PendingEmbed> pending_embed(new PendingEmbed); |
| 129 | pending_embed->url = url; |
James Robinson | 3b67967 | 2015-01-21 18:36:01 -0800 | [diff] [blame] | 130 | pending_embed->services = services.Pass(); |
| 131 | pending_embed->exposed_services = exposed_services.Pass(); |
Scott Violet | 5d674ef | 2014-10-22 15:52:54 -0700 | [diff] [blame] | 132 | pending_embeds_.push_back(pending_embed.release()); |
| 133 | } |
| 134 | |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 135 | //////////////////////////////////////////////////////////////////////////////// |
| 136 | // WindowManagerApp, ApplicationDelegate implementation: |
| 137 | |
James Robinson | 489c8f1 | 2014-12-08 12:08:55 -0800 | [diff] [blame] | 138 | void WindowManagerApp::Initialize(mojo::ApplicationImpl* impl) { |
James Robinson | baf71d3 | 2014-10-08 13:00:20 -0700 | [diff] [blame] | 139 | shell_ = impl->shell(); |
Scott Violet | 5d674ef | 2014-10-22 15:52:54 -0700 | [diff] [blame] | 140 | LaunchViewManager(impl); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | bool WindowManagerApp::ConfigureIncomingConnection( |
| 144 | ApplicationConnection* connection) { |
James Robinson | 05d5629 | 2015-02-18 17:35:49 -0800 | [diff] [blame] | 145 | connection->AddService<WindowManager>(this); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 146 | return true; |
| 147 | } |
| 148 | |
| 149 | //////////////////////////////////////////////////////////////////////////////// |
| 150 | // WindowManagerApp, ViewManagerDelegate implementation: |
| 151 | |
James Robinson | 3b67967 | 2015-01-21 18:36:01 -0800 | [diff] [blame] | 152 | void WindowManagerApp::OnEmbed( |
| 153 | View* root, |
| 154 | mojo::InterfaceRequest<mojo::ServiceProvider> services, |
| 155 | mojo::ServiceProviderPtr exposed_services) { |
Scott Violet | 92643b5 | 2014-12-18 14:35:42 -0800 | [diff] [blame] | 156 | DCHECK(!root_); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 157 | root_ = root; |
| 158 | |
James Robinson | 489c8f1 | 2014-12-08 12:08:55 -0800 | [diff] [blame] | 159 | view_event_dispatcher_.reset(new ViewEventDispatcher); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 160 | |
Elliot Glaysher | c89cc3b | 2014-11-17 16:45:06 -0800 | [diff] [blame] | 161 | RegisterSubtree(root_); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 162 | |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 163 | if (wrapped_view_manager_delegate_) { |
James Robinson | 3b67967 | 2015-01-21 18:36:01 -0800 | [diff] [blame] | 164 | wrapped_view_manager_delegate_->OnEmbed(root, services.Pass(), |
| 165 | exposed_services.Pass()); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 166 | } |
| 167 | |
James Robinson | 3b67967 | 2015-01-21 18:36:01 -0800 | [diff] [blame] | 168 | for (PendingEmbed* pending_embed : pending_embeds_) { |
| 169 | Embed(pending_embed->url, pending_embed->services.Pass(), |
| 170 | pending_embed->exposed_services.Pass()); |
| 171 | } |
Scott Violet | 5d674ef | 2014-10-22 15:52:54 -0700 | [diff] [blame] | 172 | pending_embeds_.clear(); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | void WindowManagerApp::OnViewManagerDisconnected( |
James Robinson | 489c8f1 | 2014-12-08 12:08:55 -0800 | [diff] [blame] | 176 | mojo::ViewManager* view_manager) { |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 177 | if (wrapped_view_manager_delegate_) |
| 178 | wrapped_view_manager_delegate_->OnViewManagerDisconnected(view_manager); |
Michael Wasserman | 93d013c | 2015-01-14 20:22:36 -0800 | [diff] [blame] | 179 | |
| 180 | base::MessageLoop* message_loop = base::MessageLoop::current(); |
| 181 | if (message_loop && message_loop->is_running()) |
| 182 | message_loop->Quit(); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 183 | } |
| 184 | |
Scott Violet | 6edabbf | 2015-02-25 16:02:50 -0800 | [diff] [blame] | 185 | bool WindowManagerApp::OnPerformAction(mojo::View* view, |
| 186 | const std::string& action) { |
| 187 | if (!view) |
| 188 | return false; |
| 189 | if (action == "capture") |
| 190 | return SetCaptureImpl(view); |
| 191 | if (action == "focus") |
| 192 | return FocusWindowImpl(view); |
| 193 | else if (action == "activate") |
| 194 | return ActivateWindowImpl(view); |
| 195 | return false; |
| 196 | } |
| 197 | |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 198 | //////////////////////////////////////////////////////////////////////////////// |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 199 | // WindowManagerApp, ViewObserver implementation: |
| 200 | |
| 201 | void WindowManagerApp::OnTreeChanged( |
| 202 | const ViewObserver::TreeChangeParams& params) { |
| 203 | if (params.receiver != root_) |
| 204 | return; |
| 205 | DCHECK(params.old_parent || params.new_parent); |
| 206 | if (!params.target) |
| 207 | return; |
| 208 | |
| 209 | if (params.new_parent) { |
Elliot Glaysher | c89cc3b | 2014-11-17 16:45:06 -0800 | [diff] [blame] | 210 | if (registered_view_id_set_.find(params.target->id()) == |
| 211 | registered_view_id_set_.end()) { |
| 212 | RegisteredViewIdSet::const_iterator it = |
| 213 | registered_view_id_set_.find(params.new_parent->id()); |
| 214 | DCHECK(it != registered_view_id_set_.end()); |
| 215 | RegisterSubtree(params.target); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 216 | } |
| 217 | } else if (params.old_parent) { |
| 218 | UnregisterSubtree(params.target); |
| 219 | } |
| 220 | } |
| 221 | |
James Robinson | e2ac7e8 | 2014-10-15 13:21:59 -0700 | [diff] [blame] | 222 | void WindowManagerApp::OnViewDestroying(View* view) { |
Michael Wasserman | 93d013c | 2015-01-14 20:22:36 -0800 | [diff] [blame] | 223 | Unregister(view); |
| 224 | if (view == root_) { |
| 225 | root_ = nullptr; |
| 226 | if (focus_controller_) |
| 227 | focus_controller_->RemoveObserver(this); |
| 228 | if (capture_controller_) |
| 229 | capture_controller_->RemoveObserver(this); |
James Robinson | e2ac7e8 | 2014-10-15 13:21:59 -0700 | [diff] [blame] | 230 | } |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 231 | } |
| 232 | |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 233 | //////////////////////////////////////////////////////////////////////////////// |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 234 | // WindowManagerApp, ui::EventHandler implementation: |
| 235 | |
| 236 | void WindowManagerApp::OnEvent(ui::Event* event) { |
James Robinson | e2ac7e8 | 2014-10-15 13:21:59 -0700 | [diff] [blame] | 237 | if (!window_manager_client_) |
| 238 | return; |
| 239 | |
Elliot Glaysher | c89cc3b | 2014-11-17 16:45:06 -0800 | [diff] [blame] | 240 | View* view = static_cast<ViewTarget*>(event->target())->view(); |
James Robinson | e2ac7e8 | 2014-10-15 13:21:59 -0700 | [diff] [blame] | 241 | if (!view) |
| 242 | return; |
| 243 | |
Benjamin Lerman | 63fc278 | 2015-02-26 10:33:42 +0100 | [diff] [blame] | 244 | if (event->IsKeyEvent()) { |
| 245 | const ui::KeyEvent* key_event = static_cast<const ui::KeyEvent*>(event); |
| 246 | if (key_event->type() == ui::ET_KEY_PRESSED) { |
| 247 | ui::Accelerator accelerator = ConvertEventToAccelerator(key_event); |
Scott Violet | 0dbc721 | 2015-04-13 13:31:43 -0700 | [diff] [blame] | 248 | if (accelerator_manager_.Process(accelerator, view)) |
Benjamin Lerman | 63fc278 | 2015-02-26 10:33:42 +0100 | [diff] [blame] | 249 | return; |
| 250 | } |
| 251 | } |
| 252 | |
Elliot Glaysher | 28b50b9 | 2014-11-10 17:06:49 -0800 | [diff] [blame] | 253 | if (focus_controller_) |
| 254 | focus_controller_->OnEvent(event); |
| 255 | |
James Robinson | e2ac7e8 | 2014-10-15 13:21:59 -0700 | [diff] [blame] | 256 | window_manager_client_->DispatchInputEventToView(view->id(), |
James Robinson | 489c8f1 | 2014-12-08 12:08:55 -0800 | [diff] [blame] | 257 | mojo::Event::From(*event)); |
Adam Barth | 29fd00a | 2015-01-13 17:01:10 -0800 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | //////////////////////////////////////////////////////////////////////////////// |
Elliot Glaysher | 28b50b9 | 2014-11-10 17:06:49 -0800 | [diff] [blame] | 261 | // WindowManagerApp, mojo::FocusControllerObserver implementation: |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 262 | |
James Robinson | 8c33cd9 | 2015-01-29 12:33:50 -0800 | [diff] [blame] | 263 | void WindowManagerApp::OnFocused(View* gained_focus) { |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 264 | for (Connections::const_iterator it = connections_.begin(); |
| 265 | it != connections_.end(); ++it) { |
James Robinson | 8c33cd9 | 2015-01-29 12:33:50 -0800 | [diff] [blame] | 266 | (*it)->NotifyViewFocused(GetIdForView(gained_focus)); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 267 | } |
| 268 | } |
| 269 | |
James Robinson | 8c33cd9 | 2015-01-29 12:33:50 -0800 | [diff] [blame] | 270 | void WindowManagerApp::OnActivated(View* gained_active) { |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 271 | for (Connections::const_iterator it = connections_.begin(); |
| 272 | it != connections_.end(); ++it) { |
James Robinson | 8c33cd9 | 2015-01-29 12:33:50 -0800 | [diff] [blame] | 273 | (*it)->NotifyWindowActivated(GetIdForView(gained_active)); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 274 | } |
Elliot Glaysher | 28b50b9 | 2014-11-10 17:06:49 -0800 | [diff] [blame] | 275 | if (gained_active) |
| 276 | gained_active->MoveToFront(); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | //////////////////////////////////////////////////////////////////////////////// |
Elliot Glaysher | f88a2fb | 2014-12-16 11:09:36 -0800 | [diff] [blame] | 280 | // WindowManagerApp, mojo::CaptureControllerObserver implementation: |
| 281 | |
James Robinson | 8c33cd9 | 2015-01-29 12:33:50 -0800 | [diff] [blame] | 282 | void WindowManagerApp::OnCaptureChanged(View* gained_capture) { |
Elliot Glaysher | f88a2fb | 2014-12-16 11:09:36 -0800 | [diff] [blame] | 283 | for (Connections::const_iterator it = connections_.begin(); |
| 284 | it != connections_.end(); ++it) { |
James Robinson | 8c33cd9 | 2015-01-29 12:33:50 -0800 | [diff] [blame] | 285 | (*it)->NotifyCaptureChanged(GetIdForView(gained_capture)); |
Elliot Glaysher | f88a2fb | 2014-12-16 11:09:36 -0800 | [diff] [blame] | 286 | } |
| 287 | if (gained_capture) |
| 288 | gained_capture->MoveToFront(); |
| 289 | } |
| 290 | |
| 291 | //////////////////////////////////////////////////////////////////////////////// |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 292 | // WindowManagerApp, private: |
| 293 | |
Scott Violet | 6edabbf | 2015-02-25 16:02:50 -0800 | [diff] [blame] | 294 | bool WindowManagerApp::SetCaptureImpl(View* view) { |
| 295 | CHECK(view); |
| 296 | capture_controller_->SetCapture(view); |
| 297 | return capture_controller_->GetCapture() == view; |
| 298 | } |
| 299 | |
| 300 | bool WindowManagerApp::FocusWindowImpl(View* view) { |
| 301 | CHECK(view); |
| 302 | focus_controller_->FocusView(view); |
| 303 | return focus_controller_->GetFocusedView() == view; |
| 304 | } |
| 305 | |
| 306 | bool WindowManagerApp::ActivateWindowImpl(View* view) { |
| 307 | CHECK(view); |
| 308 | focus_controller_->ActivateView(view); |
| 309 | return focus_controller_->GetActiveView() == view; |
| 310 | } |
| 311 | |
Elliot Glaysher | c89cc3b | 2014-11-17 16:45:06 -0800 | [diff] [blame] | 312 | void WindowManagerApp::RegisterSubtree(View* view) { |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 313 | view->AddObserver(this); |
Elliot Glaysher | c89cc3b | 2014-11-17 16:45:06 -0800 | [diff] [blame] | 314 | DCHECK(registered_view_id_set_.find(view->id()) == |
| 315 | registered_view_id_set_.end()); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 316 | // All events pass through the root during dispatch, so we only need a handler |
| 317 | // installed there. |
Elliot Glaysher | 28b50b9 | 2014-11-10 17:06:49 -0800 | [diff] [blame] | 318 | if (view == root_) { |
Elliot Glaysher | c89cc3b | 2014-11-17 16:45:06 -0800 | [diff] [blame] | 319 | ViewTarget* target = ViewTarget::TargetFromView(view); |
Elliot Glaysher | 28b50b9 | 2014-11-10 17:06:49 -0800 | [diff] [blame] | 320 | target->SetEventTargeter(scoped_ptr<ViewTargeter>(new ViewTargeter())); |
| 321 | target->AddPreTargetHandler(this); |
| 322 | view_event_dispatcher_->SetRootViewTarget(target); |
| 323 | } |
Elliot Glaysher | c89cc3b | 2014-11-17 16:45:06 -0800 | [diff] [blame] | 324 | registered_view_id_set_.insert(view->id()); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 325 | View::Children::const_iterator it = view->children().begin(); |
| 326 | for (; it != view->children().end(); ++it) |
Elliot Glaysher | c89cc3b | 2014-11-17 16:45:06 -0800 | [diff] [blame] | 327 | RegisterSubtree(*it); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | void WindowManagerApp::UnregisterSubtree(View* view) { |
James Robinson | e2ac7e8 | 2014-10-15 13:21:59 -0700 | [diff] [blame] | 331 | for (View* child : view->children()) |
| 332 | UnregisterSubtree(child); |
| 333 | Unregister(view); |
| 334 | } |
| 335 | |
| 336 | void WindowManagerApp::Unregister(View* view) { |
Elliot Glaysher | c89cc3b | 2014-11-17 16:45:06 -0800 | [diff] [blame] | 337 | RegisteredViewIdSet::iterator it = registered_view_id_set_.find(view->id()); |
| 338 | if (it == registered_view_id_set_.end()) { |
James Robinson | e2ac7e8 | 2014-10-15 13:21:59 -0700 | [diff] [blame] | 339 | // Because we unregister in OnViewDestroying() we can still get a subsequent |
| 340 | // OnTreeChanged for the same view. Ignore this one. |
| 341 | return; |
| 342 | } |
| 343 | view->RemoveObserver(this); |
Elliot Glaysher | c89cc3b | 2014-11-17 16:45:06 -0800 | [diff] [blame] | 344 | DCHECK(it != registered_view_id_set_.end()); |
| 345 | registered_view_id_set_.erase(it); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 346 | } |
| 347 | |
James Robinson | 489c8f1 | 2014-12-08 12:08:55 -0800 | [diff] [blame] | 348 | void WindowManagerApp::DispatchInputEventToView(View* view, |
| 349 | mojo::EventPtr event) { |
Adam Barth | 4c9c2c5 | 2014-11-12 10:32:54 -0800 | [diff] [blame] | 350 | window_manager_client_->DispatchInputEventToView(view->id(), event.Pass()); |
| 351 | } |
| 352 | |
Eric Seidel | 6a7fabe | 2014-10-30 14:20:45 -0700 | [diff] [blame] | 353 | void WindowManagerApp::SetViewportSize(const gfx::Size& size) { |
James Robinson | 489c8f1 | 2014-12-08 12:08:55 -0800 | [diff] [blame] | 354 | window_manager_client_->SetViewportSize(mojo::Size::From(size)); |
Eric Seidel | 6a7fabe | 2014-10-30 14:20:45 -0700 | [diff] [blame] | 355 | } |
| 356 | |
James Robinson | 489c8f1 | 2014-12-08 12:08:55 -0800 | [diff] [blame] | 357 | void WindowManagerApp::LaunchViewManager(mojo::ApplicationImpl* app) { |
Scott Violet | 5d674ef | 2014-10-22 15:52:54 -0700 | [diff] [blame] | 358 | // TODO(sky): figure out logic if this connection goes away. |
| 359 | view_manager_client_factory_.reset( |
James Robinson | 489c8f1 | 2014-12-08 12:08:55 -0800 | [diff] [blame] | 360 | new mojo::ViewManagerClientFactory(shell_, this)); |
Scott Violet | 5d674ef | 2014-10-22 15:52:54 -0700 | [diff] [blame] | 361 | |
Scott Violet | 5d674ef | 2014-10-22 15:52:54 -0700 | [diff] [blame] | 362 | ApplicationConnection* view_manager_app = |
| 363 | app->ConnectToApplication("mojo:view_manager"); |
James Robinson | 2297c63 | 2015-02-06 11:06:34 -0800 | [diff] [blame] | 364 | view_manager_app->ConnectToService(&view_manager_service_); |
Scott Violet | 5d674ef | 2014-10-22 15:52:54 -0700 | [diff] [blame] | 365 | |
James Robinson | 2297c63 | 2015-02-06 11:06:34 -0800 | [diff] [blame] | 366 | view_manager_app->AddService<WindowManagerInternal>(this); |
James Robinson | 05d5629 | 2015-02-18 17:35:49 -0800 | [diff] [blame] | 367 | view_manager_app->AddService<mojo::NativeViewportEventDispatcher>(this); |
Scott Violet | c947ecc | 2014-10-28 16:49:51 -0700 | [diff] [blame] | 368 | |
| 369 | view_manager_app->ConnectToService(&window_manager_client_); |
Scott Violet | 5d674ef | 2014-10-22 15:52:54 -0700 | [diff] [blame] | 370 | } |
| 371 | |
James Robinson | 489c8f1 | 2014-12-08 12:08:55 -0800 | [diff] [blame] | 372 | void WindowManagerApp::Create( |
| 373 | ApplicationConnection* connection, |
| 374 | mojo::InterfaceRequest<WindowManagerInternal> request) { |
Scott Violet | 945fd42 | 2014-11-19 15:56:54 -0800 | [diff] [blame] | 375 | if (wm_internal_binding_.get()) { |
| 376 | VLOG(1) << |
| 377 | "WindowManager allows only one WindowManagerInternal connection."; |
| 378 | return; |
| 379 | } |
| 380 | wm_internal_binding_.reset( |
James Robinson | 489c8f1 | 2014-12-08 12:08:55 -0800 | [diff] [blame] | 381 | new mojo::Binding<WindowManagerInternal>(this, request.Pass())); |
Scott Violet | 945fd42 | 2014-11-19 15:56:54 -0800 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | void WindowManagerApp::Create(ApplicationConnection* connection, |
James Robinson | 489c8f1 | 2014-12-08 12:08:55 -0800 | [diff] [blame] | 385 | mojo::InterfaceRequest<WindowManager> request) { |
Scott Violet | 945fd42 | 2014-11-19 15:56:54 -0800 | [diff] [blame] | 386 | WindowManagerImpl* wm = new WindowManagerImpl(this, false); |
| 387 | wm->Bind(request.PassMessagePipe()); |
| 388 | // WindowManagerImpl is deleted when the connection has an error, or from our |
| 389 | // destructor. |
| 390 | } |
| 391 | |
James Robinson | 05d5629 | 2015-02-18 17:35:49 -0800 | [diff] [blame] | 392 | void WindowManagerApp::Create( |
| 393 | mojo::ApplicationConnection* connection, |
| 394 | mojo::InterfaceRequest<mojo::NativeViewportEventDispatcher> request) { |
| 395 | new NativeViewportEventDispatcherImpl(this, request.Pass()); |
| 396 | } |
| 397 | |
Scott Violet | 945fd42 | 2014-11-19 15:56:54 -0800 | [diff] [blame] | 398 | void WindowManagerApp::CreateWindowManagerForViewManagerClient( |
| 399 | uint16_t connection_id, |
James Robinson | 489c8f1 | 2014-12-08 12:08:55 -0800 | [diff] [blame] | 400 | mojo::ScopedMessagePipeHandle window_manager_pipe) { |
Scott Violet | 29f7dbf | 2014-12-10 14:26:52 -0800 | [diff] [blame] | 401 | // TODO(sky): pass in |connection_id| for validation. |
Scott Violet | 945fd42 | 2014-11-19 15:56:54 -0800 | [diff] [blame] | 402 | WindowManagerImpl* wm = new WindowManagerImpl(this, true); |
| 403 | wm->Bind(window_manager_pipe.Pass()); |
| 404 | // WindowManagerImpl is deleted when the connection has an error, or from our |
| 405 | // destructor. |
Scott Violet | 37e9aea | 2014-10-31 15:06:04 -0700 | [diff] [blame] | 406 | } |
| 407 | |
James Robinson | 2297c63 | 2015-02-06 11:06:34 -0800 | [diff] [blame] | 408 | void WindowManagerApp::SetViewManagerClient( |
| 409 | mojo::ScopedMessagePipeHandle view_manager_client_request) { |
James Robinson | 3aefd1b | 2015-02-10 17:11:46 -0800 | [diff] [blame] | 410 | view_manager_client_.reset( |
James Robinson | 2297c63 | 2015-02-06 11:06:34 -0800 | [diff] [blame] | 411 | mojo::ViewManagerClientFactory::WeakBindViewManagerToPipe( |
| 412 | mojo::MakeRequest<mojo::ViewManagerClient>( |
| 413 | view_manager_client_request.Pass()), |
James Robinson | 3aefd1b | 2015-02-10 17:11:46 -0800 | [diff] [blame] | 414 | view_manager_service_.Pass(), shell_, this)); |
James Robinson | 2297c63 | 2015-02-06 11:06:34 -0800 | [diff] [blame] | 415 | } |
| 416 | |
James Robinson | 489c8f1 | 2014-12-08 12:08:55 -0800 | [diff] [blame] | 417 | } // namespace window_manager |