OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "mojo/shell/standalone/context.h" | |
6 | |
7 #include <stddef.h> | |
8 #include <stdint.h> | |
9 | |
10 #include <utility> | |
11 #include <vector> | |
12 | |
13 #include "base/bind.h" | |
14 #include "base/command_line.h" | |
15 #include "base/files/file_path.h" | |
16 #include "base/lazy_instance.h" | |
17 #include "base/macros.h" | |
18 #include "base/memory/scoped_ptr.h" | |
19 #include "base/path_service.h" | |
20 #include "base/process/process_info.h" | |
21 #include "base/run_loop.h" | |
22 #include "base/strings/string_number_conversions.h" | |
23 #include "base/strings/string_split.h" | |
24 #include "base/strings/string_util.h" | |
25 #include "base/strings/utf_string_conversions.h" | |
26 #include "base/threading/sequenced_worker_pool.h" | |
27 #include "base/trace_event/trace_event.h" | |
28 #include "build/build_config.h" | |
29 #include "components/tracing/tracing_switches.h" | |
30 #include "mojo/edk/embedder/embedder.h" | |
31 #include "mojo/public/cpp/bindings/strong_binding.h" | |
32 #include "mojo/services/catalog/factory.h" | |
33 #include "mojo/services/catalog/store.h" | |
34 #include "mojo/services/tracing/public/cpp/switches.h" | |
35 #include "mojo/services/tracing/public/cpp/trace_provider_impl.h" | |
36 #include "mojo/services/tracing/public/cpp/tracing_impl.h" | |
37 #include "mojo/services/tracing/public/interfaces/tracing.mojom.h" | |
38 #include "mojo/shell/connect_params.h" | |
39 #include "mojo/shell/public/cpp/names.h" | |
40 #include "mojo/shell/runner/host/in_process_native_runner.h" | |
41 #include "mojo/shell/runner/host/out_of_process_native_runner.h" | |
42 #include "mojo/shell/standalone/tracer.h" | |
43 #include "mojo/shell/switches.h" | |
44 #include "mojo/util/filename_util.h" | |
45 | |
46 #if defined(OS_MACOSX) | |
47 #include "mojo/shell/runner/host/mach_broker.h" | |
48 #endif | |
49 | |
50 namespace mojo { | |
51 namespace shell { | |
52 namespace { | |
53 | |
54 // Used to ensure we only init once. | |
55 class Setup { | |
56 public: | |
57 Setup() { edk::Init(); } | |
58 | |
59 ~Setup() {} | |
60 | |
61 private: | |
62 DISALLOW_COPY_AND_ASSIGN(Setup); | |
63 }; | |
64 | |
65 class TracingInterfaceProvider : public shell::mojom::InterfaceProvider { | |
66 public: | |
67 TracingInterfaceProvider(Tracer* tracer, | |
68 shell::mojom::InterfaceProviderRequest request) | |
69 : tracer_(tracer), binding_(this, std::move(request)) {} | |
70 ~TracingInterfaceProvider() override {} | |
71 | |
72 // shell::mojom::InterfaceProvider: | |
73 void GetInterface(const mojo::String& interface_name, | |
74 ScopedMessagePipeHandle client_handle) override { | |
75 if (tracer_ && interface_name == tracing::TraceProvider::Name_) { | |
76 tracer_->ConnectToProvider( | |
77 MakeRequest<tracing::TraceProvider>(std::move(client_handle))); | |
78 } | |
79 } | |
80 | |
81 private: | |
82 Tracer* tracer_; | |
83 StrongBinding<shell::mojom::InterfaceProvider> binding_; | |
84 | |
85 DISALLOW_COPY_AND_ASSIGN(TracingInterfaceProvider); | |
86 }; | |
87 | |
88 const size_t kMaxBlockingPoolThreads = 3; | |
89 | |
90 scoped_ptr<base::Thread> CreateIOThread(const char* name) { | |
91 scoped_ptr<base::Thread> thread(new base::Thread(name)); | |
92 base::Thread::Options options; | |
93 options.message_loop_type = base::MessageLoop::TYPE_IO; | |
94 thread->StartWithOptions(options); | |
95 return thread; | |
96 } | |
97 | |
98 void OnInstanceQuit(const std::string& name, const Identity& identity) { | |
99 if (name == identity.name()) | |
100 base::MessageLoop::current()->QuitWhenIdle(); | |
101 } | |
102 | |
103 } // namespace | |
104 | |
105 Context::InitParams::InitParams() {} | |
106 Context::InitParams::~InitParams() {} | |
107 | |
108 Context::Context() | |
109 : io_thread_(CreateIOThread("io_thread")), | |
110 main_entry_time_(base::Time::Now()) {} | |
111 | |
112 Context::~Context() { | |
113 DCHECK(!base::MessageLoop::current()); | |
114 blocking_pool_->Shutdown(); | |
115 } | |
116 | |
117 // static | |
118 void Context::EnsureEmbedderIsInitialized() { | |
119 static base::LazyInstance<Setup>::Leaky setup = LAZY_INSTANCE_INITIALIZER; | |
120 setup.Get(); | |
121 } | |
122 | |
123 void Context::Init(scoped_ptr<InitParams> init_params) { | |
124 TRACE_EVENT0("mojo_shell", "Context::Init"); | |
125 const base::CommandLine& command_line = | |
126 *base::CommandLine::ForCurrentProcess(); | |
127 | |
128 bool trace_startup = command_line.HasSwitch(::switches::kTraceStartup); | |
129 if (trace_startup) { | |
130 tracer_.Start( | |
131 command_line.GetSwitchValueASCII(::switches::kTraceStartup), | |
132 command_line.GetSwitchValueASCII(::switches::kTraceStartupDuration), | |
133 "mojo_runner.trace"); | |
134 } | |
135 | |
136 if (!init_params || init_params->init_edk) | |
137 EnsureEmbedderIsInitialized(); | |
138 | |
139 shell_runner_ = base::MessageLoop::current()->task_runner(); | |
140 blocking_pool_ = | |
141 new base::SequencedWorkerPool(kMaxBlockingPoolThreads, "blocking_pool"); | |
142 | |
143 init_edk_ = !init_params || init_params->init_edk; | |
144 if (init_edk_) { | |
145 edk::InitIPCSupport(this, io_thread_->task_runner().get()); | |
146 #if defined(OS_MACOSX) | |
147 edk::SetMachPortProvider(MachBroker::GetInstance()->port_provider()); | |
148 #endif | |
149 } | |
150 | |
151 scoped_ptr<NativeRunnerFactory> runner_factory; | |
152 if (command_line.HasSwitch(switches::kSingleProcess)) { | |
153 #if defined(COMPONENT_BUILD) | |
154 LOG(ERROR) << "Running Mojo in single process component build, which isn't " | |
155 << "supported because statics in apps interact. Use static build" | |
156 << " or don't pass --single-process."; | |
157 #endif | |
158 runner_factory.reset( | |
159 new InProcessNativeRunnerFactory(blocking_pool_.get())); | |
160 } else { | |
161 NativeRunnerDelegate* native_runner_delegate = init_params ? | |
162 init_params->native_runner_delegate : nullptr; | |
163 runner_factory.reset(new OutOfProcessNativeRunnerFactory( | |
164 blocking_pool_.get(), native_runner_delegate)); | |
165 } | |
166 scoped_ptr<catalog::Store> store; | |
167 if (init_params) | |
168 store = std::move(init_params->catalog_store); | |
169 catalog_.reset( | |
170 new catalog::Factory(blocking_pool_.get(), std::move(store), nullptr)); | |
171 shell_.reset(new Shell(std::move(runner_factory), | |
172 catalog_->TakeShellClient())); | |
173 | |
174 shell::mojom::InterfaceProviderPtr tracing_remote_interfaces; | |
175 shell::mojom::InterfaceProviderPtr tracing_local_interfaces; | |
176 new TracingInterfaceProvider(&tracer_, GetProxy(&tracing_local_interfaces)); | |
177 | |
178 scoped_ptr<ConnectParams> params(new ConnectParams); | |
179 params->set_source(CreateShellIdentity()); | |
180 params->set_target(Identity("mojo:tracing", mojom::kRootUserID)); | |
181 params->set_remote_interfaces(GetProxy(&tracing_remote_interfaces)); | |
182 params->set_local_interfaces(std::move(tracing_local_interfaces)); | |
183 shell_->Connect(std::move(params)); | |
184 | |
185 if (command_line.HasSwitch(tracing::kTraceStartup)) { | |
186 tracing::TraceCollectorPtr coordinator; | |
187 auto coordinator_request = GetProxy(&coordinator); | |
188 tracing_remote_interfaces->GetInterface( | |
189 tracing::TraceCollector::Name_, coordinator_request.PassMessagePipe()); | |
190 tracer_.StartCollectingFromTracingService(std::move(coordinator)); | |
191 } | |
192 | |
193 // Record the shell startup metrics used for performance testing. | |
194 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | |
195 tracing::kEnableStatsCollectionBindings)) { | |
196 tracing::StartupPerformanceDataCollectorPtr collector; | |
197 tracing_remote_interfaces->GetInterface( | |
198 tracing::StartupPerformanceDataCollector::Name_, | |
199 GetProxy(&collector).PassMessagePipe()); | |
200 #if defined(OS_MACOSX) || defined(OS_WIN) || defined(OS_LINUX) | |
201 // CurrentProcessInfo::CreationTime is only defined on some platforms. | |
202 const base::Time creation_time = base::CurrentProcessInfo::CreationTime(); | |
203 collector->SetShellProcessCreationTime(creation_time.ToInternalValue()); | |
204 #endif | |
205 collector->SetShellMainEntryPointTime(main_entry_time_.ToInternalValue()); | |
206 } | |
207 } | |
208 | |
209 void Context::Shutdown() { | |
210 // Actions triggered by Shell's destructor may require a current message loop, | |
211 // so we should destruct it explicitly now as ~Context() occurs post message | |
212 // loop shutdown. | |
213 shell_.reset(); | |
214 | |
215 DCHECK_EQ(base::MessageLoop::current()->task_runner(), shell_runner_); | |
216 | |
217 // If we didn't initialize the edk we should not shut it down. | |
218 if (!init_edk_) | |
219 return; | |
220 | |
221 TRACE_EVENT0("mojo_shell", "Context::Shutdown"); | |
222 // Post a task in case OnShutdownComplete is called synchronously. | |
223 base::MessageLoop::current()->PostTask(FROM_HERE, | |
224 base::Bind(edk::ShutdownIPCSupport)); | |
225 // We'll quit when we get OnShutdownComplete(). | |
226 base::MessageLoop::current()->Run(); | |
227 } | |
228 | |
229 void Context::OnShutdownComplete() { | |
230 DCHECK_EQ(base::MessageLoop::current()->task_runner(), shell_runner_); | |
231 base::MessageLoop::current()->QuitWhenIdle(); | |
232 } | |
233 | |
234 void Context::RunCommandLineApplication() { | |
235 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); | |
236 base::CommandLine::StringVector args = command_line->GetArgs(); | |
237 for (size_t i = 0; i < args.size(); ++i) { | |
238 #if defined(OS_WIN) | |
239 std::string possible_app = base::WideToUTF8(args[i]); | |
240 #else | |
241 std::string possible_app = args[i]; | |
242 #endif | |
243 if (GetNameType(possible_app) == "mojo") { | |
244 Run(possible_app); | |
245 break; | |
246 } | |
247 } | |
248 } | |
249 | |
250 void Context::Run(const std::string& name) { | |
251 shell_->SetInstanceQuitCallback(base::Bind(&OnInstanceQuit, name)); | |
252 | |
253 shell::mojom::InterfaceProviderPtr remote_interfaces; | |
254 shell::mojom::InterfaceProviderPtr local_interfaces; | |
255 | |
256 scoped_ptr<ConnectParams> params(new ConnectParams); | |
257 params->set_source(CreateShellIdentity()); | |
258 params->set_target(Identity(name, mojom::kRootUserID)); | |
259 params->set_remote_interfaces(GetProxy(&remote_interfaces)); | |
260 params->set_local_interfaces(std::move(local_interfaces)); | |
261 shell_->Connect(std::move(params)); | |
262 } | |
263 | |
264 } // namespace shell | |
265 } // namespace mojo | |
OLD | NEW |