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 "base/command_line.h"
8 #include "components/view_manager/client_connection.h"
9 #include "components/view_manager/connection_manager.h"
10 #include "components/view_manager/display_manager.h"
11 #include "components/view_manager/public/cpp/args.h"
12 #include "components/view_manager/view_manager_service_impl.h"
13 #include "mojo/application/public/cpp/application_connection.h"
14 #include "mojo/application/public/cpp/application_impl.h"
15 #include "mojo/application/public/cpp/application_runner.h"
16 #include "mojo/common/tracing_impl.h"
17 #include "third_party/mojo/src/mojo/public/c/system/main.h"
18 #include "ui/events/event_switches.h"
19 #include "ui/events/platform/platform_event_source.h"
20 #include "ui/gl/gl_surface.h"
22 using mojo::ApplicationConnection
;
23 using mojo::ApplicationImpl
;
25 using mojo::InterfaceRequest
;
26 using mojo::ViewManagerRoot
;
27 using mojo::ViewManagerService
;
29 namespace view_manager
{
31 ViewManagerApp::ViewManagerApp() : app_impl_(nullptr), is_headless_(false) {
34 ViewManagerApp::~ViewManagerApp() {}
36 void ViewManagerApp::Initialize(ApplicationImpl
* app
) {
38 tracing_
.Initialize(app
);
40 #if !defined(OS_ANDROID)
41 base::CommandLine
* command_line
= base::CommandLine::ForCurrentProcess();
42 is_headless_
= command_line
->HasSwitch(mojo::kUseHeadlessConfig
);
44 event_source_
= ui::PlatformEventSource::CreateDefault();
45 if (command_line
->HasSwitch(mojo::kUseTestConfig
))
46 gfx::GLSurface::InitializeOneOffForTests();
48 gfx::GLSurface::InitializeOneOff();
52 if (!gpu_state_
.get())
53 gpu_state_
= new gles2::GpuState
;
54 connection_manager_
.reset(
55 new ConnectionManager(this, is_headless_
, app_impl_
, gpu_state_
));
58 bool ViewManagerApp::ConfigureIncomingConnection(
59 ApplicationConnection
* connection
) {
61 // |connection| originates from the WindowManager. Let it connect directly
62 // to the ViewManager.
63 connection
->AddService
<ViewManagerService
>(this);
64 connection
->AddService
<ViewManagerRoot
>(this);
65 connection
->AddService
<Gpu
>(this);
70 void ViewManagerApp::OnLostConnectionToWindowManager() {
71 app_impl_
->Terminate();
74 ClientConnection
* ViewManagerApp::CreateClientConnectionForEmbedAtView(
75 ConnectionManager
* connection_manager
,
76 mojo::InterfaceRequest
<mojo::ViewManagerService
> service_request
,
77 mojo::ConnectionSpecificId creator_id
,
78 mojo::URLRequestPtr request
,
79 const ViewId
& root_id
) {
80 mojo::ViewManagerClientPtr client
;
81 app_impl_
->ConnectToService(request
.Pass(), &client
);
83 scoped_ptr
<ViewManagerServiceImpl
> service(
84 new ViewManagerServiceImpl(connection_manager
, creator_id
, root_id
));
85 return new DefaultClientConnection(service
.Pass(), connection_manager
,
86 service_request
.Pass(), client
.Pass());
89 ClientConnection
* ViewManagerApp::CreateClientConnectionForEmbedAtView(
90 ConnectionManager
* connection_manager
,
91 mojo::InterfaceRequest
<mojo::ViewManagerService
> service_request
,
92 mojo::ConnectionSpecificId creator_id
,
93 const ViewId
& root_id
,
94 mojo::ViewManagerClientPtr view_manager_client
) {
95 scoped_ptr
<ViewManagerServiceImpl
> service(
96 new ViewManagerServiceImpl(connection_manager
, creator_id
, root_id
));
97 return new DefaultClientConnection(service
.Pass(), connection_manager
,
98 service_request
.Pass(),
99 view_manager_client
.Pass());
102 void ViewManagerApp::Create(ApplicationConnection
* connection
,
103 InterfaceRequest
<ViewManagerService
> request
) {
104 if (connection_manager_
->has_window_manager_client_connection()) {
105 VLOG(1) << "ViewManager interface requested more than once.";
109 scoped_ptr
<ViewManagerServiceImpl
> service(new ViewManagerServiceImpl(
110 connection_manager_
.get(), kInvalidConnectionId
, RootViewId(0)));
111 mojo::ViewManagerClientPtr client
;
112 connection
->ConnectToService(&client
);
113 scoped_ptr
<ClientConnection
> client_connection(
114 new DefaultClientConnection(service
.Pass(), connection_manager_
.get(),
115 request
.Pass(), client
.Pass()));
116 connection_manager_
->SetWindowManagerClientConnection(
117 client_connection
.Pass());
120 void ViewManagerApp::Create(ApplicationConnection
* connection
,
121 InterfaceRequest
<ViewManagerRoot
> request
) {
122 if (view_manager_root_binding_
.get()) {
123 VLOG(1) << "ViewManagerRoot requested more than once.";
127 // ConfigureIncomingConnection() must have been called before getting here.
128 DCHECK(connection_manager_
.get());
129 view_manager_root_binding_
.reset(new mojo::Binding
<ViewManagerRoot
>(
130 connection_manager_
->view_manager_root(), request
.Pass()));
131 view_manager_root_binding_
->set_error_handler(this);
134 void ViewManagerApp::Create(
135 mojo::ApplicationConnection
* connection
,
136 mojo::InterfaceRequest
<Gpu
> request
) {
137 if (!gpu_state_
.get())
138 gpu_state_
= new gles2::GpuState
;
139 new gles2::GpuImpl(request
.Pass(), gpu_state_
);
142 void ViewManagerApp::OnConnectionError() {
143 app_impl_
->Terminate();
146 } // namespace view_manager