| 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 "mojo/shell/application_manager.h" |
| 6 |
| 7 #include <utility> |
| 8 |
| 5 #include "base/at_exit.h" | 9 #include "base/at_exit.h" |
| 6 #include "base/bind.h" | 10 #include "base/bind.h" |
| 7 #include "base/macros.h" | 11 #include "base/macros.h" |
| 8 #include "base/memory/scoped_vector.h" | 12 #include "base/memory/scoped_vector.h" |
| 9 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
| 10 #include "base/run_loop.h" | 14 #include "base/run_loop.h" |
| 11 #include "mojo/application/public/cpp/application_connection.h" | 15 #include "mojo/application/public/cpp/application_connection.h" |
| 12 #include "mojo/application/public/cpp/application_delegate.h" | 16 #include "mojo/application/public/cpp/application_delegate.h" |
| 13 #include "mojo/application/public/cpp/application_impl.h" | 17 #include "mojo/application/public/cpp/application_impl.h" |
| 14 #include "mojo/application/public/cpp/interface_factory.h" | 18 #include "mojo/application/public/cpp/interface_factory.h" |
| 15 #include "mojo/application/public/interfaces/service_provider.mojom.h" | 19 #include "mojo/application/public/interfaces/service_provider.mojom.h" |
| 16 #include "mojo/public/cpp/bindings/strong_binding.h" | 20 #include "mojo/public/cpp/bindings/strong_binding.h" |
| 17 #include "mojo/shell/application_loader.h" | 21 #include "mojo/shell/application_loader.h" |
| 18 #include "mojo/shell/application_manager.h" | |
| 19 #include "mojo/shell/connect_util.h" | 22 #include "mojo/shell/connect_util.h" |
| 20 #include "mojo/shell/fetcher.h" | 23 #include "mojo/shell/fetcher.h" |
| 21 #include "mojo/shell/package_manager.h" | 24 #include "mojo/shell/package_manager.h" |
| 22 #include "mojo/shell/test.mojom.h" | 25 #include "mojo/shell/test.mojom.h" |
| 23 #include "mojo/shell/test_package_manager.h" | 26 #include "mojo/shell/test_package_manager.h" |
| 24 #include "testing/gtest/include/gtest/gtest.h" | 27 #include "testing/gtest/include/gtest/gtest.h" |
| 25 | 28 |
| 26 namespace mojo { | 29 namespace mojo { |
| 27 namespace shell { | 30 namespace shell { |
| 28 namespace test { | 31 namespace test { |
| (...skipping 10 matching lines...) Expand all Loading... |
| 39 }; | 42 }; |
| 40 | 43 |
| 41 void QuitClosure(bool* value) { | 44 void QuitClosure(bool* value) { |
| 42 *value = true; | 45 *value = true; |
| 43 base::MessageLoop::current()->QuitWhenIdle(); | 46 base::MessageLoop::current()->QuitWhenIdle(); |
| 44 } | 47 } |
| 45 | 48 |
| 46 class TestServiceImpl : public TestService { | 49 class TestServiceImpl : public TestService { |
| 47 public: | 50 public: |
| 48 TestServiceImpl(TestContext* context, InterfaceRequest<TestService> request) | 51 TestServiceImpl(TestContext* context, InterfaceRequest<TestService> request) |
| 49 : context_(context), binding_(this, request.Pass()) { | 52 : context_(context), binding_(this, std::move(request)) { |
| 50 ++context_->num_impls; | 53 ++context_->num_impls; |
| 51 } | 54 } |
| 52 | 55 |
| 53 ~TestServiceImpl() override { | 56 ~TestServiceImpl() override { |
| 54 --context_->num_impls; | 57 --context_->num_impls; |
| 55 if (!base::MessageLoop::current()->is_running()) | 58 if (!base::MessageLoop::current()->is_running()) |
| 56 return; | 59 return; |
| 57 base::MessageLoop::current()->QuitWhenIdle(); | 60 base::MessageLoop::current()->QuitWhenIdle(); |
| 58 } | 61 } |
| 59 | 62 |
| 60 // TestService implementation: | 63 // TestService implementation: |
| 61 void Test(const String& test_string, | 64 void Test(const String& test_string, |
| 62 const Callback<void()>& callback) override { | 65 const Callback<void()>& callback) override { |
| 63 context_->last_test_string = test_string; | 66 context_->last_test_string = test_string; |
| 64 callback.Run(); | 67 callback.Run(); |
| 65 } | 68 } |
| 66 | 69 |
| 67 private: | 70 private: |
| 68 TestContext* context_; | 71 TestContext* context_; |
| 69 StrongBinding<TestService> binding_; | 72 StrongBinding<TestService> binding_; |
| 70 }; | 73 }; |
| 71 | 74 |
| 72 class TestClient { | 75 class TestClient { |
| 73 public: | 76 public: |
| 74 explicit TestClient(TestServicePtr service) | 77 explicit TestClient(TestServicePtr service) |
| 75 : service_(service.Pass()), quit_after_ack_(false) {} | 78 : service_(std::move(service)), quit_after_ack_(false) {} |
| 76 | 79 |
| 77 void AckTest() { | 80 void AckTest() { |
| 78 if (quit_after_ack_) | 81 if (quit_after_ack_) |
| 79 base::MessageLoop::current()->QuitWhenIdle(); | 82 base::MessageLoop::current()->QuitWhenIdle(); |
| 80 } | 83 } |
| 81 | 84 |
| 82 void Test(const std::string& test_string) { | 85 void Test(const std::string& test_string) { |
| 83 quit_after_ack_ = true; | 86 quit_after_ack_ = true; |
| 84 service_->Test(test_string, | 87 service_->Test(test_string, |
| 85 base::Bind(&TestClient::AckTest, base::Unretained(this))); | 88 base::Bind(&TestClient::AckTest, base::Unretained(this))); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 106 | 109 |
| 107 void set_context(TestContext* context) { context_ = context; } | 110 void set_context(TestContext* context) { context_ = context; } |
| 108 int num_loads() const { return num_loads_; } | 111 int num_loads() const { return num_loads_; } |
| 109 const GURL& last_requestor_url() const { return last_requestor_url_; } | 112 const GURL& last_requestor_url() const { return last_requestor_url_; } |
| 110 | 113 |
| 111 private: | 114 private: |
| 112 // ApplicationLoader implementation. | 115 // ApplicationLoader implementation. |
| 113 void Load(const GURL& url, | 116 void Load(const GURL& url, |
| 114 InterfaceRequest<Application> application_request) override { | 117 InterfaceRequest<Application> application_request) override { |
| 115 ++num_loads_; | 118 ++num_loads_; |
| 116 test_app_.reset(new ApplicationImpl(this, application_request.Pass())); | 119 test_app_.reset(new ApplicationImpl(this, std::move(application_request))); |
| 117 } | 120 } |
| 118 | 121 |
| 119 // ApplicationDelegate implementation. | 122 // ApplicationDelegate implementation. |
| 120 bool ConfigureIncomingConnection(ApplicationConnection* connection) override { | 123 bool ConfigureIncomingConnection(ApplicationConnection* connection) override { |
| 121 connection->AddService<TestService>(this); | 124 connection->AddService<TestService>(this); |
| 122 last_requestor_url_ = GURL(connection->GetRemoteApplicationURL()); | 125 last_requestor_url_ = GURL(connection->GetRemoteApplicationURL()); |
| 123 return true; | 126 return true; |
| 124 } | 127 } |
| 125 | 128 |
| 126 // InterfaceFactory<TestService> implementation. | 129 // InterfaceFactory<TestService> implementation. |
| 127 void Create(ApplicationConnection* connection, | 130 void Create(ApplicationConnection* connection, |
| 128 InterfaceRequest<TestService> request) override { | 131 InterfaceRequest<TestService> request) override { |
| 129 new TestServiceImpl(context_, request.Pass()); | 132 new TestServiceImpl(context_, std::move(request)); |
| 130 } | 133 } |
| 131 | 134 |
| 132 scoped_ptr<ApplicationImpl> test_app_; | 135 scoped_ptr<ApplicationImpl> test_app_; |
| 133 TestContext* context_; | 136 TestContext* context_; |
| 134 int num_loads_; | 137 int num_loads_; |
| 135 GURL last_requestor_url_; | 138 GURL last_requestor_url_; |
| 136 | 139 |
| 137 DISALLOW_COPY_AND_ASSIGN(TestApplicationLoader); | 140 DISALLOW_COPY_AND_ASSIGN(TestApplicationLoader); |
| 138 }; | 141 }; |
| 139 | 142 |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 238 | 241 |
| 239 base::MessageLoop* loop_; | 242 base::MessageLoop* loop_; |
| 240 }; | 243 }; |
| 241 | 244 |
| 242 // Used to test that the requestor url will be correctly passed. | 245 // Used to test that the requestor url will be correctly passed. |
| 243 class TestAImpl : public TestA { | 246 class TestAImpl : public TestA { |
| 244 public: | 247 public: |
| 245 TestAImpl(ApplicationImpl* app_impl, | 248 TestAImpl(ApplicationImpl* app_impl, |
| 246 TesterContext* test_context, | 249 TesterContext* test_context, |
| 247 InterfaceRequest<TestA> request) | 250 InterfaceRequest<TestA> request) |
| 248 : test_context_(test_context), binding_(this, request.Pass()) { | 251 : test_context_(test_context), binding_(this, std::move(request)) { |
| 249 connection_ = app_impl->ConnectToApplication(kTestBURLString); | 252 connection_ = app_impl->ConnectToApplication(kTestBURLString); |
| 250 connection_->ConnectToService(&b_); | 253 connection_->ConnectToService(&b_); |
| 251 } | 254 } |
| 252 | 255 |
| 253 ~TestAImpl() override { | 256 ~TestAImpl() override { |
| 254 test_context_->IncrementNumADeletes(); | 257 test_context_->IncrementNumADeletes(); |
| 255 if (base::MessageLoop::current()->is_running()) | 258 if (base::MessageLoop::current()->is_running()) |
| 256 Quit(); | 259 Quit(); |
| 257 } | 260 } |
| 258 | 261 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 275 TesterContext* test_context_; | 278 TesterContext* test_context_; |
| 276 TestBPtr b_; | 279 TestBPtr b_; |
| 277 StrongBinding<TestA> binding_; | 280 StrongBinding<TestA> binding_; |
| 278 }; | 281 }; |
| 279 | 282 |
| 280 class TestBImpl : public TestB { | 283 class TestBImpl : public TestB { |
| 281 public: | 284 public: |
| 282 TestBImpl(ApplicationConnection* connection, | 285 TestBImpl(ApplicationConnection* connection, |
| 283 TesterContext* test_context, | 286 TesterContext* test_context, |
| 284 InterfaceRequest<TestB> request) | 287 InterfaceRequest<TestB> request) |
| 285 : test_context_(test_context), binding_(this, request.Pass()) { | 288 : test_context_(test_context), binding_(this, std::move(request)) { |
| 286 connection->ConnectToService(&c_); | 289 connection->ConnectToService(&c_); |
| 287 } | 290 } |
| 288 | 291 |
| 289 ~TestBImpl() override { | 292 ~TestBImpl() override { |
| 290 test_context_->IncrementNumBDeletes(); | 293 test_context_->IncrementNumBDeletes(); |
| 291 if (base::MessageLoop::current()->is_running()) | 294 if (base::MessageLoop::current()->is_running()) |
| 292 base::MessageLoop::current()->QuitWhenIdle(); | 295 base::MessageLoop::current()->QuitWhenIdle(); |
| 293 test_context_->QuitSoon(); | 296 test_context_->QuitSoon(); |
| 294 } | 297 } |
| 295 | 298 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 307 TesterContext* test_context_; | 310 TesterContext* test_context_; |
| 308 TestCPtr c_; | 311 TestCPtr c_; |
| 309 StrongBinding<TestB> binding_; | 312 StrongBinding<TestB> binding_; |
| 310 }; | 313 }; |
| 311 | 314 |
| 312 class TestCImpl : public TestC { | 315 class TestCImpl : public TestC { |
| 313 public: | 316 public: |
| 314 TestCImpl(ApplicationConnection* connection, | 317 TestCImpl(ApplicationConnection* connection, |
| 315 TesterContext* test_context, | 318 TesterContext* test_context, |
| 316 InterfaceRequest<TestC> request) | 319 InterfaceRequest<TestC> request) |
| 317 : test_context_(test_context), binding_(this, request.Pass()) {} | 320 : test_context_(test_context), binding_(this, std::move(request)) {} |
| 318 | 321 |
| 319 ~TestCImpl() override { test_context_->IncrementNumCDeletes(); } | 322 ~TestCImpl() override { test_context_->IncrementNumCDeletes(); } |
| 320 | 323 |
| 321 private: | 324 private: |
| 322 void C(const Callback<void()>& callback) override { | 325 void C(const Callback<void()>& callback) override { |
| 323 test_context_->IncrementNumCCalls(); | 326 test_context_->IncrementNumCCalls(); |
| 324 callback.Run(); | 327 callback.Run(); |
| 325 } | 328 } |
| 326 | 329 |
| 327 TesterContext* test_context_; | 330 TesterContext* test_context_; |
| 328 StrongBinding<TestC> binding_; | 331 StrongBinding<TestC> binding_; |
| 329 }; | 332 }; |
| 330 | 333 |
| 331 class Tester : public ApplicationDelegate, | 334 class Tester : public ApplicationDelegate, |
| 332 public ApplicationLoader, | 335 public ApplicationLoader, |
| 333 public InterfaceFactory<TestA>, | 336 public InterfaceFactory<TestA>, |
| 334 public InterfaceFactory<TestB>, | 337 public InterfaceFactory<TestB>, |
| 335 public InterfaceFactory<TestC> { | 338 public InterfaceFactory<TestC> { |
| 336 public: | 339 public: |
| 337 Tester(TesterContext* context, const std::string& requestor_url) | 340 Tester(TesterContext* context, const std::string& requestor_url) |
| 338 : context_(context), requestor_url_(requestor_url) {} | 341 : context_(context), requestor_url_(requestor_url) {} |
| 339 ~Tester() override {} | 342 ~Tester() override {} |
| 340 | 343 |
| 341 private: | 344 private: |
| 342 void Load(const GURL& url, | 345 void Load(const GURL& url, |
| 343 InterfaceRequest<Application> application_request) override { | 346 InterfaceRequest<Application> application_request) override { |
| 344 app_.reset(new ApplicationImpl(this, application_request.Pass())); | 347 app_.reset(new ApplicationImpl(this, std::move(application_request))); |
| 345 } | 348 } |
| 346 | 349 |
| 347 bool ConfigureIncomingConnection(ApplicationConnection* connection) override { | 350 bool ConfigureIncomingConnection(ApplicationConnection* connection) override { |
| 348 if (!requestor_url_.empty() && | 351 if (!requestor_url_.empty() && |
| 349 requestor_url_ != connection->GetRemoteApplicationURL()) { | 352 requestor_url_ != connection->GetRemoteApplicationURL()) { |
| 350 context_->set_tester_called_quit(); | 353 context_->set_tester_called_quit(); |
| 351 context_->QuitSoon(); | 354 context_->QuitSoon(); |
| 352 base::MessageLoop::current()->QuitWhenIdle(); | 355 base::MessageLoop::current()->QuitWhenIdle(); |
| 353 return false; | 356 return false; |
| 354 } | 357 } |
| 355 // If we're coming from A, then add B, otherwise A. | 358 // If we're coming from A, then add B, otherwise A. |
| 356 if (connection->GetRemoteApplicationURL() == kTestAURLString) | 359 if (connection->GetRemoteApplicationURL() == kTestAURLString) |
| 357 connection->AddService<TestB>(this); | 360 connection->AddService<TestB>(this); |
| 358 else | 361 else |
| 359 connection->AddService<TestA>(this); | 362 connection->AddService<TestA>(this); |
| 360 return true; | 363 return true; |
| 361 } | 364 } |
| 362 | 365 |
| 363 bool ConfigureOutgoingConnection(ApplicationConnection* connection) override { | 366 bool ConfigureOutgoingConnection(ApplicationConnection* connection) override { |
| 364 // If we're connecting to B, then add C. | 367 // If we're connecting to B, then add C. |
| 365 if (connection->GetRemoteApplicationURL() == kTestBURLString) | 368 if (connection->GetRemoteApplicationURL() == kTestBURLString) |
| 366 connection->AddService<TestC>(this); | 369 connection->AddService<TestC>(this); |
| 367 return true; | 370 return true; |
| 368 } | 371 } |
| 369 | 372 |
| 370 void Create(ApplicationConnection* connection, | 373 void Create(ApplicationConnection* connection, |
| 371 InterfaceRequest<TestA> request) override { | 374 InterfaceRequest<TestA> request) override { |
| 372 a_bindings_.push_back(new TestAImpl(app_.get(), context_, request.Pass())); | 375 a_bindings_.push_back( |
| 376 new TestAImpl(app_.get(), context_, std::move(request))); |
| 373 } | 377 } |
| 374 | 378 |
| 375 void Create(ApplicationConnection* connection, | 379 void Create(ApplicationConnection* connection, |
| 376 InterfaceRequest<TestB> request) override { | 380 InterfaceRequest<TestB> request) override { |
| 377 new TestBImpl(connection, context_, request.Pass()); | 381 new TestBImpl(connection, context_, std::move(request)); |
| 378 } | 382 } |
| 379 | 383 |
| 380 void Create(ApplicationConnection* connection, | 384 void Create(ApplicationConnection* connection, |
| 381 InterfaceRequest<TestC> request) override { | 385 InterfaceRequest<TestC> request) override { |
| 382 new TestCImpl(connection, context_, request.Pass()); | 386 new TestCImpl(connection, context_, std::move(request)); |
| 383 } | 387 } |
| 384 | 388 |
| 385 TesterContext* context_; | 389 TesterContext* context_; |
| 386 scoped_ptr<ApplicationImpl> app_; | 390 scoped_ptr<ApplicationImpl> app_; |
| 387 std::string requestor_url_; | 391 std::string requestor_url_; |
| 388 ScopedVector<TestAImpl> a_bindings_; | 392 ScopedVector<TestAImpl> a_bindings_; |
| 389 }; | 393 }; |
| 390 | 394 |
| 391 class ApplicationManagerTest : public testing::Test { | 395 class ApplicationManagerTest : public testing::Test { |
| 392 public: | 396 public: |
| 393 ApplicationManagerTest() : tester_context_(&loop_) {} | 397 ApplicationManagerTest() : tester_context_(&loop_) {} |
| 394 | 398 |
| 395 ~ApplicationManagerTest() override {} | 399 ~ApplicationManagerTest() override {} |
| 396 | 400 |
| 397 void SetUp() override { | 401 void SetUp() override { |
| 398 application_manager_.reset(new ApplicationManager( | 402 application_manager_.reset(new ApplicationManager( |
| 399 make_scoped_ptr(new TestPackageManager))); | 403 make_scoped_ptr(new TestPackageManager))); |
| 400 test_loader_ = new TestApplicationLoader; | 404 test_loader_ = new TestApplicationLoader; |
| 401 test_loader_->set_context(&context_); | 405 test_loader_->set_context(&context_); |
| 402 application_manager_->set_default_loader( | 406 application_manager_->set_default_loader( |
| 403 scoped_ptr<ApplicationLoader>(test_loader_)); | 407 scoped_ptr<ApplicationLoader>(test_loader_)); |
| 404 | 408 |
| 405 TestServicePtr service_proxy; | 409 TestServicePtr service_proxy; |
| 406 ConnectToService(application_manager_.get(), GURL(kTestURLString), | 410 ConnectToService(application_manager_.get(), GURL(kTestURLString), |
| 407 &service_proxy); | 411 &service_proxy); |
| 408 test_client_.reset(new TestClient(service_proxy.Pass())); | 412 test_client_.reset(new TestClient(std::move(service_proxy))); |
| 409 } | 413 } |
| 410 | 414 |
| 411 void TearDown() override { | 415 void TearDown() override { |
| 412 test_client_.reset(); | 416 test_client_.reset(); |
| 413 application_manager_.reset(); | 417 application_manager_.reset(); |
| 414 } | 418 } |
| 415 | 419 |
| 416 void AddLoaderForURL(const GURL& url, const std::string& requestor_url) { | 420 void AddLoaderForURL(const GURL& url, const std::string& requestor_url) { |
| 417 application_manager_->SetLoaderForURL( | 421 application_manager_->SetLoaderForURL( |
| 418 make_scoped_ptr(new Tester(&tester_context_, requestor_url)), url); | 422 make_scoped_ptr(new Tester(&tester_context_, requestor_url)), url); |
| (...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 583 TEST_F(ApplicationManagerTest, TestEndApplicationClosure) { | 587 TEST_F(ApplicationManagerTest, TestEndApplicationClosure) { |
| 584 ClosingApplicationLoader* loader = new ClosingApplicationLoader(); | 588 ClosingApplicationLoader* loader = new ClosingApplicationLoader(); |
| 585 application_manager_->SetLoaderForURL( | 589 application_manager_->SetLoaderForURL( |
| 586 scoped_ptr<ApplicationLoader>(loader), GURL("test:test")); | 590 scoped_ptr<ApplicationLoader>(loader), GURL("test:test")); |
| 587 | 591 |
| 588 bool called = false; | 592 bool called = false; |
| 589 scoped_ptr<ConnectToApplicationParams> params(new ConnectToApplicationParams); | 593 scoped_ptr<ConnectToApplicationParams> params(new ConnectToApplicationParams); |
| 590 params->SetTargetURL(GURL("test:test")); | 594 params->SetTargetURL(GURL("test:test")); |
| 591 params->set_on_application_end( | 595 params->set_on_application_end( |
| 592 base::Bind(&QuitClosure, base::Unretained(&called))); | 596 base::Bind(&QuitClosure, base::Unretained(&called))); |
| 593 application_manager_->ConnectToApplication(params.Pass()); | 597 application_manager_->ConnectToApplication(std::move(params)); |
| 594 loop_.Run(); | 598 loop_.Run(); |
| 595 EXPECT_TRUE(called); | 599 EXPECT_TRUE(called); |
| 596 } | 600 } |
| 597 | 601 |
| 598 TEST_F(ApplicationManagerTest, SameIdentityShouldNotCauseDuplicateLoad) { | 602 TEST_F(ApplicationManagerTest, SameIdentityShouldNotCauseDuplicateLoad) { |
| 599 // 1 because ApplicationManagerTest connects once at startup. | 603 // 1 because ApplicationManagerTest connects once at startup. |
| 600 EXPECT_EQ(1, test_loader_->num_loads()); | 604 EXPECT_EQ(1, test_loader_->num_loads()); |
| 601 | 605 |
| 602 TestServicePtr test_service; | 606 TestServicePtr test_service; |
| 603 ConnectToService(application_manager_.get(), | 607 ConnectToService(application_manager_.get(), |
| (...skipping 18 matching lines...) Expand all Loading... |
| 622 | 626 |
| 623 // A different identity because the domain is different. | 627 // A different identity because the domain is different. |
| 624 ConnectToService(application_manager_.get(), | 628 ConnectToService(application_manager_.get(), |
| 625 GURL("http://www.another_domain.org/abc"), &test_service); | 629 GURL("http://www.another_domain.org/abc"), &test_service); |
| 626 EXPECT_EQ(4, test_loader_->num_loads()); | 630 EXPECT_EQ(4, test_loader_->num_loads()); |
| 627 } | 631 } |
| 628 | 632 |
| 629 } // namespace test | 633 } // namespace test |
| 630 } // namespace shell | 634 } // namespace shell |
| 631 } // namespace mojo | 635 } // namespace mojo |
| OLD | NEW |