| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include <memory> | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/guid.h" | |
| 9 #include "base/macros.h" | |
| 10 #include "base/message_loop/message_loop.h" | |
| 11 #include "base/run_loop.h" | |
| 12 #include "mojo/public/cpp/bindings/binding_set.h" | |
| 13 #include "services/shell/public/c/main.h" | |
| 14 #include "services/shell/public/cpp/connector.h" | |
| 15 #include "services/shell/public/cpp/interface_factory.h" | |
| 16 #include "services/shell/public/cpp/interface_registry.h" | |
| 17 #include "services/shell/public/cpp/service.h" | |
| 18 #include "services/shell/public/cpp/service_runner.h" | |
| 19 #include "services/shell/public/interfaces/connector.mojom.h" | |
| 20 #include "services/shell/tests/connect/connect_test.mojom.h" | |
| 21 | |
| 22 namespace shell { | |
| 23 | |
| 24 namespace { | |
| 25 | |
| 26 void QuitLoop(base::RunLoop* loop) { | |
| 27 loop->Quit(); | |
| 28 } | |
| 29 | |
| 30 void ReceiveString(std::string* string, | |
| 31 base::RunLoop* loop, | |
| 32 const std::string& response) { | |
| 33 *string = response; | |
| 34 loop->Quit(); | |
| 35 } | |
| 36 | |
| 37 } // namespace | |
| 38 | |
| 39 using GetTitleCallback = test::mojom::ConnectTestService::GetTitleCallback; | |
| 40 | |
| 41 class ConnectTestApp : public Service, | |
| 42 public InterfaceFactory<test::mojom::ConnectTestService>, | |
| 43 public InterfaceFactory<test::mojom::StandaloneApp>, | |
| 44 public InterfaceFactory<test::mojom::BlockedInterface>, | |
| 45 public InterfaceFactory<test::mojom::UserIdTest>, | |
| 46 public test::mojom::ConnectTestService, | |
| 47 public test::mojom::StandaloneApp, | |
| 48 public test::mojom::BlockedInterface, | |
| 49 public test::mojom::UserIdTest { | |
| 50 public: | |
| 51 ConnectTestApp() {} | |
| 52 ~ConnectTestApp() override {} | |
| 53 | |
| 54 private: | |
| 55 // shell::Service: | |
| 56 void OnStart(const Identity& identity) override { | |
| 57 identity_ = identity; | |
| 58 bindings_.set_connection_error_handler( | |
| 59 base::Bind(&ConnectTestApp::OnConnectionError, | |
| 60 base::Unretained(this))); | |
| 61 standalone_bindings_.set_connection_error_handler( | |
| 62 base::Bind(&ConnectTestApp::OnConnectionError, | |
| 63 base::Unretained(this))); | |
| 64 } | |
| 65 bool OnConnect(const Identity& remote_identity, | |
| 66 InterfaceRegistry* registry) override { | |
| 67 registry->AddInterface<test::mojom::ConnectTestService>(this); | |
| 68 registry->AddInterface<test::mojom::StandaloneApp>(this); | |
| 69 registry->AddInterface<test::mojom::BlockedInterface>(this); | |
| 70 registry->AddInterface<test::mojom::UserIdTest>(this); | |
| 71 | |
| 72 test::mojom::ConnectionStatePtr state(test::mojom::ConnectionState::New()); | |
| 73 state->connection_remote_name = remote_identity.name(); | |
| 74 state->connection_remote_userid = remote_identity.user_id(); | |
| 75 state->initialize_local_name = identity_.name(); | |
| 76 state->initialize_userid = identity_.user_id(); | |
| 77 | |
| 78 connector()->ConnectToInterface(remote_identity, &caller_); | |
| 79 caller_->ConnectionAccepted(std::move(state)); | |
| 80 | |
| 81 return true; | |
| 82 } | |
| 83 | |
| 84 // InterfaceFactory<test::mojom::ConnectTestService>: | |
| 85 void Create(const Identity& remote_identity, | |
| 86 test::mojom::ConnectTestServiceRequest request) override { | |
| 87 bindings_.AddBinding(this, std::move(request)); | |
| 88 } | |
| 89 | |
| 90 // InterfaceFactory<test::mojom::StandaloneApp>: | |
| 91 void Create(const Identity& remote_identity, | |
| 92 test::mojom::StandaloneAppRequest request) override { | |
| 93 standalone_bindings_.AddBinding(this, std::move(request)); | |
| 94 } | |
| 95 | |
| 96 // InterfaceFactory<test::mojom::BlockedInterface>: | |
| 97 void Create(const Identity& remote_identity, | |
| 98 test::mojom::BlockedInterfaceRequest request) override { | |
| 99 blocked_bindings_.AddBinding(this, std::move(request)); | |
| 100 } | |
| 101 | |
| 102 // InterfaceFactory<test::mojom::UserIdTest>: | |
| 103 void Create(const Identity& remote_identity, | |
| 104 test::mojom::UserIdTestRequest request) override { | |
| 105 user_id_test_bindings_.AddBinding(this, std::move(request)); | |
| 106 } | |
| 107 | |
| 108 // test::mojom::ConnectTestService: | |
| 109 void GetTitle(const GetTitleCallback& callback) override { | |
| 110 callback.Run("APP"); | |
| 111 } | |
| 112 void GetInstance(const GetInstanceCallback& callback) override { | |
| 113 callback.Run(identity_.instance()); | |
| 114 } | |
| 115 | |
| 116 // test::mojom::StandaloneApp: | |
| 117 void ConnectToAllowedAppInBlockedPackage( | |
| 118 const ConnectToAllowedAppInBlockedPackageCallback& callback) override { | |
| 119 base::RunLoop run_loop; | |
| 120 std::unique_ptr<Connection> connection = | |
| 121 connector()->Connect("service:connect_test_a"); | |
| 122 connection->SetConnectionLostClosure( | |
| 123 base::Bind(&ConnectTestApp::OnConnectionBlocked, | |
| 124 base::Unretained(this), callback, &run_loop)); | |
| 125 test::mojom::ConnectTestServicePtr test_service; | |
| 126 connection->GetInterface(&test_service); | |
| 127 test_service->GetTitle( | |
| 128 base::Bind(&ConnectTestApp::OnGotTitle, base::Unretained(this), | |
| 129 callback, &run_loop)); | |
| 130 { | |
| 131 // This message is dispatched as a task on the same run loop, so we need | |
| 132 // to allow nesting in order to pump additional signals. | |
| 133 base::MessageLoop::ScopedNestableTaskAllower allow( | |
| 134 base::MessageLoop::current()); | |
| 135 run_loop.Run(); | |
| 136 } | |
| 137 } | |
| 138 void ConnectToClassInterface( | |
| 139 const ConnectToClassInterfaceCallback& callback) override { | |
| 140 std::unique_ptr<Connection> connection = | |
| 141 connector()->Connect("service:connect_test_class_app"); | |
| 142 test::mojom::ClassInterfacePtr class_interface; | |
| 143 connection->GetInterface(&class_interface); | |
| 144 std::string ping_response; | |
| 145 { | |
| 146 base::RunLoop loop; | |
| 147 class_interface->Ping(base::Bind(&ReceiveString, &ping_response, &loop)); | |
| 148 base::MessageLoop::ScopedNestableTaskAllower allow( | |
| 149 base::MessageLoop::current()); | |
| 150 loop.Run(); | |
| 151 } | |
| 152 test::mojom::ConnectTestServicePtr service; | |
| 153 connection->GetInterface(&service); | |
| 154 std::string title_response; | |
| 155 { | |
| 156 base::RunLoop loop; | |
| 157 service->GetTitle(base::Bind(&ReceiveString, &title_response, &loop)); | |
| 158 base::MessageLoop::ScopedNestableTaskAllower allow( | |
| 159 base::MessageLoop::current()); | |
| 160 loop.Run(); | |
| 161 } | |
| 162 callback.Run(ping_response, title_response); | |
| 163 } | |
| 164 | |
| 165 // test::mojom::BlockedInterface: | |
| 166 void GetTitleBlocked(const GetTitleBlockedCallback& callback) override { | |
| 167 callback.Run("Called Blocked Interface!"); | |
| 168 } | |
| 169 | |
| 170 // test::mojom::UserIdTest: | |
| 171 void ConnectToClassAppAsDifferentUser( | |
| 172 const shell::Identity& target, | |
| 173 const ConnectToClassAppAsDifferentUserCallback& callback) override { | |
| 174 Connector::ConnectParams params(target); | |
| 175 std::unique_ptr<Connection> connection = | |
| 176 connector()->Connect(¶ms); | |
| 177 { | |
| 178 base::RunLoop loop; | |
| 179 connection->AddConnectionCompletedClosure(base::Bind(&QuitLoop, &loop)); | |
| 180 base::MessageLoop::ScopedNestableTaskAllower allow( | |
| 181 base::MessageLoop::current()); | |
| 182 loop.Run(); | |
| 183 } | |
| 184 callback.Run(static_cast<int32_t>(connection->GetResult()), | |
| 185 connection->GetRemoteIdentity()); | |
| 186 } | |
| 187 | |
| 188 void OnConnectionBlocked( | |
| 189 const ConnectToAllowedAppInBlockedPackageCallback& callback, | |
| 190 base::RunLoop* run_loop) { | |
| 191 callback.Run("uninitialized"); | |
| 192 run_loop->Quit(); | |
| 193 } | |
| 194 | |
| 195 void OnGotTitle( | |
| 196 const ConnectToAllowedAppInBlockedPackageCallback& callback, | |
| 197 base::RunLoop* run_loop, | |
| 198 const std::string& title) { | |
| 199 callback.Run(title); | |
| 200 run_loop->Quit(); | |
| 201 } | |
| 202 | |
| 203 void OnConnectionError() { | |
| 204 if (bindings_.empty() && standalone_bindings_.empty()) | |
| 205 base::MessageLoop::current()->QuitWhenIdle(); | |
| 206 } | |
| 207 | |
| 208 Identity identity_; | |
| 209 mojo::BindingSet<test::mojom::ConnectTestService> bindings_; | |
| 210 mojo::BindingSet<test::mojom::StandaloneApp> standalone_bindings_; | |
| 211 mojo::BindingSet<test::mojom::BlockedInterface> blocked_bindings_; | |
| 212 mojo::BindingSet<test::mojom::UserIdTest> user_id_test_bindings_; | |
| 213 test::mojom::ExposedInterfacePtr caller_; | |
| 214 | |
| 215 DISALLOW_COPY_AND_ASSIGN(ConnectTestApp); | |
| 216 }; | |
| 217 | |
| 218 } // namespace shell | |
| 219 | |
| 220 MojoResult ServiceMain(MojoHandle service_request_handle) { | |
| 221 shell::ServiceRunner runner(new shell::ConnectTestApp); | |
| 222 return runner.Run(service_request_handle); | |
| 223 } | |
| OLD | NEW |