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 scoped_ptr
<DefaultDisplayManager
> display_manager(new DefaultDisplayManager(
58 base::Bind(&ViewManagerApp::OnLostConnectionToWindowManager
,
59 base::Unretained(this))));
60 connection_manager_
.reset(
61 new ConnectionManager(this, display_manager
.Pass()));
64 bool ViewManagerApp::ConfigureIncomingConnection(
65 ApplicationConnection
* connection
) {
67 // |connection| originates from the WindowManager. Let it connect directly
68 // to the ViewManager.
69 connection
->AddService
<ViewManagerService
>(this);
70 connection
->AddService
<ViewManagerRoot
>(this);
71 connection
->AddService
<Gpu
>(this);
76 void ViewManagerApp::OnLostConnectionToWindowManager() {
77 app_impl_
->Terminate();
80 ClientConnection
* ViewManagerApp::CreateClientConnectionForEmbedAtView(
81 ConnectionManager
* connection_manager
,
82 mojo::InterfaceRequest
<mojo::ViewManagerService
> service_request
,
83 mojo::ConnectionSpecificId creator_id
,
84 mojo::URLRequestPtr request
,
85 const ViewId
& root_id
) {
86 mojo::ViewManagerClientPtr client
;
87 app_impl_
->ConnectToService(request
.Pass(), &client
);
89 scoped_ptr
<ViewManagerServiceImpl
> service(
90 new ViewManagerServiceImpl(connection_manager
, creator_id
, root_id
));
91 return new DefaultClientConnection(service
.Pass(), connection_manager
,
92 service_request
.Pass(), client
.Pass());
95 ClientConnection
* ViewManagerApp::CreateClientConnectionForEmbedAtView(
96 ConnectionManager
* connection_manager
,
97 mojo::InterfaceRequest
<mojo::ViewManagerService
> service_request
,
98 mojo::ConnectionSpecificId creator_id
,
99 const ViewId
& root_id
,
100 mojo::ViewManagerClientPtr view_manager_client
) {
101 scoped_ptr
<ViewManagerServiceImpl
> service(
102 new ViewManagerServiceImpl(connection_manager
, creator_id
, root_id
));
103 return new DefaultClientConnection(service
.Pass(), connection_manager
,
104 service_request
.Pass(),
105 view_manager_client
.Pass());
108 void ViewManagerApp::Create(ApplicationConnection
* connection
,
109 InterfaceRequest
<ViewManagerService
> request
) {
110 if (connection_manager_
->has_window_manager_client_connection()) {
111 VLOG(1) << "ViewManager interface requested more than once.";
115 scoped_ptr
<ViewManagerServiceImpl
> service(new ViewManagerServiceImpl(
116 connection_manager_
.get(), kInvalidConnectionId
, RootViewId(0)));
117 mojo::ViewManagerClientPtr client
;
118 connection
->ConnectToService(&client
);
119 scoped_ptr
<ClientConnection
> client_connection(
120 new DefaultClientConnection(service
.Pass(), connection_manager_
.get(),
121 request
.Pass(), client
.Pass()));
122 connection_manager_
->SetWindowManagerClientConnection(
123 client_connection
.Pass());
126 void ViewManagerApp::Create(ApplicationConnection
* connection
,
127 InterfaceRequest
<ViewManagerRoot
> request
) {
128 if (view_manager_root_binding_
.get()) {
129 VLOG(1) << "ViewManagerRoot requested more than once.";
133 // ConfigureIncomingConnection() must have been called before getting here.
134 DCHECK(connection_manager_
.get());
135 view_manager_root_binding_
.reset(new mojo::Binding
<ViewManagerRoot
>(
136 connection_manager_
.get(), request
.Pass()));
137 view_manager_root_binding_
->set_error_handler(this);
140 void ViewManagerApp::Create(
141 mojo::ApplicationConnection
* connection
,
142 mojo::InterfaceRequest
<Gpu
> request
) {
143 if (!gpu_state_
.get())
144 gpu_state_
= new gles2::GpuState
;
145 new gles2::GpuImpl(request
.Pass(), gpu_state_
);
148 void ViewManagerApp::OnConnectionError() {
149 app_impl_
->Terminate();
152 } // namespace view_manager