| 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_service_loader.h" | 5 #include "mojo/shell/dynamic_service_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/file_util.h" | 9 #include "base/file_util.h" |
| 10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| 11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
| 13 #include "mojo/common/common_type_converters.h" | 13 #include "mojo/common/common_type_converters.h" |
| 14 #include "mojo/common/data_pipe_utils.h" | 14 #include "mojo/common/data_pipe_utils.h" |
| 15 #include "mojo/services/public/interfaces/network/url_loader.mojom.h" | 15 #include "mojo/services/public/interfaces/network/url_loader.mojom.h" |
| 16 #include "mojo/shell/context.h" | 16 #include "mojo/shell/context.h" |
| 17 #include "mojo/shell/keep_alive.h" | 17 #include "mojo/shell/keep_alive.h" |
| 18 #include "mojo/shell/switches.h" | 18 #include "mojo/shell/switches.h" |
| 19 #include "net/base/filename_util.h" | 19 #include "net/base/filename_util.h" |
| 20 | 20 |
| 21 namespace mojo { | 21 namespace mojo { |
| 22 namespace shell { | 22 namespace shell { |
| 23 |
| 23 namespace { | 24 namespace { |
| 24 | 25 |
| 25 class Loader { | 26 void DeleteRunner(DynamicServiceRunner* runner) { |
| 26 public: | 27 delete runner; |
| 27 explicit Loader(scoped_ptr<DynamicServiceRunner> runner) | 28 } |
| 28 : runner_(runner.Pass()) { | |
| 29 } | |
| 30 | |
| 31 virtual void Start(const GURL& url, | |
| 32 ScopedMessagePipeHandle service_handle, | |
| 33 Context* context) = 0; | |
| 34 | |
| 35 void StartService(const base::FilePath& path, | |
| 36 ScopedMessagePipeHandle service_handle, | |
| 37 bool path_is_valid) { | |
| 38 if (path_is_valid) { | |
| 39 runner_->Start(path, service_handle.Pass(), | |
| 40 base::Bind(&Loader::AppCompleted, base::Unretained(this))); | |
| 41 } else { | |
| 42 AppCompleted(); | |
| 43 } | |
| 44 } | |
| 45 | |
| 46 protected: | |
| 47 virtual ~Loader() {} | |
| 48 | |
| 49 private: | |
| 50 void AppCompleted() { | |
| 51 delete this; | |
| 52 } | |
| 53 | |
| 54 scoped_ptr<DynamicServiceRunner> runner_; | |
| 55 }; | |
| 56 | |
| 57 // For loading services via file:// URLs. | |
| 58 class LocalLoader : public Loader { | |
| 59 public: | |
| 60 explicit LocalLoader(scoped_ptr<DynamicServiceRunner> runner) | |
| 61 : Loader(runner.Pass()) { | |
| 62 } | |
| 63 | |
| 64 virtual void Start(const GURL& url, | |
| 65 ScopedMessagePipeHandle service_handle, | |
| 66 Context* context) OVERRIDE { | |
| 67 base::FilePath path; | |
| 68 net::FileURLToFilePath(url, &path); | |
| 69 | |
| 70 // Complete asynchronously for consistency with NetworkServiceLoader. | |
| 71 base::MessageLoop::current()->PostTask( | |
| 72 FROM_HERE, | |
| 73 base::Bind(&Loader::StartService, | |
| 74 base::Unretained(this), | |
| 75 path, | |
| 76 base::Passed(&service_handle), | |
| 77 base::PathExists(path))); | |
| 78 } | |
| 79 }; | |
| 80 | |
| 81 // For loading services via the network stack. | |
| 82 class NetworkLoader : public Loader { | |
| 83 public: | |
| 84 explicit NetworkLoader(scoped_ptr<DynamicServiceRunner> runner, | |
| 85 NetworkService* network_service) | |
| 86 : Loader(runner.Pass()) { | |
| 87 network_service->CreateURLLoader(Get(&url_loader_)); | |
| 88 } | |
| 89 | |
| 90 virtual void Start(const GURL& url, | |
| 91 ScopedMessagePipeHandle service_handle, | |
| 92 Context* context) OVERRIDE { | |
| 93 service_handle_ = service_handle.Pass(); | |
| 94 context_ = context; | |
| 95 | |
| 96 URLRequestPtr request(URLRequest::New()); | |
| 97 request->url = String::From(url); | |
| 98 request->auto_follow_redirects = true; | |
| 99 | |
| 100 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | |
| 101 switches::kDisableCache)) { | |
| 102 request->bypass_cache = true; | |
| 103 } | |
| 104 | |
| 105 url_loader_->Start(request.Pass(), | |
| 106 base::Bind(&NetworkLoader::OnReceivedResponse, | |
| 107 base::Unretained(this))); | |
| 108 } | |
| 109 | |
| 110 private: | |
| 111 virtual ~NetworkLoader() { | |
| 112 if (!file_.empty()) | |
| 113 base::DeleteFile(file_, false); | |
| 114 } | |
| 115 | |
| 116 void OnReceivedResponse(URLResponsePtr response) { | |
| 117 if (response->error) { | |
| 118 LOG(ERROR) << "Error (" << response->error->code << ": " | |
| 119 << response->error->description << ") while fetching " | |
| 120 << response->url; | |
| 121 } | |
| 122 | |
| 123 base::CreateTemporaryFile(&file_); | |
| 124 common::CopyToFile(response->body.Pass(), | |
| 125 file_, | |
| 126 context_->task_runners()->blocking_pool(), | |
| 127 base::Bind(&Loader::StartService, | |
| 128 base::Unretained(this), | |
| 129 file_, | |
| 130 base::Passed(&service_handle_))); | |
| 131 } | |
| 132 | |
| 133 Context* context_; | |
| 134 NetworkServicePtr network_service_; | |
| 135 URLLoaderPtr url_loader_; | |
| 136 ScopedMessagePipeHandle service_handle_; | |
| 137 base::FilePath file_; | |
| 138 }; | |
| 139 | 29 |
| 140 } // namespace | 30 } // namespace |
| 141 | 31 |
| 142 DynamicServiceLoader::DynamicServiceLoader( | 32 DynamicServiceLoader::DynamicServiceLoader( |
| 143 Context* context, | 33 Context* context, |
| 144 scoped_ptr<DynamicServiceRunnerFactory> runner_factory) | 34 scoped_ptr<DynamicServiceRunnerFactory> runner_factory) |
| 145 : context_(context), | 35 : context_(context), |
| 146 runner_factory_(runner_factory.Pass()) { | 36 runner_factory_(runner_factory.Pass()), |
| 37 weak_ptr_factory_(this) { |
| 147 } | 38 } |
| 148 | 39 |
| 149 DynamicServiceLoader::~DynamicServiceLoader() { | 40 DynamicServiceLoader::~DynamicServiceLoader() { |
| 150 } | 41 } |
| 151 | 42 |
| 152 void DynamicServiceLoader::LoadService(ServiceManager* manager, | 43 void DynamicServiceLoader::set_content_handler( |
| 153 const GURL& url, | 44 const std::string& mime_type, |
| 154 ScopedMessagePipeHandle shell_handle) { | 45 const GURL& content_handler_url) { |
| 155 scoped_ptr<DynamicServiceRunner> runner = runner_factory_->Create(context_); | 46 mime_type_to_url_[mime_type] = content_handler_url; |
| 47 } |
| 156 | 48 |
| 49 void DynamicServiceLoader::LoadService( |
| 50 ServiceManager* manager, |
| 51 const GURL& url, |
| 52 scoped_refptr<LoadServiceCallbacks> callbacks) { |
| 157 GURL resolved_url; | 53 GURL resolved_url; |
| 158 if (url.SchemeIs("mojo")) { | 54 if (url.SchemeIs("mojo")) { |
| 159 resolved_url = context_->mojo_url_resolver()->Resolve(url); | 55 resolved_url = context_->mojo_url_resolver()->Resolve(url); |
| 160 } else { | 56 } else { |
| 161 resolved_url = url; | 57 resolved_url = url; |
| 162 } | 58 } |
| 163 | 59 |
| 164 Loader* loader; | 60 if (resolved_url.SchemeIsFile()) |
| 165 if (resolved_url.SchemeIsFile()) { | 61 LoadLocalService(resolved_url, callbacks); |
| 166 loader = new LocalLoader(runner.Pass()); | 62 else |
| 167 } else { | 63 LoadNetworkService(resolved_url, callbacks); |
| 168 if (!network_service_) { | 64 } |
| 169 context_->service_manager()->ConnectToService( | 65 |
| 170 GURL("mojo:mojo_network_service"), | 66 void DynamicServiceLoader::LoadLocalService( |
| 171 &network_service_); | 67 const GURL& resolved_url, |
| 172 } | 68 scoped_refptr<LoadServiceCallbacks> callbacks) { |
| 173 loader = new NetworkLoader(runner.Pass(), network_service_.get()); | 69 base::FilePath path; |
| 70 net::FileURLToFilePath(resolved_url, &path); |
| 71 RunLibrary(path, callbacks, base::PathExists(path)); |
| 72 } |
| 73 |
| 74 void DynamicServiceLoader::LoadNetworkService( |
| 75 const GURL& resolved_url, |
| 76 scoped_refptr<LoadServiceCallbacks> callbacks) { |
| 77 if (!network_service_) { |
| 78 context_->service_manager()->ConnectToService( |
| 79 GURL("mojo:mojo_network_service"), |
| 80 &network_service_); |
| 174 } | 81 } |
| 175 loader->Start(resolved_url, shell_handle.Pass(), context_); | 82 if (!url_loader_) |
| 83 network_service_->CreateURLLoader(Get(&url_loader_)); |
| 84 |
| 85 URLRequestPtr request(URLRequest::New()); |
| 86 request->url = String::From(resolved_url); |
| 87 request->auto_follow_redirects = true; |
| 88 |
| 89 if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 90 switches::kDisableCache)) { |
| 91 request->bypass_cache = true; |
| 92 } |
| 93 |
| 94 url_loader_->Start( |
| 95 request.Pass(), |
| 96 base::Bind(&DynamicServiceLoader::OnLoadNetworkServiceComplete, |
| 97 weak_ptr_factory_.GetWeakPtr(), |
| 98 callbacks)); |
| 99 } |
| 100 |
| 101 void DynamicServiceLoader::OnLoadNetworkServiceComplete( |
| 102 scoped_refptr<LoadServiceCallbacks> callbacks, URLResponsePtr response) { |
| 103 if (response->error) { |
| 104 LOG(ERROR) << "Error (" << response->error->code << ": " |
| 105 << response->error->description << ") while fetching " |
| 106 << response->url; |
| 107 } |
| 108 |
| 109 MimeTypeToURLMap::iterator iter = |
| 110 mime_type_to_url_.find(response->mime_type); |
| 111 if (iter != mime_type_to_url_.end()) { |
| 112 callbacks->LoadWithContentHandler(iter->second, response.Pass()); |
| 113 return; |
| 114 } |
| 115 |
| 116 base::FilePath file; |
| 117 base::CreateTemporaryFile(&file); |
| 118 common::CopyToFile(response->body.Pass(), |
| 119 file, |
| 120 context_->task_runners()->blocking_pool(), |
| 121 base::Bind(&DynamicServiceLoader::RunLibrary, |
| 122 weak_ptr_factory_.GetWeakPtr(), |
| 123 file, |
| 124 callbacks)); |
| 125 } |
| 126 |
| 127 void DynamicServiceLoader::RunLibrary( |
| 128 const base::FilePath& path, |
| 129 scoped_refptr<LoadServiceCallbacks> callbacks, |
| 130 bool path_exists) { |
| 131 if (!path_exists) |
| 132 return; |
| 133 |
| 134 ScopedMessagePipeHandle shell_handle = callbacks->RegisterApplication(); |
| 135 if (!shell_handle.is_valid()) |
| 136 return; |
| 137 |
| 138 DynamicServiceRunner* runner = runner_factory_->Create(context_).release(); |
| 139 runner->Start(path, |
| 140 shell_handle.Pass(), |
| 141 base::Bind(&DeleteRunner, base::Unretained(runner))); |
| 176 } | 142 } |
| 177 | 143 |
| 178 void DynamicServiceLoader::OnServiceError(ServiceManager* manager, | 144 void DynamicServiceLoader::OnServiceError(ServiceManager* manager, |
| 179 const GURL& url) { | 145 const GURL& url) { |
| 180 // TODO(darin): What should we do about service errors? This implies that | 146 // TODO(darin): What should we do about service errors? This implies that |
| 181 // the app closed its handle to the service manager. Maybe we don't care? | 147 // the app closed its handle to the service manager. Maybe we don't care? |
| 182 } | 148 } |
| 183 | 149 |
| 184 } // namespace shell | 150 } // namespace shell |
| 185 } // namespace mojo | 151 } // namespace mojo |
| OLD | NEW |