| 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" | 18 #include "mojo/public/cpp/application/interface_factory.h" |
| 19 #include "mojo/public/cpp/application/service_provider_impl.h" | |
| 20 #include "mojo/public/cpp/bindings/strong_binding.h" | 19 #include "mojo/public/cpp/bindings/strong_binding.h" |
| 21 #include "mojo/public/interfaces/application/service_provider.mojom.h" | 20 #include "mojo/public/interfaces/application/service_provider.mojom.h" |
| 22 #include "shell/application_manager/application_loader.h" | 21 #include "shell/application_manager/application_loader.h" |
| 23 #include "shell/application_manager/test.mojom.h" | 22 #include "shell/application_manager/test.mojom.h" |
| 24 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
| 25 | 24 |
| 26 using mojo::Application; | 25 using mojo::Application; |
| 27 using mojo::ApplicationConnection; | 26 using mojo::ApplicationConnection; |
| 28 using mojo::ApplicationDelegate; | 27 using mojo::ApplicationDelegate; |
| 29 using mojo::ApplicationImpl; | 28 using mojo::ApplicationImpl; |
| (...skipping 228 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 258 test_context_->IncrementNumADeletes(); | 257 test_context_->IncrementNumADeletes(); |
| 259 if (base::MessageLoop::current()->is_running()) | 258 if (base::MessageLoop::current()->is_running()) |
| 260 Quit(); | 259 Quit(); |
| 261 } | 260 } |
| 262 | 261 |
| 263 private: | 262 private: |
| 264 void CallB() override { | 263 void CallB() override { |
| 265 b_->B(base::Bind(&TestAImpl::Quit, base::Unretained(this))); | 264 b_->B(base::Bind(&TestAImpl::Quit, base::Unretained(this))); |
| 266 } | 265 } |
| 267 | 266 |
| 268 void CallCFromB() override { | |
| 269 b_->CallC(base::Bind(&TestAImpl::Quit, base::Unretained(this))); | |
| 270 } | |
| 271 | |
| 272 void Quit() { | 267 void Quit() { |
| 273 base::MessageLoop::current()->Quit(); | 268 base::MessageLoop::current()->Quit(); |
| 274 test_context_->set_a_called_quit(); | 269 test_context_->set_a_called_quit(); |
| 275 test_context_->QuitSoon(); | 270 test_context_->QuitSoon(); |
| 276 } | 271 } |
| 277 | 272 |
| 278 TesterContext* test_context_; | 273 TesterContext* test_context_; |
| 279 TestBPtr b_; | 274 TestBPtr b_; |
| 280 StrongBinding<TestA> binding_; | 275 StrongBinding<TestA> binding_; |
| 281 }; | 276 }; |
| 282 | 277 |
| 283 class TestBImpl : public TestB { | 278 class TestBImpl : public TestB { |
| 284 public: | 279 public: |
| 285 TestBImpl(ApplicationConnection* connection, | 280 TestBImpl(ApplicationConnection* connection, |
| 286 TesterContext* test_context, | 281 TesterContext* test_context, |
| 287 InterfaceRequest<TestB> request) | 282 InterfaceRequest<TestB> request) |
| 288 : test_context_(test_context), binding_(this, request.Pass()) { | 283 : test_context_(test_context), binding_(this, request.Pass()) {} |
| 289 connection->ConnectToService(&c_); | |
| 290 } | |
| 291 | 284 |
| 292 ~TestBImpl() override { | 285 ~TestBImpl() override { |
| 293 test_context_->IncrementNumBDeletes(); | 286 test_context_->IncrementNumBDeletes(); |
| 294 if (base::MessageLoop::current()->is_running()) | 287 if (base::MessageLoop::current()->is_running()) |
| 295 base::MessageLoop::current()->Quit(); | 288 base::MessageLoop::current()->Quit(); |
| 296 test_context_->QuitSoon(); | 289 test_context_->QuitSoon(); |
| 297 } | 290 } |
| 298 | 291 |
| 299 private: | 292 private: |
| 300 void B(const Callback<void()>& callback) override { | 293 void B(const Callback<void()>& callback) override { |
| 301 test_context_->IncrementNumBCalls(); | 294 test_context_->IncrementNumBCalls(); |
| 302 callback.Run(); | 295 callback.Run(); |
| 303 } | 296 } |
| 304 | 297 |
| 305 void CallC(const Callback<void()>& callback) override { | |
| 306 test_context_->IncrementNumBCalls(); | |
| 307 c_->C(callback); | |
| 308 } | |
| 309 | |
| 310 TesterContext* test_context_; | |
| 311 TestCPtr c_; | |
| 312 StrongBinding<TestB> binding_; | |
| 313 }; | |
| 314 | |
| 315 class TestCImpl : public TestC { | |
| 316 public: | |
| 317 TestCImpl(ApplicationConnection* connection, | |
| 318 TesterContext* test_context, | |
| 319 InterfaceRequest<TestC> request) | |
| 320 : test_context_(test_context), binding_(this, request.Pass()) {} | |
| 321 | |
| 322 ~TestCImpl() override { test_context_->IncrementNumCDeletes(); } | |
| 323 | |
| 324 private: | |
| 325 void C(const Callback<void()>& callback) override { | |
| 326 test_context_->IncrementNumCCalls(); | |
| 327 callback.Run(); | |
| 328 } | |
| 329 | |
| 330 TesterContext* test_context_; | 298 TesterContext* test_context_; |
| 331 StrongBinding<TestC> binding_; | 299 StrongBinding<TestB> binding_; |
| 332 }; | 300 }; |
| 333 | 301 |
| 334 class Tester : public ApplicationDelegate, | 302 class Tester : public ApplicationDelegate, |
| 335 public ApplicationLoader, | 303 public ApplicationLoader, |
| 336 public InterfaceFactory<TestA>, | 304 public InterfaceFactory<TestA>, |
| 337 public InterfaceFactory<TestB>, | 305 public InterfaceFactory<TestB> { |
| 338 public InterfaceFactory<TestC> { | |
| 339 public: | 306 public: |
| 340 Tester(TesterContext* context, const std::string& requestor_url) | 307 Tester(TesterContext* context, const std::string& requestor_url) |
| 341 : context_(context), requestor_url_(requestor_url) {} | 308 : context_(context), requestor_url_(requestor_url) {} |
| 342 ~Tester() override {} | 309 ~Tester() override {} |
| 343 | 310 |
| 344 private: | 311 private: |
| 345 void Load(const GURL& url, | 312 void Load(const GURL& url, |
| 346 InterfaceRequest<Application> application_request) override { | 313 InterfaceRequest<Application> application_request) override { |
| 347 app_.reset(new ApplicationImpl(this, application_request.Pass())); | 314 app_.reset(new ApplicationImpl(this, application_request.Pass())); |
| 348 } | 315 } |
| (...skipping 10 matching lines...) Expand all Loading... |
| 359 if (connection->GetRemoteApplicationURL() == kTestAURLString) | 326 if (connection->GetRemoteApplicationURL() == kTestAURLString) |
| 360 connection->AddService<TestB>(this); | 327 connection->AddService<TestB>(this); |
| 361 else | 328 else |
| 362 connection->AddService<TestA>(this); | 329 connection->AddService<TestA>(this); |
| 363 return true; | 330 return true; |
| 364 } | 331 } |
| 365 | 332 |
| 366 void Create(ApplicationConnection* connection, | 333 void Create(ApplicationConnection* connection, |
| 367 InterfaceRequest<TestA> request) override { | 334 InterfaceRequest<TestA> request) override { |
| 368 mojo::InterfaceHandle<mojo::ServiceProvider> incoming_sp_handle; | 335 mojo::InterfaceHandle<mojo::ServiceProvider> incoming_sp_handle; |
| 369 mojo::InterfaceHandle<mojo::ServiceProvider> outgoing_sp_handle; | |
| 370 mojo::InterfaceRequest<mojo::ServiceProvider> outgoing_sp_request = | |
| 371 GetProxy(&outgoing_sp_handle); | |
| 372 app_->shell()->ConnectToApplication(kTestBURLString, | 336 app_->shell()->ConnectToApplication(kTestBURLString, |
| 373 GetProxy(&incoming_sp_handle), | 337 GetProxy(&incoming_sp_handle), nullptr); |
| 374 outgoing_sp_handle.Pass()); | |
| 375 std::unique_ptr<mojo::ServiceProviderImpl> outgoing_sp_impl( | |
| 376 new mojo::ServiceProviderImpl(outgoing_sp_request.Pass())); | |
| 377 outgoing_sp_impl->AddService<TestC>(this); | |
| 378 outgoing_sp_impls_for_b_.push_back(std::move(outgoing_sp_impl)); | |
| 379 a_bindings_.push_back( | 338 a_bindings_.push_back( |
| 380 new TestAImpl(incoming_sp_handle.Pass(), context_, request.Pass())); | 339 new TestAImpl(incoming_sp_handle.Pass(), context_, request.Pass())); |
| 381 } | 340 } |
| 382 | 341 |
| 383 void Create(ApplicationConnection* connection, | 342 void Create(ApplicationConnection* connection, |
| 384 InterfaceRequest<TestB> request) override { | 343 InterfaceRequest<TestB> request) override { |
| 385 new TestBImpl(connection, context_, request.Pass()); | 344 new TestBImpl(connection, context_, request.Pass()); |
| 386 } | 345 } |
| 387 | 346 |
| 388 void Create(ApplicationConnection* connection, | |
| 389 InterfaceRequest<TestC> request) override { | |
| 390 new TestCImpl(connection, context_, request.Pass()); | |
| 391 } | |
| 392 | |
| 393 TesterContext* context_; | 347 TesterContext* context_; |
| 394 scoped_ptr<ApplicationImpl> app_; | 348 scoped_ptr<ApplicationImpl> app_; |
| 395 std::string requestor_url_; | 349 std::string requestor_url_; |
| 396 std::vector<std::unique_ptr<mojo::ServiceProviderImpl>> | |
| 397 outgoing_sp_impls_for_b_; | |
| 398 ScopedVector<TestAImpl> a_bindings_; | 350 ScopedVector<TestAImpl> a_bindings_; |
| 399 }; | 351 }; |
| 400 | 352 |
| 401 class TestDelegate : public ApplicationManager::Delegate { | 353 class TestDelegate : public ApplicationManager::Delegate { |
| 402 public: | 354 public: |
| 403 void AddMapping(const GURL& from, const GURL& to) { mappings_[from] = to; } | 355 void AddMapping(const GURL& from, const GURL& to) { mappings_[from] = to; } |
| 404 | 356 |
| 405 // ApplicationManager::Delegate | 357 // ApplicationManager::Delegate |
| 406 GURL ResolveMappings(const GURL& url) override { | 358 GURL ResolveMappings(const GURL& url) override { |
| 407 auto it = mappings_.find(url); | 359 auto it = mappings_.find(url); |
| (...skipping 308 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 716 AddLoaderForURL(GURL(kTestBURLString), kTestAURLString); | 668 AddLoaderForURL(GURL(kTestBURLString), kTestAURLString); |
| 717 | 669 |
| 718 TestAPtr a; | 670 TestAPtr a; |
| 719 application_manager_->ConnectToService(GURL(kTestAURLString), &a); | 671 application_manager_->ConnectToService(GURL(kTestAURLString), &a); |
| 720 a->CallB(); | 672 a->CallB(); |
| 721 loop_.Run(); | 673 loop_.Run(); |
| 722 EXPECT_EQ(1, tester_context_.num_b_calls()); | 674 EXPECT_EQ(1, tester_context_.num_b_calls()); |
| 723 EXPECT_TRUE(tester_context_.a_called_quit()); | 675 EXPECT_TRUE(tester_context_.a_called_quit()); |
| 724 } | 676 } |
| 725 | 677 |
| 726 // A calls B which calls C. | |
| 727 TEST_F(ApplicationManagerTest, BCallC) { | |
| 728 // Any url can load a. | |
| 729 AddLoaderForURL(GURL(kTestAURLString), std::string()); | |
| 730 | |
| 731 // Only a can load b. | |
| 732 AddLoaderForURL(GURL(kTestBURLString), kTestAURLString); | |
| 733 | |
| 734 TestAPtr a; | |
| 735 application_manager_->ConnectToService(GURL(kTestAURLString), &a); | |
| 736 a->CallCFromB(); | |
| 737 loop_.Run(); | |
| 738 | |
| 739 EXPECT_EQ(1, tester_context_.num_b_calls()); | |
| 740 EXPECT_EQ(1, tester_context_.num_c_calls()); | |
| 741 EXPECT_TRUE(tester_context_.a_called_quit()); | |
| 742 } | |
| 743 | |
| 744 // Confirm that a service impl will be deleted if the app that connected to | 678 // Confirm that a service impl will be deleted if the app that connected to |
| 745 // it goes away. | 679 // it goes away. |
| 746 TEST_F(ApplicationManagerTest, BDeleted) { | 680 TEST_F(ApplicationManagerTest, BDeleted) { |
| 747 AddLoaderForURL(GURL(kTestAURLString), std::string()); | 681 AddLoaderForURL(GURL(kTestAURLString), std::string()); |
| 748 AddLoaderForURL(GURL(kTestBURLString), std::string()); | 682 AddLoaderForURL(GURL(kTestBURLString), std::string()); |
| 749 | 683 |
| 750 TestAPtr a; | 684 TestAPtr a; |
| 751 application_manager_->ConnectToService(GURL(kTestAURLString), &a); | 685 application_manager_->ConnectToService(GURL(kTestAURLString), &a); |
| 752 | 686 |
| 753 a->CallB(); | 687 a->CallB(); |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 856 bool called = false; | 790 bool called = false; |
| 857 application_manager_->ConnectToApplication( | 791 application_manager_->ConnectToApplication( |
| 858 GURL("test:test"), GURL(), nullptr, nullptr, | 792 GURL("test:test"), GURL(), nullptr, nullptr, |
| 859 base::Bind(&QuitClosure, base::Unretained(&called))); | 793 base::Bind(&QuitClosure, base::Unretained(&called))); |
| 860 loop_.Run(); | 794 loop_.Run(); |
| 861 EXPECT_TRUE(called); | 795 EXPECT_TRUE(called); |
| 862 } | 796 } |
| 863 | 797 |
| 864 } // namespace | 798 } // namespace |
| 865 } // namespace shell | 799 } // namespace shell |
| OLD | NEW |