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/view_manager/view_manager_app.h"
7 #include "components/view_manager/client_connection.h"
8 #include "components/view_manager/connection_manager.h"
9 #include "components/view_manager/display_manager.h"
10 #include "components/view_manager/view_manager_service_impl.h"
11 #include "mojo/application/application_runner_chromium.h"
12 #include "mojo/common/tracing_impl.h"
13 #include "third_party/mojo/src/mojo/public/c/system/main.h"
14 #include "third_party/mojo/src/mojo/public/cpp/application/application_connection.h"
15 #include "third_party/mojo/src/mojo/public/cpp/application/application_impl.h"
17 using mojo::ApplicationConnection
;
18 using mojo::ApplicationImpl
;
19 using mojo::InterfaceRequest
;
20 using mojo::ViewManagerService
;
21 using mojo::WindowManagerInternalClient
;
23 namespace view_manager
{
25 ViewManagerApp::ViewManagerApp()
26 : app_impl_(nullptr), wm_app_connection_(nullptr) {
29 ViewManagerApp::~ViewManagerApp() {}
31 void ViewManagerApp::Initialize(ApplicationImpl
* app
) {
33 tracing_
.Initialize(app
);
36 bool ViewManagerApp::ConfigureIncomingConnection(
37 ApplicationConnection
* connection
) {
38 if (connection_manager_
.get()) {
39 VLOG(1) << "ViewManager allows only one window manager connection.";
42 wm_app_connection_
= connection
;
43 // |connection| originates from the WindowManager. Let it connect directly
44 // to the ViewManager and WindowManagerInternalClient.
45 connection
->AddService
<ViewManagerService
>(this);
46 connection
->AddService
<WindowManagerInternalClient
>(this);
47 connection
->ConnectToService(&wm_internal_
);
48 // If no ServiceProvider has been sent, refuse the connection.
51 wm_internal_
.set_error_handler(this);
53 scoped_ptr
<DefaultDisplayManager
> display_manager(new DefaultDisplayManager(
54 app_impl_
, base::Bind(&ViewManagerApp::OnLostConnectionToWindowManager
,
55 base::Unretained(this))));
56 connection_manager_
.reset(
57 new ConnectionManager(this, display_manager
.Pass(), wm_internal_
.get()));
61 void ViewManagerApp::OnLostConnectionToWindowManager() {
62 ApplicationImpl::Terminate();
65 ClientConnection
* ViewManagerApp::CreateClientConnectionForEmbedAtView(
66 ConnectionManager
* connection_manager
,
67 mojo::InterfaceRequest
<mojo::ViewManagerService
> service_request
,
68 mojo::ConnectionSpecificId creator_id
,
69 const std::string
& creator_url
,
70 const std::string
& url
,
71 const ViewId
& root_id
) {
72 mojo::ViewManagerClientPtr client
;
73 app_impl_
->ConnectToService(url
, &client
);
75 scoped_ptr
<ViewManagerServiceImpl
> service(new ViewManagerServiceImpl(
76 connection_manager
, creator_id
, creator_url
, url
, root_id
));
77 return new DefaultClientConnection(service
.Pass(), connection_manager
,
78 service_request
.Pass(), client
.Pass());
81 ClientConnection
* ViewManagerApp::CreateClientConnectionForEmbedAtView(
82 ConnectionManager
* connection_manager
,
83 mojo::InterfaceRequest
<mojo::ViewManagerService
> service_request
,
84 mojo::ConnectionSpecificId creator_id
,
85 const std::string
& creator_url
,
86 const ViewId
& root_id
,
87 mojo::ViewManagerClientPtr view_manager_client
) {
88 scoped_ptr
<ViewManagerServiceImpl
> service(new ViewManagerServiceImpl(
89 connection_manager
, creator_id
, creator_url
, std::string(), root_id
));
90 return new DefaultClientConnection(service
.Pass(), connection_manager
,
91 service_request
.Pass(),
92 view_manager_client
.Pass());
95 void ViewManagerApp::Create(ApplicationConnection
* connection
,
96 InterfaceRequest
<ViewManagerService
> request
) {
97 if (connection_manager_
->has_window_manager_client_connection()) {
98 VLOG(1) << "ViewManager interface requested more than once.";
102 scoped_ptr
<ViewManagerServiceImpl
> service(new ViewManagerServiceImpl(
103 connection_manager_
.get(), kInvalidConnectionId
, std::string(),
104 std::string("mojo:window_manager"), RootViewId()));
105 mojo::ViewManagerClientPtr client
;
106 wm_internal_client_request_
= GetProxy(&client
);
107 scoped_ptr
<ClientConnection
> client_connection(
108 new DefaultClientConnection(service
.Pass(), connection_manager_
.get(),
109 request
.Pass(), client
.Pass()));
110 connection_manager_
->SetWindowManagerClientConnection(
111 client_connection
.Pass());
114 void ViewManagerApp::Create(
115 ApplicationConnection
* connection
,
116 InterfaceRequest
<WindowManagerInternalClient
> request
) {
117 if (wm_internal_client_binding_
.get()) {
118 VLOG(1) << "WindowManagerInternalClient requested more than once.";
122 // ConfigureIncomingConnection() must have been called before getting here.
123 DCHECK(connection_manager_
.get());
124 wm_internal_client_binding_
.reset(
125 new mojo::Binding
<WindowManagerInternalClient
>(connection_manager_
.get(),
127 wm_internal_client_binding_
->set_error_handler(this);
128 wm_internal_
->SetViewManagerClient(
129 wm_internal_client_request_
.PassMessagePipe());
132 void ViewManagerApp::OnConnectionError() {
133 ApplicationImpl::Terminate();
136 } // namespace view_manager