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