Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(77)

Side by Side Diff: services/shell/standalone/context.cc

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

Powered by Google App Engine
This is Rietveld 408576698