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.
5 #include "components/window_manager/window_manager_app.h"
7 #include "base/message_loop/message_loop.h"
8 #include "base/stl_util.h"
9 #include "components/view_manager/public/cpp/view.h"
10 #include "components/view_manager/public/cpp/view_manager.h"
11 #include "components/window_manager/window_manager_delegate.h"
12 #include "mojo/converters/geometry/geometry_type_converters.h"
13 #include "third_party/mojo/src/mojo/public/cpp/application/application_connection.h"
14 #include "third_party/mojo/src/mojo/public/cpp/application/application_impl.h"
15 #include "third_party/mojo/src/mojo/public/interfaces/application/shell.mojom.h"
17 using mojo::ApplicationConnection
;
19 using mojo::ServiceProvider
;
21 using mojo::WindowManager
;
23 namespace window_manager
{
25 // Used for calls to Embed() that occur before we've connected to the
27 struct WindowManagerApp::PendingEmbed
{
29 mojo::InterfaceRequest
<ServiceProvider
> services
;
30 mojo::ServiceProviderPtr exposed_services
;
33 ////////////////////////////////////////////////////////////////////////////////
34 // WindowManagerApp, public:
36 WindowManagerApp::WindowManagerApp(
37 ViewManagerDelegate
* view_manager_delegate
,
38 WindowManagerDelegate
* window_manager_delegate
)
40 wrapped_view_manager_delegate_(view_manager_delegate
),
41 window_manager_delegate_(window_manager_delegate
),
45 WindowManagerApp::~WindowManagerApp() {
46 // TODO(msw|sky): Should this destructor explicitly delete the ViewManager?
48 root_
->RemoveObserver(this);
50 STLDeleteElements(&connections_
);
53 void WindowManagerApp::AddConnection(WindowManagerImpl
* connection
) {
54 DCHECK(connections_
.find(connection
) == connections_
.end());
55 connections_
.insert(connection
);
58 void WindowManagerApp::RemoveConnection(WindowManagerImpl
* connection
) {
59 DCHECK(connections_
.find(connection
) != connections_
.end());
60 connections_
.erase(connection
);
63 bool WindowManagerApp::IsReady() const {
67 void WindowManagerApp::AddAccelerator(mojo::KeyboardCode keyboard_code
,
68 mojo::EventFlags flags
) {
69 window_manager_client_
->AddAccelerator(keyboard_code
, flags
);
72 void WindowManagerApp::Embed(
73 const mojo::String
& url
,
74 mojo::InterfaceRequest
<mojo::ServiceProvider
> services
,
75 mojo::ServiceProviderPtr exposed_services
) {
77 window_manager_delegate_
->Embed(url
, services
.Pass(),
78 exposed_services
.Pass());
81 scoped_ptr
<PendingEmbed
> pending_embed(new PendingEmbed
);
82 pending_embed
->url
= url
;
83 pending_embed
->services
= services
.Pass();
84 pending_embed
->exposed_services
= exposed_services
.Pass();
85 pending_embeds_
.push_back(pending_embed
.release());
88 ////////////////////////////////////////////////////////////////////////////////
89 // WindowManagerApp, ApplicationDelegate implementation:
91 void WindowManagerApp::Initialize(mojo::ApplicationImpl
* impl
) {
92 shell_
= impl
->shell();
93 LaunchViewManager(impl
);
96 bool WindowManagerApp::ConfigureIncomingConnection(
97 ApplicationConnection
* connection
) {
98 connection
->AddService
<WindowManager
>(this);
102 ////////////////////////////////////////////////////////////////////////////////
103 // WindowManagerApp, ViewManagerDelegate implementation:
105 void WindowManagerApp::OnEmbed(
107 mojo::InterfaceRequest
<mojo::ServiceProvider
> services
,
108 mojo::ServiceProviderPtr exposed_services
) {
112 root_
->AddObserver(this);
114 if (wrapped_view_manager_delegate_
) {
115 wrapped_view_manager_delegate_
->OnEmbed(root
, services
.Pass(),
116 exposed_services
.Pass());
119 for (PendingEmbed
* pending_embed
: pending_embeds_
) {
120 Embed(pending_embed
->url
, pending_embed
->services
.Pass(),
121 pending_embed
->exposed_services
.Pass());
123 pending_embeds_
.clear();
126 void WindowManagerApp::OnViewManagerDisconnected(
127 mojo::ViewManager
* view_manager
) {
128 if (wrapped_view_manager_delegate_
)
129 wrapped_view_manager_delegate_
->OnViewManagerDisconnected(view_manager
);
131 base::MessageLoop
* message_loop
= base::MessageLoop::current();
132 if (message_loop
&& message_loop
->is_running())
133 message_loop
->Quit();
136 ////////////////////////////////////////////////////////////////////////////////
137 // WindowManagerApp, ViewObserver implementation:
139 void WindowManagerApp::OnViewDestroying(View
* view
) {
140 DCHECK_EQ(root_
, view
);
141 root_
->RemoveObserver(this);
145 ////////////////////////////////////////////////////////////////////////////////
146 // WindowManagerApp, private:
148 void WindowManagerApp::DispatchInputEventToViewDEPRECATED(
150 mojo::EventPtr event
) {
151 window_manager_client_
->DispatchInputEventToViewDEPRECATED(view
->id(),
155 void WindowManagerApp::SetViewportSize(const gfx::Size
& size
) {
156 window_manager_client_
->SetViewportSize(mojo::Size::From(size
));
159 void WindowManagerApp::LaunchViewManager(mojo::ApplicationImpl
* app
) {
160 // TODO(sky): figure out logic if this connection goes away.
161 view_manager_client_factory_
.reset(
162 new mojo::ViewManagerClientFactory(shell_
, this));
164 ApplicationConnection
* view_manager_app
=
165 app
->ConnectToApplication("mojo:view_manager");
166 view_manager_app
->ConnectToService(&view_manager_service_
);
168 view_manager_app
->AddService
<WindowManagerInternal
>(this);
170 view_manager_app
->ConnectToService(&window_manager_client_
);
173 void WindowManagerApp::Create(
174 ApplicationConnection
* connection
,
175 mojo::InterfaceRequest
<WindowManagerInternal
> request
) {
176 if (wm_internal_binding_
.get()) {
178 "WindowManager allows only one WindowManagerInternal connection.";
181 wm_internal_binding_
.reset(
182 new mojo::Binding
<WindowManagerInternal
>(this, request
.Pass()));
185 void WindowManagerApp::Create(ApplicationConnection
* connection
,
186 mojo::InterfaceRequest
<WindowManager
> request
) {
187 WindowManagerImpl
* wm
= new WindowManagerImpl(this, false);
188 wm
->Bind(request
.PassMessagePipe());
189 // WindowManagerImpl is deleted when the connection has an error, or from our
193 void WindowManagerApp::SetViewManagerClient(
194 mojo::ScopedMessagePipeHandle view_manager_client_request
) {
195 view_manager_client_
.reset(
196 mojo::ViewManagerClientFactory::WeakBindViewManagerToPipe(
197 mojo::MakeRequest
<mojo::ViewManagerClient
>(
198 view_manager_client_request
.Pass()),
199 view_manager_service_
.Pass(), shell_
, this));
202 void WindowManagerApp::OnAccelerator(mojo::EventPtr event
) {
203 window_manager_delegate_
->OnAcceleratorPressed(
204 root_
->view_manager()->GetFocusedView(),
205 event
->key_data
->windows_key_code
, event
->flags
);
208 } // namespace window_manager