1 // Copyright 2019 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.
4 
5 #include "content/app/service_manager_environment.h"
6 
7 #include <utility>
8 
9 #include "base/command_line.h"
10 #include "build/build_config.h"
11 #include "content/browser/browser_process_sub_thread.h"
12 #include "content/browser/service_manager/service_manager_context.h"
13 #include "content/browser/startup_data_impl.h"
14 #include "content/common/mojo_core_library_support.h"
15 #include "content/public/common/content_features.h"
16 #include "content/public/common/service_manager_connection.h"
17 #include "mojo/core/embedder/embedder.h"
18 #include "mojo/core/embedder/scoped_ipc_support.h"
19 
20 namespace content {
21 
ServiceManagerEnvironment(std::unique_ptr<BrowserProcessSubThread> io_thread)22 ServiceManagerEnvironment::ServiceManagerEnvironment(
23     std::unique_ptr<BrowserProcessSubThread> io_thread)
24     : io_thread_(std::move(io_thread)) {
25   scoped_refptr<base::SingleThreadTaskRunner> mojo_ipc_task_runner =
26       io_thread_->task_runner();
27   if (!IsMojoCoreSharedLibraryEnabled()) {
28     // NOTE: If Mojo Core was loaded via shared library, IPC support is already
29     // initialized.
30     if (base::FeatureList::IsEnabled(features::kMojoDedicatedThread)) {
31       mojo_ipc_thread_.StartWithOptions(
32           base::Thread::Options(base::MessagePumpType::IO, 0));
33       mojo_ipc_task_runner = mojo_ipc_thread_.task_runner();
34     }
35     mojo_ipc_support_ = std::make_unique<mojo::core::ScopedIPCSupport>(
36         mojo_ipc_task_runner,
37         mojo::core::ScopedIPCSupport::ShutdownPolicy::FAST);
38   }
39   service_manager_context_ =
40       std::make_unique<ServiceManagerContext>(io_thread_->task_runner());
41   ServiceManagerConnection::GetForProcess()->Start();
42 }
43 
44 ServiceManagerEnvironment::~ServiceManagerEnvironment() = default;
45 
46 std::unique_ptr<StartupDataImpl>
CreateBrowserStartupData()47 ServiceManagerEnvironment::CreateBrowserStartupData() {
48   auto startup_data = std::make_unique<StartupDataImpl>();
49   startup_data->io_thread = std::move(io_thread_);
50   startup_data->mojo_ipc_support = std::move(mojo_ipc_support_);
51   startup_data->service_manager_shutdown_closure =
52       base::BindOnce(&ServiceManagerContext::ShutDown,
53                      base::Unretained(service_manager_context_.get()));
54   return startup_data;
55 }
56 
57 }  // namespace content
58