diff options
author | davemoore <davemoore@chromium.org> | 2014-09-19 16:55:33 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2014-09-19 23:55:52 +0000 |
commit | 2245c392b9a37199c868213d32e73865756416d3 (patch) | |
tree | 9d922bccd7a2d6d822833890808c634a5b5708db | |
parent | f01a9aee4db445d8f920ae191f27f0e52c76b50c (diff) | |
download | chromium_src-2245c392b9a37199c868213d32e73865756416d3.zip chromium_src-2245c392b9a37199c868213d32e73865756416d3.tar.gz chromium_src-2245c392b9a37199c868213d32e73865756416d3.tar.bz2 |
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}
6 files changed, 86 insertions, 10 deletions
diff --git a/mojo/application_manager/application_manager.cc b/mojo/application_manager/application_manager.cc index b0a300e..382cb03 100644 --- a/mojo/application_manager/application_manager.cc +++ b/mojo/application_manager/application_manager.cc @@ -220,9 +220,14 @@ void ApplicationManager::RegisterLoadedApplication( shell_impl = iter->second; } else { 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()); url_to_shell_impl_[url] = shell_impl; *shell_handle = pipe.handle0.Pass(); + shell_impl->client()->Initialize(args.Pass()); } ConnectToClient(shell_impl, url, requestor_url, service_provider.Pass()); @@ -267,6 +272,11 @@ void ApplicationManager::SetLoaderForScheme( 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) { interceptor_ = interceptor; } diff --git a/mojo/application_manager/application_manager.h b/mojo/application_manager/application_manager.h index bb988a0..940aa30 100644 --- a/mojo/application_manager/application_manager.h +++ b/mojo/application_manager/application_manager.h @@ -92,6 +92,10 @@ class MOJO_APPLICATION_MANAGER_EXPORT ApplicationManager { // Sets a Loader to be used for a specific url scheme. void SetLoaderForScheme(scoped_ptr<ApplicationLoader> loader, 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. void SetInterceptor(Interceptor* interceptor); @@ -109,6 +113,7 @@ class MOJO_APPLICATION_MANAGER_EXPORT ApplicationManager { typedef std::map<GURL, ApplicationLoader*> URLToLoaderMap; typedef std::map<GURL, ShellImpl*> URLToShellImplMap; typedef std::map<GURL, ContentHandlerConnection*> URLToContentHandlerMap; + typedef std::map<GURL, std::vector<std::string> > URLToArgsMap; void ConnectToClient(ShellImpl* shell_impl, const GURL& url, @@ -143,6 +148,7 @@ class MOJO_APPLICATION_MANAGER_EXPORT ApplicationManager { URLToShellImplMap url_to_shell_impl_; URLToContentHandlerMap url_to_content_handler_; + URLToArgsMap url_to_args_; base::WeakPtrFactory<ApplicationManager> weak_ptr_factory_; diff --git a/mojo/application_manager/application_manager_unittest.cc b/mojo/application_manager/application_manager_unittest.cc index 7dd35e87..658b670 100644 --- a/mojo/application_manager/application_manager_unittest.cc +++ b/mojo/application_manager/application_manager_unittest.cc @@ -107,6 +107,9 @@ class TestApplicationLoader : public ApplicationLoader, void set_context(TestContext* context) { context_ = context; } int num_loads() const { return num_loads_; } + std::vector<std::string> GetArgs() { + return test_app_->args().To<std::vector<std::string> >(); + } private: // ApplicationLoader implementation. @@ -466,6 +469,46 @@ TEST_F(ApplicationManagerTest, Basic) { 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_client_->Test("test"); EXPECT_TRUE(HasFactoryForTestURL()); @@ -479,7 +522,7 @@ TEST_F(ApplicationManagerTest, ClientError) { TEST_F(ApplicationManagerTest, Deletes) { { - ApplicationManager sm; + ApplicationManager am; TestApplicationLoader* default_loader = new TestApplicationLoader; default_loader->set_context(&context_); TestApplicationLoader* url_loader1 = new TestApplicationLoader; @@ -490,14 +533,14 @@ TEST_F(ApplicationManagerTest, Deletes) { TestApplicationLoader* scheme_loader2 = new TestApplicationLoader; scheme_loader1->set_context(&context_); scheme_loader2->set_context(&context_); - sm.set_default_loader(scoped_ptr<ApplicationLoader>(default_loader)); - sm.SetLoaderForURL(scoped_ptr<ApplicationLoader>(url_loader1), + am.set_default_loader(scoped_ptr<ApplicationLoader>(default_loader)); + am.SetLoaderForURL(scoped_ptr<ApplicationLoader>(url_loader1), GURL("test:test1")); - sm.SetLoaderForURL(scoped_ptr<ApplicationLoader>(url_loader2), + am.SetLoaderForURL(scoped_ptr<ApplicationLoader>(url_loader2), GURL("test:test1")); - sm.SetLoaderForScheme(scoped_ptr<ApplicationLoader>(scheme_loader1), + am.SetLoaderForScheme(scoped_ptr<ApplicationLoader>(scheme_loader1), "test"); - sm.SetLoaderForScheme(scoped_ptr<ApplicationLoader>(scheme_loader2), + am.SetLoaderForScheme(scoped_ptr<ApplicationLoader>(scheme_loader2), "test"); } EXPECT_EQ(5, context_.num_loader_deletes); @@ -505,7 +548,6 @@ TEST_F(ApplicationManagerTest, Deletes) { // Confirm that both urls and schemes can have their loaders explicitly set. TEST_F(ApplicationManagerTest, SetLoaders) { - ApplicationManager sm; TestApplicationLoader* default_loader = new TestApplicationLoader; TestApplicationLoader* url_loader = new TestApplicationLoader; TestApplicationLoader* scheme_loader = new TestApplicationLoader; diff --git a/mojo/public/cpp/application/application_impl.h b/mojo/public/cpp/application/application_impl.h index 8d6ebd7..afd2376 100644 --- a/mojo/public/cpp/application/application_impl.h +++ b/mojo/public/cpp/application/application_impl.h @@ -60,6 +60,9 @@ class ApplicationImpl : public InterfaceImpl<Application> { 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. ApplicationConnection* ConnectToApplication(const String& application_url); @@ -85,15 +88,19 @@ class ApplicationImpl : public InterfaceImpl<Application> { static void Terminate(); // Application implementation. + virtual void Initialize(Array<String> args) MOJO_OVERRIDE; virtual void AcceptConnection(const String& requestor_url, ServiceProviderPtr provider) MOJO_OVERRIDE; typedef std::vector<internal::ServiceRegistry*> ServiceRegistryList; + + bool initialized_; ServiceRegistryList incoming_service_registries_; ServiceRegistryList outgoing_service_registries_; ApplicationDelegate* delegate_; ShellPtr shell_; ShellPtrWatcher* shell_watch_; + Array<String> args_; MOJO_DISALLOW_COPY_AND_ASSIGN(ApplicationImpl); }; diff --git a/mojo/public/cpp/application/lib/application_impl.cc b/mojo/public/cpp/application/lib/application_impl.cc index 469ba3a..fc3857f 100644 --- a/mojo/public/cpp/application/lib/application_impl.cc +++ b/mojo/public/cpp/application/lib/application_impl.cc @@ -7,6 +7,7 @@ #include "mojo/public/cpp/application/application_delegate.h" #include "mojo/public/cpp/application/lib/service_registry.h" #include "mojo/public/cpp/bindings/interface_ptr.h" +#include "mojo/public/cpp/environment/logging.h" namespace mojo { @@ -28,13 +29,13 @@ class ApplicationImpl::ShellPtrWatcher : public ErrorHandler { ApplicationImpl::ApplicationImpl(ApplicationDelegate* delegate, ScopedMessagePipeHandle shell_handle) - : delegate_(delegate), shell_watch_(NULL) { + : initialized_(false), delegate_(delegate), shell_watch_(NULL) { BindShell(shell_handle.Pass()); } ApplicationImpl::ApplicationImpl(ApplicationDelegate* delegate, MojoHandle shell_handle) - : delegate_(delegate), shell_watch_(NULL) { + : initialized_(false), delegate_(delegate), shell_watch_(NULL) { BindShell(MakeScopedHandle(MessagePipeHandle(shell_handle))); } @@ -54,8 +55,16 @@ ApplicationImpl::~ApplicationImpl() { delete shell_watch_; } +void ApplicationImpl::Initialize(Array<String> args) { + MOJO_CHECK(!initialized_); + initialized_ = true; + args_ = args.Pass(); + delegate_->Initialize(this); +} + ApplicationConnection* ApplicationImpl::ConnectToApplication( const String& application_url) { + MOJO_CHECK(initialized_); ServiceProviderPtr out_service_provider; shell_->ConnectToApplication(application_url, Get(&out_service_provider)); internal::ServiceRegistry* registry = new internal::ServiceRegistry( @@ -75,7 +84,6 @@ void ApplicationImpl::BindShell(ScopedMessagePipeHandle shell_handle) { shell_.Bind(shell_handle.Pass()); shell_.set_client(this); shell_.set_error_handler(shell_watch_); - delegate_->Initialize(this); } void ApplicationImpl::AcceptConnection(const String& requestor_url, diff --git a/mojo/public/interfaces/application/application.mojom b/mojo/public/interfaces/application/application.mojom index 35ed34c..8807bc9 100644 --- a/mojo/public/interfaces/application/application.mojom +++ b/mojo/public/interfaces/application/application.mojom @@ -10,6 +10,9 @@ module mojo { // implement Interfaces. [Client=Shell] interface Application { + // Initialize is guaranteed to be called before any AcceptConnection calls. + Initialize(string[]? args); + AcceptConnection(string? requestor_url, ServiceProvider? provider); }; |