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

Side by Side Diff: shell/dynamic_application_loader.cc

Issue 884303002: Launch nexes from mojo_shell. Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Formatting error Created 5 years, 10 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 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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/dynamic_application_loader.h" 5 #include "shell/dynamic_application_loader.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/command_line.h" 8 #include "base/command_line.h"
9 #include "base/files/file.h" 9 #include "base/files/file.h"
10 #include "base/files/file_path.h" 10 #include "base/files/file_path.h"
11 #include "base/files/file_util.h" 11 #include "base/files/file_util.h"
12 #include "base/format_macros.h" 12 #include "base/format_macros.h"
13 #include "base/logging.h" 13 #include "base/logging.h"
14 #include "base/md5.h" 14 #include "base/md5.h"
15 #include "base/memory/scoped_ptr.h" 15 #include "base/memory/scoped_ptr.h"
16 #include "base/memory/weak_ptr.h" 16 #include "base/memory/weak_ptr.h"
17 #include "base/message_loop/message_loop.h" 17 #include "base/message_loop/message_loop.h"
18 #include "base/process/process.h" 18 #include "base/process/process.h"
19 #include "base/strings/string_util.h" 19 #include "base/strings/string_util.h"
20 #include "base/strings/stringprintf.h" 20 #include "base/strings/stringprintf.h"
21 #include "base/strings/utf_string_conversions.h" 21 #include "base/strings/utf_string_conversions.h"
22 #include "mojo/common/common_type_converters.h" 22 #include "mojo/common/common_type_converters.h"
23 #include "mojo/common/data_pipe_utils.h" 23 #include "mojo/common/data_pipe_utils.h"
24 #include "mojo/public/cpp/system/data_pipe.h" 24 #include "mojo/public/cpp/system/data_pipe.h"
25 #include "mojo/services/network/public/interfaces/url_loader.mojom.h" 25 #include "mojo/services/network/public/interfaces/url_loader.mojom.h"
26 #include "shell/context.h" 26 #include "shell/context.h"
27 #include "shell/data_pipe_peek.h" 27 #include "shell/data_pipe_peek.h"
28 #include "shell/filename_util.h" 28 #include "shell/filename_util.h"
29 #if MOJO_USE_NACL
30 #include "shell/nacl/nacl_service_runner.h"
31 #endif
29 #include "shell/switches.h" 32 #include "shell/switches.h"
30 #include "url/url_util.h" 33 #include "url/url_util.h"
31 34
32 namespace mojo { 35 namespace mojo {
33 namespace shell { 36 namespace shell {
34 37
35 namespace { 38 namespace {
36 39
37 static const char kMojoMagic[] = "#!mojo "; 40 static const char kMojoMagic[] = "#!mojo ";
38 static const size_t kMaxShebangLength = 2048; 41 static const size_t kMaxShebangLength = 2048;
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
72 virtual ~Loader() {} 75 virtual ~Loader() {}
73 76
74 protected: 77 protected:
75 virtual URLResponsePtr AsURLResponse(base::TaskRunner* task_runner, 78 virtual URLResponsePtr AsURLResponse(base::TaskRunner* task_runner,
76 uint32_t skip) = 0; 79 uint32_t skip) = 0;
77 80
78 virtual void AsPath( 81 virtual void AsPath(
79 base::TaskRunner* task_runner, 82 base::TaskRunner* task_runner,
80 base::Callback<void(const base::FilePath&, bool)> callback) = 0; 83 base::Callback<void(const base::FilePath&, bool)> callback) = 0;
81 84
85 virtual GURL URL() = 0;
86
87 std::string Extension() {
88 std::string filename = URL().ExtractFileName();
89 size_t dot = filename.rfind('.');
90 if (dot != std::string::npos) {
91 return filename.substr(dot);
92 }
93 return "";
94 }
95
82 virtual std::string MimeType() = 0; 96 virtual std::string MimeType() = 0;
83 97
84 virtual bool HasMojoMagic() = 0; 98 virtual bool HasMojoMagic() = 0;
85 99
86 virtual bool PeekFirstLine(std::string* line) = 0; 100 virtual bool PeekFirstLine(std::string* line) = 0;
87 101
88 void Load() { 102 void Load() {
89 // If the response begins with a #!mojo <content-handler-url>, use it. 103 // If the response begins with a #!mojo <content-handler-url>, use it.
90 GURL url; 104 GURL url;
91 std::string shebang; 105 std::string shebang;
(...skipping 11 matching lines...) Expand all
103 iter->second, application_request_.Pass(), 117 iter->second, application_request_.Pass(),
104 AsURLResponse(context_->task_runners()->blocking_pool(), 0)); 118 AsURLResponse(context_->task_runners()->blocking_pool(), 0));
105 return; 119 return;
106 } 120 }
107 121
108 // TODO(aa): Sanity check that the thing we got looks vaguely like a mojo 122 // TODO(aa): Sanity check that the thing we got looks vaguely like a mojo
109 // application. That could either mean looking for the platform-specific dll 123 // application. That could either mean looking for the platform-specific dll
110 // header, or looking for some specific mojo signature prepended to the 124 // header, or looking for some specific mojo signature prepended to the
111 // library. 125 // library.
112 126
127 // TODO(ncbray) sniff or infer the content type at a previous stage in the
128 // pipeline.
129 if (Extension() == ".nexe") {
130 AsPath(context_->task_runners()->blocking_pool(),
131 base::Bind(&Loader::RunNexe, weak_ptr_factory_.GetWeakPtr()));
132 return;
133 }
134
113 AsPath(context_->task_runners()->blocking_pool(), 135 AsPath(context_->task_runners()->blocking_pool(),
114 base::Bind(&Loader::RunLibrary, weak_ptr_factory_.GetWeakPtr())); 136 base::Bind(&Loader::RunLibrary, weak_ptr_factory_.GetWeakPtr()));
115 } 137 }
116 138
117 void ReportComplete() { loader_complete_callback_.Run(this); } 139 void ReportComplete() { loader_complete_callback_.Run(this); }
118 140
119 private: 141 private:
120 bool PeekContentHandler(std::string* mojo_shebang, 142 bool PeekContentHandler(std::string* mojo_shebang,
121 GURL* mojo_content_handler_url) { 143 GURL* mojo_content_handler_url) {
122 std::string shebang; 144 std::string shebang;
(...skipping 17 matching lines...) Expand all
140 ReportComplete(); 162 ReportComplete();
141 return; 163 return;
142 } 164 }
143 165
144 runner_ = runner_factory_->Create(context_); 166 runner_ = runner_factory_->Create(context_);
145 runner_->Start( 167 runner_->Start(
146 path, cleanup_behavior_, application_request_.Pass(), 168 path, cleanup_behavior_, application_request_.Pass(),
147 base::Bind(&Loader::ReportComplete, weak_ptr_factory_.GetWeakPtr())); 169 base::Bind(&Loader::ReportComplete, weak_ptr_factory_.GetWeakPtr()));
148 } 170 }
149 171
172 void RunNexe(const base::FilePath& path, bool path_exists) {
173 DCHECK(application_request_.is_pending());
174
175 if (!path_exists) {
176 LOG(ERROR) << "Library not started because library path '" << path.value()
177 << "' does not exist.";
178 ReportComplete();
179 return;
180 }
181
182 #if MOJO_USE_NACL
183 nacl_runner_.reset(new NaClServiceRunner(context_));
184 nacl_runner_->Start(path, application_request_.Pass());
185 #else
186 LOG(ERROR) << "Library not started because Native Client is not enabled.";
187 ReportComplete();
188 #endif
189 }
190
150 DynamicServiceRunner::CleanupBehavior cleanup_behavior_; 191 DynamicServiceRunner::CleanupBehavior cleanup_behavior_;
151 InterfaceRequest<Application> application_request_; 192 InterfaceRequest<Application> application_request_;
152 ApplicationLoader::LoadCallback load_callback_; 193 ApplicationLoader::LoadCallback load_callback_;
153 LoaderCompleteCallback loader_complete_callback_; 194 LoaderCompleteCallback loader_complete_callback_;
154 Context* context_; 195 Context* context_;
155 MimeTypeToURLMap* mime_type_to_url_; 196 MimeTypeToURLMap* mime_type_to_url_;
156 DynamicServiceRunnerFactory* runner_factory_; 197 DynamicServiceRunnerFactory* runner_factory_;
157 scoped_ptr<DynamicServiceRunner> runner_; 198 scoped_ptr<DynamicServiceRunner> runner_;
199 #if MOJO_USE_NACL
200 scoped_ptr<NaClServiceRunner> nacl_runner_;
201 #endif
158 base::WeakPtrFactory<Loader> weak_ptr_factory_; 202 base::WeakPtrFactory<Loader> weak_ptr_factory_;
159 }; 203 };
160 204
161 // A loader for local files. 205 // A loader for local files.
162 class DynamicApplicationLoader::LocalLoader : public Loader { 206 class DynamicApplicationLoader::LocalLoader : public Loader {
163 public: 207 public:
164 LocalLoader(const GURL& url, 208 LocalLoader(const GURL& url,
165 MimeTypeToURLMap* mime_type_to_url, 209 MimeTypeToURLMap* mime_type_to_url,
166 Context* context, 210 Context* context,
167 DynamicServiceRunnerFactory* runner_factory, 211 DynamicServiceRunnerFactory* runner_factory,
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
215 } 259 }
216 260
217 void AsPath( 261 void AsPath(
218 base::TaskRunner* task_runner, 262 base::TaskRunner* task_runner,
219 base::Callback<void(const base::FilePath&, bool)> callback) override { 263 base::Callback<void(const base::FilePath&, bool)> callback) override {
220 // Async for consistency with network case. 264 // Async for consistency with network case.
221 base::MessageLoop::current()->PostTask( 265 base::MessageLoop::current()->PostTask(
222 FROM_HERE, base::Bind(callback, path_, base::PathExists(path_))); 266 FROM_HERE, base::Bind(callback, path_, base::PathExists(path_)));
223 } 267 }
224 268
269 GURL URL() override { return url_; }
270
225 std::string MimeType() override { return ""; } 271 std::string MimeType() override { return ""; }
226 272
227 bool HasMojoMagic() override { 273 bool HasMojoMagic() override {
228 std::string magic; 274 std::string magic;
229 ReadFileToString(path_, &magic, strlen(kMojoMagic)); 275 ReadFileToString(path_, &magic, strlen(kMojoMagic));
230 return magic == kMojoMagic; 276 return magic == kMojoMagic;
231 } 277 }
232 278
233 bool PeekFirstLine(std::string* line) override { 279 bool PeekFirstLine(std::string* line) override {
234 std::string start_of_file; 280 std::string start_of_file;
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after
381 FROM_HERE, base::Bind(callback, path_, base::PathExists(path_))); 427 FROM_HERE, base::Bind(callback, path_, base::PathExists(path_)));
382 return; 428 return;
383 } 429 }
384 430
385 base::CreateTemporaryFile(&path_); 431 base::CreateTemporaryFile(&path_);
386 common::CopyToFile(response_->body.Pass(), path_, task_runner, 432 common::CopyToFile(response_->body.Pass(), path_, task_runner,
387 base::Bind(&NetworkLoader::CopyCompleted, 433 base::Bind(&NetworkLoader::CopyCompleted,
388 weak_ptr_factory_.GetWeakPtr(), callback)); 434 weak_ptr_factory_.GetWeakPtr(), callback));
389 } 435 }
390 436
437 GURL URL() override { return url_; }
438
391 std::string MimeType() override { 439 std::string MimeType() override {
392 DCHECK(response_); 440 DCHECK(response_);
393 return response_->mime_type; 441 return response_->mime_type;
394 } 442 }
395 443
396 bool HasMojoMagic() override { 444 bool HasMojoMagic() override {
397 std::string magic; 445 std::string magic;
398 return BlockingPeekNBytes(response_->body.get(), &magic, strlen(kMojoMagic), 446 return BlockingPeekNBytes(response_->body.get(), &magic, strlen(kMojoMagic),
399 kPeekTimeout) && 447 kPeekTimeout) &&
400 magic == kMojoMagic; 448 magic == kMojoMagic;
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
494 // TODO(darin): What should we do about service errors? This implies that 542 // TODO(darin): What should we do about service errors? This implies that
495 // the app closed its handle to the service manager. Maybe we don't care? 543 // the app closed its handle to the service manager. Maybe we don't care?
496 } 544 }
497 545
498 void DynamicApplicationLoader::LoaderComplete(Loader* loader) { 546 void DynamicApplicationLoader::LoaderComplete(Loader* loader) {
499 loaders_.erase(std::find(loaders_.begin(), loaders_.end(), loader)); 547 loaders_.erase(std::find(loaders_.begin(), loaders_.end(), loader));
500 } 548 }
501 549
502 } // namespace shell 550 } // namespace shell
503 } // namespace mojo 551 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698