Mandoline: Merge Surfaces and Views apps
[chromium-blink-merge.git] / components / view_manager / view_manager_app.cc
blob248142e9ea34c55a5f9d7850e4e0167a872dd2e0
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 "base/stl_util.h"
9 #include "components/view_manager/client_connection.h"
10 #include "components/view_manager/connection_manager.h"
11 #include "components/view_manager/public/cpp/args.h"
12 #include "components/view_manager/surfaces/surfaces_impl.h"
13 #include "components/view_manager/surfaces/surfaces_scheduler.h"
14 #include "components/view_manager/view_manager_root_connection.h"
15 #include "components/view_manager/view_manager_root_impl.h"
16 #include "components/view_manager/view_manager_service_impl.h"
17 #include "mojo/application/public/cpp/application_connection.h"
18 #include "mojo/application/public/cpp/application_impl.h"
19 #include "mojo/application/public/cpp/application_runner.h"
20 #include "mojo/common/tracing_impl.h"
21 #include "third_party/mojo/src/mojo/public/c/system/main.h"
22 #include "ui/events/event_switches.h"
23 #include "ui/events/platform/platform_event_source.h"
24 #include "ui/gl/gl_surface.h"
25 #include "ui/gl/test/gl_surface_test_support.h"
27 using mojo::ApplicationConnection;
28 using mojo::ApplicationImpl;
29 using mojo::Gpu;
30 using mojo::InterfaceRequest;
31 using mojo::ViewManagerRoot;
32 using mojo::ViewManagerService;
34 namespace view_manager {
36 ViewManagerApp::ViewManagerApp()
37 : app_impl_(nullptr),
38 is_headless_(false) {
41 ViewManagerApp::~ViewManagerApp() {
42 if (gpu_state_)
43 gpu_state_->StopControlThread();
45 auto surfaces = surfaces_;
46 for (auto& surface : surfaces)
47 surface->CloseConnection();
50 void ViewManagerApp::Initialize(ApplicationImpl* app) {
51 app_impl_ = app;
52 tracing_.Initialize(app);
53 surfaces_state_ = new surfaces::SurfacesState;
55 #if !defined(OS_ANDROID)
56 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
57 is_headless_ = command_line->HasSwitch(mojo::kUseHeadlessConfig);
58 if (!is_headless_) {
59 event_source_ = ui::PlatformEventSource::CreateDefault();
60 if (command_line->HasSwitch(mojo::kUseTestConfig))
61 gfx::GLSurfaceTestSupport::InitializeOneOff();
62 else
63 gfx::GLSurface::InitializeOneOff();
65 #endif
67 if (!gpu_state_.get())
68 gpu_state_ = new gles2::GpuState;
69 connection_manager_.reset(new ConnectionManager(this));
72 bool ViewManagerApp::ConfigureIncomingConnection(
73 ApplicationConnection* connection) {
74 // ViewManager
75 connection->AddService<ViewManagerRoot>(this);
76 // Surfaces
77 // TODO(fsamuel): This should go away soon.
78 connection->AddService<mojo::Surface>(this);
79 // GPU
80 connection->AddService<Gpu>(this);
81 return true;
84 void ViewManagerApp::OnNoMoreRootConnections() {
85 app_impl_->Quit();
88 ClientConnection* ViewManagerApp::CreateClientConnectionForEmbedAtView(
89 ConnectionManager* connection_manager,
90 mojo::InterfaceRequest<mojo::ViewManagerService> service_request,
91 mojo::ConnectionSpecificId creator_id,
92 mojo::URLRequestPtr request,
93 const ViewId& root_id) {
94 mojo::ViewManagerClientPtr client;
95 app_impl_->ConnectToService(request.Pass(), &client);
97 scoped_ptr<ViewManagerServiceImpl> service(
98 new ViewManagerServiceImpl(connection_manager, creator_id, root_id));
99 return new DefaultClientConnection(service.Pass(), connection_manager,
100 service_request.Pass(), client.Pass());
103 ClientConnection* ViewManagerApp::CreateClientConnectionForEmbedAtView(
104 ConnectionManager* connection_manager,
105 mojo::InterfaceRequest<mojo::ViewManagerService> service_request,
106 mojo::ConnectionSpecificId creator_id,
107 const ViewId& root_id,
108 mojo::ViewManagerClientPtr view_manager_client) {
109 scoped_ptr<ViewManagerServiceImpl> service(
110 new ViewManagerServiceImpl(connection_manager, creator_id, root_id));
111 return new DefaultClientConnection(service.Pass(), connection_manager,
112 service_request.Pass(),
113 view_manager_client.Pass());
116 void ViewManagerApp::Create(ApplicationConnection* connection,
117 InterfaceRequest<ViewManagerRoot> request) {
118 DCHECK(connection_manager_.get());
119 // TODO(fsamuel): We need to make sure that only the window manager can create
120 // new roots.
121 ViewManagerRootImpl* view_manager_root = new ViewManagerRootImpl(
122 connection_manager_.get(), is_headless_, app_impl_, gpu_state_,
123 surfaces_state_);
125 mojo::ViewManagerClientPtr client;
126 connection->ConnectToService(&client);
128 // ViewManagerRootConnection manages its own lifetime.
129 view_manager_root->Init(new ViewManagerRootConnectionImpl(
130 request.Pass(), make_scoped_ptr(view_manager_root), client.Pass(),
131 connection_manager_.get()));
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::Create(
143 mojo::ApplicationConnection* connection,
144 mojo::InterfaceRequest<mojo::Surface> request) {
145 surfaces_.insert(
146 new surfaces::SurfacesImpl(this, surfaces_state_, request.Pass()));
149 void ViewManagerApp::OnSurfaceConnectionClosed(
150 surfaces::SurfacesImpl* surface) {
151 surfaces_.erase(surface);
154 } // namespace view_manager