| OLD | NEW | 
|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be | 
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. | 
| 4 | 4 | 
| 5 #include "shell/application_manager/application_manager.h" | 5 #include "shell/application_manager/application_manager.h" | 
| 6 | 6 | 
| 7 #include <utility> | 7 #include <utility> | 
| 8 | 8 | 
| 9 #include "base/at_exit.h" | 9 #include "base/at_exit.h" | 
| 10 #include "base/bind.h" | 10 #include "base/bind.h" | 
| 11 #include "base/macros.h" | 11 #include "base/macros.h" | 
| 12 #include "base/memory/scoped_vector.h" | 12 #include "base/memory/scoped_vector.h" | 
| 13 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" | 
| 14 #include "mojo/public/cpp/application/application_connection.h" | 14 #include "mojo/public/cpp/application/application_connection.h" | 
| 15 #include "mojo/public/cpp/application/application_delegate.h" | 15 #include "mojo/public/cpp/application/application_delegate.h" | 
| 16 #include "mojo/public/cpp/application/application_impl.h" | 16 #include "mojo/public/cpp/application/application_impl.h" | 
| 17 #include "mojo/public/cpp/application/connect.h" | 17 #include "mojo/public/cpp/application/connect.h" | 
| 18 #include "mojo/public/cpp/application/interface_factory.h" |  | 
| 19 #include "mojo/public/cpp/bindings/strong_binding.h" | 18 #include "mojo/public/cpp/bindings/strong_binding.h" | 
| 20 #include "mojo/public/interfaces/application/service_provider.mojom.h" | 19 #include "mojo/public/interfaces/application/service_provider.mojom.h" | 
| 21 #include "shell/application_manager/application_loader.h" | 20 #include "shell/application_manager/application_loader.h" | 
| 22 #include "shell/application_manager/test.mojom.h" | 21 #include "shell/application_manager/test.mojom.h" | 
| 23 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" | 
| 24 | 23 | 
| 25 using mojo::Application; | 24 using mojo::Application; | 
| 26 using mojo::ApplicationConnection; | 25 using mojo::ApplicationConnection; | 
| 27 using mojo::ApplicationDelegate; | 26 using mojo::ApplicationDelegate; | 
| 28 using mojo::ApplicationImpl; | 27 using mojo::ApplicationImpl; | 
| 29 using mojo::Callback; | 28 using mojo::Callback; | 
| 30 using mojo::ConnectionContext; | 29 using mojo::ConnectionContext; | 
| 31 using mojo::InterfaceFactory; |  | 
| 32 using mojo::InterfaceRequest; | 30 using mojo::InterfaceRequest; | 
| 33 using mojo::StrongBinding; | 31 using mojo::StrongBinding; | 
| 34 | 32 | 
| 35 namespace shell { | 33 namespace shell { | 
| 36 namespace { | 34 namespace { | 
| 37 | 35 | 
| 38 const char kTestURLString[] = "test:testService"; | 36 const char kTestURLString[] = "test:testService"; | 
| 39 const char kTestAURLString[] = "test:TestA"; | 37 const char kTestAURLString[] = "test:TestA"; | 
| 40 const char kTestBURLString[] = "test:TestB"; | 38 const char kTestBURLString[] = "test:TestB"; | 
| 41 | 39 | 
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 93                    base::Bind(&TestClient::AckTest, base::Unretained(this))); | 91                    base::Bind(&TestClient::AckTest, base::Unretained(this))); | 
| 94   } | 92   } | 
| 95 | 93 | 
| 96  private: | 94  private: | 
| 97   TestServicePtr service_; | 95   TestServicePtr service_; | 
| 98   bool quit_after_ack_; | 96   bool quit_after_ack_; | 
| 99   DISALLOW_COPY_AND_ASSIGN(TestClient); | 97   DISALLOW_COPY_AND_ASSIGN(TestClient); | 
| 100 }; | 98 }; | 
| 101 | 99 | 
| 102 class TestApplicationLoader : public ApplicationLoader, | 100 class TestApplicationLoader : public ApplicationLoader, | 
| 103                               public ApplicationDelegate, | 101                               public ApplicationDelegate { | 
| 104                               public InterfaceFactory<TestService> { |  | 
| 105  public: | 102  public: | 
| 106   TestApplicationLoader() : context_(nullptr), num_loads_(0) {} | 103   TestApplicationLoader() : context_(nullptr), num_loads_(0) {} | 
| 107 | 104 | 
| 108   ~TestApplicationLoader() override { | 105   ~TestApplicationLoader() override { | 
| 109     if (context_) | 106     if (context_) | 
| 110       ++context_->num_loader_deletes; | 107       ++context_->num_loader_deletes; | 
| 111     test_app_.reset(); | 108     test_app_.reset(); | 
| 112   } | 109   } | 
| 113 | 110 | 
| 114   void set_context(TestContext* context) { context_ = context; } | 111   void set_context(TestContext* context) { context_ = context; } | 
| 115   int num_loads() const { return num_loads_; } | 112   int num_loads() const { return num_loads_; } | 
| 116   const std::vector<std::string>& GetArgs() const { return test_app_->args(); } | 113   const std::vector<std::string>& GetArgs() const { return test_app_->args(); } | 
| 117 | 114 | 
| 118  private: | 115  private: | 
| 119   // ApplicationLoader implementation. | 116   // ApplicationLoader implementation. | 
| 120   void Load(const GURL& url, | 117   void Load(const GURL& url, | 
| 121             InterfaceRequest<Application> application_request) override { | 118             InterfaceRequest<Application> application_request) override { | 
| 122     ++num_loads_; | 119     ++num_loads_; | 
| 123     test_app_.reset(new ApplicationImpl(this, application_request.Pass())); | 120     test_app_.reset(new ApplicationImpl(this, application_request.Pass())); | 
| 124   } | 121   } | 
| 125 | 122 | 
| 126   // ApplicationDelegate implementation. | 123   // ApplicationDelegate implementation. | 
| 127   bool ConfigureIncomingConnection(ApplicationConnection* connection) override { | 124   bool ConfigureIncomingConnection(ApplicationConnection* connection) override { | 
| 128     connection->AddService(this); | 125     connection->GetServiceProviderImpl().AddService<TestService>( | 
|  | 126         [this](const ConnectionContext& connection_context, | 
|  | 127                InterfaceRequest<TestService> request) { | 
|  | 128           new TestServiceImpl(context_, request.Pass()); | 
|  | 129         }); | 
| 129     return true; | 130     return true; | 
| 130   } | 131   } | 
| 131 | 132 | 
| 132   // InterfaceFactory implementation. |  | 
| 133   void Create(const ConnectionContext& connection_context, |  | 
| 134               InterfaceRequest<TestService> request) override { |  | 
| 135     new TestServiceImpl(context_, request.Pass()); |  | 
| 136   } |  | 
| 137 |  | 
| 138   scoped_ptr<ApplicationImpl> test_app_; | 133   scoped_ptr<ApplicationImpl> test_app_; | 
| 139   TestContext* context_; | 134   TestContext* context_; | 
| 140   int num_loads_; | 135   int num_loads_; | 
| 141   DISALLOW_COPY_AND_ASSIGN(TestApplicationLoader); | 136   DISALLOW_COPY_AND_ASSIGN(TestApplicationLoader); | 
| 142 }; | 137 }; | 
| 143 | 138 | 
| 144 class ClosingApplicationLoader : public ApplicationLoader { | 139 class ClosingApplicationLoader : public ApplicationLoader { | 
| 145  private: | 140  private: | 
| 146   // ApplicationLoader implementation. | 141   // ApplicationLoader implementation. | 
| 147   void Load(const GURL& url, | 142   void Load(const GURL& url, | 
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 291  private: | 286  private: | 
| 292   void B(const Callback<void()>& callback) override { | 287   void B(const Callback<void()>& callback) override { | 
| 293     test_context_->IncrementNumBCalls(); | 288     test_context_->IncrementNumBCalls(); | 
| 294     callback.Run(); | 289     callback.Run(); | 
| 295   } | 290   } | 
| 296 | 291 | 
| 297   TesterContext* test_context_; | 292   TesterContext* test_context_; | 
| 298   StrongBinding<TestB> binding_; | 293   StrongBinding<TestB> binding_; | 
| 299 }; | 294 }; | 
| 300 | 295 | 
| 301 class Tester : public ApplicationDelegate, | 296 class Tester : public ApplicationDelegate, public ApplicationLoader { | 
| 302                public ApplicationLoader, |  | 
| 303                public InterfaceFactory<TestA>, |  | 
| 304                public InterfaceFactory<TestB> { |  | 
| 305  public: | 297  public: | 
| 306   Tester(TesterContext* context, const std::string& requestor_url) | 298   Tester(TesterContext* context, const std::string& requestor_url) | 
| 307       : context_(context), requestor_url_(requestor_url) {} | 299       : context_(context), requestor_url_(requestor_url) {} | 
| 308   ~Tester() override {} | 300   ~Tester() override {} | 
| 309 | 301 | 
| 310  private: | 302  private: | 
| 311   void Load(const GURL& url, | 303   void Load(const GURL& url, | 
| 312             InterfaceRequest<Application> application_request) override { | 304             InterfaceRequest<Application> application_request) override { | 
| 313     app_.reset(new ApplicationImpl(this, application_request.Pass())); | 305     app_.reset(new ApplicationImpl(this, application_request.Pass())); | 
| 314   } | 306   } | 
| 315 | 307 | 
| 316   bool ConfigureIncomingConnection(ApplicationConnection* connection) override { | 308   bool ConfigureIncomingConnection(ApplicationConnection* connection) override { | 
| 317     const std::string& remote_url = | 309     const std::string& remote_url = | 
| 318         connection->GetServiceProviderImpl().connection_context().remote_url; | 310         connection->GetServiceProviderImpl().connection_context().remote_url; | 
| 319     if (!requestor_url_.empty() && requestor_url_ != remote_url) { | 311     if (!requestor_url_.empty() && requestor_url_ != remote_url) { | 
| 320       context_->set_tester_called_quit(); | 312       context_->set_tester_called_quit(); | 
| 321       context_->QuitSoon(); | 313       context_->QuitSoon(); | 
| 322       base::MessageLoop::current()->Quit(); | 314       base::MessageLoop::current()->Quit(); | 
| 323       return false; | 315       return false; | 
| 324     } | 316     } | 
| 325     // If we're coming from A, then add B, otherwise A. | 317     // If we're coming from A, then add B, otherwise A. | 
| 326     if (remote_url == kTestAURLString) | 318     if (remote_url == kTestAURLString) { | 
| 327       connection->AddService<TestB>(this); | 319       connection->GetServiceProviderImpl().AddService<TestB>( | 
| 328     else | 320           [this](const ConnectionContext& connection_context, | 
| 329       connection->AddService<TestA>(this); | 321                  InterfaceRequest<TestB> test_b_request) { | 
|  | 322             new TestBImpl(context_, test_b_request.Pass()); | 
|  | 323           }); | 
|  | 324     } else { | 
|  | 325       connection->GetServiceProviderImpl().AddService<TestA>( | 
|  | 326           [this](const ConnectionContext& connection_context, | 
|  | 327                  InterfaceRequest<TestA> test_a_request) { | 
|  | 328             mojo::InterfaceHandle<mojo::ServiceProvider> incoming_sp_handle; | 
|  | 329             app_->shell()->ConnectToApplication( | 
|  | 330                 kTestBURLString, GetProxy(&incoming_sp_handle), nullptr); | 
|  | 331             a_bindings_.push_back(new TestAImpl( | 
|  | 332                 incoming_sp_handle.Pass(), context_, test_a_request.Pass())); | 
|  | 333           }); | 
|  | 334     } | 
| 330     return true; | 335     return true; | 
| 331   } | 336   } | 
| 332 | 337 | 
| 333   void Create(const ConnectionContext& connection_context, |  | 
| 334               InterfaceRequest<TestA> request) override { |  | 
| 335     mojo::InterfaceHandle<mojo::ServiceProvider> incoming_sp_handle; |  | 
| 336     app_->shell()->ConnectToApplication(kTestBURLString, |  | 
| 337                                         GetProxy(&incoming_sp_handle), nullptr); |  | 
| 338     a_bindings_.push_back( |  | 
| 339         new TestAImpl(incoming_sp_handle.Pass(), context_, request.Pass())); |  | 
| 340   } |  | 
| 341 |  | 
| 342   void Create(const ConnectionContext& connection_context, |  | 
| 343               InterfaceRequest<TestB> request) override { |  | 
| 344     new TestBImpl(context_, request.Pass()); |  | 
| 345   } |  | 
| 346 |  | 
| 347   TesterContext* context_; | 338   TesterContext* context_; | 
| 348   scoped_ptr<ApplicationImpl> app_; | 339   scoped_ptr<ApplicationImpl> app_; | 
| 349   std::string requestor_url_; | 340   std::string requestor_url_; | 
| 350   ScopedVector<TestAImpl> a_bindings_; | 341   ScopedVector<TestAImpl> a_bindings_; | 
| 351 }; | 342 }; | 
| 352 | 343 | 
| 353 class TestDelegate : public ApplicationManager::Delegate { | 344 class TestDelegate : public ApplicationManager::Delegate { | 
| 354  public: | 345  public: | 
| 355   void AddMapping(const GURL& from, const GURL& to) { mappings_[from] = to; } | 346   void AddMapping(const GURL& from, const GURL& to) { mappings_[from] = to; } | 
| 356 | 347 | 
| (...skipping 433 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 790   bool called = false; | 781   bool called = false; | 
| 791   application_manager_->ConnectToApplication( | 782   application_manager_->ConnectToApplication( | 
| 792       GURL("test:test"), GURL(), nullptr, | 783       GURL("test:test"), GURL(), nullptr, | 
| 793       base::Bind(&QuitClosure, base::Unretained(&called))); | 784       base::Bind(&QuitClosure, base::Unretained(&called))); | 
| 794   loop_.Run(); | 785   loop_.Run(); | 
| 795   EXPECT_TRUE(called); | 786   EXPECT_TRUE(called); | 
| 796 } | 787 } | 
| 797 | 788 | 
| 798 }  // namespace | 789 }  // namespace | 
| 799 }  // namespace shell | 790 }  // namespace shell | 
| OLD | NEW | 
|---|