Index: mojo/shell/dynamic_application_loader.cc |
diff --git a/mojo/shell/dynamic_application_loader.cc b/mojo/shell/dynamic_application_loader.cc |
index 1bccd58182cc739f4a62ba8a5e8f067d97a618d1..88c8d40e952648261ba436873ccc906732d2e214 100644 |
--- a/mojo/shell/dynamic_application_loader.cc |
+++ b/mojo/shell/dynamic_application_loader.cc |
@@ -20,6 +20,130 @@ |
namespace mojo { |
namespace shell { |
+namespace { |
+ |
+class Loader { |
+ public: |
+ Loader(scoped_refptr<ApplicationLoader::LoadCallbacks> callbacks, |
+ scoped_ptr<DynamicServiceRunner> runner) |
+ : callbacks_(callbacks), |
+ runner_(runner.Pass()) { |
+ } |
+ |
+ virtual ~Loader() { |
+ } |
+ |
+ protected: |
+ void RunLibrary(const base::FilePath& path, bool path_exists) { |
+ ScopedMessagePipeHandle shell_handle = callbacks_->RegisterApplication(); |
+ if (!shell_handle.is_valid()) |
+ return; |
+ |
+ if (!path_exists) { |
+ DVLOG(1) << "Library not started because library path '" |
+ << path.value() << "' does not exist."; |
+ return; |
+ } |
+ |
+ runner_->Start(path, |
+ shell_handle.Pass(), |
+ base::Bind(&Loader::AppCompleted, |
+ base::Unretained(this))); |
+ } |
+ |
+ void AppCompleted() { |
+ delete this; |
+ } |
+ |
+ scoped_refptr<ApplicationLoader::LoadCallbacks> callbacks_; |
+ scoped_ptr<DynamicServiceRunner> runner_; |
+}; |
+ |
+class LocalLoader : public Loader { |
+ public: |
+ LocalLoader(const GURL& url, |
+ scoped_refptr<ApplicationLoader::LoadCallbacks> callbacks, |
+ scoped_ptr<DynamicServiceRunner> runner) |
+ : Loader(callbacks, runner.Pass()) { |
+ base::FilePath path; |
+ net::FileURLToFilePath(url, &path); |
+ |
+ // Async for consistency with network case. |
+ base::MessageLoop::current()->PostTask( |
+ FROM_HERE, |
+ base::Bind(&LocalLoader::RunLibrary, |
+ base::Unretained(this), |
+ path, |
+ base::PathExists(path))); |
+ } |
+}; |
+ |
+class NetworkLoader : public Loader { |
+ public: |
+ NetworkLoader(NetworkService* network_service, |
+ const GURL& url, |
+ DynamicApplicationLoader::MimeTypeToURLMap mime_type_to_url, |
+ scoped_refptr<ApplicationLoader::LoadCallbacks> callbacks, |
+ scoped_refptr<base::SequencedWorkerPool> blocking_pool, |
+ scoped_ptr<DynamicServiceRunner> runner) |
+ : Loader(callbacks, runner.Pass()), |
+ mime_type_to_url_(mime_type_to_url), |
+ blocking_pool_(blocking_pool) { |
+ 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; |
+ } |
+ |
+ network_service->CreateURLLoader(Get(&url_loader_)); |
+ url_loader_->Start(request.Pass(), |
+ base::Bind(&NetworkLoader::OnLoadComplete, |
+ base::Unretained(this))); // Passed() ? |
+ } |
+ |
+ virtual ~NetworkLoader() { |
+ if (!file_.empty()) |
+ base::DeleteFile(file_, false); |
+ } |
+ |
+ private: |
+ void OnLoadComplete(URLResponsePtr response) { |
+ if (response->error) { |
+ LOG(ERROR) << "Error (" << response->error->code << ": " |
+ << response->error->description << ") while fetching " |
+ << response->url; |
+ } |
+ |
+ DynamicApplicationLoader::MimeTypeToURLMap::iterator iter = |
+ mime_type_to_url_.find(response->mime_type); |
+ if (iter != mime_type_to_url_.end()) { |
+ callbacks_->LoadWithContentHandler(iter->second, |
+ response.Pass(), |
+ url_loader_.Pass()); |
+ delete this; |
+ return; |
+ } |
+ |
+ base::CreateTemporaryFile(&file_); |
+ common::CopyToFile(response->body.Pass(), |
+ file_, |
+ blocking_pool_, |
+ base::Bind(&NetworkLoader::RunLibrary, |
+ base::Unretained(this), |
+ file_)); |
+ } |
+ |
+ DynamicApplicationLoader::MimeTypeToURLMap mime_type_to_url_; |
+ URLLoaderPtr url_loader_; |
+ base::FilePath file_; |
+ scoped_refptr<base::SequencedWorkerPool> blocking_pool_; |
+}; |
+ |
+} // namespace |
+ |
DynamicApplicationLoader::DynamicApplicationLoader( |
Context* context, |
scoped_ptr<DynamicServiceRunnerFactory> runner_factory) |
@@ -56,19 +180,7 @@ void DynamicApplicationLoader::Load(ApplicationManager* manager, |
void DynamicApplicationLoader::LoadLocalService( |
const GURL& resolved_url, |
scoped_refptr<LoadCallbacks> callbacks) { |
- base::FilePath path; |
- net::FileURLToFilePath(resolved_url, &path); |
- const bool kDeleteFileAfter = false; |
- |
- // Async for consistency with network case. |
- base::MessageLoop::current()->PostTask( |
- FROM_HERE, |
- base::Bind(&DynamicApplicationLoader::RunLibrary, |
- weak_ptr_factory_.GetWeakPtr(), |
- path, |
- callbacks, |
- kDeleteFileAfter, |
- base::PathExists(path))); |
+ new LocalLoader(resolved_url, callbacks, runner_factory_->Create(context_)); |
} |
void DynamicApplicationLoader::LoadNetworkService( |
@@ -78,93 +190,13 @@ void DynamicApplicationLoader::LoadNetworkService( |
context_->application_manager()->ConnectToService( |
GURL("mojo:mojo_network_service"), &network_service_); |
} |
- if (!url_loader_) |
- 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(&DynamicApplicationLoader::OnLoadNetworkServiceComplete, |
- weak_ptr_factory_.GetWeakPtr(), |
- callbacks)); |
-} |
- |
-void DynamicApplicationLoader::OnLoadNetworkServiceComplete( |
- scoped_refptr<LoadCallbacks> callbacks, |
- URLResponsePtr response) { |
- if (response->error) { |
- LOG(ERROR) << "Error (" << response->error->code << ": " |
- << response->error->description << ") while fetching " |
- << response->url; |
- } |
- |
- MimeTypeToURLMap::iterator iter = mime_type_to_url_.find(response->mime_type); |
- if (iter != mime_type_to_url_.end()) { |
- callbacks->LoadWithContentHandler(iter->second, response.Pass()); |
- return; |
- } |
- |
- base::FilePath file; |
- base::CreateTemporaryFile(&file); |
- |
- const bool kDeleteFileAfter = true; |
- common::CopyToFile(response->body.Pass(), |
- file, |
- context_->task_runners()->blocking_pool(), |
- base::Bind(&DynamicApplicationLoader::RunLibrary, |
- weak_ptr_factory_.GetWeakPtr(), |
- file, |
- callbacks, |
- kDeleteFileAfter)); |
-} |
- |
-void DynamicApplicationLoader::RunLibrary( |
- const base::FilePath& path, |
- scoped_refptr<LoadCallbacks> callbacks, |
- bool delete_file_after, |
- bool path_exists) { |
- |
- ScopedMessagePipeHandle shell_handle = callbacks->RegisterApplication(); |
- if (!shell_handle.is_valid()) |
- return; |
- |
- if (!path_exists) { |
- DVLOG(1) << "Library not started because library path '" |
- << path.value() << "' does not exist."; |
- return; |
- } |
- |
- scoped_ptr<DynamicServiceRunner> runner = |
- runner_factory_->Create(context_).Pass(); |
- runner->Start(path, |
- shell_handle.Pass(), |
- base::Bind(&DynamicApplicationLoader::OnRunLibraryComplete, |
- weak_ptr_factory_.GetWeakPtr(), |
- base::Unretained(runner.get()), |
- delete_file_after ? path : base::FilePath())); |
- runners_.push_back(runner.release()); |
-} |
-void DynamicApplicationLoader::OnRunLibraryComplete( |
- DynamicServiceRunner* runner, |
- const base::FilePath& temp_file) { |
- for (ScopedVector<DynamicServiceRunner>::iterator it = runners_.begin(); |
- it != runners_.end(); ++it) { |
- if (*it == runner) { |
- runners_.erase(it); |
- if (!temp_file.empty()) |
- base::DeleteFile(temp_file, false); |
- return; |
- } |
- } |
+ new NetworkLoader(network_service_.get(), |
+ resolved_url, |
+ mime_type_to_url_, |
+ callbacks, |
+ context_->task_runners()->blocking_pool(), |
+ runner_factory_->Create(context_)); |
} |
void DynamicApplicationLoader::OnServiceError(ApplicationManager* manager, |