Chromium Code Reviews| Index: mojo/shell/dynamic_service_loader.cc |
| diff --git a/mojo/shell/dynamic_service_loader.cc b/mojo/shell/dynamic_service_loader.cc |
| index a8e6914ae9e5a8bb3cd18ffa4e22dc51fd75b58d..0088c11b264630dafc9abee16c426ddf5f0047b8 100644 |
| --- a/mojo/shell/dynamic_service_loader.cc |
| +++ b/mojo/shell/dynamic_service_loader.cc |
| @@ -20,122 +20,12 @@ |
| namespace mojo { |
| namespace shell { |
| -namespace { |
| - |
| -class Loader { |
| - public: |
| - explicit Loader(scoped_ptr<DynamicServiceRunner> runner) |
| - : runner_(runner.Pass()) { |
| - } |
| - |
| - virtual void Start(const GURL& url, |
| - ScopedMessagePipeHandle service_handle, |
| - Context* context) = 0; |
| - |
| - void StartService(const base::FilePath& path, |
| - ScopedMessagePipeHandle service_handle, |
| - bool path_is_valid) { |
| - if (path_is_valid) { |
| - runner_->Start(path, service_handle.Pass(), |
| - base::Bind(&Loader::AppCompleted, base::Unretained(this))); |
| - } else { |
| - AppCompleted(); |
| - } |
| - } |
| - |
| - protected: |
| - virtual ~Loader() {} |
| - |
| - private: |
| - void AppCompleted() { |
| - delete this; |
| - } |
| - |
| - scoped_ptr<DynamicServiceRunner> runner_; |
| -}; |
| - |
| -// For loading services via file:// URLs. |
| -class LocalLoader : public Loader { |
| - public: |
| - explicit LocalLoader(scoped_ptr<DynamicServiceRunner> runner) |
| - : Loader(runner.Pass()) { |
| - } |
| - |
| - virtual void Start(const GURL& url, |
| - ScopedMessagePipeHandle service_handle, |
| - Context* context) OVERRIDE { |
| - base::FilePath path; |
| - net::FileURLToFilePath(url, &path); |
| - |
| - // Complete asynchronously for consistency with NetworkServiceLoader. |
| - base::MessageLoop::current()->PostTask( |
| - FROM_HERE, |
| - base::Bind(&Loader::StartService, |
| - base::Unretained(this), |
| - path, |
| - base::Passed(&service_handle), |
| - base::PathExists(path))); |
| - } |
| -}; |
| - |
| -// For loading services via the network stack. |
| -class NetworkLoader : public Loader { |
| - public: |
| - explicit NetworkLoader(scoped_ptr<DynamicServiceRunner> runner, |
| - NetworkService* network_service) |
| - : Loader(runner.Pass()) { |
| - network_service->CreateURLLoader(Get(&url_loader_)); |
| - } |
| - |
| - virtual void Start(const GURL& url, |
| - ScopedMessagePipeHandle service_handle, |
| - Context* context) OVERRIDE { |
| - service_handle_ = service_handle.Pass(); |
| - context_ = context; |
| - |
| - URLRequestPtr request(URLRequest::New()); |
| - request->url = String::From(url); |
| - request->auto_follow_redirects = true; |
| - |
| - if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
| - switches::kDisableCache)) { |
| - request->bypass_cache = true; |
| - } |
| - |
| - url_loader_->Start(request.Pass(), |
| - base::Bind(&NetworkLoader::OnReceivedResponse, |
| - base::Unretained(this))); |
| - } |
| - |
| - private: |
| - virtual ~NetworkLoader() { |
| - if (!file_.empty()) |
| - base::DeleteFile(file_, false); |
| - } |
| - void OnReceivedResponse(URLResponsePtr response) { |
| - if (response->error) { |
| - LOG(ERROR) << "Error (" << response->error->code << ": " |
| - << response->error->description << ") while fetching " |
| - << response->url; |
| - } |
| - |
| - base::CreateTemporaryFile(&file_); |
| - common::CopyToFile(response->body.Pass(), |
| - file_, |
| - context_->task_runners()->blocking_pool(), |
| - base::Bind(&Loader::StartService, |
| - base::Unretained(this), |
| - file_, |
| - base::Passed(&service_handle_))); |
| - } |
| +namespace { |
| - Context* context_; |
| - NetworkServicePtr network_service_; |
| - URLLoaderPtr url_loader_; |
| - ScopedMessagePipeHandle service_handle_; |
| - base::FilePath file_; |
| -}; |
| +void DeleteRunner(DynamicServiceRunner* runner) { |
| + delete runner; |
| +} |
| } // namespace |
| @@ -143,17 +33,23 @@ DynamicServiceLoader::DynamicServiceLoader( |
| Context* context, |
| scoped_ptr<DynamicServiceRunnerFactory> runner_factory) |
| : context_(context), |
| - runner_factory_(runner_factory.Pass()) { |
| + runner_factory_(runner_factory.Pass()), |
| + weak_ptr_factory_(this) { |
| } |
| DynamicServiceLoader::~DynamicServiceLoader() { |
| } |
| -void DynamicServiceLoader::LoadService(ServiceManager* manager, |
| - const GURL& url, |
| - ScopedMessagePipeHandle shell_handle) { |
| - scoped_ptr<DynamicServiceRunner> runner = runner_factory_->Create(context_); |
| +void DynamicServiceLoader::set_content_handler( |
| + const std::string& mime_type, |
| + const GURL& content_handler_url) { |
| + content_handler_map_[mime_type] = content_handler_url; |
| +} |
| +void DynamicServiceLoader::LoadService( |
| + ServiceManager* manager, |
| + const GURL& url, |
| + scoped_refptr<LoadServiceCallbacks> callbacks) { |
| GURL resolved_url; |
| if (url.SchemeIs("mojo")) { |
| resolved_url = context_->mojo_url_resolver()->Resolve(url); |
| @@ -161,18 +57,92 @@ void DynamicServiceLoader::LoadService(ServiceManager* manager, |
| resolved_url = url; |
| } |
| - Loader* loader; |
| if (resolved_url.SchemeIsFile()) { |
|
DaveMoore
2014/08/04 21:50:51
Nit: no braces
Aaron Boodman
2014/08/05 05:44:54
Done.
|
| - loader = new LocalLoader(runner.Pass()); |
| + LoadLocalService(resolved_url, callbacks); |
| } else { |
| - if (!network_service_) { |
| - context_->service_manager()->ConnectToService( |
| - GURL("mojo:mojo_network_service"), |
| - &network_service_); |
| - } |
| - loader = new NetworkLoader(runner.Pass(), network_service_.get()); |
| + LoadNetworkService(resolved_url, callbacks); |
| + } |
| +} |
| + |
| +void DynamicServiceLoader::LoadLocalService( |
| + const GURL& resolved_url, |
| + scoped_refptr<LoadServiceCallbacks> callbacks) { |
| + base::FilePath path; |
| + net::FileURLToFilePath(resolved_url, &path); |
| + RunLibrary(path, callbacks, base::PathExists(path)); |
| +} |
| + |
| +void DynamicServiceLoader::LoadNetworkService( |
| + const GURL& resolved_url, |
| + scoped_refptr<LoadServiceCallbacks> callbacks) { |
| + if (!network_service_) { |
| + context_->service_manager()->ConnectToService( |
| + GURL("mojo:mojo_network_service"), |
| + &network_service_); |
| } |
| - loader->Start(resolved_url, shell_handle.Pass(), context_); |
| + if (!url_loader_) { |
|
DaveMoore
2014/08/04 21:50:51
Nit: no braces
Aaron Boodman
2014/08/05 05:44:54
Done.
|
| + network_service_->CreateURLLoader(Get(&url_loader_)); |
| + } |
| + |
| + URLRequestPtr request(URLRequest::New()); |
| + request->url = String::From(resolved_url); |
| + request->auto_follow_redirects = true; |
| + |
| + if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
| + switches::kDisableCache)) { |
| + request->bypass_cache = true; |
| + } |
| + |
| + url_loader_->Start( |
| + request.Pass(), |
| + base::Bind(&DynamicServiceLoader::OnLoadNetworkServiceComplete, |
| + weak_ptr_factory_.GetWeakPtr(), |
| + callbacks)); |
| +} |
| + |
| +void DynamicServiceLoader::OnLoadNetworkServiceComplete( |
| + scoped_refptr<LoadServiceCallbacks> callbacks, URLResponsePtr response) { |
| + if (response->error) { |
| + LOG(ERROR) << "Error (" << response->error->code << ": " |
| + << response->error->description << ") while fetching " |
| + << response->url; |
| + } |
| + |
| + ContentHandlerMap::iterator iter = |
| + content_handler_map_.find(response->mime_type); |
| + if (iter != content_handler_map_.end()) { |
| + callbacks->LoadWithContentHandler(iter->second, response.Pass()); |
| + return; |
| + } |
| + |
| + base::FilePath file; |
| + base::CreateTemporaryFile(&file); |
| + common::CopyToFile(response->body.Pass(), |
| + file, |
| + context_->task_runners()->blocking_pool(), |
| + base::Bind(&DynamicServiceLoader::RunLibrary, |
| + weak_ptr_factory_.GetWeakPtr(), |
| + file, |
| + callbacks)); |
| +} |
| + |
| +void DynamicServiceLoader::RunLibrary( |
| + const base::FilePath& path, |
| + scoped_refptr<LoadServiceCallbacks> callbacks, |
| + bool path_exists) { |
| + if (!path_exists) { |
|
DaveMoore
2014/08/04 21:50:52
Nit: no braces
Aaron Boodman
2014/08/05 05:44:55
Done.
|
| + return; |
| + } |
| + |
| + ScopedMessagePipeHandle shell_handle = callbacks->RegisterApplication(); |
| + if (!shell_handle.is_valid()) { |
|
DaveMoore
2014/08/04 21:50:52
Nit: no braces
Aaron Boodman
2014/08/05 05:44:54
Done.
|
| + return; |
| + } |
| + |
| + DynamicServiceRunner* runner = runner_factory_->Create(context_).release(); |
| + runner->Start(path, |
| + shell_handle.Pass(), |
| + base::Bind(&DeleteRunner, base::Unretained(runner))); |
| } |
| void DynamicServiceLoader::OnServiceError(ServiceManager* manager, |