Commit 2245c392 authored by davemoore's avatar davemoore Committed by Commit bot

Add Initialize() method to Application with ability to send args using

new api on ApplicationManager.

BUG=

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

Cr-Commit-Position: refs/heads/master@{#295816}
parent f01a9aee
...@@ -220,9 +220,14 @@ void ApplicationManager::RegisterLoadedApplication( ...@@ -220,9 +220,14 @@ void ApplicationManager::RegisterLoadedApplication(
shell_impl = iter->second; shell_impl = iter->second;
} else { } else {
MessagePipe pipe; MessagePipe pipe;
URLToArgsMap::const_iterator args_it = url_to_args_.find(url);
Array<String> args;
if (args_it != url_to_args_.end())
args = Array<String>::From(args_it->second);
shell_impl = WeakBindToPipe(new ShellImpl(this, url), pipe.handle1.Pass()); shell_impl = WeakBindToPipe(new ShellImpl(this, url), pipe.handle1.Pass());
url_to_shell_impl_[url] = shell_impl; url_to_shell_impl_[url] = shell_impl;
*shell_handle = pipe.handle0.Pass(); *shell_handle = pipe.handle0.Pass();
shell_impl->client()->Initialize(args.Pass());
} }
ConnectToClient(shell_impl, url, requestor_url, service_provider.Pass()); ConnectToClient(shell_impl, url, requestor_url, service_provider.Pass());
...@@ -267,6 +272,11 @@ void ApplicationManager::SetLoaderForScheme( ...@@ -267,6 +272,11 @@ void ApplicationManager::SetLoaderForScheme(
scheme_to_loader_[scheme] = loader.release(); scheme_to_loader_[scheme] = loader.release();
} }
void ApplicationManager::SetArgsForURL(const std::vector<std::string>& args,
const GURL& url) {
url_to_args_[url] = args;
}
void ApplicationManager::SetInterceptor(Interceptor* interceptor) { void ApplicationManager::SetInterceptor(Interceptor* interceptor) {
interceptor_ = interceptor; interceptor_ = interceptor;
} }
......
...@@ -92,6 +92,10 @@ class MOJO_APPLICATION_MANAGER_EXPORT ApplicationManager { ...@@ -92,6 +92,10 @@ class MOJO_APPLICATION_MANAGER_EXPORT ApplicationManager {
// Sets a Loader to be used for a specific url scheme. // Sets a Loader to be used for a specific url scheme.
void SetLoaderForScheme(scoped_ptr<ApplicationLoader> loader, void SetLoaderForScheme(scoped_ptr<ApplicationLoader> loader,
const std::string& scheme); const std::string& scheme);
// These strings will be passed to the Initialize() method when an
// Application is instantiated.
void SetArgsForURL(const std::vector<std::string>& args, const GURL& url);
// Allows to interpose a debugger to service connections. // Allows to interpose a debugger to service connections.
void SetInterceptor(Interceptor* interceptor); void SetInterceptor(Interceptor* interceptor);
...@@ -109,6 +113,7 @@ class MOJO_APPLICATION_MANAGER_EXPORT ApplicationManager { ...@@ -109,6 +113,7 @@ class MOJO_APPLICATION_MANAGER_EXPORT ApplicationManager {
typedef std::map<GURL, ApplicationLoader*> URLToLoaderMap; typedef std::map<GURL, ApplicationLoader*> URLToLoaderMap;
typedef std::map<GURL, ShellImpl*> URLToShellImplMap; typedef std::map<GURL, ShellImpl*> URLToShellImplMap;
typedef std::map<GURL, ContentHandlerConnection*> URLToContentHandlerMap; typedef std::map<GURL, ContentHandlerConnection*> URLToContentHandlerMap;
typedef std::map<GURL, std::vector<std::string> > URLToArgsMap;
void ConnectToClient(ShellImpl* shell_impl, void ConnectToClient(ShellImpl* shell_impl,
const GURL& url, const GURL& url,
...@@ -143,6 +148,7 @@ class MOJO_APPLICATION_MANAGER_EXPORT ApplicationManager { ...@@ -143,6 +148,7 @@ class MOJO_APPLICATION_MANAGER_EXPORT ApplicationManager {
URLToShellImplMap url_to_shell_impl_; URLToShellImplMap url_to_shell_impl_;
URLToContentHandlerMap url_to_content_handler_; URLToContentHandlerMap url_to_content_handler_;
URLToArgsMap url_to_args_;
base::WeakPtrFactory<ApplicationManager> weak_ptr_factory_; base::WeakPtrFactory<ApplicationManager> weak_ptr_factory_;
......
...@@ -107,6 +107,9 @@ class TestApplicationLoader : public ApplicationLoader, ...@@ -107,6 +107,9 @@ class TestApplicationLoader : public ApplicationLoader,
void set_context(TestContext* context) { context_ = context; } void set_context(TestContext* context) { context_ = context; }
int num_loads() const { return num_loads_; } int num_loads() const { return num_loads_; }
std::vector<std::string> GetArgs() {
return test_app_->args().To<std::vector<std::string> >();
}
private: private:
// ApplicationLoader implementation. // ApplicationLoader implementation.
...@@ -466,6 +469,46 @@ TEST_F(ApplicationManagerTest, Basic) { ...@@ -466,6 +469,46 @@ TEST_F(ApplicationManagerTest, Basic) {
EXPECT_EQ(std::string("test"), context_.last_test_string); EXPECT_EQ(std::string("test"), context_.last_test_string);
} }
// Confirm that no arguments are sent to an application by default.
TEST_F(ApplicationManagerTest, NoArgs) {
ApplicationManager am;
GURL test_url("test:test");
TestContext context;
TestApplicationLoader* loader = new TestApplicationLoader;
loader->set_context(&context);
am.SetLoaderForURL(scoped_ptr<ApplicationLoader>(loader), test_url);
TestServicePtr test_service;
am.ConnectToService(test_url, &test_service);
TestClientImpl test_client(test_service.Pass());
test_client.Test("test");
loop_.Run();
std::vector<std::string> app_args = loader->GetArgs();
EXPECT_EQ(0U, app_args.size());
}
// Confirm that arguments are sent to an application.
TEST_F(ApplicationManagerTest, Args) {
ApplicationManager am;
GURL test_url("test:test");
std::vector<std::string> args;
args.push_back("test_arg1");
args.push_back("test_arg2");
am.SetArgsForURL(args, test_url);
TestContext context;
TestApplicationLoader* loader = new TestApplicationLoader;
loader->set_context(&context);
am.SetLoaderForURL(scoped_ptr<ApplicationLoader>(loader), test_url);
TestServicePtr test_service;
am.ConnectToService(test_url, &test_service);
TestClientImpl test_client(test_service.Pass());
test_client.Test("test");
loop_.Run();
std::vector<std::string> app_args = loader->GetArgs();
ASSERT_EQ(args.size(), app_args.size());
EXPECT_EQ(args[0], app_args[0]);
EXPECT_EQ(args[1], app_args[1]);
}
TEST_F(ApplicationManagerTest, ClientError) { TEST_F(ApplicationManagerTest, ClientError) {
test_client_->Test("test"); test_client_->Test("test");
EXPECT_TRUE(HasFactoryForTestURL()); EXPECT_TRUE(HasFactoryForTestURL());
...@@ -479,7 +522,7 @@ TEST_F(ApplicationManagerTest, ClientError) { ...@@ -479,7 +522,7 @@ TEST_F(ApplicationManagerTest, ClientError) {
TEST_F(ApplicationManagerTest, Deletes) { TEST_F(ApplicationManagerTest, Deletes) {
{ {
ApplicationManager sm; ApplicationManager am;
TestApplicationLoader* default_loader = new TestApplicationLoader; TestApplicationLoader* default_loader = new TestApplicationLoader;
default_loader->set_context(&context_); default_loader->set_context(&context_);
TestApplicationLoader* url_loader1 = new TestApplicationLoader; TestApplicationLoader* url_loader1 = new TestApplicationLoader;
...@@ -490,14 +533,14 @@ TEST_F(ApplicationManagerTest, Deletes) { ...@@ -490,14 +533,14 @@ TEST_F(ApplicationManagerTest, Deletes) {
TestApplicationLoader* scheme_loader2 = new TestApplicationLoader; TestApplicationLoader* scheme_loader2 = new TestApplicationLoader;
scheme_loader1->set_context(&context_); scheme_loader1->set_context(&context_);
scheme_loader2->set_context(&context_); scheme_loader2->set_context(&context_);
sm.set_default_loader(scoped_ptr<ApplicationLoader>(default_loader)); am.set_default_loader(scoped_ptr<ApplicationLoader>(default_loader));
sm.SetLoaderForURL(scoped_ptr<ApplicationLoader>(url_loader1), am.SetLoaderForURL(scoped_ptr<ApplicationLoader>(url_loader1),
GURL("test:test1")); GURL("test:test1"));
sm.SetLoaderForURL(scoped_ptr<ApplicationLoader>(url_loader2), am.SetLoaderForURL(scoped_ptr<ApplicationLoader>(url_loader2),
GURL("test:test1")); GURL("test:test1"));
sm.SetLoaderForScheme(scoped_ptr<ApplicationLoader>(scheme_loader1), am.SetLoaderForScheme(scoped_ptr<ApplicationLoader>(scheme_loader1),
"test"); "test");
sm.SetLoaderForScheme(scoped_ptr<ApplicationLoader>(scheme_loader2), am.SetLoaderForScheme(scoped_ptr<ApplicationLoader>(scheme_loader2),
"test"); "test");
} }
EXPECT_EQ(5, context_.num_loader_deletes); EXPECT_EQ(5, context_.num_loader_deletes);
...@@ -505,7 +548,6 @@ TEST_F(ApplicationManagerTest, Deletes) { ...@@ -505,7 +548,6 @@ TEST_F(ApplicationManagerTest, Deletes) {
// Confirm that both urls and schemes can have their loaders explicitly set. // Confirm that both urls and schemes can have their loaders explicitly set.
TEST_F(ApplicationManagerTest, SetLoaders) { TEST_F(ApplicationManagerTest, SetLoaders) {
ApplicationManager sm;
TestApplicationLoader* default_loader = new TestApplicationLoader; TestApplicationLoader* default_loader = new TestApplicationLoader;
TestApplicationLoader* url_loader = new TestApplicationLoader; TestApplicationLoader* url_loader = new TestApplicationLoader;
TestApplicationLoader* scheme_loader = new TestApplicationLoader; TestApplicationLoader* scheme_loader = new TestApplicationLoader;
......
...@@ -60,6 +60,9 @@ class ApplicationImpl : public InterfaceImpl<Application> { ...@@ -60,6 +60,9 @@ class ApplicationImpl : public InterfaceImpl<Application> {
Shell* shell() const { return shell_.get(); } Shell* shell() const { return shell_.get(); }
// Returns any initial configuration arguments, passed by the Shell.
const Array<String>& args() { return args_; }
// Establishes a new connection to an application. Caller does not own. // Establishes a new connection to an application. Caller does not own.
ApplicationConnection* ConnectToApplication(const String& application_url); ApplicationConnection* ConnectToApplication(const String& application_url);
...@@ -85,15 +88,19 @@ class ApplicationImpl : public InterfaceImpl<Application> { ...@@ -85,15 +88,19 @@ class ApplicationImpl : public InterfaceImpl<Application> {
static void Terminate(); static void Terminate();
// Application implementation. // Application implementation.
virtual void Initialize(Array<String> args) MOJO_OVERRIDE;
virtual void AcceptConnection(const String& requestor_url, virtual void AcceptConnection(const String& requestor_url,
ServiceProviderPtr provider) MOJO_OVERRIDE; ServiceProviderPtr provider) MOJO_OVERRIDE;
typedef std::vector<internal::ServiceRegistry*> ServiceRegistryList; typedef std::vector<internal::ServiceRegistry*> ServiceRegistryList;
bool initialized_;
ServiceRegistryList incoming_service_registries_; ServiceRegistryList incoming_service_registries_;
ServiceRegistryList outgoing_service_registries_; ServiceRegistryList outgoing_service_registries_;
ApplicationDelegate* delegate_; ApplicationDelegate* delegate_;
ShellPtr shell_; ShellPtr shell_;
ShellPtrWatcher* shell_watch_; ShellPtrWatcher* shell_watch_;
Array<String> args_;
MOJO_DISALLOW_COPY_AND_ASSIGN(ApplicationImpl); MOJO_DISALLOW_COPY_AND_ASSIGN(ApplicationImpl);
}; };
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#include "mojo/public/cpp/application/application_delegate.h" #include "mojo/public/cpp/application/application_delegate.h"
#include "mojo/public/cpp/application/lib/service_registry.h" #include "mojo/public/cpp/application/lib/service_registry.h"
#include "mojo/public/cpp/bindings/interface_ptr.h" #include "mojo/public/cpp/bindings/interface_ptr.h"
#include "mojo/public/cpp/environment/logging.h"
namespace mojo { namespace mojo {
...@@ -28,13 +29,13 @@ class ApplicationImpl::ShellPtrWatcher : public ErrorHandler { ...@@ -28,13 +29,13 @@ class ApplicationImpl::ShellPtrWatcher : public ErrorHandler {
ApplicationImpl::ApplicationImpl(ApplicationDelegate* delegate, ApplicationImpl::ApplicationImpl(ApplicationDelegate* delegate,
ScopedMessagePipeHandle shell_handle) ScopedMessagePipeHandle shell_handle)
: delegate_(delegate), shell_watch_(NULL) { : initialized_(false), delegate_(delegate), shell_watch_(NULL) {
BindShell(shell_handle.Pass()); BindShell(shell_handle.Pass());
} }
ApplicationImpl::ApplicationImpl(ApplicationDelegate* delegate, ApplicationImpl::ApplicationImpl(ApplicationDelegate* delegate,
MojoHandle shell_handle) MojoHandle shell_handle)
: delegate_(delegate), shell_watch_(NULL) { : initialized_(false), delegate_(delegate), shell_watch_(NULL) {
BindShell(MakeScopedHandle(MessagePipeHandle(shell_handle))); BindShell(MakeScopedHandle(MessagePipeHandle(shell_handle)));
} }
...@@ -54,8 +55,16 @@ ApplicationImpl::~ApplicationImpl() { ...@@ -54,8 +55,16 @@ ApplicationImpl::~ApplicationImpl() {
delete shell_watch_; delete shell_watch_;
} }
void ApplicationImpl::Initialize(Array<String> args) {
MOJO_CHECK(!initialized_);
initialized_ = true;
args_ = args.Pass();
delegate_->Initialize(this);
}
ApplicationConnection* ApplicationImpl::ConnectToApplication( ApplicationConnection* ApplicationImpl::ConnectToApplication(
const String& application_url) { const String& application_url) {
MOJO_CHECK(initialized_);
ServiceProviderPtr out_service_provider; ServiceProviderPtr out_service_provider;
shell_->ConnectToApplication(application_url, Get(&out_service_provider)); shell_->ConnectToApplication(application_url, Get(&out_service_provider));
internal::ServiceRegistry* registry = new internal::ServiceRegistry( internal::ServiceRegistry* registry = new internal::ServiceRegistry(
...@@ -75,7 +84,6 @@ void ApplicationImpl::BindShell(ScopedMessagePipeHandle shell_handle) { ...@@ -75,7 +84,6 @@ void ApplicationImpl::BindShell(ScopedMessagePipeHandle shell_handle) {
shell_.Bind(shell_handle.Pass()); shell_.Bind(shell_handle.Pass());
shell_.set_client(this); shell_.set_client(this);
shell_.set_error_handler(shell_watch_); shell_.set_error_handler(shell_watch_);
delegate_->Initialize(this);
} }
void ApplicationImpl::AcceptConnection(const String& requestor_url, void ApplicationImpl::AcceptConnection(const String& requestor_url,
......
...@@ -10,6 +10,9 @@ module mojo { ...@@ -10,6 +10,9 @@ module mojo {
// implement Interfaces. // implement Interfaces.
[Client=Shell] [Client=Shell]
interface Application { interface Application {
// Initialize is guaranteed to be called before any AcceptConnection calls.
Initialize(string[]? args);
AcceptConnection(string? requestor_url, ServiceProvider? provider); AcceptConnection(string? requestor_url, ServiceProvider? provider);
}; };
......
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