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 "shell/context.h" | 5 #include "shell/context.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/base_switches.h" | 9 #include "base/base_switches.h" |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/command_line.h" | 11 #include "base/command_line.h" |
12 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
13 #include "base/lazy_instance.h" | 13 #include "base/lazy_instance.h" |
14 #include "base/macros.h" | 14 #include "base/macros.h" |
15 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
16 #include "base/memory/scoped_vector.h" | 16 #include "base/memory/scoped_vector.h" |
| 17 #include "base/path_service.h" |
17 #include "base/strings/string_split.h" | 18 #include "base/strings/string_split.h" |
18 #include "base/strings/string_util.h" | 19 #include "base/strings/string_util.h" |
19 #include "build/build_config.h" | 20 #include "build/build_config.h" |
20 #include "mojo/common/tracing_impl.h" | 21 #include "mojo/common/tracing_impl.h" |
21 #include "mojo/edk/embedder/embedder.h" | 22 #include "mojo/edk/embedder/embedder.h" |
22 #include "mojo/edk/embedder/simple_platform_support.h" | 23 #include "mojo/edk/embedder/simple_platform_support.h" |
23 #include "mojo/public/cpp/application/application_connection.h" | 24 #include "mojo/public/cpp/application/application_connection.h" |
24 #include "mojo/public/cpp/application/application_delegate.h" | 25 #include "mojo/public/cpp/application/application_delegate.h" |
25 #include "mojo/public/cpp/application/application_impl.h" | 26 #include "mojo/public/cpp/application/application_impl.h" |
26 #include "services/tracing/tracing.mojom.h" | 27 #include "services/tracing/tracing.mojom.h" |
27 #include "shell/application_manager/application_loader.h" | 28 #include "shell/application_manager/application_loader.h" |
28 #include "shell/application_manager/application_manager.h" | 29 #include "shell/application_manager/application_manager.h" |
29 #include "shell/dynamic_application_loader.h" | 30 #include "shell/dynamic_application_loader.h" |
30 #include "shell/external_application_listener.h" | 31 #include "shell/external_application_listener.h" |
| 32 #include "shell/filename_util.h" |
31 #include "shell/in_process_dynamic_service_runner.h" | 33 #include "shell/in_process_dynamic_service_runner.h" |
32 #include "shell/out_of_process_dynamic_service_runner.h" | 34 #include "shell/out_of_process_dynamic_service_runner.h" |
33 #include "shell/switches.h" | 35 #include "shell/switches.h" |
34 #include "url/gurl.h" | 36 #include "url/gurl.h" |
35 | 37 |
36 namespace mojo { | 38 namespace mojo { |
37 namespace shell { | 39 namespace shell { |
38 namespace { | 40 namespace { |
39 | 41 |
40 // These mojo: URLs are loaded directly from the local filesystem. They | |
41 // correspond to shared libraries bundled alongside the mojo_shell. | |
42 const char* kLocalMojoURLs[] = { | |
43 "mojo:network_service", | |
44 }; | |
45 | |
46 // Used to ensure we only init once. | 42 // Used to ensure we only init once. |
47 class Setup { | 43 class Setup { |
48 public: | 44 public: |
49 Setup() { | 45 Setup() { |
50 embedder::Init(scoped_ptr<mojo::embedder::PlatformSupport>( | 46 embedder::Init(scoped_ptr<mojo::embedder::PlatformSupport>( |
51 new mojo::embedder::SimplePlatformSupport())); | 47 new mojo::embedder::SimplePlatformSupport())); |
52 } | 48 } |
53 | 49 |
54 ~Setup() {} | 50 ~Setup() {} |
55 | 51 |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
99 LOG(ERROR) << "Invalid value for switch " << switches::kContentHandlers | 95 LOG(ERROR) << "Invalid value for switch " << switches::kContentHandlers |
100 << ": '" << parts[i + 1] << "' is not a valid URL."; | 96 << ": '" << parts[i + 1] << "' is not a valid URL."; |
101 return; | 97 return; |
102 } | 98 } |
103 // TODO(eseidel): We should also validate that the mimetype is valid | 99 // TODO(eseidel): We should also validate that the mimetype is valid |
104 // net/base/mime_util.h could do this, but we don't want to depend on net. | 100 // net/base/mime_util.h could do this, but we don't want to depend on net. |
105 loader->RegisterContentHandler(parts[i], url); | 101 loader->RegisterContentHandler(parts[i], url); |
106 } | 102 } |
107 } | 103 } |
108 | 104 |
109 bool ConfigureURLMappings(const std::string& mappings, | 105 bool ConfigureURLMappings(base::CommandLine* command_line, |
110 mojo::shell::MojoURLResolver* resolver) { | 106 Context* context) { |
111 base::StringPairs pairs; | 107 MojoURLResolver* resolver = context->mojo_url_resolver(); |
112 if (!base::SplitStringIntoKeyValuePairs(mappings, '=', ',', &pairs)) | 108 |
| 109 // Configure the resolution of unknown mojo: URLs. |
| 110 GURL base_url; |
| 111 if (command_line->HasSwitch(switches::kOrigin)) |
| 112 base_url = GURL(command_line->GetSwitchValueASCII(switches::kOrigin)); |
| 113 else |
| 114 // Use the shell's file root if the base was not specified. |
| 115 base_url = context->ResolveShellFileURL(""); |
| 116 |
| 117 if (!base_url.is_valid()) |
113 return false; | 118 return false; |
114 using StringPair = std::pair<std::string, std::string>; | 119 |
115 for (const StringPair& pair : pairs) { | 120 resolver->SetBaseURL(base_url); |
116 const GURL from(pair.first); | 121 |
117 const GURL to(pair.second); | 122 // The network service must be loaded from the filesystem. |
118 if (!from.is_valid() || !to.is_valid()) | 123 // This mapping is done before the command line URL mapping are processed, so |
| 124 // that it can be overridden. |
| 125 resolver->AddCustomMapping( |
| 126 GURL("mojo:network_service"), |
| 127 context->ResolveShellFileURL("file:network_service.mojo")); |
| 128 |
| 129 // Command line URL mapping. |
| 130 if (command_line->HasSwitch(switches::kURLMappings)) { |
| 131 const std::string mappings = |
| 132 command_line->GetSwitchValueASCII(switches::kURLMappings); |
| 133 |
| 134 base::StringPairs pairs; |
| 135 if (!base::SplitStringIntoKeyValuePairs(mappings, '=', ',', &pairs)) |
119 return false; | 136 return false; |
120 resolver->AddCustomMapping(from, to); | 137 using StringPair = std::pair<std::string, std::string>; |
| 138 for (const StringPair& pair : pairs) { |
| 139 const GURL from(pair.first); |
| 140 const GURL to(pair.second); |
| 141 if (!from.is_valid() || !to.is_valid()) |
| 142 return false; |
| 143 resolver->AddCustomMapping(from, to); |
| 144 } |
121 } | 145 } |
122 return true; | 146 return true; |
123 } | 147 } |
124 | 148 |
125 } // namespace | 149 } // namespace |
126 | 150 |
127 Context::Context() : application_manager_(this) { | 151 Context::Context() : application_manager_(this) { |
128 DCHECK(!base::MessageLoop::current()); | 152 DCHECK(!base::MessageLoop::current()); |
| 153 |
| 154 // By default assume that the local apps reside alongside the shell. |
| 155 // TODO(ncbray): really, this should be passed in rather than defaulting. |
| 156 // This default makes sense for desktop but not Android. |
| 157 base::FilePath shell_dir; |
| 158 PathService::Get(base::DIR_MODULE, &shell_dir); |
| 159 SetShellFileRoot(shell_dir); |
129 } | 160 } |
130 | 161 |
131 Context::~Context() { | 162 Context::~Context() { |
132 DCHECK(!base::MessageLoop::current()); | 163 DCHECK(!base::MessageLoop::current()); |
133 } | 164 } |
134 | 165 |
135 void Context::EnsureEmbedderIsInitialized() { | 166 void Context::EnsureEmbedderIsInitialized() { |
136 setup.Get(); | 167 setup.Get(); |
137 } | 168 } |
138 | 169 |
| 170 void Context::SetShellFileRoot(const base::FilePath& path) { |
| 171 shell_file_root_ = AddTrailingSlashIfNeeded(FilePathToFileURL(path)); |
| 172 } |
| 173 |
| 174 GURL Context::ResolveShellFileURL(const std::string& path) { |
| 175 return shell_file_root_.Resolve(path); |
| 176 } |
| 177 |
139 bool Context::Init() { | 178 bool Context::Init() { |
140 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); | 179 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
141 | 180 |
142 if (command_line->HasSwitch(switches::kWaitForDebugger)) | 181 if (command_line->HasSwitch(switches::kWaitForDebugger)) |
143 base::debug::WaitForDebugger(60, true); | 182 base::debug::WaitForDebugger(60, true); |
144 | 183 |
145 EnsureEmbedderIsInitialized(); | 184 EnsureEmbedderIsInitialized(); |
146 task_runners_.reset( | 185 task_runners_.reset( |
147 new TaskRunners(base::MessageLoop::current()->message_loop_proxy())); | 186 new TaskRunners(base::MessageLoop::current()->message_loop_proxy())); |
148 | 187 |
149 for (size_t i = 0; i < arraysize(kLocalMojoURLs); ++i) | 188 if (!shell_file_root_.is_valid()) |
150 mojo_url_resolver_.AddLocalFileMapping(GURL(kLocalMojoURLs[i])); | 189 return false; |
| 190 if (!ConfigureURLMappings(command_line, this)) |
| 191 return false; |
151 | 192 |
152 if (command_line->HasSwitch(switches::kEnableExternalApplications)) { | 193 if (command_line->HasSwitch(switches::kEnableExternalApplications)) { |
153 listener_.reset(new ExternalApplicationListener( | 194 listener_.reset(new ExternalApplicationListener( |
154 task_runners_->shell_runner(), task_runners_->io_runner())); | 195 task_runners_->shell_runner(), task_runners_->io_runner())); |
155 | 196 |
156 base::FilePath socket_path = | 197 base::FilePath socket_path = |
157 command_line->GetSwitchValuePath(switches::kEnableExternalApplications); | 198 command_line->GetSwitchValuePath(switches::kEnableExternalApplications); |
158 if (socket_path.empty()) | 199 if (socket_path.empty()) |
159 socket_path = ExternalApplicationListener::ConstructDefaultSocketPath(); | 200 socket_path = ExternalApplicationListener::ConstructDefaultSocketPath(); |
160 | 201 |
161 listener_->ListenInBackground( | 202 listener_->ListenInBackground( |
162 socket_path, | 203 socket_path, |
163 base::Bind(&ApplicationManager::RegisterExternalApplication, | 204 base::Bind(&ApplicationManager::RegisterExternalApplication, |
164 base::Unretained(&application_manager_))); | 205 base::Unretained(&application_manager_))); |
165 } | 206 } |
166 if (command_line->HasSwitch(switches::kOrigin)) { | |
167 mojo_url_resolver()->SetBaseURL( | |
168 GURL(command_line->GetSwitchValueASCII(switches::kOrigin))); | |
169 } | |
170 if (command_line->HasSwitch(switches::kURLMappings) && | |
171 !ConfigureURLMappings( | |
172 command_line->GetSwitchValueASCII(switches::kURLMappings), | |
173 mojo_url_resolver())) { | |
174 return false; | |
175 } | |
176 | 207 |
177 scoped_ptr<DynamicServiceRunnerFactory> runner_factory; | 208 scoped_ptr<DynamicServiceRunnerFactory> runner_factory; |
178 if (command_line->HasSwitch(switches::kEnableMultiprocess)) | 209 if (command_line->HasSwitch(switches::kEnableMultiprocess)) |
179 runner_factory.reset(new OutOfProcessDynamicServiceRunnerFactory()); | 210 runner_factory.reset(new OutOfProcessDynamicServiceRunnerFactory()); |
180 else | 211 else |
181 runner_factory.reset(new InProcessDynamicServiceRunnerFactory()); | 212 runner_factory.reset(new InProcessDynamicServiceRunnerFactory()); |
182 | 213 |
183 DynamicApplicationLoader* dynamic_application_loader = | 214 DynamicApplicationLoader* dynamic_application_loader = |
184 new DynamicApplicationLoader(this, runner_factory.Pass()); | 215 new DynamicApplicationLoader(this, runner_factory.Pass()); |
185 InitContentHandlers(dynamic_application_loader, command_line); | 216 InitContentHandlers(dynamic_application_loader, command_line); |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
225 ScopedMessagePipeHandle Context::ConnectToServiceByName( | 256 ScopedMessagePipeHandle Context::ConnectToServiceByName( |
226 const GURL& application_url, | 257 const GURL& application_url, |
227 const std::string& service_name) { | 258 const std::string& service_name) { |
228 app_urls_.insert(application_url); | 259 app_urls_.insert(application_url); |
229 return application_manager_.ConnectToServiceByName(application_url, | 260 return application_manager_.ConnectToServiceByName(application_url, |
230 service_name).Pass(); | 261 service_name).Pass(); |
231 } | 262 } |
232 | 263 |
233 } // namespace shell | 264 } // namespace shell |
234 } // namespace mojo | 265 } // namespace mojo |
OLD | NEW |