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 GURL AddTrailingSlashIfNeeded(const GURL& url) { |
110 mojo::shell::MojoURLResolver* resolver) { | 106 if (!url.has_path() || *url.path().rbegin() == '/') |
111 base::StringPairs pairs; | 107 return url; |
112 if (!base::SplitStringIntoKeyValuePairs(mappings, '=', ',', &pairs)) | 108 |
109 std::string path(url.path() + '/'); | |
110 GURL::Replacements replacements; | |
111 replacements.SetPathStr(path); | |
112 return url.ReplaceComponents(replacements); | |
113 } | |
114 | |
115 bool ConfigureURLMappings(base::CommandLine *command_line, | |
116 MojoURLResolver *resolver) { | |
abarth-chromium
2015/01/23 02:23:15
s/base::CommandLine */base::CommandLine* /
Same fo
abarth-chromium
2015/01/23 02:23:15
s/base::CommandLine */base::CommandLine* /
Same fo
Nick Bray (chromium)
2015/01/24 01:09:04
Done.
Nick Bray (chromium)
2015/01/24 01:09:04
Done.
| |
117 // By default assume that the local apps reside alongside the shell. | |
118 base::FilePath shell_dir; | |
119 PathService::Get(base::DIR_MODULE, &shell_dir); | |
120 GURL shell_dir_url = AddTrailingSlashIfNeeded(FilePathToFileURL(shell_dir)); | |
Aaron Boodman
2015/01/23 02:34:06
Is there really ambiguity about whether FilePathTo
Nick Bray (chromium)
2015/01/24 01:09:04
I believe it never returns a trailing slash. Ther
| |
121 | |
122 // Configure the resolution of unknown mojo: URLs. | |
123 GURL base_url; | |
124 if (command_line->HasSwitch(switches::kOrigin)) { | |
125 base_url = GURL(command_line->GetSwitchValueASCII(switches::kOrigin)); | |
126 base_url = AddTrailingSlashIfNeeded(base_url); | |
Aaron Boodman
2015/01/23 02:34:06
Why add the trailing slash in this case? Seems lik
Nick Bray (chromium)
2015/01/24 01:09:04
Ther difference is subtle:
GURL("http://foo/bar").
Aaron Boodman
2015/01/27 00:37:13
In that case, I think that we should ensure the ca
Nick Bray (chromium)
2015/01/27 01:44:35
Requiring no slash will interact badly with tab co
Aaron Boodman
2015/01/27 07:29:54
OK, I was initially going to suggest requiring a s
| |
127 } else { | |
128 base_url = shell_dir_url; | |
129 } | |
130 if (!base_url.is_valid()) { | |
Aaron Boodman
2015/01/23 02:34:06
no braces.
Nick Bray (chromium)
2015/01/24 01:09:03
Sad panda about this style - goto fail, and all th
| |
113 return false; | 131 return false; |
114 using StringPair = std::pair<std::string, std::string>; | 132 } |
115 for (const StringPair& pair : pairs) { | 133 resolver->SetBaseURL(base_url); |
116 const GURL from(pair.first); | 134 |
117 const GURL to(pair.second); | 135 // The network service must be loaded from the filesystem. |
118 if (!from.is_valid() || !to.is_valid()) | 136 // This mapping is done before the command line URL mapping are processed, so |
137 // that it can be overridden. | |
138 resolver->AddCustomMapping(GURL("mojo:network_service"), | |
139 shell_dir_url.Resolve("network_service.mojo")); | |
140 | |
141 // Command line URL mapping. | |
142 if (command_line->HasSwitch(switches::kURLMappings)) { | |
143 const std::string mappings = | |
144 command_line->GetSwitchValueASCII(switches::kURLMappings); | |
145 | |
146 base::StringPairs pairs; | |
147 if (!base::SplitStringIntoKeyValuePairs(mappings, '=', ',', &pairs)) | |
119 return false; | 148 return false; |
120 resolver->AddCustomMapping(from, to); | 149 using StringPair = std::pair<std::string, std::string>; |
150 for (const StringPair& pair : pairs) { | |
151 const GURL from(pair.first); | |
152 const GURL to(pair.second); | |
153 if (!from.is_valid() || !to.is_valid()) | |
154 return false; | |
155 resolver->AddCustomMapping(from, to); | |
156 } | |
121 } | 157 } |
122 return true; | 158 return true; |
123 } | 159 } |
124 | 160 |
125 } // namespace | 161 } // namespace |
126 | 162 |
127 Context::Context() : application_manager_(this) { | 163 Context::Context() : application_manager_(this) { |
128 DCHECK(!base::MessageLoop::current()); | 164 DCHECK(!base::MessageLoop::current()); |
129 } | 165 } |
130 | 166 |
131 Context::~Context() { | 167 Context::~Context() { |
132 DCHECK(!base::MessageLoop::current()); | 168 DCHECK(!base::MessageLoop::current()); |
133 } | 169 } |
134 | 170 |
135 void Context::EnsureEmbedderIsInitialized() { | 171 void Context::EnsureEmbedderIsInitialized() { |
136 setup.Get(); | 172 setup.Get(); |
137 } | 173 } |
138 | 174 |
139 bool Context::Init() { | 175 bool Context::Init() { |
140 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); | 176 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); |
141 | 177 |
142 if (command_line->HasSwitch(switches::kWaitForDebugger)) | 178 if (command_line->HasSwitch(switches::kWaitForDebugger)) |
143 base::debug::WaitForDebugger(60, true); | 179 base::debug::WaitForDebugger(60, true); |
144 | 180 |
145 EnsureEmbedderIsInitialized(); | 181 EnsureEmbedderIsInitialized(); |
146 task_runners_.reset( | 182 task_runners_.reset( |
147 new TaskRunners(base::MessageLoop::current()->message_loop_proxy())); | 183 new TaskRunners(base::MessageLoop::current()->message_loop_proxy())); |
148 | 184 |
149 for (size_t i = 0; i < arraysize(kLocalMojoURLs); ++i) | 185 if (!ConfigureURLMappings(command_line, &mojo_url_resolver_)) { |
Aaron Boodman
2015/01/23 02:34:06
This file uses the no-braces-for-one-line-if style
Nick Bray (chromium)
2015/01/24 01:09:04
Done.
| |
150 mojo_url_resolver_.AddLocalFileMapping(GURL(kLocalMojoURLs[i])); | 186 return false; |
187 } | |
151 | 188 |
152 if (command_line->HasSwitch(switches::kEnableExternalApplications)) { | 189 if (command_line->HasSwitch(switches::kEnableExternalApplications)) { |
153 listener_.reset(new ExternalApplicationListener( | 190 listener_.reset(new ExternalApplicationListener( |
154 task_runners_->shell_runner(), task_runners_->io_runner())); | 191 task_runners_->shell_runner(), task_runners_->io_runner())); |
155 | 192 |
156 base::FilePath socket_path = | 193 base::FilePath socket_path = |
157 command_line->GetSwitchValuePath(switches::kEnableExternalApplications); | 194 command_line->GetSwitchValuePath(switches::kEnableExternalApplications); |
158 if (socket_path.empty()) | 195 if (socket_path.empty()) |
159 socket_path = ExternalApplicationListener::ConstructDefaultSocketPath(); | 196 socket_path = ExternalApplicationListener::ConstructDefaultSocketPath(); |
160 | 197 |
161 listener_->ListenInBackground( | 198 listener_->ListenInBackground( |
162 socket_path, | 199 socket_path, |
163 base::Bind(&ApplicationManager::RegisterExternalApplication, | 200 base::Bind(&ApplicationManager::RegisterExternalApplication, |
164 base::Unretained(&application_manager_))); | 201 base::Unretained(&application_manager_))); |
165 } | 202 } |
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 | 203 |
177 scoped_ptr<DynamicServiceRunnerFactory> runner_factory; | 204 scoped_ptr<DynamicServiceRunnerFactory> runner_factory; |
178 if (command_line->HasSwitch(switches::kEnableMultiprocess)) | 205 if (command_line->HasSwitch(switches::kEnableMultiprocess)) |
179 runner_factory.reset(new OutOfProcessDynamicServiceRunnerFactory()); | 206 runner_factory.reset(new OutOfProcessDynamicServiceRunnerFactory()); |
180 else | 207 else |
181 runner_factory.reset(new InProcessDynamicServiceRunnerFactory()); | 208 runner_factory.reset(new InProcessDynamicServiceRunnerFactory()); |
182 | 209 |
183 DynamicApplicationLoader* dynamic_application_loader = | 210 DynamicApplicationLoader* dynamic_application_loader = |
184 new DynamicApplicationLoader(this, runner_factory.Pass()); | 211 new DynamicApplicationLoader(this, runner_factory.Pass()); |
185 InitContentHandlers(dynamic_application_loader, command_line); | 212 InitContentHandlers(dynamic_application_loader, command_line); |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
225 ScopedMessagePipeHandle Context::ConnectToServiceByName( | 252 ScopedMessagePipeHandle Context::ConnectToServiceByName( |
226 const GURL& application_url, | 253 const GURL& application_url, |
227 const std::string& service_name) { | 254 const std::string& service_name) { |
228 app_urls_.insert(application_url); | 255 app_urls_.insert(application_url); |
229 return application_manager_.ConnectToServiceByName(application_url, | 256 return application_manager_.ConnectToServiceByName(application_url, |
230 service_name).Pass(); | 257 service_name).Pass(); |
231 } | 258 } |
232 | 259 |
233 } // namespace shell | 260 } // namespace shell |
234 } // namespace mojo | 261 } // namespace mojo |
OLD | NEW |