Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(688)

Side by Side Diff: services/shell/tests/connect/connect_test_app.cc

Issue 2179023004: Make Service own ServiceContext. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: . Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/guid.h" 8 #include "base/guid.h"
9 #include "base/macros.h" 9 #include "base/macros.h"
10 #include "base/message_loop/message_loop.h" 10 #include "base/message_loop/message_loop.h"
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
45 public test::mojom::ConnectTestService, 45 public test::mojom::ConnectTestService,
46 public test::mojom::StandaloneApp, 46 public test::mojom::StandaloneApp,
47 public test::mojom::BlockedInterface, 47 public test::mojom::BlockedInterface,
48 public test::mojom::UserIdTest { 48 public test::mojom::UserIdTest {
49 public: 49 public:
50 ConnectTestApp() {} 50 ConnectTestApp() {}
51 ~ConnectTestApp() override {} 51 ~ConnectTestApp() override {}
52 52
53 private: 53 private:
54 // shell::Service: 54 // shell::Service:
55 void OnStart(Connector* connector, const Identity& identity, 55 void OnStart(const Identity& identity) override {
56 uint32_t id) override {
57 connector_ = connector;
58 identity_ = identity; 56 identity_ = identity;
59 id_ = id;
60 bindings_.set_connection_error_handler( 57 bindings_.set_connection_error_handler(
61 base::Bind(&ConnectTestApp::OnConnectionError, 58 base::Bind(&ConnectTestApp::OnConnectionError,
62 base::Unretained(this))); 59 base::Unretained(this)));
63 standalone_bindings_.set_connection_error_handler( 60 standalone_bindings_.set_connection_error_handler(
64 base::Bind(&ConnectTestApp::OnConnectionError, 61 base::Bind(&ConnectTestApp::OnConnectionError,
65 base::Unretained(this))); 62 base::Unretained(this)));
66 } 63 }
67 bool OnConnect(Connection* connection) override { 64 bool OnConnect(Connection* connection) override {
68 connection->AddInterface<test::mojom::ConnectTestService>(this); 65 connection->AddInterface<test::mojom::ConnectTestService>(this);
69 connection->AddInterface<test::mojom::StandaloneApp>(this); 66 connection->AddInterface<test::mojom::StandaloneApp>(this);
70 connection->AddInterface<test::mojom::BlockedInterface>(this); 67 connection->AddInterface<test::mojom::BlockedInterface>(this);
71 connection->AddInterface<test::mojom::UserIdTest>(this); 68 connection->AddInterface<test::mojom::UserIdTest>(this);
72 69
73 uint32_t remote_id = connection->GetRemoteInstanceID(); 70 uint32_t remote_id = connection->GetRemoteInstanceID();
74 test::mojom::ConnectionStatePtr state(test::mojom::ConnectionState::New()); 71 test::mojom::ConnectionStatePtr state(test::mojom::ConnectionState::New());
75 state->connection_local_name = connection->GetConnectionName(); 72 state->connection_local_name = connection->GetConnectionName();
76 state->connection_remote_name = connection->GetRemoteIdentity().name(); 73 state->connection_remote_name = connection->GetRemoteIdentity().name();
77 state->connection_remote_userid = connection->GetRemoteIdentity().user_id(); 74 state->connection_remote_userid = connection->GetRemoteIdentity().user_id();
78 state->connection_remote_id = remote_id; 75 state->connection_remote_id = remote_id;
79 state->initialize_local_name = identity_.name(); 76 state->initialize_local_name = identity_.name();
80 state->initialize_id = id_;
81 state->initialize_userid = identity_.user_id(); 77 state->initialize_userid = identity_.user_id();
82 connection->GetInterface(&caller_); 78 connection->GetInterface(&caller_);
83 caller_->ConnectionAccepted(std::move(state)); 79 caller_->ConnectionAccepted(std::move(state));
84 80
85 return true; 81 return true;
86 } 82 }
87 83
88 // InterfaceFactory<test::mojom::ConnectTestService>: 84 // InterfaceFactory<test::mojom::ConnectTestService>:
89 void Create(const Identity& remote_identity, 85 void Create(const Identity& remote_identity,
90 test::mojom::ConnectTestServiceRequest request) override { 86 test::mojom::ConnectTestServiceRequest request) override {
(...skipping 24 matching lines...) Expand all
115 } 111 }
116 void GetInstance(const GetInstanceCallback& callback) override { 112 void GetInstance(const GetInstanceCallback& callback) override {
117 callback.Run(identity_.instance()); 113 callback.Run(identity_.instance());
118 } 114 }
119 115
120 // test::mojom::StandaloneApp: 116 // test::mojom::StandaloneApp:
121 void ConnectToAllowedAppInBlockedPackage( 117 void ConnectToAllowedAppInBlockedPackage(
122 const ConnectToAllowedAppInBlockedPackageCallback& callback) override { 118 const ConnectToAllowedAppInBlockedPackageCallback& callback) override {
123 base::RunLoop run_loop; 119 base::RunLoop run_loop;
124 std::unique_ptr<Connection> connection = 120 std::unique_ptr<Connection> connection =
125 connector_->Connect("mojo:connect_test_a"); 121 connector()->Connect("mojo:connect_test_a");
126 connection->SetConnectionLostClosure( 122 connection->SetConnectionLostClosure(
127 base::Bind(&ConnectTestApp::OnConnectionBlocked, 123 base::Bind(&ConnectTestApp::OnConnectionBlocked,
128 base::Unretained(this), callback, &run_loop)); 124 base::Unretained(this), callback, &run_loop));
129 test::mojom::ConnectTestServicePtr test_service; 125 test::mojom::ConnectTestServicePtr test_service;
130 connection->GetInterface(&test_service); 126 connection->GetInterface(&test_service);
131 test_service->GetTitle( 127 test_service->GetTitle(
132 base::Bind(&ConnectTestApp::OnGotTitle, base::Unretained(this), 128 base::Bind(&ConnectTestApp::OnGotTitle, base::Unretained(this),
133 callback, &run_loop)); 129 callback, &run_loop));
134 { 130 {
135 // This message is dispatched as a task on the same run loop, so we need 131 // This message is dispatched as a task on the same run loop, so we need
136 // to allow nesting in order to pump additional signals. 132 // to allow nesting in order to pump additional signals.
137 base::MessageLoop::ScopedNestableTaskAllower allow( 133 base::MessageLoop::ScopedNestableTaskAllower allow(
138 base::MessageLoop::current()); 134 base::MessageLoop::current());
139 run_loop.Run(); 135 run_loop.Run();
140 } 136 }
141 } 137 }
142 void ConnectToClassInterface( 138 void ConnectToClassInterface(
143 const ConnectToClassInterfaceCallback& callback) override { 139 const ConnectToClassInterfaceCallback& callback) override {
144 std::unique_ptr<Connection> connection = 140 std::unique_ptr<Connection> connection =
145 connector_->Connect("mojo:connect_test_class_app"); 141 connector()->Connect("mojo:connect_test_class_app");
146 test::mojom::ClassInterfacePtr class_interface; 142 test::mojom::ClassInterfacePtr class_interface;
147 connection->GetInterface(&class_interface); 143 connection->GetInterface(&class_interface);
148 std::string ping_response; 144 std::string ping_response;
149 { 145 {
150 base::RunLoop loop; 146 base::RunLoop loop;
151 class_interface->Ping(base::Bind(&ReceiveString, &ping_response, &loop)); 147 class_interface->Ping(base::Bind(&ReceiveString, &ping_response, &loop));
152 base::MessageLoop::ScopedNestableTaskAllower allow( 148 base::MessageLoop::ScopedNestableTaskAllower allow(
153 base::MessageLoop::current()); 149 base::MessageLoop::current());
154 loop.Run(); 150 loop.Run();
155 } 151 }
(...skipping 13 matching lines...) Expand all
169 // test::mojom::BlockedInterface: 165 // test::mojom::BlockedInterface:
170 void GetTitleBlocked(const GetTitleBlockedCallback& callback) override { 166 void GetTitleBlocked(const GetTitleBlockedCallback& callback) override {
171 callback.Run("Called Blocked Interface!"); 167 callback.Run("Called Blocked Interface!");
172 } 168 }
173 169
174 // test::mojom::UserIdTest: 170 // test::mojom::UserIdTest:
175 void ConnectToClassAppAsDifferentUser( 171 void ConnectToClassAppAsDifferentUser(
176 mojom::IdentityPtr target, 172 mojom::IdentityPtr target,
177 const ConnectToClassAppAsDifferentUserCallback& callback) override { 173 const ConnectToClassAppAsDifferentUserCallback& callback) override {
178 Connector::ConnectParams params(target.To<Identity>()); 174 Connector::ConnectParams params(target.To<Identity>());
179 std::unique_ptr<Connection> connection = connector_->Connect(&params); 175 std::unique_ptr<Connection> connection =
176 connector()->Connect(&params);
180 { 177 {
181 base::RunLoop loop; 178 base::RunLoop loop;
182 connection->AddConnectionCompletedClosure(base::Bind(&QuitLoop, &loop)); 179 connection->AddConnectionCompletedClosure(base::Bind(&QuitLoop, &loop));
183 base::MessageLoop::ScopedNestableTaskAllower allow( 180 base::MessageLoop::ScopedNestableTaskAllower allow(
184 base::MessageLoop::current()); 181 base::MessageLoop::current());
185 loop.Run(); 182 loop.Run();
186 } 183 }
187 callback.Run(static_cast<int32_t>(connection->GetResult()), 184 callback.Run(static_cast<int32_t>(connection->GetResult()),
188 mojom::Identity::From(connection->GetRemoteIdentity())); 185 mojom::Identity::From(connection->GetRemoteIdentity()));
189 } 186 }
(...skipping 11 matching lines...) Expand all
201 mojo::String title) { 198 mojo::String title) {
202 callback.Run(title); 199 callback.Run(title);
203 run_loop->Quit(); 200 run_loop->Quit();
204 } 201 }
205 202
206 void OnConnectionError() { 203 void OnConnectionError() {
207 if (bindings_.empty() && standalone_bindings_.empty()) 204 if (bindings_.empty() && standalone_bindings_.empty())
208 base::MessageLoop::current()->QuitWhenIdle(); 205 base::MessageLoop::current()->QuitWhenIdle();
209 } 206 }
210 207
211 Connector* connector_ = nullptr;
212 Identity identity_; 208 Identity identity_;
213 uint32_t id_ = mojom::kInvalidInstanceID;
214 mojo::BindingSet<test::mojom::ConnectTestService> bindings_; 209 mojo::BindingSet<test::mojom::ConnectTestService> bindings_;
215 mojo::BindingSet<test::mojom::StandaloneApp> standalone_bindings_; 210 mojo::BindingSet<test::mojom::StandaloneApp> standalone_bindings_;
216 mojo::BindingSet<test::mojom::BlockedInterface> blocked_bindings_; 211 mojo::BindingSet<test::mojom::BlockedInterface> blocked_bindings_;
217 mojo::BindingSet<test::mojom::UserIdTest> user_id_test_bindings_; 212 mojo::BindingSet<test::mojom::UserIdTest> user_id_test_bindings_;
218 test::mojom::ExposedInterfacePtr caller_; 213 test::mojom::ExposedInterfacePtr caller_;
219 214
220 DISALLOW_COPY_AND_ASSIGN(ConnectTestApp); 215 DISALLOW_COPY_AND_ASSIGN(ConnectTestApp);
221 }; 216 };
222 217
223 } // namespace shell 218 } // namespace shell
224 219
225 MojoResult MojoMain(MojoHandle shell_handle) { 220 MojoResult MojoMain(MojoHandle shell_handle) {
226 MojoResult rv = 221 MojoResult rv =
227 shell::ServiceRunner(new shell::ConnectTestApp).Run(shell_handle); 222 shell::ServiceRunner(new shell::ConnectTestApp).Run(shell_handle);
228 return rv; 223 return rv;
229 } 224 }
OLDNEW
« no previous file with comments | « services/shell/service_manager.cc ('k') | services/shell/tests/connect/connect_test_class_app.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698