| 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" | 5 #include "mojo/shell/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 "base/run_loop.h" | 14 #include "base/run_loop.h" |
| 15 #include "mojo/public/cpp/bindings/strong_binding.h" | 15 #include "mojo/public/cpp/bindings/strong_binding.h" |
| 16 #include "mojo/shell/application_loader.h" | 16 #include "mojo/shell/application_loader.h" |
| 17 #include "mojo/shell/connect_util.h" | 17 #include "mojo/shell/connect_util.h" |
| 18 #include "mojo/shell/fetcher.h" | 18 #include "mojo/shell/fetcher.h" |
| 19 #include "mojo/shell/package_manager.h" | 19 #include "mojo/shell/package_manager.h" |
| 20 #include "mojo/shell/public/cpp/application_connection.h" | |
| 21 #include "mojo/shell/public/cpp/application_delegate.h" | |
| 22 #include "mojo/shell/public/cpp/application_impl.h" | 20 #include "mojo/shell/public/cpp/application_impl.h" |
| 23 #include "mojo/shell/public/cpp/interface_factory.h" | 21 #include "mojo/shell/public/cpp/interface_factory.h" |
| 22 #include "mojo/shell/public/cpp/shell_client.h" |
| 24 #include "mojo/shell/public/interfaces/service_provider.mojom.h" | 23 #include "mojo/shell/public/interfaces/service_provider.mojom.h" |
| 25 #include "mojo/shell/test.mojom.h" | 24 #include "mojo/shell/test.mojom.h" |
| 26 #include "mojo/shell/test_package_manager.h" | 25 #include "mojo/shell/test_package_manager.h" |
| 27 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
| 28 | 27 |
| 29 namespace mojo { | 28 namespace mojo { |
| 30 namespace shell { | 29 namespace shell { |
| 31 namespace test { | 30 namespace test { |
| 32 | 31 |
| 33 const char kTestURLString[] = "test:testService"; | 32 const char kTestURLString[] = "test:testService"; |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 88 base::Bind(&TestClient::AckTest, base::Unretained(this))); | 87 base::Bind(&TestClient::AckTest, base::Unretained(this))); |
| 89 } | 88 } |
| 90 | 89 |
| 91 private: | 90 private: |
| 92 TestServicePtr service_; | 91 TestServicePtr service_; |
| 93 bool quit_after_ack_; | 92 bool quit_after_ack_; |
| 94 DISALLOW_COPY_AND_ASSIGN(TestClient); | 93 DISALLOW_COPY_AND_ASSIGN(TestClient); |
| 95 }; | 94 }; |
| 96 | 95 |
| 97 class TestApplicationLoader : public ApplicationLoader, | 96 class TestApplicationLoader : public ApplicationLoader, |
| 98 public ApplicationDelegate, | 97 public ShellClient, |
| 99 public InterfaceFactory<TestService> { | 98 public InterfaceFactory<TestService> { |
| 100 public: | 99 public: |
| 101 TestApplicationLoader() | 100 TestApplicationLoader() |
| 102 : context_(nullptr), num_loads_(0) {} | 101 : context_(nullptr), num_loads_(0) {} |
| 103 | 102 |
| 104 ~TestApplicationLoader() override { | 103 ~TestApplicationLoader() override { |
| 105 if (context_) | 104 if (context_) |
| 106 ++context_->num_loader_deletes; | 105 ++context_->num_loader_deletes; |
| 107 test_app_.reset(); | 106 test_app_.reset(); |
| 108 } | 107 } |
| 109 | 108 |
| 110 void set_context(TestContext* context) { context_ = context; } | 109 void set_context(TestContext* context) { context_ = context; } |
| 111 int num_loads() const { return num_loads_; } | 110 int num_loads() const { return num_loads_; } |
| 112 const GURL& last_requestor_url() const { return last_requestor_url_; } | 111 const GURL& last_requestor_url() const { return last_requestor_url_; } |
| 113 | 112 |
| 114 private: | 113 private: |
| 115 // ApplicationLoader implementation. | 114 // ApplicationLoader implementation. |
| 116 void Load(const GURL& url, | 115 void Load(const GURL& url, |
| 117 InterfaceRequest<mojom::Application> application_request) override { | 116 InterfaceRequest<mojom::Application> application_request) override { |
| 118 ++num_loads_; | 117 ++num_loads_; |
| 119 test_app_.reset(new ApplicationImpl(this, std::move(application_request))); | 118 test_app_.reset(new ApplicationImpl(this, std::move(application_request))); |
| 120 } | 119 } |
| 121 | 120 |
| 122 // ApplicationDelegate implementation. | 121 // mojo::ShellClient implementation. |
| 123 bool AcceptConnection(ApplicationConnection* connection) override { | 122 bool AcceptConnection(Connection* connection) override { |
| 124 connection->AddService<TestService>(this); | 123 connection->AddService<TestService>(this); |
| 125 last_requestor_url_ = GURL(connection->GetRemoteApplicationURL()); | 124 last_requestor_url_ = GURL(connection->GetRemoteApplicationURL()); |
| 126 return true; | 125 return true; |
| 127 } | 126 } |
| 128 | 127 |
| 129 // InterfaceFactory<TestService> implementation. | 128 // InterfaceFactory<TestService> implementation. |
| 130 void Create(ApplicationConnection* connection, | 129 void Create(Connection* connection, |
| 131 InterfaceRequest<TestService> request) override { | 130 InterfaceRequest<TestService> request) override { |
| 132 new TestServiceImpl(context_, std::move(request)); | 131 new TestServiceImpl(context_, std::move(request)); |
| 133 } | 132 } |
| 134 | 133 |
| 135 scoped_ptr<ApplicationImpl> test_app_; | 134 scoped_ptr<ApplicationImpl> test_app_; |
| 136 TestContext* context_; | 135 TestContext* context_; |
| 137 int num_loads_; | 136 int num_loads_; |
| 138 GURL last_requestor_url_; | 137 GURL last_requestor_url_; |
| 139 | 138 |
| 140 DISALLOW_COPY_AND_ASSIGN(TestApplicationLoader); | 139 DISALLOW_COPY_AND_ASSIGN(TestApplicationLoader); |
| (...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 270 void CallCFromB() override { | 269 void CallCFromB() override { |
| 271 b_->CallC(base::Bind(&TestAImpl::Quit, base::Unretained(this))); | 270 b_->CallC(base::Bind(&TestAImpl::Quit, base::Unretained(this))); |
| 272 } | 271 } |
| 273 | 272 |
| 274 void Quit() { | 273 void Quit() { |
| 275 base::MessageLoop::current()->QuitWhenIdle(); | 274 base::MessageLoop::current()->QuitWhenIdle(); |
| 276 test_context_->set_a_called_quit(); | 275 test_context_->set_a_called_quit(); |
| 277 test_context_->QuitSoon(); | 276 test_context_->QuitSoon(); |
| 278 } | 277 } |
| 279 | 278 |
| 280 scoped_ptr<ApplicationConnection> connection_; | 279 scoped_ptr<Connection> connection_; |
| 281 TesterContext* test_context_; | 280 TesterContext* test_context_; |
| 282 TestBPtr b_; | 281 TestBPtr b_; |
| 283 StrongBinding<TestA> binding_; | 282 StrongBinding<TestA> binding_; |
| 284 }; | 283 }; |
| 285 | 284 |
| 286 class TestBImpl : public TestB { | 285 class TestBImpl : public TestB { |
| 287 public: | 286 public: |
| 288 TestBImpl(ApplicationConnection* connection, | 287 TestBImpl(Connection* connection, |
| 289 TesterContext* test_context, | 288 TesterContext* test_context, |
| 290 InterfaceRequest<TestB> request) | 289 InterfaceRequest<TestB> request) |
| 291 : test_context_(test_context), binding_(this, std::move(request)) { | 290 : test_context_(test_context), binding_(this, std::move(request)) { |
| 292 connection->ConnectToService(&c_); | 291 connection->ConnectToService(&c_); |
| 293 } | 292 } |
| 294 | 293 |
| 295 ~TestBImpl() override { | 294 ~TestBImpl() override { |
| 296 test_context_->IncrementNumBDeletes(); | 295 test_context_->IncrementNumBDeletes(); |
| 297 if (base::MessageLoop::current()->is_running()) | 296 if (base::MessageLoop::current()->is_running()) |
| 298 base::MessageLoop::current()->QuitWhenIdle(); | 297 base::MessageLoop::current()->QuitWhenIdle(); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 310 c_->C(callback); | 309 c_->C(callback); |
| 311 } | 310 } |
| 312 | 311 |
| 313 TesterContext* test_context_; | 312 TesterContext* test_context_; |
| 314 TestCPtr c_; | 313 TestCPtr c_; |
| 315 StrongBinding<TestB> binding_; | 314 StrongBinding<TestB> binding_; |
| 316 }; | 315 }; |
| 317 | 316 |
| 318 class TestCImpl : public TestC { | 317 class TestCImpl : public TestC { |
| 319 public: | 318 public: |
| 320 TestCImpl(ApplicationConnection* connection, | 319 TestCImpl(Connection* connection, |
| 321 TesterContext* test_context, | 320 TesterContext* test_context, |
| 322 InterfaceRequest<TestC> request) | 321 InterfaceRequest<TestC> request) |
| 323 : test_context_(test_context), binding_(this, std::move(request)) {} | 322 : test_context_(test_context), binding_(this, std::move(request)) {} |
| 324 | 323 |
| 325 ~TestCImpl() override { test_context_->IncrementNumCDeletes(); } | 324 ~TestCImpl() override { test_context_->IncrementNumCDeletes(); } |
| 326 | 325 |
| 327 private: | 326 private: |
| 328 void C(const Callback<void()>& callback) override { | 327 void C(const Callback<void()>& callback) override { |
| 329 test_context_->IncrementNumCCalls(); | 328 test_context_->IncrementNumCCalls(); |
| 330 callback.Run(); | 329 callback.Run(); |
| 331 } | 330 } |
| 332 | 331 |
| 333 TesterContext* test_context_; | 332 TesterContext* test_context_; |
| 334 StrongBinding<TestC> binding_; | 333 StrongBinding<TestC> binding_; |
| 335 }; | 334 }; |
| 336 | 335 |
| 337 class Tester : public ApplicationDelegate, | 336 class Tester : public ShellClient, |
| 338 public ApplicationLoader, | 337 public ApplicationLoader, |
| 339 public InterfaceFactory<TestA>, | 338 public InterfaceFactory<TestA>, |
| 340 public InterfaceFactory<TestB>, | 339 public InterfaceFactory<TestB>, |
| 341 public InterfaceFactory<TestC> { | 340 public InterfaceFactory<TestC> { |
| 342 public: | 341 public: |
| 343 Tester(TesterContext* context, const std::string& requestor_url) | 342 Tester(TesterContext* context, const std::string& requestor_url) |
| 344 : context_(context), requestor_url_(requestor_url) {} | 343 : context_(context), requestor_url_(requestor_url) {} |
| 345 ~Tester() override {} | 344 ~Tester() override {} |
| 346 | 345 |
| 347 private: | 346 private: |
| 348 void Load(const GURL& url, | 347 void Load(const GURL& url, |
| 349 InterfaceRequest<mojom::Application> application_request) override { | 348 InterfaceRequest<mojom::Application> application_request) override { |
| 350 app_.reset(new ApplicationImpl(this, std::move(application_request))); | 349 app_.reset(new ApplicationImpl(this, std::move(application_request))); |
| 351 } | 350 } |
| 352 | 351 |
| 353 bool AcceptConnection(ApplicationConnection* connection) override { | 352 bool AcceptConnection(Connection* connection) override { |
| 354 if (!requestor_url_.empty() && | 353 if (!requestor_url_.empty() && |
| 355 requestor_url_ != connection->GetRemoteApplicationURL()) { | 354 requestor_url_ != connection->GetRemoteApplicationURL()) { |
| 356 context_->set_tester_called_quit(); | 355 context_->set_tester_called_quit(); |
| 357 context_->QuitSoon(); | 356 context_->QuitSoon(); |
| 358 base::MessageLoop::current()->QuitWhenIdle(); | 357 base::MessageLoop::current()->QuitWhenIdle(); |
| 359 return false; | 358 return false; |
| 360 } | 359 } |
| 361 // If we're coming from A, then add B, otherwise A. | 360 // If we're coming from A, then add B, otherwise A. |
| 362 if (connection->GetRemoteApplicationURL() == kTestAURLString) | 361 if (connection->GetRemoteApplicationURL() == kTestAURLString) |
| 363 connection->AddService<TestB>(this); | 362 connection->AddService<TestB>(this); |
| 364 else | 363 else |
| 365 connection->AddService<TestA>(this); | 364 connection->AddService<TestA>(this); |
| 366 return true; | 365 return true; |
| 367 } | 366 } |
| 368 | 367 |
| 369 void Create(ApplicationConnection* connection, | 368 void Create(Connection* connection, |
| 370 InterfaceRequest<TestA> request) override { | 369 InterfaceRequest<TestA> request) override { |
| 371 a_bindings_.push_back( | 370 a_bindings_.push_back( |
| 372 new TestAImpl(app_.get(), context_, std::move(request), this)); | 371 new TestAImpl(app_.get(), context_, std::move(request), this)); |
| 373 } | 372 } |
| 374 | 373 |
| 375 void Create(ApplicationConnection* connection, | 374 void Create(Connection* connection, |
| 376 InterfaceRequest<TestB> request) override { | 375 InterfaceRequest<TestB> request) override { |
| 377 new TestBImpl(connection, context_, std::move(request)); | 376 new TestBImpl(connection, context_, std::move(request)); |
| 378 } | 377 } |
| 379 | 378 |
| 380 void Create(ApplicationConnection* connection, | 379 void Create(Connection* connection, |
| 381 InterfaceRequest<TestC> request) override { | 380 InterfaceRequest<TestC> request) override { |
| 382 new TestCImpl(connection, context_, std::move(request)); | 381 new TestCImpl(connection, context_, std::move(request)); |
| 383 } | 382 } |
| 384 | 383 |
| 385 TesterContext* context_; | 384 TesterContext* context_; |
| 386 scoped_ptr<ApplicationImpl> app_; | 385 scoped_ptr<ApplicationImpl> app_; |
| 387 std::string requestor_url_; | 386 std::string requestor_url_; |
| 388 ScopedVector<TestAImpl> a_bindings_; | 387 ScopedVector<TestAImpl> a_bindings_; |
| 389 }; | 388 }; |
| 390 | 389 |
| (...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 622 | 621 |
| 623 // A different identity because the domain is different. | 622 // A different identity because the domain is different. |
| 624 ConnectToService(application_manager_.get(), | 623 ConnectToService(application_manager_.get(), |
| 625 GURL("http://www.another_domain.org/abc"), &test_service); | 624 GURL("http://www.another_domain.org/abc"), &test_service); |
| 626 EXPECT_EQ(4, test_loader_->num_loads()); | 625 EXPECT_EQ(4, test_loader_->num_loads()); |
| 627 } | 626 } |
| 628 | 627 |
| 629 } // namespace test | 628 } // namespace test |
| 630 } // namespace shell | 629 } // namespace shell |
| 631 } // namespace mojo | 630 } // namespace mojo |
| OLD | NEW |