Commit 54efb235 authored by aa's avatar aa Committed by Commit bot

Mojo: Fix two bugs in content handling

1. URLLoaders can't be reused, so we need to construct one
   per-load. This became too gnarly to handle with callbacks
   so I put it back to having a loader object that owns the
   URLLoader.

2. The relevant URLLoader needs to be passed to
   ContentHandler::OnConnect() so that it stays alive long
   enough to read the entire response.

BUG=

Review URL: https://codereview.chromium.org/513573002

Cr-Commit-Position: refs/heads/master@{#292460}
parent 82e8b989
...@@ -23,7 +23,7 @@ ApplicationLoader::SimpleLoadCallbacks::RegisterApplication() { ...@@ -23,7 +23,7 @@ ApplicationLoader::SimpleLoadCallbacks::RegisterApplication() {
void ApplicationLoader::SimpleLoadCallbacks::LoadWithContentHandler( void ApplicationLoader::SimpleLoadCallbacks::LoadWithContentHandler(
const GURL& content_handle_url, const GURL& content_handle_url,
URLResponsePtr content) { URLResponsePtr url_response) {
NOTREACHED(); NOTREACHED();
} }
......
...@@ -34,7 +34,7 @@ class MOJO_APPLICATION_MANAGER_EXPORT ApplicationLoader { ...@@ -34,7 +34,7 @@ class MOJO_APPLICATION_MANAGER_EXPORT ApplicationLoader {
// Load the requested application with a content handler. // Load the requested application with a content handler.
virtual void LoadWithContentHandler(const GURL& content_handler_url, virtual void LoadWithContentHandler(const GURL& content_handler_url,
URLResponsePtr response) = 0; URLResponsePtr url_response) = 0;
protected: protected:
friend base::RefCounted<LoadCallbacks>; friend base::RefCounted<LoadCallbacks>;
......
...@@ -66,12 +66,12 @@ class ApplicationManager::LoadCallbacksImpl ...@@ -66,12 +66,12 @@ class ApplicationManager::LoadCallbacksImpl
} }
virtual void LoadWithContentHandler(const GURL& content_handler_url, virtual void LoadWithContentHandler(const GURL& content_handler_url,
URLResponsePtr content) OVERRIDE { URLResponsePtr url_response) OVERRIDE {
if (manager_) { if (manager_) {
manager_->LoadWithContentHandler(requested_url_, manager_->LoadWithContentHandler(requested_url_,
requestor_url_, requestor_url_,
content_handler_url, content_handler_url,
content.Pass(), url_response.Pass(),
service_provider_.Pass()); service_provider_.Pass());
} }
} }
...@@ -232,7 +232,7 @@ void ApplicationManager::LoadWithContentHandler( ...@@ -232,7 +232,7 @@ void ApplicationManager::LoadWithContentHandler(
const GURL& content_url, const GURL& content_url,
const GURL& requestor_url, const GURL& requestor_url,
const GURL& content_handler_url, const GURL& content_handler_url,
URLResponsePtr content, URLResponsePtr url_response,
ServiceProviderPtr service_provider) { ServiceProviderPtr service_provider) {
ContentHandlerConnection* connection = NULL; ContentHandlerConnection* connection = NULL;
URLToContentHandlerMap::iterator iter = URLToContentHandlerMap::iterator iter =
...@@ -243,8 +243,11 @@ void ApplicationManager::LoadWithContentHandler( ...@@ -243,8 +243,11 @@ void ApplicationManager::LoadWithContentHandler(
connection = new ContentHandlerConnection(this, content_handler_url); connection = new ContentHandlerConnection(this, content_handler_url);
url_to_content_handler_[content_handler_url] = connection; url_to_content_handler_[content_handler_url] = connection;
} }
InterfaceRequest<ServiceProvider> spir;
spir.Bind(service_provider.PassMessagePipe());
connection->content_handler->OnConnect( connection->content_handler->OnConnect(
content_url.spec(), content.Pass(), service_provider.Pass()); content_url.spec(), url_response.Pass(), spir.Pass());
} }
void ApplicationManager::SetLoaderForURL(scoped_ptr<ApplicationLoader> loader, void ApplicationManager::SetLoaderForURL(scoped_ptr<ApplicationLoader> loader,
......
...@@ -123,7 +123,7 @@ class MOJO_APPLICATION_MANAGER_EXPORT ApplicationManager { ...@@ -123,7 +123,7 @@ class MOJO_APPLICATION_MANAGER_EXPORT ApplicationManager {
void LoadWithContentHandler(const GURL& content_url, void LoadWithContentHandler(const GURL& content_url,
const GURL& requestor_url, const GURL& requestor_url,
const GURL& content_handler_url, const GURL& content_handler_url,
URLResponsePtr content, URLResponsePtr url_response,
ServiceProviderPtr service_provider); ServiceProviderPtr service_provider);
// Returns the Loader to use for a url (using default if not overridden.) // Returns the Loader to use for a url (using default if not overridden.)
......
...@@ -23,8 +23,9 @@ class ContentHandlerImpl : public InterfaceImpl<ContentHandler> { ...@@ -23,8 +23,9 @@ class ContentHandlerImpl : public InterfaceImpl<ContentHandler> {
private: private:
virtual void OnConnect(const mojo::String& url, virtual void OnConnect(const mojo::String& url,
URLResponsePtr content, URLResponsePtr response,
ServiceProviderPtr service_provider) MOJO_OVERRIDE; InterfaceRequest<ServiceProvider> service_provider)
MOJO_OVERRIDE;
ContentHandlerApp* content_handler_app_; ContentHandlerApp* content_handler_app_;
}; };
...@@ -71,12 +72,13 @@ class ContentHandlerApp : public ApplicationDelegate { ...@@ -71,12 +72,13 @@ class ContentHandlerApp : public ApplicationDelegate {
ContentHandlerApp> content_handler_factory_; ContentHandlerApp> content_handler_factory_;
}; };
void ContentHandlerImpl::OnConnect(const mojo::String& url, void ContentHandlerImpl::OnConnect(
URLResponsePtr content, const mojo::String& url,
ServiceProviderPtr service_provider) { URLResponsePtr response,
InterfaceRequest<ServiceProvider> service_provider) {
printf("ContentHandler::OnConnect - url:%s - body follows\n\n", printf("ContentHandler::OnConnect - url:%s - body follows\n\n",
url.To<std::string>().c_str()); url.To<std::string>().c_str());
content_handler_app_->PrintResponse(content->body.Pass()); content_handler_app_->PrintResponse(response->body.Pass());
} }
} // namespace examples } // namespace examples
......
...@@ -8,9 +8,9 @@ import "mojo/services/public/interfaces/network/url_loader.mojom" ...@@ -8,9 +8,9 @@ import "mojo/services/public/interfaces/network/url_loader.mojom"
module mojo { module mojo {
interface ContentHandler { interface ContentHandler {
OnConnect(string? url, OnConnect(string url,
URLResponse? url_response, URLResponse response,
ServiceProvider? service_provider); ServiceProvider&? service_provider);
}; };
} }
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
#include "base/file_util.h" #include "base/file_util.h"
#include "base/files/file_path.h" #include "base/files/file_path.h"
#include "base/memory/scoped_ptr.h" #include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/message_loop/message_loop.h" #include "base/message_loop/message_loop.h"
#include "mojo/common/common_type_converters.h" #include "mojo/common/common_type_converters.h"
#include "mojo/common/data_pipe_utils.h" #include "mojo/common/data_pipe_utils.h"
...@@ -20,69 +21,113 @@ ...@@ -20,69 +21,113 @@
namespace mojo { namespace mojo {
namespace shell { namespace shell {
DynamicApplicationLoader::DynamicApplicationLoader( // Encapsulates loading and running one individual application.
Context* context, //
scoped_ptr<DynamicServiceRunnerFactory> runner_factory) // Loaders are owned by DynamicApplicationLoader. DynamicApplicationLoader must
: context_(context), // ensure that all the parameters passed to Loader subclasses stay valid through
runner_factory_(runner_factory.Pass()), // Loader's lifetime.
weak_ptr_factory_(this) { //
} // Async operations are done with WeakPtr to protect against
// DynamicApplicationLoader going away (and taking all the Loaders with it)
DynamicApplicationLoader::~DynamicApplicationLoader() { // while the async operation is outstanding.
} class DynamicApplicationLoader::Loader {
public:
Loader(Context* context,
DynamicServiceRunnerFactory* runner_factory,
scoped_refptr<ApplicationLoader::LoadCallbacks> load_callbacks,
const LoaderCompleteCallback& loader_complete_callback)
: load_callbacks_(load_callbacks),
loader_complete_callback_(loader_complete_callback),
context_(context),
runner_factory_(runner_factory),
weak_ptr_factory_(this) {}
virtual ~Loader() {}
protected:
void RunLibrary(const base::FilePath& path, bool path_exists) {
ScopedMessagePipeHandle shell_handle =
load_callbacks_->RegisterApplication();
if (!shell_handle.is_valid()) {
LoaderComplete();
return;
}
void DynamicApplicationLoader::RegisterContentHandler( if (!path_exists) {
const std::string& mime_type, DVLOG(1) << "Library not started because library path '" << path.value()
const GURL& content_handler_url) { << "' does not exist.";
mime_type_to_url_[mime_type] = content_handler_url; LoaderComplete();
} return;
}
void DynamicApplicationLoader::Load(ApplicationManager* manager, runner_ = runner_factory_->Create(context_);
const GURL& url, runner_->Start(
scoped_refptr<LoadCallbacks> callbacks) { path,
GURL resolved_url; shell_handle.Pass(),
if (url.SchemeIs("mojo")) { base::Bind(&Loader::LoaderComplete, weak_ptr_factory_.GetWeakPtr()));
resolved_url = context_->mojo_url_resolver()->Resolve(url);
} else {
resolved_url = url;
} }
if (resolved_url.SchemeIsFile()) void LoaderComplete() { loader_complete_callback_.Run(this); }
LoadLocalService(resolved_url, callbacks);
else scoped_refptr<ApplicationLoader::LoadCallbacks> load_callbacks_;
LoadNetworkService(resolved_url, callbacks); LoaderCompleteCallback loader_complete_callback_;
} Context* context_;
private:
DynamicServiceRunnerFactory* runner_factory_;
scoped_ptr<DynamicServiceRunner> runner_;
base::WeakPtrFactory<Loader> weak_ptr_factory_;
};
void DynamicApplicationLoader::LoadLocalService( // A loader for local files.
const GURL& resolved_url, class DynamicApplicationLoader::LocalLoader : public Loader {
scoped_refptr<LoadCallbacks> callbacks) { public:
LocalLoader(const GURL& url,
Context* context,
DynamicServiceRunnerFactory* runner_factory,
scoped_refptr<ApplicationLoader::LoadCallbacks> load_callbacks,
const LoaderCompleteCallback& loader_complete_callback)
: Loader(context,
runner_factory,
load_callbacks,
loader_complete_callback),
weak_ptr_factory_(this) {
base::FilePath path; base::FilePath path;
net::FileURLToFilePath(resolved_url, &path); net::FileURLToFilePath(url, &path);
const bool kDeleteFileAfter = false;
// Async for consistency with network case. // Async for consistency with network case.
base::MessageLoop::current()->PostTask( base::MessageLoop::current()->PostTask(
FROM_HERE, FROM_HERE,
base::Bind(&DynamicApplicationLoader::RunLibrary, base::Bind(&LocalLoader::RunLibrary,
weak_ptr_factory_.GetWeakPtr(), weak_ptr_factory_.GetWeakPtr(),
path, path,
callbacks,
kDeleteFileAfter,
base::PathExists(path))); base::PathExists(path)));
}
void DynamicApplicationLoader::LoadNetworkService(
const GURL& resolved_url,
scoped_refptr<LoadCallbacks> callbacks) {
if (!network_service_) {
context_->application_manager()->ConnectToService(
GURL("mojo:mojo_network_service"), &network_service_);
} }
if (!url_loader_)
network_service_->CreateURLLoader(Get(&url_loader_));
virtual ~LocalLoader() {}
private:
base::WeakPtrFactory<LocalLoader> weak_ptr_factory_;
};
// A loader for network files.
class DynamicApplicationLoader::NetworkLoader : public Loader {
public:
NetworkLoader(const GURL& url,
MimeTypeToURLMap* mime_type_to_url,
Context* context,
DynamicServiceRunnerFactory* runner_factory,
NetworkService* network_service,
scoped_refptr<ApplicationLoader::LoadCallbacks> load_callbacks,
const LoaderCompleteCallback& loader_complete_callback)
: Loader(context,
runner_factory,
load_callbacks,
loader_complete_callback),
mime_type_to_url_(mime_type_to_url),
weak_ptr_factory_(this) {
URLRequestPtr request(URLRequest::New()); URLRequestPtr request(URLRequest::New());
request->url = String::From(resolved_url); request->url = String::From(url);
request->auto_follow_redirects = true; request->auto_follow_redirects = true;
if (base::CommandLine::ForCurrentProcess()->HasSwitch( if (base::CommandLine::ForCurrentProcess()->HasSwitch(
...@@ -90,81 +135,104 @@ void DynamicApplicationLoader::LoadNetworkService( ...@@ -90,81 +135,104 @@ void DynamicApplicationLoader::LoadNetworkService(
request->bypass_cache = true; request->bypass_cache = true;
} }
url_loader_->Start( network_service->CreateURLLoader(Get(&url_loader_));
request.Pass(), url_loader_->Start(request.Pass(),
base::Bind(&DynamicApplicationLoader::OnLoadNetworkServiceComplete, base::Bind(&NetworkLoader::OnLoadComplete,
weak_ptr_factory_.GetWeakPtr(), weak_ptr_factory_.GetWeakPtr()));
callbacks)); }
}
virtual ~NetworkLoader() {
if (!file_.empty())
base::DeleteFile(file_, false);
}
void DynamicApplicationLoader::OnLoadNetworkServiceComplete( private:
scoped_refptr<LoadCallbacks> callbacks, void OnLoadComplete(URLResponsePtr response) {
URLResponsePtr response) {
if (response->error) { if (response->error) {
LOG(ERROR) << "Error (" << response->error->code << ": " LOG(ERROR) << "Error (" << response->error->code << ": "
<< response->error->description << ") while fetching " << response->error->description << ") while fetching "
<< response->url; << response->url;
LoaderComplete();
return;
} }
MimeTypeToURLMap::iterator iter = mime_type_to_url_.find(response->mime_type); MimeTypeToURLMap::iterator iter =
if (iter != mime_type_to_url_.end()) { mime_type_to_url_->find(response->mime_type);
callbacks->LoadWithContentHandler(iter->second, response.Pass()); if (iter != mime_type_to_url_->end()) {
load_callbacks_->LoadWithContentHandler(iter->second, response.Pass());
LoaderComplete();
return; return;
} }
base::FilePath file; base::CreateTemporaryFile(&file_);
base::CreateTemporaryFile(&file); common::CopyToFile(
response->body.Pass(),
const bool kDeleteFileAfter = true; file_,
common::CopyToFile(response->body.Pass(),
file,
context_->task_runners()->blocking_pool(), context_->task_runners()->blocking_pool(),
base::Bind(&DynamicApplicationLoader::RunLibrary, base::Bind(
weak_ptr_factory_.GetWeakPtr(), &NetworkLoader::RunLibrary, weak_ptr_factory_.GetWeakPtr(), file_));
file, }
callbacks,
kDeleteFileAfter));
}
void DynamicApplicationLoader::RunLibrary( MimeTypeToURLMap* mime_type_to_url_;
const base::FilePath& path, URLLoaderPtr url_loader_;
scoped_refptr<LoadCallbacks> callbacks, base::FilePath file_;
bool delete_file_after, base::WeakPtrFactory<NetworkLoader> weak_ptr_factory_;
bool path_exists) { };
ScopedMessagePipeHandle shell_handle = callbacks->RegisterApplication(); DynamicApplicationLoader::DynamicApplicationLoader(
if (!shell_handle.is_valid()) Context* context,
return; scoped_ptr<DynamicServiceRunnerFactory> runner_factory)
: context_(context),
runner_factory_(runner_factory.Pass()),
if (!path_exists) { // Unretained() is correct here because DynamicApplicationLoader owns the
DVLOG(1) << "Library not started because library path '" // loaders that we pass this callback to.
<< path.value() << "' does not exist."; loader_complete_callback_(
return; base::Bind(&DynamicApplicationLoader::LoaderComplete,
} base::Unretained(this))) {
}
scoped_ptr<DynamicServiceRunner> runner = DynamicApplicationLoader::~DynamicApplicationLoader() {
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( void DynamicApplicationLoader::RegisterContentHandler(
DynamicServiceRunner* runner, const std::string& mime_type,
const base::FilePath& temp_file) { const GURL& content_handler_url) {
for (ScopedVector<DynamicServiceRunner>::iterator it = runners_.begin(); mime_type_to_url_[mime_type] = content_handler_url;
it != runners_.end(); ++it) { }
if (*it == runner) {
runners_.erase(it); void DynamicApplicationLoader::Load(
if (!temp_file.empty()) ApplicationManager* manager,
base::DeleteFile(temp_file, false); const GURL& url,
scoped_refptr<LoadCallbacks> load_callbacks) {
GURL resolved_url;
if (url.SchemeIs("mojo")) {
resolved_url = context_->mojo_url_resolver()->Resolve(url);
} else {
resolved_url = url;
}
if (resolved_url.SchemeIsFile()) {
loaders_.push_back(new LocalLoader(resolved_url,
context_,
runner_factory_.get(),
load_callbacks,
loader_complete_callback_));
return; return;
} }
if (!network_service_) {
context_->application_manager()->ConnectToService(
GURL("mojo:mojo_network_service"), &network_service_);
} }
loaders_.push_back(new NetworkLoader(resolved_url,
&mime_type_to_url_,
context_,
runner_factory_.get(),
network_service_.get(),
load_callbacks,
loader_complete_callback_));
} }
void DynamicApplicationLoader::OnApplicationError(ApplicationManager* manager, void DynamicApplicationLoader::OnApplicationError(ApplicationManager* manager,
...@@ -173,5 +241,9 @@ void DynamicApplicationLoader::OnApplicationError(ApplicationManager* manager, ...@@ -173,5 +241,9 @@ void DynamicApplicationLoader::OnApplicationError(ApplicationManager* manager,
// the app closed its handle to the service manager. Maybe we don't care? // the app closed its handle to the service manager. Maybe we don't care?
} }
void DynamicApplicationLoader::LoaderComplete(Loader* loader) {
loaders_.erase(std::find(loaders_.begin(), loaders_.end(), loader));
}
} // namespace shell } // namespace shell
} // namespace mojo } // namespace mojo
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#include <map> #include <map>
#include "base/callback.h"
#include "base/macros.h" #include "base/macros.h"
#include "base/memory/scoped_vector.h" #include "base/memory/scoped_vector.h"
#include "base/memory/weak_ptr.h" #include "base/memory/weak_ptr.h"
...@@ -44,28 +45,21 @@ class DynamicApplicationLoader : public ApplicationLoader { ...@@ -44,28 +45,21 @@ class DynamicApplicationLoader : public ApplicationLoader {
const GURL& url) OVERRIDE; const GURL& url) OVERRIDE;
private: private:
class Loader;
class LocalLoader;
class NetworkLoader;
typedef std::map<std::string, GURL> MimeTypeToURLMap; typedef std::map<std::string, GURL> MimeTypeToURLMap;
typedef base::Callback<void(Loader*)> LoaderCompleteCallback;
void LoadLocalService(const GURL& resolved_url, void LoaderComplete(Loader* loader);
scoped_refptr<LoadCallbacks> callbacks);
void LoadNetworkService(const GURL& resolved_url,
scoped_refptr<LoadCallbacks> callbacks);
void OnLoadNetworkServiceComplete(scoped_refptr<LoadCallbacks> callbacks,
URLResponsePtr url_response);
void RunLibrary(const base::FilePath& response_file,
scoped_refptr<LoadCallbacks> callbacks,
bool delete_file_after,
bool response_path_exists);
void OnRunLibraryComplete(DynamicServiceRunner* runner,
const base::FilePath& temp_file);
Context* const context_; Context* const context_;
scoped_ptr<DynamicServiceRunnerFactory> runner_factory_; scoped_ptr<DynamicServiceRunnerFactory> runner_factory_;
ScopedVector<DynamicServiceRunner> runners_;
NetworkServicePtr network_service_; NetworkServicePtr network_service_;
URLLoaderPtr url_loader_;
MimeTypeToURLMap mime_type_to_url_; MimeTypeToURLMap mime_type_to_url_;
base::WeakPtrFactory<DynamicApplicationLoader> weak_ptr_factory_; ScopedVector<Loader> loaders_;
LoaderCompleteCallback loader_complete_callback_;
DISALLOW_COPY_AND_ASSIGN(DynamicApplicationLoader); DISALLOW_COPY_AND_ASSIGN(DynamicApplicationLoader);
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment