Chromium Code Reviews| 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 "mojo/shell/dynamic_application_loader.h" | 5 #include "mojo/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_path.h" | 9 #include "base/files/file_path.h" |
| 10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
| 11 #include "base/format_macros.h" | |
| 12 #include "base/logging.h" | |
| 11 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
| 13 #include "base/message_loop/message_loop.h" | 15 #include "base/message_loop/message_loop.h" |
| 14 #include "base/strings/string_util.h" | 16 #include "base/strings/string_util.h" |
| 17 #include "base/strings/stringprintf.h" | |
| 15 #include "base/strings/utf_string_conversions.h" | 18 #include "base/strings/utf_string_conversions.h" |
| 16 #include "mojo/common/common_type_converters.h" | 19 #include "mojo/common/common_type_converters.h" |
| 17 #include "mojo/common/data_pipe_utils.h" | 20 #include "mojo/common/data_pipe_utils.h" |
| 21 #include "mojo/public/cpp/system/data_pipe.h" | |
| 18 #include "mojo/services/public/interfaces/network/url_loader.mojom.h" | 22 #include "mojo/services/public/interfaces/network/url_loader.mojom.h" |
| 19 #include "mojo/shell/context.h" | 23 #include "mojo/shell/context.h" |
| 20 #include "mojo/shell/data_pipe_peek.h" | 24 #include "mojo/shell/data_pipe_peek.h" |
| 21 #include "mojo/shell/filename_util.h" | 25 #include "mojo/shell/filename_util.h" |
| 22 #include "mojo/shell/switches.h" | 26 #include "mojo/shell/switches.h" |
| 23 #include "url/url_util.h" | 27 #include "url/url_util.h" |
| 24 | 28 |
| 25 namespace mojo { | 29 namespace mojo { |
| 26 namespace shell { | 30 namespace shell { |
| 27 | 31 |
| 32 namespace { | |
| 33 | |
| 34 static const char kMojoMagic[] = "#!mojo:"; | |
| 35 static const size_t kMaxShebangLength = 2048; | |
| 36 | |
| 37 void IgnoreResult(bool result) { | |
| 38 } | |
| 39 | |
| 40 } // namespace | |
| 41 | |
| 28 // Encapsulates loading and running one individual application. | 42 // Encapsulates loading and running one individual application. |
| 29 // | 43 // |
| 30 // Loaders are owned by DynamicApplicationLoader. DynamicApplicationLoader must | 44 // Loaders are owned by DynamicApplicationLoader. DynamicApplicationLoader must |
| 31 // ensure that all the parameters passed to Loader subclasses stay valid through | 45 // ensure that all the parameters passed to Loader subclasses stay valid through |
| 32 // Loader's lifetime. | 46 // Loader's lifetime. |
| 33 // | 47 // |
| 34 // Async operations are done with WeakPtr to protect against | 48 // Async operations are done with WeakPtr to protect against |
| 35 // DynamicApplicationLoader going away (and taking all the Loaders with it) | 49 // DynamicApplicationLoader going away (and taking all the Loaders with it) |
| 36 // while the async operation is outstanding. | 50 // while the async operation is outstanding. |
| 37 class DynamicApplicationLoader::Loader { | 51 class DynamicApplicationLoader::Loader { |
| 38 public: | 52 public: |
| 39 Loader(Context* context, | 53 Loader(MimeTypeToURLMap* mime_type_to_url, |
| 54 Context* context, | |
| 40 DynamicServiceRunnerFactory* runner_factory, | 55 DynamicServiceRunnerFactory* runner_factory, |
| 41 scoped_refptr<ApplicationLoader::LoadCallbacks> load_callbacks, | 56 scoped_refptr<ApplicationLoader::LoadCallbacks> load_callbacks, |
| 42 const LoaderCompleteCallback& loader_complete_callback) | 57 const LoaderCompleteCallback& loader_complete_callback) |
| 43 : load_callbacks_(load_callbacks), | 58 : load_callbacks_(load_callbacks), |
| 44 loader_complete_callback_(loader_complete_callback), | 59 loader_complete_callback_(loader_complete_callback), |
| 45 context_(context), | 60 context_(context), |
| 61 mime_type_to_url_(mime_type_to_url), | |
| 46 runner_factory_(runner_factory), | 62 runner_factory_(runner_factory), |
| 47 weak_ptr_factory_(this) {} | 63 weak_ptr_factory_(this) {} |
| 48 | 64 |
| 49 virtual ~Loader() {} | 65 virtual ~Loader() {} |
| 50 | 66 |
| 51 protected: | 67 protected: |
| 68 virtual URLResponsePtr AsURLResponse(base::TaskRunner* task_runner, | |
| 69 uint32_t skip) = 0; | |
| 70 | |
| 71 virtual void AsPath( | |
| 72 base::TaskRunner* task_runner, | |
| 73 base::Callback<void(const base::FilePath&, bool)> callback) = 0; | |
| 74 | |
| 75 virtual std::string MimeType() = 0; | |
| 76 | |
| 77 virtual bool HasMojoMagic() = 0; | |
| 78 | |
| 79 virtual bool PeekFirstLine(std::string* line) = 0; | |
| 80 | |
| 81 void OnResponse(bool success) { | |
|
Aaron Boodman
2014/11/07 09:44:36
Rename this Load().
Rename LoaderComplete() to Re
qsr
2014/11/07 09:58:19
Done.
| |
| 82 if (!success) { | |
| 83 LoaderComplete(); | |
| 84 return; | |
| 85 } | |
| 86 | |
| 87 // If the response begins with a #!mojo:<content-handler-url>, use it. | |
| 88 { | |
|
Aaron Boodman
2014/11/07 09:44:36
Not your deal, but remove these braces. No need.
qsr
2014/11/07 09:58:19
Done.
| |
| 89 GURL url; | |
| 90 std::string shebang; | |
| 91 if (PeekContentHandler(&shebang, &url)) { | |
| 92 load_callbacks_->LoadWithContentHandler( | |
| 93 url, AsURLResponse(context_->task_runners()->blocking_pool(), | |
| 94 shebang.size())); | |
| 95 return; | |
| 96 } | |
| 97 } | |
| 98 | |
| 99 MimeTypeToURLMap::iterator iter = mime_type_to_url_->find(MimeType()); | |
| 100 if (iter != mime_type_to_url_->end()) { | |
| 101 load_callbacks_->LoadWithContentHandler( | |
| 102 iter->second, | |
| 103 AsURLResponse(context_->task_runners()->blocking_pool(), 0)); | |
| 104 return; | |
| 105 } | |
| 106 | |
| 107 // TODO(aa): Sanity check that the thing we got looks vaguely like a mojo | |
| 108 // application. That could either mean looking for the platform-specific dll | |
| 109 // header, or looking for some specific mojo signature prepended to the | |
| 110 // library. | |
| 111 | |
| 112 AsPath(context_->task_runners()->blocking_pool(), | |
| 113 base::Bind(&Loader::RunLibrary, weak_ptr_factory_.GetWeakPtr())); | |
| 114 } | |
| 115 | |
| 116 private: | |
| 117 bool PeekContentHandler(std::string* mojo_shebang, | |
| 118 GURL* mojo_content_handler_url) { | |
| 119 std::string shebang; | |
| 120 if (HasMojoMagic() && PeekFirstLine(&shebang)) { | |
| 121 GURL url(shebang.substr(2, std::string::npos)); | |
| 122 if (url.is_valid()) { | |
| 123 *mojo_shebang = shebang; | |
| 124 *mojo_content_handler_url = url; | |
| 125 return true; | |
| 126 } | |
| 127 } | |
| 128 return false; | |
| 129 } | |
| 130 | |
| 52 void RunLibrary(const base::FilePath& path, bool path_exists) { | 131 void RunLibrary(const base::FilePath& path, bool path_exists) { |
| 53 ScopedMessagePipeHandle shell_handle = | 132 ScopedMessagePipeHandle shell_handle = |
| 54 load_callbacks_->RegisterApplication(); | 133 load_callbacks_->RegisterApplication(); |
| 55 if (!shell_handle.is_valid()) { | 134 if (!shell_handle.is_valid()) { |
| 56 LoaderComplete(); | 135 LoaderComplete(); |
| 57 return; | 136 return; |
| 58 } | 137 } |
| 59 | 138 |
| 60 if (!path_exists) { | 139 if (!path_exists) { |
| 61 LOG(ERROR) << "Library not started because library path '" << path.value() | 140 LOG(ERROR) << "Library not started because library path '" << path.value() |
| 62 << "' does not exist."; | 141 << "' does not exist."; |
| 63 LoaderComplete(); | 142 LoaderComplete(); |
| 64 return; | 143 return; |
| 65 } | 144 } |
| 66 | 145 |
| 67 runner_ = runner_factory_->Create(context_); | 146 runner_ = runner_factory_->Create(context_); |
| 68 runner_->Start( | 147 runner_->Start( |
| 69 path, | 148 path, shell_handle.Pass(), |
| 70 shell_handle.Pass(), | |
| 71 base::Bind(&Loader::LoaderComplete, weak_ptr_factory_.GetWeakPtr())); | 149 base::Bind(&Loader::LoaderComplete, weak_ptr_factory_.GetWeakPtr())); |
| 72 } | 150 } |
| 73 | 151 |
| 74 void LoaderComplete() { loader_complete_callback_.Run(this); } | 152 void LoaderComplete() { loader_complete_callback_.Run(this); } |
| 75 | 153 |
| 76 scoped_refptr<ApplicationLoader::LoadCallbacks> load_callbacks_; | 154 scoped_refptr<ApplicationLoader::LoadCallbacks> load_callbacks_; |
| 77 LoaderCompleteCallback loader_complete_callback_; | 155 LoaderCompleteCallback loader_complete_callback_; |
| 78 Context* context_; | 156 Context* context_; |
| 79 | 157 MimeTypeToURLMap* mime_type_to_url_; |
| 80 private: | |
| 81 DynamicServiceRunnerFactory* runner_factory_; | 158 DynamicServiceRunnerFactory* runner_factory_; |
| 82 scoped_ptr<DynamicServiceRunner> runner_; | 159 scoped_ptr<DynamicServiceRunner> runner_; |
| 83 base::WeakPtrFactory<Loader> weak_ptr_factory_; | 160 base::WeakPtrFactory<Loader> weak_ptr_factory_; |
| 84 }; | 161 }; |
| 85 | 162 |
| 86 // A loader for local files. | 163 // A loader for local files. |
| 87 class DynamicApplicationLoader::LocalLoader : public Loader { | 164 class DynamicApplicationLoader::LocalLoader : public Loader { |
| 88 public: | 165 public: |
| 89 LocalLoader(const GURL& url, | 166 LocalLoader(const GURL& url, |
| 167 MimeTypeToURLMap* mime_type_to_url, | |
| 90 Context* context, | 168 Context* context, |
| 91 DynamicServiceRunnerFactory* runner_factory, | 169 DynamicServiceRunnerFactory* runner_factory, |
| 92 scoped_refptr<ApplicationLoader::LoadCallbacks> load_callbacks, | 170 scoped_refptr<ApplicationLoader::LoadCallbacks> load_callbacks, |
| 93 const LoaderCompleteCallback& loader_complete_callback) | 171 const LoaderCompleteCallback& loader_complete_callback) |
| 94 : Loader(context, | 172 : Loader(mime_type_to_url, |
| 173 context, | |
| 95 runner_factory, | 174 runner_factory, |
| 96 load_callbacks, | 175 load_callbacks, |
| 97 loader_complete_callback), | 176 loader_complete_callback), |
| 98 weak_ptr_factory_(this) { | 177 url_(url), |
| 178 path_(UrlToFile(url)) { | |
| 179 OnResponse(true); | |
| 180 } | |
| 181 | |
| 182 private: | |
| 183 static base::FilePath UrlToFile(const GURL& url) { | |
| 99 DCHECK(url.SchemeIsFile()); | 184 DCHECK(url.SchemeIsFile()); |
| 100 url::RawCanonOutputW<1024> output; | 185 url::RawCanonOutputW<1024> output; |
| 101 url::DecodeURLEscapeSequences( | 186 url::DecodeURLEscapeSequences( |
| 102 url.path().data(), static_cast<int>(url.path().length()), &output); | 187 url.path().data(), static_cast<int>(url.path().length()), &output); |
| 103 base::string16 decoded_path = | 188 base::string16 decoded_path = |
| 104 base::string16(output.data(), output.length()); | 189 base::string16(output.data(), output.length()); |
| 105 #if defined(OS_WIN) | 190 #if defined(OS_WIN) |
| 106 base::TrimString(decoded_path, L"/", &decoded_path); | 191 base::TrimString(decoded_path, L"/", &decoded_path); |
| 107 base::FilePath path(decoded_path); | 192 base::FilePath path(decoded_path); |
| 108 #else | 193 #else |
| 109 base::FilePath path(base::UTF16ToUTF8(decoded_path)); | 194 base::FilePath path(base::UTF16ToUTF8(decoded_path)); |
| 110 #endif | 195 #endif |
| 196 return path; | |
| 197 } | |
| 111 | 198 |
| 199 URLResponsePtr AsURLResponse(base::TaskRunner* task_runner, | |
| 200 uint32_t skip) override { | |
| 201 URLResponsePtr response(URLResponse::New()); | |
| 202 response->url = String::From(url_); | |
| 203 DataPipe data_pipe; | |
| 204 response->body = data_pipe.consumer_handle.Pass(); | |
| 205 int64 file_size; | |
| 206 if (base::GetFileSize(path_, &file_size)) { | |
| 207 response->headers = Array<String>(1); | |
| 208 response->headers[0] = | |
| 209 base::StringPrintf("Content-Length: %" PRId64, file_size); | |
| 210 } | |
| 211 common::CopyFromFile(path_, data_pipe.producer_handle.Pass(), skip, | |
| 212 task_runner, base::Bind(&IgnoreResult)); | |
| 213 return response.Pass(); | |
| 214 } | |
| 215 | |
| 216 void AsPath( | |
| 217 base::TaskRunner* task_runner, | |
| 218 base::Callback<void(const base::FilePath&, bool)> callback) override { | |
| 112 // Async for consistency with network case. | 219 // Async for consistency with network case. |
| 113 base::MessageLoop::current()->PostTask( | 220 base::MessageLoop::current()->PostTask( |
| 114 FROM_HERE, | 221 FROM_HERE, base::Bind(callback, path_, base::PathExists(path_))); |
| 115 base::Bind(&LocalLoader::RunLibrary, | |
| 116 weak_ptr_factory_.GetWeakPtr(), | |
| 117 path, | |
| 118 base::PathExists(path))); | |
| 119 } | 222 } |
| 120 | 223 |
| 121 ~LocalLoader() override {} | 224 std::string MimeType() override { return ""; } |
| 122 | 225 |
| 123 private: | 226 bool HasMojoMagic() override { |
| 124 base::WeakPtrFactory<LocalLoader> weak_ptr_factory_; | 227 std::string magic; |
| 228 ReadFileToString(path_, &magic, strlen(kMojoMagic)); | |
| 229 return magic == kMojoMagic; | |
| 230 } | |
| 231 | |
| 232 bool PeekFirstLine(std::string* line) override { | |
| 233 std::string start_of_file; | |
| 234 ReadFileToString(path_, &start_of_file, kMaxShebangLength); | |
| 235 size_t return_position = start_of_file.find('\n'); | |
| 236 if (return_position == std::string::npos) | |
| 237 return false; | |
| 238 *line = start_of_file.substr(0, return_position + 1); | |
| 239 return true; | |
| 240 } | |
| 241 | |
| 242 GURL url_; | |
| 243 base::FilePath path_; | |
| 244 | |
| 245 DISALLOW_COPY_AND_ASSIGN(LocalLoader); | |
| 125 }; | 246 }; |
| 126 | 247 |
| 127 // A loader for network files. | 248 // A loader for network files. |
| 128 class DynamicApplicationLoader::NetworkLoader : public Loader { | 249 class DynamicApplicationLoader::NetworkLoader : public Loader { |
| 129 public: | 250 public: |
| 130 NetworkLoader(const GURL& url, | 251 NetworkLoader(const GURL& url, |
| 252 NetworkService* network_service, | |
| 131 MimeTypeToURLMap* mime_type_to_url, | 253 MimeTypeToURLMap* mime_type_to_url, |
| 132 Context* context, | 254 Context* context, |
| 133 DynamicServiceRunnerFactory* runner_factory, | 255 DynamicServiceRunnerFactory* runner_factory, |
| 134 NetworkService* network_service, | |
| 135 scoped_refptr<ApplicationLoader::LoadCallbacks> load_callbacks, | 256 scoped_refptr<ApplicationLoader::LoadCallbacks> load_callbacks, |
| 136 const LoaderCompleteCallback& loader_complete_callback) | 257 const LoaderCompleteCallback& loader_complete_callback) |
| 137 : Loader(context, | 258 : Loader(mime_type_to_url, |
| 259 context, | |
| 138 runner_factory, | 260 runner_factory, |
| 139 load_callbacks, | 261 load_callbacks, |
| 140 loader_complete_callback), | 262 loader_complete_callback), |
| 141 mime_type_to_url_(mime_type_to_url), | |
| 142 weak_ptr_factory_(this) { | 263 weak_ptr_factory_(this) { |
| 264 Load(url, network_service); | |
| 265 } | |
| 266 | |
| 267 ~NetworkLoader() override { | |
| 268 if (!path_.empty()) | |
| 269 base::DeleteFile(path_, false); | |
| 270 } | |
| 271 | |
| 272 private: | |
| 273 // TODO(hansmuller): Revisit this when a real peek operation is available. | |
| 274 static const MojoDeadline kPeekTimeout = MOJO_DEADLINE_INDEFINITE; | |
| 275 | |
| 276 URLResponsePtr AsURLResponse(base::TaskRunner* task_runner, | |
| 277 uint32_t skip) override { | |
| 278 if (skip != 0) { | |
| 279 MojoResult result = ReadDataRaw( | |
| 280 response_->body.get(), nullptr, &skip, | |
| 281 MOJO_READ_DATA_FLAG_ALL_OR_NONE | MOJO_READ_DATA_FLAG_DISCARD); | |
| 282 DCHECK_EQ(result, MOJO_RESULT_OK); | |
| 283 } | |
| 284 return response_.Pass(); | |
| 285 } | |
| 286 | |
| 287 void AsPath( | |
| 288 base::TaskRunner* task_runner, | |
| 289 base::Callback<void(const base::FilePath&, bool)> callback) override { | |
| 290 if (!path_.empty() || !response_) { | |
| 291 base::MessageLoop::current()->PostTask( | |
| 292 FROM_HERE, base::Bind(callback, path_, base::PathExists(path_))); | |
| 293 return; | |
| 294 } | |
| 295 base::CreateTemporaryFile(&path_); | |
| 296 common::CopyToFile(response_->body.Pass(), path_, task_runner, | |
| 297 base::Bind(callback, path_)); | |
| 298 } | |
| 299 | |
| 300 std::string MimeType() override { | |
| 301 DCHECK(response_); | |
| 302 return response_->mime_type; | |
| 303 } | |
| 304 | |
| 305 bool HasMojoMagic() override { | |
| 306 std::string magic; | |
| 307 return BlockingPeekNBytes(response_->body.get(), &magic, strlen(kMojoMagic), | |
| 308 kPeekTimeout) && | |
| 309 magic == kMojoMagic; | |
| 310 } | |
| 311 | |
| 312 bool PeekFirstLine(std::string* line) override { | |
| 313 return BlockingPeekLine(response_->body.get(), line, kMaxShebangLength, | |
| 314 kPeekTimeout); | |
| 315 } | |
| 316 | |
| 317 void Load(const GURL& url, NetworkService* network_service) { | |
| 143 URLRequestPtr request(URLRequest::New()); | 318 URLRequestPtr request(URLRequest::New()); |
| 144 request->url = String::From(url); | 319 request->url = String::From(url); |
| 145 request->auto_follow_redirects = true; | 320 request->auto_follow_redirects = true; |
| 146 | 321 |
| 147 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | 322 if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 148 switches::kDisableCache)) { | 323 switches::kDisableCache)) { |
| 149 request->bypass_cache = true; | 324 request->bypass_cache = true; |
| 150 } | 325 } |
| 151 | 326 |
| 152 network_service->CreateURLLoader(GetProxy(&url_loader_)); | 327 network_service->CreateURLLoader(GetProxy(&url_loader_)); |
| 153 url_loader_->Start(request.Pass(), | 328 url_loader_->Start(request.Pass(), |
| 154 base::Bind(&NetworkLoader::OnLoadComplete, | 329 base::Bind(&NetworkLoader::OnLoadComplete, |
| 155 weak_ptr_factory_.GetWeakPtr())); | 330 weak_ptr_factory_.GetWeakPtr())); |
| 156 } | 331 } |
| 157 | 332 |
| 158 ~NetworkLoader() override { | |
| 159 if (!file_.empty()) | |
| 160 base::DeleteFile(file_, false); | |
| 161 } | |
| 162 | |
| 163 private: | |
| 164 bool PeekContentHandler(DataPipeConsumerHandle source, | |
| 165 std::string* mojo_shebang, | |
| 166 GURL* mojo_content_handler_url) | |
| 167 { | |
| 168 const char* kMojoMagic = "#!mojo:"; | |
| 169 // TODO(hansmuller): Revisit this when a real peek operation is available. | |
| 170 const MojoDeadline kPeekTimeout = MOJO_DEADLINE_INDEFINITE; | |
| 171 const size_t kMaxShebangLength = 2048; | |
| 172 | |
| 173 std::string magic; | |
| 174 std::string shebang; | |
| 175 if (BlockingPeekNBytes(source, &magic, strlen(kMojoMagic), kPeekTimeout) && | |
| 176 magic == kMojoMagic && | |
| 177 BlockingPeekLine(source, &shebang, kMaxShebangLength, kPeekTimeout)) { | |
| 178 GURL url(shebang.substr(2, std::string::npos)); | |
| 179 if (url.is_valid()) { | |
| 180 *mojo_shebang = shebang; | |
| 181 *mojo_content_handler_url = url; | |
| 182 return true; | |
| 183 } | |
| 184 } | |
| 185 return false; | |
| 186 } | |
| 187 | |
| 188 void OnLoadComplete(URLResponsePtr response) { | 333 void OnLoadComplete(URLResponsePtr response) { |
| 189 if (response->error) { | 334 if (response->error) { |
| 190 LOG(ERROR) << "Error (" << response->error->code << ": " | 335 LOG(ERROR) << "Error (" << response->error->code << ": " |
| 191 << response->error->description << ") while fetching " | 336 << response->error->description << ") while fetching " |
| 192 << response->url; | 337 << response->url; |
| 193 LoaderComplete(); | 338 OnResponse(false); |
| 194 return; | 339 return; |
| 195 } | 340 } |
| 196 | 341 response_ = response.Pass(); |
| 197 // If the response begins with a #!mojo:<content-handler-url>, use it. | 342 OnResponse(true); |
| 198 { | |
| 199 GURL url; | |
| 200 std::string shebang; | |
| 201 if (PeekContentHandler(response->body.get(), &shebang, &url)) { | |
| 202 uint32_t num_skip_bytes = shebang.size(); | |
| 203 if (ReadDataRaw(response->body.get(), | |
| 204 nullptr, | |
| 205 &num_skip_bytes, | |
| 206 MOJO_READ_DATA_FLAG_ALL_OR_NONE | | |
| 207 MOJO_READ_DATA_FLAG_DISCARD) == | |
| 208 MOJO_RESULT_OK) { | |
| 209 load_callbacks_->LoadWithContentHandler(url, response.Pass()); | |
| 210 return; | |
| 211 } | |
| 212 } | |
| 213 } | |
| 214 | |
| 215 MimeTypeToURLMap::iterator iter = | |
| 216 mime_type_to_url_->find(response->mime_type); | |
| 217 if (iter != mime_type_to_url_->end()) { | |
| 218 load_callbacks_->LoadWithContentHandler(iter->second, response.Pass()); | |
| 219 return; | |
| 220 } | |
| 221 | |
| 222 // TODO(aa): Sanity check that the thing we got looks vaguely like a mojo | |
| 223 // application. That could either mean looking for the platform-specific dll | |
| 224 // header, or looking for some specific mojo signature prepended to the | |
| 225 // library. | |
| 226 | |
| 227 base::CreateTemporaryFile(&file_); | |
| 228 common::CopyToFile( | |
| 229 response->body.Pass(), | |
| 230 file_, | |
| 231 context_->task_runners()->blocking_pool(), | |
| 232 base::Bind( | |
| 233 &NetworkLoader::RunLibrary, weak_ptr_factory_.GetWeakPtr(), file_)); | |
| 234 } | 343 } |
| 235 | 344 |
| 236 MimeTypeToURLMap* mime_type_to_url_; | |
| 237 URLLoaderPtr url_loader_; | 345 URLLoaderPtr url_loader_; |
| 238 base::FilePath file_; | 346 URLResponsePtr response_; |
| 347 base::FilePath path_; | |
| 239 base::WeakPtrFactory<NetworkLoader> weak_ptr_factory_; | 348 base::WeakPtrFactory<NetworkLoader> weak_ptr_factory_; |
| 349 | |
| 350 DISALLOW_COPY_AND_ASSIGN(NetworkLoader); | |
| 240 }; | 351 }; |
| 352 | |
| 241 DynamicApplicationLoader::DynamicApplicationLoader( | 353 DynamicApplicationLoader::DynamicApplicationLoader( |
| 242 Context* context, | 354 Context* context, |
| 243 scoped_ptr<DynamicServiceRunnerFactory> runner_factory) | 355 scoped_ptr<DynamicServiceRunnerFactory> runner_factory) |
| 244 : context_(context), | 356 : context_(context), |
| 245 runner_factory_(runner_factory.Pass()), | 357 runner_factory_(runner_factory.Pass()), |
| 246 | 358 |
| 247 // Unretained() is correct here because DynamicApplicationLoader owns the | 359 // Unretained() is correct here because DynamicApplicationLoader owns the |
| 248 // loaders that we pass this callback to. | 360 // loaders that we pass this callback to. |
| 249 loader_complete_callback_( | 361 loader_complete_callback_( |
| 250 base::Bind(&DynamicApplicationLoader::LoaderComplete, | 362 base::Bind(&DynamicApplicationLoader::LoaderComplete, |
| 251 base::Unretained(this))) { | 363 base::Unretained(this))) { |
| 252 } | 364 } |
| 253 | 365 |
| 254 DynamicApplicationLoader::~DynamicApplicationLoader() { | 366 DynamicApplicationLoader::~DynamicApplicationLoader() { |
| 255 } | 367 } |
| 256 | 368 |
| 257 void DynamicApplicationLoader::RegisterContentHandler( | 369 void DynamicApplicationLoader::RegisterContentHandler( |
| 258 const std::string& mime_type, | 370 const std::string& mime_type, |
| 259 const GURL& content_handler_url) { | 371 const GURL& content_handler_url) { |
| 260 mime_type_to_url_[mime_type] = content_handler_url; | 372 mime_type_to_url_[mime_type] = content_handler_url; |
| 261 } | 373 } |
| 262 | 374 |
| 263 void DynamicApplicationLoader::Load( | 375 void DynamicApplicationLoader::Load( |
| 264 ApplicationManager* manager, | 376 ApplicationManager* manager, |
| 265 const GURL& url, | 377 const GURL& url, |
| 266 scoped_refptr<LoadCallbacks> load_callbacks) { | 378 scoped_refptr<LoadCallbacks> load_callbacks) { |
| 267 if (url.SchemeIsFile()) { | 379 if (url.SchemeIsFile()) { |
| 268 loaders_.push_back(new LocalLoader(url, | 380 loaders_.push_back(new LocalLoader(url, &mime_type_to_url_, context_, |
| 269 context_, | 381 runner_factory_.get(), load_callbacks, |
| 270 runner_factory_.get(), | |
| 271 load_callbacks, | |
| 272 loader_complete_callback_)); | 382 loader_complete_callback_)); |
| 273 return; | 383 return; |
| 274 } | 384 } |
| 275 | 385 |
| 276 if (!network_service_) { | 386 if (!network_service_) { |
| 277 context_->application_manager()->ConnectToService( | 387 context_->application_manager()->ConnectToService( |
| 278 GURL("mojo:network_service"), &network_service_); | 388 GURL("mojo:network_service"), &network_service_); |
| 279 } | 389 } |
| 280 | 390 |
| 281 loaders_.push_back(new NetworkLoader(url, | 391 loaders_.push_back(new NetworkLoader( |
| 282 &mime_type_to_url_, | 392 url, network_service_.get(), &mime_type_to_url_, context_, |
| 283 context_, | 393 runner_factory_.get(), load_callbacks, loader_complete_callback_)); |
| 284 runner_factory_.get(), | |
| 285 network_service_.get(), | |
| 286 load_callbacks, | |
| 287 loader_complete_callback_)); | |
| 288 } | 394 } |
| 289 | 395 |
| 290 void DynamicApplicationLoader::OnApplicationError(ApplicationManager* manager, | 396 void DynamicApplicationLoader::OnApplicationError(ApplicationManager* manager, |
| 291 const GURL& url) { | 397 const GURL& url) { |
| 292 // TODO(darin): What should we do about service errors? This implies that | 398 // TODO(darin): What should we do about service errors? This implies that |
| 293 // the app closed its handle to the service manager. Maybe we don't care? | 399 // the app closed its handle to the service manager. Maybe we don't care? |
| 294 } | 400 } |
| 295 | 401 |
| 296 void DynamicApplicationLoader::LoaderComplete(Loader* loader) { | 402 void DynamicApplicationLoader::LoaderComplete(Loader* loader) { |
| 297 loaders_.erase(std::find(loaders_.begin(), loaders_.end(), loader)); | 403 loaders_.erase(std::find(loaders_.begin(), loaders_.end(), loader)); |
| 298 } | 404 } |
| 299 | 405 |
| 300 } // namespace shell | 406 } // namespace shell |
| 301 } // namespace mojo | 407 } // namespace mojo |
| OLD | NEW |