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

Side by Side Diff: mojo/service_manager/service_manager_unittest.cc

Issue 380413003: Mojo: Use InterfaceFactory<Interface> for service registration (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: convert everything over, remove ApplicationConnection::AddService Created 6 years, 5 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 | Annotate | Revision Log
OLDNEW
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 "base/at_exit.h" 5 #include "base/at_exit.h"
6 #include "base/bind.h" 6 #include "base/bind.h"
7 #include "base/message_loop/message_loop.h" 7 #include "base/message_loop/message_loop.h"
8 #include "mojo/public/cpp/application/application_connection.h" 8 #include "mojo/public/cpp/application/application_connection.h"
9 #include "mojo/public/cpp/application/application_delegate.h" 9 #include "mojo/public/cpp/application/application_delegate.h"
10 #include "mojo/public/cpp/application/application_impl.h" 10 #include "mojo/public/cpp/application/application_impl.h"
(...skipping 26 matching lines...) Expand all
37 virtual void OnConnectionError() OVERRIDE { 37 virtual void OnConnectionError() OVERRIDE {
38 base::MessageLoop::current()->QuitWhenIdle(); 38 base::MessageLoop::current()->QuitWhenIdle();
39 } 39 }
40 40
41 private: 41 private:
42 DISALLOW_COPY_AND_ASSIGN(QuitMessageLoopErrorHandler); 42 DISALLOW_COPY_AND_ASSIGN(QuitMessageLoopErrorHandler);
43 }; 43 };
44 44
45 class TestServiceImpl : public InterfaceImpl<TestService> { 45 class TestServiceImpl : public InterfaceImpl<TestService> {
46 public: 46 public:
47 explicit TestServiceImpl(ApplicationConnection* connection, 47 explicit TestServiceImpl(TestContext* context) : context_(context) {
48 TestContext* context) : context_(context) {
49 ++context_->num_impls; 48 ++context_->num_impls;
50 } 49 }
51 50
52 virtual ~TestServiceImpl() { 51 virtual ~TestServiceImpl() {
53 --context_->num_impls; 52 --context_->num_impls;
54 } 53 }
55 54
56 virtual void OnConnectionError() OVERRIDE { 55 virtual void OnConnectionError() OVERRIDE {
57 if (!base::MessageLoop::current()->is_running()) 56 if (!base::MessageLoop::current()->is_running())
58 return; 57 return;
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
91 service_->Test(test_string); 90 service_->Test(test_string);
92 } 91 }
93 92
94 private: 93 private:
95 TestServicePtr service_; 94 TestServicePtr service_;
96 bool quit_after_ack_; 95 bool quit_after_ack_;
97 DISALLOW_COPY_AND_ASSIGN(TestClientImpl); 96 DISALLOW_COPY_AND_ASSIGN(TestClientImpl);
98 }; 97 };
99 98
100 class TestServiceLoader : public ServiceLoader, 99 class TestServiceLoader : public ServiceLoader,
101 public ApplicationDelegate { 100 public ApplicationDelegate,
101 public InterfaceFactory<TestService> {
102 public: 102 public:
103 TestServiceLoader() 103 TestServiceLoader()
104 : context_(NULL), 104 : context_(NULL),
105 num_loads_(0) { 105 num_loads_(0) {
106 } 106 }
107 107
108 virtual ~TestServiceLoader() { 108 virtual ~TestServiceLoader() {
109 if (context_) 109 if (context_)
110 ++context_->num_loader_deletes; 110 ++context_->num_loader_deletes;
111 test_app_.reset(NULL); 111 test_app_.reset(NULL);
(...skipping 10 matching lines...) Expand all
122 ++num_loads_; 122 ++num_loads_;
123 test_app_.reset(new ApplicationImpl(this, service_provider_handle.Pass())); 123 test_app_.reset(new ApplicationImpl(this, service_provider_handle.Pass()));
124 } 124 }
125 125
126 virtual void OnServiceError(ServiceManager* manager, 126 virtual void OnServiceError(ServiceManager* manager,
127 const GURL& url) OVERRIDE { 127 const GURL& url) OVERRIDE {
128 } 128 }
129 129
130 virtual bool ConfigureIncomingConnection( 130 virtual bool ConfigureIncomingConnection(
131 ApplicationConnection* connection) OVERRIDE { 131 ApplicationConnection* connection) OVERRIDE {
132 connection->AddService<TestServiceImpl>(context_); 132 connection->AddServiceFactory(this);
133 return true; 133 return true;
134 } 134 }
135 135
136 virtual void Create(ApplicationConnection* connection,
137 InterfaceRequest<TestService> request) OVERRIDE {
138 mojo::BindToRequest(new TestServiceImpl(context_), &request);
139 }
140
136 scoped_ptr<ApplicationImpl> test_app_; 141 scoped_ptr<ApplicationImpl> test_app_;
137 TestContext* context_; 142 TestContext* context_;
138 int num_loads_; 143 int num_loads_;
139 DISALLOW_COPY_AND_ASSIGN(TestServiceLoader); 144 DISALLOW_COPY_AND_ASSIGN(TestServiceLoader);
140 }; 145 };
141 146
142 struct TesterContext { 147 struct TesterContext {
143 TesterContext() 148 TesterContext()
144 : num_b_calls(0), 149 : num_b_calls(0),
145 num_c_calls(0), 150 num_c_calls(0),
146 num_a_deletes(0), 151 num_a_deletes(0),
147 num_b_deletes(0), 152 num_b_deletes(0),
148 num_c_deletes(0), 153 num_c_deletes(0),
149 tester_called_quit(false), 154 tester_called_quit(false),
150 a_called_quit(false) {} 155 a_called_quit(false) {}
151 int num_b_calls; 156 int num_b_calls;
152 int num_c_calls; 157 int num_c_calls;
153 int num_a_deletes; 158 int num_a_deletes;
154 int num_b_deletes; 159 int num_b_deletes;
155 int num_c_deletes; 160 int num_c_deletes;
156 bool tester_called_quit; 161 bool tester_called_quit;
157 bool a_called_quit; 162 bool a_called_quit;
158 }; 163 };
159 164
160 // Used to test that the requestor url will be correctly passed. 165 // Used to test that the requestor url will be correctly passed.
161 class TestAImpl : public InterfaceImpl<TestA> { 166 class TestAImpl : public InterfaceImpl<TestA> {
162 public: 167 public:
163 explicit TestAImpl(ApplicationConnection* connection, 168 TestAImpl(ApplicationConnection* connection, TesterContext* test_context)
164 TesterContext* test_context)
165 : test_context_(test_context) { 169 : test_context_(test_context) {
166 connection->ConnectToApplication(kTestBURLString)->ConnectToService(&b_); 170 connection->ConnectToApplication(kTestBURLString)->ConnectToService(&b_);
167 } 171 }
168 virtual ~TestAImpl() { test_context_->num_a_deletes++; } 172 virtual ~TestAImpl() { test_context_->num_a_deletes++; }
169 173
170 private: 174 private:
171 virtual void CallB() OVERRIDE { 175 virtual void CallB() OVERRIDE {
172 b_->B(base::Bind(&TestAImpl::Quit, base::Unretained(this))); 176 b_->B(base::Bind(&TestAImpl::Quit, base::Unretained(this)));
173 } 177 }
174 178
175 virtual void CallCFromB() OVERRIDE { 179 virtual void CallCFromB() OVERRIDE {
176 b_->CallC(base::Bind(&TestAImpl::Quit, base::Unretained(this))); 180 b_->CallC(base::Bind(&TestAImpl::Quit, base::Unretained(this)));
177 } 181 }
178 182
179 void Quit() { 183 void Quit() {
180 test_context_->a_called_quit = true; 184 test_context_->a_called_quit = true;
181 base::MessageLoop::current()->Quit(); 185 base::MessageLoop::current()->Quit();
182 } 186 }
183 187
184 TesterContext* test_context_; 188 TesterContext* test_context_;
185 TestBPtr b_; 189 TestBPtr b_;
186 }; 190 };
187 191
188 class TestBImpl : public InterfaceImpl<TestB> { 192 class TestBImpl : public InterfaceImpl<TestB> {
189 public: 193 public:
190 explicit TestBImpl(ApplicationConnection* connection, 194 TestBImpl(ApplicationConnection* connection, TesterContext* test_context)
191 TesterContext* test_context)
192 : test_context_(test_context) { 195 : test_context_(test_context) {
193 connection->ConnectToService(&c_); 196 connection->ConnectToService(&c_);
194 } 197 }
195 198
196 virtual ~TestBImpl() { 199 virtual ~TestBImpl() {
197 test_context_->num_b_deletes++; 200 test_context_->num_b_deletes++;
198 if (!base::MessageLoop::current()->is_running()) 201 if (!base::MessageLoop::current()->is_running())
199 return; 202 return;
200 base::MessageLoop::current()->Quit(); 203 base::MessageLoop::current()->Quit();
201 } 204 }
202 205
203 private: 206 private:
204 virtual void B(const mojo::Callback<void()>& callback) OVERRIDE { 207 virtual void B(const mojo::Callback<void()>& callback) OVERRIDE {
205 ++test_context_->num_b_calls; 208 ++test_context_->num_b_calls;
206 callback.Run(); 209 callback.Run();
207 } 210 }
208 211
209 virtual void CallC(const mojo::Callback<void()>& callback) OVERRIDE { 212 virtual void CallC(const mojo::Callback<void()>& callback) OVERRIDE {
210 ++test_context_->num_b_calls; 213 ++test_context_->num_b_calls;
211 c_->C(callback); 214 c_->C(callback);
212 } 215 }
213 216
214 TesterContext* test_context_; 217 TesterContext* test_context_;
215 TestCPtr c_; 218 TestCPtr c_;
216 }; 219 };
217 220
218 class TestCImpl : public InterfaceImpl<TestC> { 221 class TestCImpl : public InterfaceImpl<TestC> {
219 public: 222 public:
220 explicit TestCImpl(ApplicationConnection* connection, 223 TestCImpl(ApplicationConnection* connection, TesterContext* test_context)
221 TesterContext* test_context) 224 : test_context_(test_context) {}
222 : test_context_(test_context) {
223 }
224 225
225 virtual ~TestCImpl() { test_context_->num_c_deletes++; } 226 virtual ~TestCImpl() { test_context_->num_c_deletes++; }
226 227
227 private: 228 private:
228 virtual void C(const mojo::Callback<void()>& callback) OVERRIDE { 229 virtual void C(const mojo::Callback<void()>& callback) OVERRIDE {
229 ++test_context_->num_c_calls; 230 ++test_context_->num_c_calls;
230 callback.Run(); 231 callback.Run();
231 } 232 }
232 TesterContext* test_context_; 233 TesterContext* test_context_;
233 }; 234 };
234 235
235 class Tester : public ApplicationDelegate, public ServiceLoader { 236 class Tester : public ApplicationDelegate,
237 public ServiceLoader,
238 public InterfaceFactory<TestA>,
239 public InterfaceFactory<TestB>,
240 public InterfaceFactory<TestC> {
236 public: 241 public:
237 Tester(TesterContext* context, const std::string& requestor_url) 242 Tester(TesterContext* context, const std::string& requestor_url)
238 : context_(context), 243 : context_(context),
239 requestor_url_(requestor_url) {} 244 requestor_url_(requestor_url) {}
240 virtual ~Tester() {} 245 virtual ~Tester() {}
241 246
242 private: 247 private:
243 virtual void LoadService( 248 virtual void LoadService(
244 ServiceManager* manager, 249 ServiceManager* manager,
245 const GURL& url, 250 const GURL& url,
246 ScopedMessagePipeHandle shell_handle) OVERRIDE { 251 ScopedMessagePipeHandle shell_handle) OVERRIDE {
247 app_.reset(new ApplicationImpl(this, shell_handle.Pass())); 252 app_.reset(new ApplicationImpl(this, shell_handle.Pass()));
248 } 253 }
249 254
250 virtual void OnServiceError(ServiceManager* manager, 255 virtual void OnServiceError(ServiceManager* manager,
251 const GURL& url) OVERRIDE {} 256 const GURL& url) OVERRIDE {}
252 257
253 virtual bool ConfigureIncomingConnection( 258 virtual bool ConfigureIncomingConnection(
254 ApplicationConnection* connection) OVERRIDE { 259 ApplicationConnection* connection) OVERRIDE {
255 if (!requestor_url_.empty() && 260 if (!requestor_url_.empty() &&
256 requestor_url_ != connection->GetRemoteApplicationURL()) { 261 requestor_url_ != connection->GetRemoteApplicationURL()) {
257 context_->tester_called_quit = true; 262 context_->tester_called_quit = true;
258 base::MessageLoop::current()->Quit(); 263 base::MessageLoop::current()->Quit();
259 return false; 264 return false;
260 } 265 }
261 // If we're coming from A, then add B, otherwise A. 266 // If we're coming from A, then add B, otherwise A.
262 if (connection->GetRemoteApplicationURL() == kTestAURLString) 267 if (connection->GetRemoteApplicationURL() == kTestAURLString)
263 connection->AddService<TestBImpl>(context_); 268 connection->AddServiceFactory<TestB>(this);
264 else 269 else
265 connection->AddService<TestAImpl>(context_); 270 connection->AddServiceFactory<TestA>(this);
266 return true; 271 return true;
267 } 272 }
268 273
269 virtual bool ConfigureOutgoingConnection( 274 virtual bool ConfigureOutgoingConnection(
270 ApplicationConnection* connection) OVERRIDE { 275 ApplicationConnection* connection) OVERRIDE {
271 // If we're connecting to B, then add C. 276 // If we're connecting to B, then add C.
272 if (connection->GetRemoteApplicationURL() == kTestBURLString) 277 if (connection->GetRemoteApplicationURL() == kTestBURLString)
273 connection->AddService<TestCImpl>(context_); 278 connection->AddServiceFactory<TestC>(this);
274 return true; 279 return true;
275 } 280 }
276 281
282 virtual void Create(ApplicationConnection* connection,
283 InterfaceRequest<TestA> request) OVERRIDE {
284 mojo::BindToRequest(new TestAImpl(connection, context_), &request);
285 }
286
287 virtual void Create(ApplicationConnection* connection,
288 InterfaceRequest<TestB> request) OVERRIDE {
289 mojo::BindToRequest(new TestBImpl(connection, context_), &request);
290 }
291
292 virtual void Create(ApplicationConnection* connection,
293 InterfaceRequest<TestC> request) OVERRIDE {
294 mojo::BindToRequest(new TestCImpl(connection, context_), &request);
295 }
296
277 TesterContext* context_; 297 TesterContext* context_;
278 scoped_ptr<ApplicationImpl> app_; 298 scoped_ptr<ApplicationImpl> app_;
279 std::string requestor_url_; 299 std::string requestor_url_;
280 }; 300 };
281 301
282 class TestServiceInterceptor : public ServiceManager::Interceptor { 302 class TestServiceInterceptor : public ServiceManager::Interceptor {
283 public: 303 public:
284 TestServiceInterceptor() : call_count_(0) {} 304 TestServiceInterceptor() : call_count_(0) {}
285 305
286 virtual ServiceProviderPtr OnConnectToClient( 306 virtual ServiceProviderPtr OnConnectToClient(
(...skipping 259 matching lines...) Expand 10 before | Expand all | Expand 10 after
546 std::string url("test:test3"); 566 std::string url("test:test3");
547 TestServicePtr test_service; 567 TestServicePtr test_service;
548 sm.ConnectToService(GURL(url), &test_service); 568 sm.ConnectToService(GURL(url), &test_service);
549 569
550 EXPECT_EQ(1, interceptor.call_count()); 570 EXPECT_EQ(1, interceptor.call_count());
551 EXPECT_EQ(url, interceptor.url_spec()); 571 EXPECT_EQ(url, interceptor.url_spec());
552 EXPECT_EQ(1, default_loader->num_loads()); 572 EXPECT_EQ(1, default_loader->num_loads());
553 } 573 }
554 574
555 } // namespace mojo 575 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698