OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "services/service_manager/standalone/context.h" | 5 #include "services/service_manager/standalone/context.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <utility> | 10 #include <utility> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "base/bind.h" | 13 #include "base/bind.h" |
14 #include "base/command_line.h" | 14 #include "base/command_line.h" |
15 #include "base/files/file_path.h" | 15 #include "base/files/file_path.h" |
16 #include "base/json/json_file_value_serializer.h" | 16 #include "base/json/json_file_value_serializer.h" |
| 17 #include "base/lazy_instance.h" |
| 18 #include "base/location.h" |
17 #include "base/macros.h" | 19 #include "base/macros.h" |
18 #include "base/memory/ptr_util.h" | 20 #include "base/memory/ptr_util.h" |
19 #include "base/path_service.h" | 21 #include "base/path_service.h" |
20 #include "base/process/process_info.h" | 22 #include "base/process/process_info.h" |
| 23 #include "base/run_loop.h" |
| 24 #include "base/single_thread_task_runner.h" |
21 #include "base/strings/string_number_conversions.h" | 25 #include "base/strings/string_number_conversions.h" |
22 #include "base/strings/string_split.h" | 26 #include "base/strings/string_split.h" |
23 #include "base/strings/string_util.h" | 27 #include "base/strings/string_util.h" |
24 #include "base/strings/utf_string_conversions.h" | 28 #include "base/strings/utf_string_conversions.h" |
25 #include "base/threading/sequenced_worker_pool.h" | 29 #include "base/threading/sequenced_worker_pool.h" |
| 30 #include "base/threading/thread_task_runner_handle.h" |
26 #include "base/trace_event/trace_event.h" | 31 #include "base/trace_event/trace_event.h" |
27 #include "build/build_config.h" | 32 #include "build/build_config.h" |
28 #include "components/tracing/common/tracing_switches.h" | 33 #include "components/tracing/common/tracing_switches.h" |
| 34 #include "mojo/edk/embedder/embedder.h" |
| 35 #include "mojo/public/cpp/bindings/strong_binding.h" |
29 #include "services/catalog/catalog.h" | 36 #include "services/catalog/catalog.h" |
| 37 #include "services/catalog/store.h" |
30 #include "services/service_manager/connect_params.h" | 38 #include "services/service_manager/connect_params.h" |
31 #include "services/service_manager/connect_util.h" | 39 #include "services/service_manager/connect_util.h" |
32 #include "services/service_manager/runner/common/switches.h" | 40 #include "services/service_manager/runner/common/switches.h" |
33 #include "services/service_manager/runner/host/service_process_launcher.h" | 41 #include "services/service_manager/runner/host/service_process_launcher.h" |
34 #include "services/service_manager/service_manager.h" | |
35 #include "services/service_manager/standalone/tracer.h" | 42 #include "services/service_manager/standalone/tracer.h" |
36 #include "services/service_manager/switches.h" | 43 #include "services/service_manager/switches.h" |
37 #include "services/tracing/public/cpp/provider.h" | 44 #include "services/tracing/public/cpp/provider.h" |
38 #include "services/tracing/public/cpp/switches.h" | 45 #include "services/tracing/public/cpp/switches.h" |
39 #include "services/tracing/public/interfaces/constants.mojom.h" | 46 #include "services/tracing/public/interfaces/constants.mojom.h" |
40 #include "services/tracing/public/interfaces/tracing.mojom.h" | 47 #include "services/tracing/public/interfaces/tracing.mojom.h" |
41 | 48 |
42 #if defined(OS_MACOSX) | 49 #if defined(OS_MACOSX) |
43 #include "services/service_manager/public/cpp/standalone_service/mach_broker.h" | 50 #include "services/service_manager/public/cpp/standalone_service/mach_broker.h" |
44 #endif | 51 #endif |
45 | 52 |
46 namespace base { | |
47 class TaskRunner; | |
48 } | |
49 | |
50 namespace service_manager { | 53 namespace service_manager { |
51 namespace { | 54 namespace { |
52 | 55 |
| 56 base::FilePath::StringType GetPathFromCommandLineSwitch( |
| 57 const base::StringPiece& value) { |
| 58 #if defined(OS_POSIX) |
| 59 return value.as_string(); |
| 60 #elif defined(OS_WIN) |
| 61 return base::UTF8ToUTF16(value); |
| 62 #endif // OS_POSIX |
| 63 } |
| 64 |
53 // Used to ensure we only init once. | 65 // Used to ensure we only init once. |
| 66 class Setup { |
| 67 public: |
| 68 Setup() { mojo::edk::Init(); } |
| 69 |
| 70 ~Setup() {} |
| 71 |
| 72 private: |
| 73 DISALLOW_COPY_AND_ASSIGN(Setup); |
| 74 }; |
| 75 |
54 class ServiceProcessLauncherFactoryImpl : public ServiceProcessLauncherFactory { | 76 class ServiceProcessLauncherFactoryImpl : public ServiceProcessLauncherFactory { |
55 public: | 77 public: |
56 ServiceProcessLauncherFactoryImpl(base::TaskRunner* launch_process_runner, | 78 ServiceProcessLauncherFactoryImpl(base::TaskRunner* launch_process_runner, |
57 ServiceProcessLauncher::Delegate* delegate) | 79 ServiceProcessLauncher::Delegate* delegate) |
58 : launch_process_runner_(launch_process_runner), | 80 : launch_process_runner_(launch_process_runner), |
59 delegate_(delegate) { | 81 delegate_(delegate) { |
60 } | 82 } |
61 | 83 |
62 private: | 84 private: |
63 std::unique_ptr<ServiceProcessLauncher> Create( | 85 std::unique_ptr<ServiceProcessLauncher> Create( |
64 const base::FilePath& service_path) override { | 86 const base::FilePath& service_path) override { |
65 return base::MakeUnique<ServiceProcessLauncher>( | 87 return base::MakeUnique<ServiceProcessLauncher>( |
66 launch_process_runner_, delegate_, service_path); | 88 launch_process_runner_, delegate_, service_path); |
67 } | 89 } |
68 | 90 |
69 base::TaskRunner* launch_process_runner_; | 91 base::TaskRunner* launch_process_runner_; |
70 ServiceProcessLauncher::Delegate* delegate_; | 92 ServiceProcessLauncher::Delegate* delegate_; |
71 }; | 93 }; |
72 | 94 |
| 95 std::unique_ptr<base::Thread> CreateIOThread(const char* name) { |
| 96 std::unique_ptr<base::Thread> thread(new base::Thread(name)); |
| 97 base::Thread::Options options; |
| 98 options.message_loop_type = base::MessageLoop::TYPE_IO; |
| 99 thread->StartWithOptions(options); |
| 100 return thread; |
| 101 } |
| 102 |
73 void OnInstanceQuit(const std::string& name, const Identity& identity) { | 103 void OnInstanceQuit(const std::string& name, const Identity& identity) { |
74 if (name == identity.name()) | 104 if (name == identity.name()) |
75 base::MessageLoop::current()->QuitWhenIdle(); | 105 base::MessageLoop::current()->QuitWhenIdle(); |
76 } | 106 } |
77 | 107 |
78 const char kService[] = "service"; | 108 const char kService[] = "service"; |
79 | 109 |
80 } // namespace | 110 } // namespace |
81 | 111 |
82 Context::Context( | 112 Context::InitParams::InitParams() {} |
83 ServiceProcessLauncher::Delegate* service_process_launcher_delegate, | 113 Context::InitParams::~InitParams() {} |
84 std::unique_ptr<base::Value> catalog_contents) | 114 |
85 : main_entry_time_(base::Time::Now()) { | 115 Context::Context() |
86 TRACE_EVENT0("service_manager", "Context::Context"); | 116 : io_thread_(CreateIOThread("io_thread")), |
| 117 main_entry_time_(base::Time::Now()) {} |
| 118 |
| 119 Context::~Context() { |
| 120 DCHECK(!base::MessageLoop::current()); |
| 121 blocking_pool_->Shutdown(); |
| 122 } |
| 123 |
| 124 // static |
| 125 void Context::EnsureEmbedderIsInitialized() { |
| 126 static base::LazyInstance<Setup>::Leaky setup = LAZY_INSTANCE_INITIALIZER; |
| 127 setup.Get(); |
| 128 } |
| 129 |
| 130 void Context::Init(std::unique_ptr<InitParams> init_params) { |
| 131 TRACE_EVENT0("service_manager", "Context::Init"); |
87 const base::CommandLine& command_line = | 132 const base::CommandLine& command_line = |
88 *base::CommandLine::ForCurrentProcess(); | 133 *base::CommandLine::ForCurrentProcess(); |
89 | 134 |
90 bool trace_startup = command_line.HasSwitch(::switches::kTraceStartup); | 135 bool trace_startup = command_line.HasSwitch(::switches::kTraceStartup); |
91 if (trace_startup) { | 136 if (trace_startup) { |
92 tracer_.Start( | 137 tracer_.Start( |
93 command_line.GetSwitchValueASCII(::switches::kTraceStartup), | 138 command_line.GetSwitchValueASCII(::switches::kTraceStartup), |
94 command_line.GetSwitchValueASCII(::switches::kTraceStartupDuration), | 139 command_line.GetSwitchValueASCII(::switches::kTraceStartupDuration), |
95 "mojo_runner.trace"); | 140 "mojo_runner.trace"); |
96 } | 141 } |
97 | 142 |
| 143 if (!init_params || init_params->init_edk) |
| 144 EnsureEmbedderIsInitialized(); |
| 145 |
| 146 service_manager_runner_ = base::ThreadTaskRunnerHandle::Get(); |
98 blocking_pool_ = new base::SequencedWorkerPool( | 147 blocking_pool_ = new base::SequencedWorkerPool( |
99 kThreadPoolMaxThreads, "blocking_pool", base::TaskPriority::USER_VISIBLE); | 148 kThreadPoolMaxThreads, "blocking_pool", base::TaskPriority::USER_VISIBLE); |
100 | 149 |
| 150 init_edk_ = !init_params || init_params->init_edk; |
| 151 if (init_edk_) { |
| 152 mojo::edk::InitIPCSupport(io_thread_->task_runner().get()); |
| 153 #if defined(OS_MACOSX) |
| 154 mojo::edk::SetMachPortProvider(MachBroker::GetInstance()->port_provider()); |
| 155 #endif |
| 156 } |
| 157 |
101 std::unique_ptr<ServiceProcessLauncherFactory> | 158 std::unique_ptr<ServiceProcessLauncherFactory> |
102 service_process_launcher_factory = | 159 service_process_launcher_factory = |
103 base::MakeUnique<ServiceProcessLauncherFactoryImpl>( | 160 base::MakeUnique<ServiceProcessLauncherFactoryImpl>( |
104 blocking_pool_.get(), | 161 blocking_pool_.get(), |
105 service_process_launcher_delegate); | 162 init_params ? init_params->service_process_launcher_delegate |
106 catalog_.reset(new catalog::Catalog(std::move(catalog_contents))); | 163 : nullptr); |
| 164 if (init_params && init_params->static_catalog) { |
| 165 catalog_.reset( |
| 166 new catalog::Catalog(std::move(init_params->static_catalog))); |
| 167 } else { |
| 168 catalog_.reset( |
| 169 new catalog::Catalog(blocking_pool_.get(), nullptr)); |
| 170 } |
107 service_manager_.reset( | 171 service_manager_.reset( |
108 new ServiceManager(std::move(service_process_launcher_factory), | 172 new ServiceManager(std::move(service_process_launcher_factory), |
109 catalog_->TakeService())); | 173 catalog_->TakeService())); |
110 | 174 |
| 175 if (command_line.HasSwitch(::switches::kServiceOverrides)) { |
| 176 base::FilePath overrides_file(GetPathFromCommandLineSwitch( |
| 177 command_line.GetSwitchValueASCII(::switches::kServiceOverrides))); |
| 178 JSONFileValueDeserializer deserializer(overrides_file); |
| 179 int error = 0; |
| 180 std::string message; |
| 181 std::unique_ptr<base::Value> contents = |
| 182 deserializer.Deserialize(&error, &message); |
| 183 if (!contents) { |
| 184 LOG(ERROR) << "Failed to parse service overrides file: " << message; |
| 185 } else { |
| 186 std::unique_ptr<ServiceOverrides> service_overrides = |
| 187 base::MakeUnique<ServiceOverrides>(std::move(contents)); |
| 188 for (const auto& iter : service_overrides->entries()) { |
| 189 if (!iter.second.package_name.empty()) |
| 190 catalog_->OverridePackageName(iter.first, iter.second.package_name); |
| 191 } |
| 192 service_manager_->SetServiceOverrides(std::move(service_overrides)); |
| 193 } |
| 194 } |
| 195 |
111 bool enable_stats_collection_bindings = | 196 bool enable_stats_collection_bindings = |
112 command_line.HasSwitch(tracing::kEnableStatsCollectionBindings); | 197 command_line.HasSwitch(tracing::kEnableStatsCollectionBindings); |
113 | 198 |
114 if (enable_stats_collection_bindings || | 199 if (enable_stats_collection_bindings || |
115 command_line.HasSwitch(switches::kEnableTracing)) { | 200 command_line.HasSwitch(switches::kEnableTracing)) { |
116 Identity source_identity = CreateServiceManagerIdentity(); | 201 Identity source_identity = CreateServiceManagerIdentity(); |
117 Identity tracing_identity(tracing::mojom::kServiceName, mojom::kRootUserID); | 202 Identity tracing_identity(tracing::mojom::kServiceName, mojom::kRootUserID); |
118 tracing::mojom::FactoryPtr factory; | 203 tracing::mojom::FactoryPtr factory; |
119 BindInterface(service_manager(), source_identity, tracing_identity, | 204 BindInterface(service_manager(), source_identity, tracing_identity, |
120 &factory); | 205 &factory); |
(...skipping 16 matching lines...) Expand all Loading... |
137 const base::Time creation_time = base::CurrentProcessInfo::CreationTime(); | 222 const base::Time creation_time = base::CurrentProcessInfo::CreationTime(); |
138 collector->SetServiceManagerProcessCreationTime( | 223 collector->SetServiceManagerProcessCreationTime( |
139 creation_time.ToInternalValue()); | 224 creation_time.ToInternalValue()); |
140 #endif | 225 #endif |
141 collector->SetServiceManagerMainEntryPointTime( | 226 collector->SetServiceManagerMainEntryPointTime( |
142 main_entry_time_.ToInternalValue()); | 227 main_entry_time_.ToInternalValue()); |
143 } | 228 } |
144 } | 229 } |
145 } | 230 } |
146 | 231 |
147 Context::~Context() { blocking_pool_->Shutdown(); } | 232 void Context::Shutdown() { |
| 233 // Actions triggered by Service Manager's destructor may require a current |
| 234 // message loop, |
| 235 // so we should destruct it explicitly now as ~Context() occurs post message |
| 236 // loop shutdown. |
| 237 service_manager_.reset(); |
| 238 |
| 239 DCHECK_EQ(base::ThreadTaskRunnerHandle::Get(), service_manager_runner_); |
| 240 |
| 241 // If we didn't initialize the edk we should not shut it down. |
| 242 if (!init_edk_) |
| 243 return; |
| 244 |
| 245 TRACE_EVENT0("service_manager", "Context::Shutdown"); |
| 246 mojo::edk::ShutdownIPCSupport( |
| 247 base::Bind(IgnoreResult(&base::TaskRunner::PostTask), |
| 248 base::ThreadTaskRunnerHandle::Get(), FROM_HERE, |
| 249 base::Bind(&Context::OnShutdownComplete, |
| 250 base::Unretained(this)))); |
| 251 // We'll quit when we get OnShutdownComplete(). |
| 252 base::RunLoop().Run(); |
| 253 } |
| 254 |
| 255 void Context::OnShutdownComplete() { |
| 256 DCHECK_EQ(base::ThreadTaskRunnerHandle::Get(), service_manager_runner_); |
| 257 base::MessageLoop::current()->QuitWhenIdle(); |
| 258 } |
148 | 259 |
149 void Context::RunCommandLineApplication() { | 260 void Context::RunCommandLineApplication() { |
150 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); | 261 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
151 if (command_line->HasSwitch(kService)) | 262 if (command_line->HasSwitch(kService)) |
152 Run(command_line->GetSwitchValueASCII(kService)); | 263 Run(command_line->GetSwitchValueASCII(kService)); |
153 } | 264 } |
154 | 265 |
155 void Context::Run(const std::string& name) { | 266 void Context::Run(const std::string& name) { |
156 service_manager_->SetInstanceQuitCallback(base::Bind(&OnInstanceQuit, name)); | 267 service_manager_->SetInstanceQuitCallback(base::Bind(&OnInstanceQuit, name)); |
157 | 268 |
158 mojom::InterfaceProviderPtr remote_interfaces; | 269 mojom::InterfaceProviderPtr remote_interfaces; |
159 mojom::InterfaceProviderPtr local_interfaces; | 270 mojom::InterfaceProviderPtr local_interfaces; |
160 | 271 |
161 std::unique_ptr<ConnectParams> params(new ConnectParams); | 272 std::unique_ptr<ConnectParams> params(new ConnectParams); |
162 params->set_source(CreateServiceManagerIdentity()); | 273 params->set_source(CreateServiceManagerIdentity()); |
163 params->set_target(Identity(name, mojom::kRootUserID)); | 274 params->set_target(Identity(name, mojom::kRootUserID)); |
164 params->set_remote_interfaces(mojo::MakeRequest(&remote_interfaces)); | 275 params->set_remote_interfaces(mojo::MakeRequest(&remote_interfaces)); |
165 service_manager_->Connect(std::move(params)); | 276 service_manager_->Connect(std::move(params)); |
166 } | 277 } |
167 | 278 |
168 } // namespace service_manager | 279 } // namespace service_manager |
OLD | NEW |