| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 <memory> | 5 #include <memory> |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
| 9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
| 10 #include "mojo/public/c/system/main.h" | 10 #include "mojo/public/c/system/main.h" |
| 11 #include "mojo/public/cpp/bindings/binding_set.h" | 11 #include "mojo/public/cpp/bindings/binding_set.h" |
| 12 #include "services/shell/public/cpp/connector.h" | 12 #include "services/shell/public/cpp/connector.h" |
| 13 #include "services/shell/public/cpp/service.h" | 13 #include "services/shell/public/cpp/service.h" |
| 14 #include "services/shell/public/cpp/service_runner.h" | 14 #include "services/shell/public/cpp/service_runner.h" |
| 15 #include "services/shell/tests/lifecycle/lifecycle_unittest.mojom.h" | 15 #include "services/shell/tests/lifecycle/lifecycle_unittest.mojom.h" |
| 16 | 16 |
| 17 namespace { | 17 namespace { |
| 18 | 18 |
| 19 void QuitLoop(base::RunLoop* loop) { | 19 void QuitLoop(base::RunLoop* loop) { |
| 20 loop->Quit(); | 20 loop->Quit(); |
| 21 } | 21 } |
| 22 | 22 |
| 23 class Parent : public shell::Service, | 23 class Parent : public shell::Service, |
| 24 public shell::InterfaceFactory<shell::test::mojom::Parent>, | 24 public shell::InterfaceFactory<shell::test::mojom::Parent>, |
| 25 public shell::test::mojom::Parent { | 25 public shell::test::mojom::Parent { |
| 26 public: | 26 public: |
| 27 Parent() {} | 27 Parent() {} |
| 28 ~Parent() override { | 28 ~Parent() override { |
| 29 connector_ = nullptr; | |
| 30 child_connection_.reset(); | 29 child_connection_.reset(); |
| 31 parent_bindings_.CloseAllBindings(); | 30 parent_bindings_.CloseAllBindings(); |
| 32 } | 31 } |
| 33 | 32 |
| 34 private: | 33 private: |
| 35 // Service: | 34 // Service: |
| 36 void OnStart(shell::Connector* connector, | |
| 37 const shell::Identity& identity, | |
| 38 uint32_t id) override { | |
| 39 connector_ = connector; | |
| 40 } | |
| 41 bool OnConnect(shell::Connection* connection) override { | 35 bool OnConnect(shell::Connection* connection) override { |
| 42 connection->AddInterface<shell::test::mojom::Parent>(this); | 36 connection->AddInterface<shell::test::mojom::Parent>(this); |
| 43 return true; | 37 return true; |
| 44 } | 38 } |
| 45 | 39 |
| 46 // InterfaceFactory<shell::test::mojom::Parent>: | 40 // InterfaceFactory<shell::test::mojom::Parent>: |
| 47 void Create(const shell::Identity& remote_identity, | 41 void Create(const shell::Identity& remote_identity, |
| 48 shell::test::mojom::ParentRequest request) override { | 42 shell::test::mojom::ParentRequest request) override { |
| 49 parent_bindings_.AddBinding(this, std::move(request)); | 43 parent_bindings_.AddBinding(this, std::move(request)); |
| 50 } | 44 } |
| 51 | 45 |
| 52 // Parent: | 46 // Parent: |
| 53 void ConnectToChild(const ConnectToChildCallback& callback) override { | 47 void ConnectToChild(const ConnectToChildCallback& callback) override { |
| 54 child_connection_ = connector_->Connect("mojo:lifecycle_unittest_app"); | 48 child_connection_ = connector()->Connect("mojo:lifecycle_unittest_app"); |
| 55 shell::test::mojom::LifecycleControlPtr lifecycle; | 49 shell::test::mojom::LifecycleControlPtr lifecycle; |
| 56 child_connection_->GetInterface(&lifecycle); | 50 child_connection_->GetInterface(&lifecycle); |
| 57 { | 51 { |
| 58 base::RunLoop loop; | 52 base::RunLoop loop; |
| 59 lifecycle->Ping(base::Bind(&QuitLoop, &loop)); | 53 lifecycle->Ping(base::Bind(&QuitLoop, &loop)); |
| 60 base::MessageLoop::ScopedNestableTaskAllower allow( | 54 base::MessageLoop::ScopedNestableTaskAllower allow( |
| 61 base::MessageLoop::current()); | 55 base::MessageLoop::current()); |
| 62 loop.Run(); | 56 loop.Run(); |
| 63 } | 57 } |
| 64 callback.Run(); | 58 callback.Run(); |
| 65 } | 59 } |
| 66 void Quit() override { | 60 void Quit() override { |
| 67 base::MessageLoop::current()->QuitWhenIdle(); | 61 base::MessageLoop::current()->QuitWhenIdle(); |
| 68 } | 62 } |
| 69 | 63 |
| 70 shell::Connector* connector_; | |
| 71 std::unique_ptr<shell::Connection> child_connection_; | 64 std::unique_ptr<shell::Connection> child_connection_; |
| 72 mojo::BindingSet<shell::test::mojom::Parent> parent_bindings_; | 65 mojo::BindingSet<shell::test::mojom::Parent> parent_bindings_; |
| 73 | 66 |
| 74 DISALLOW_COPY_AND_ASSIGN(Parent); | 67 DISALLOW_COPY_AND_ASSIGN(Parent); |
| 75 }; | 68 }; |
| 76 | 69 |
| 77 } // namespace | 70 } // namespace |
| 78 | 71 |
| 79 MojoResult MojoMain(MojoHandle shell_handle) { | 72 MojoResult MojoMain(MojoHandle shell_handle) { |
| 80 Parent* parent = new Parent; | 73 Parent* parent = new Parent; |
| 81 return shell::ServiceRunner(parent).Run(shell_handle); | 74 return shell::ServiceRunner(parent).Run(shell_handle); |
| 82 } | 75 } |
| OLD | NEW |