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