Index: mojo/shell/service_manager.cc |
diff --git a/mojo/shell/service_manager.cc b/mojo/shell/service_manager.cc |
index cb774ae0bdbdbe60866483ff78e5993be97f1d58..cea78daade0ffbcb55fbb709a115b58337ff76f1 100644 |
--- a/mojo/shell/service_manager.cc |
+++ b/mojo/shell/service_manager.cc |
@@ -4,19 +4,10 @@ |
#include "mojo/shell/service_manager.h" |
-#include "base/bind.h" |
-#include "base/callback_helpers.h" |
-#include "base/command_line.h" |
-#include "base/file_util.h" |
-#include "base/scoped_native_library.h" |
-#include "base/threading/simple_thread.h" |
+#include "base/logging.h" |
#include "mojo/public/bindings/lib/remote_ptr.h" |
-#include "mojo/shell/context.h" |
-#include "mojo/shell/switches.h" |
#include "mojom/shell.h" |
-typedef MojoResult (*MojoMainFunction)(MojoHandle pipe); |
- |
namespace mojo { |
namespace shell { |
@@ -28,9 +19,8 @@ class ServiceManager::Service : public ShellStub { |
MessagePipe pipe; |
shell_client_.reset(pipe.handle0.Pass()); |
shell_client_.SetPeer(this); |
- manager_->GetLoaderForURL(url)->Load(url, manager_, pipe.handle1.Pass()); |
+ manager_->GetLoaderForURL(url)->Load(url, pipe.handle1.Pass()); |
} |
- |
virtual ~Service() {} |
void ConnectToClient(ScopedMessagePipeHandle handle) { |
@@ -50,128 +40,18 @@ class ServiceManager::Service : public ShellStub { |
DISALLOW_COPY_AND_ASSIGN(Service); |
}; |
-class ServiceManager::DynamicLoader : public ServiceManager::Loader { |
- public: |
- explicit DynamicLoader(ServiceManager* manager) : manager_(manager) {} |
- virtual ~DynamicLoader() {} |
- |
- private: |
- class Context : public mojo::shell::Loader::Delegate, |
- public base::DelegateSimpleThread::Delegate { |
- public: |
- Context(DynamicLoader* loader, |
- const GURL& url, |
- ScopedMessagePipeHandle service_handle) |
- : loader_(loader), |
- url_(url), |
- service_handle_(service_handle.Pass()), |
- weak_factory_(this) { |
- url_ = url; |
- if (url.scheme() == "mojo") { |
- const CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
- std::string origin = |
- command_line.GetSwitchValueASCII(switches::kOrigin); |
-#if defined(OS_WIN) |
- std::string lib(url.ExtractFileName() + ".dll"); |
-#elif defined(OS_LINUX) |
- std::string lib("lib" + url.ExtractFileName() + ".so"); |
-#elif defined(OS_MACOSX) |
- std::string lib("lib" + url.ExtractFileName() + ".dylib"); |
-#else |
- std::string lib; |
- NOTREACHED() << "dynamic loading of services not supported"; |
- return; |
-#endif |
- url_ = GURL(origin + std::string("/") + lib); |
- } |
- request_ = loader_->manager_->context_->loader()->Load(url_, this); |
- } |
- |
- private: |
- friend class base::WeakPtrFactory<Context>; |
- |
- // From Loader::Delegate. |
- virtual void DidCompleteLoad(const GURL& app_url, |
- const base::FilePath& app_path) OVERRIDE { |
- app_path_ = app_path; |
- ack_closure_ = |
- base::Bind(&ServiceManager::DynamicLoader::Context::AppCompleted, |
- weak_factory_.GetWeakPtr()); |
- thread_.reset(new base::DelegateSimpleThread(this, "app_thread")); |
- thread_->Start(); |
- } |
- |
- // From base::DelegateSimpleThread::Delegate. |
- virtual void Run() OVERRIDE { |
- base::ScopedClosureRunner app_deleter( |
- base::Bind(base::IgnoreResult(&base::DeleteFile), app_path_, false)); |
- std::string load_error; |
- base::ScopedNativeLibrary app_library( |
- base::LoadNativeLibrary(app_path_, &load_error)); |
- if (!app_library.is_valid()) { |
- LOG(ERROR) << "Failed to load library: " << app_path_.value().c_str(); |
- LOG(ERROR) << "error: " << load_error; |
- return; |
- } |
- |
- MojoMainFunction main_function = reinterpret_cast<MojoMainFunction>( |
- app_library.GetFunctionPointer("MojoMain")); |
- if (!main_function) { |
- LOG(ERROR) << "Entrypoint MojoMain not found."; |
- return; |
- } |
- |
- MojoHandle handle = service_handle_.release().value(); |
- // |MojoMain()| takes ownership of the app handle. |
- MojoResult result = main_function(handle); |
- if (result < MOJO_RESULT_OK) { |
- LOG(ERROR) << "MojoMain returned an error: " << result; |
- return; |
- } |
- loader_->manager_->context_->task_runners()->ui_runner()->PostTask( |
- FROM_HERE, |
- ack_closure_); |
- } |
- |
- void AppCompleted() { |
- thread_->Join(); |
- thread_.reset(); |
- loader_->url_to_context_.erase(url_); |
- delete this; |
- } |
- |
- DynamicLoader* loader_; |
- GURL url_; |
- base::FilePath app_path_; |
- base::Closure ack_closure_; |
- scoped_ptr<mojo::shell::Loader::Job> request_; |
- scoped_ptr<base::DelegateSimpleThread> thread_; |
- ScopedMessagePipeHandle service_handle_; |
- base::WeakPtrFactory<Context> weak_factory_; |
- }; |
- |
- virtual void Load(const GURL& url, |
- ServiceManager* manager, |
- ScopedMessagePipeHandle service_handle) |
- MOJO_OVERRIDE { |
- DCHECK(url_to_context_.find(url) == url_to_context_.end()); |
- url_to_context_[url] = new Context(this, url, service_handle.Pass()); |
- } |
- |
- typedef std::map<GURL, Context*> ContextMap; |
- ContextMap url_to_context_; |
- ServiceManager* manager_; |
-}; |
- |
ServiceManager::Loader::Loader() {} |
ServiceManager::Loader::~Loader() {} |
-ServiceManager::ServiceManager(Context* context) |
- : context_(context), |
- default_loader_(new DynamicLoader(this)) { |
+ServiceManager::ServiceManager() : default_loader_(NULL) { |
} |
ServiceManager::~ServiceManager() { |
+ for (ServiceMap::iterator it = url_to_service_.begin(); |
+ it != url_to_service_.end(); ++it) { |
+ delete it->second; |
+ } |
+ url_to_service_.clear(); |
} |
void ServiceManager::SetLoaderForURL(Loader* loader, const GURL& gurl) { |
@@ -183,7 +63,8 @@ ServiceManager::Loader* ServiceManager::GetLoaderForURL(const GURL& gurl) { |
LoaderMap::const_iterator it = url_to_loader_.find(gurl); |
if (it != url_to_loader_.end()) |
return it->second; |
- return default_loader_.get(); |
+ DCHECK(default_loader_); |
+ return default_loader_; |
} |
void ServiceManager::Connect(const GURL& url, |