OLD | NEW |
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/macros.h" | 7 #include "base/macros.h" |
8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
9 #include "mojo/application_manager/application_loader.h" | 9 #include "mojo/application_manager/application_loader.h" |
10 #include "mojo/application_manager/application_manager.h" | 10 #include "mojo/application_manager/application_manager.h" |
(...skipping 16 matching lines...) Expand all Loading... |
27 struct TestContext { | 27 struct TestContext { |
28 TestContext() : num_impls(0), num_loader_deletes(0) {} | 28 TestContext() : num_impls(0), num_loader_deletes(0) {} |
29 std::string last_test_string; | 29 std::string last_test_string; |
30 int num_impls; | 30 int num_impls; |
31 int num_loader_deletes; | 31 int num_loader_deletes; |
32 }; | 32 }; |
33 | 33 |
34 class QuitMessageLoopErrorHandler : public ErrorHandler { | 34 class QuitMessageLoopErrorHandler : public ErrorHandler { |
35 public: | 35 public: |
36 QuitMessageLoopErrorHandler() {} | 36 QuitMessageLoopErrorHandler() {} |
37 virtual ~QuitMessageLoopErrorHandler() {} | 37 ~QuitMessageLoopErrorHandler() override {} |
38 | 38 |
39 // |ErrorHandler| implementation: | 39 // |ErrorHandler| implementation: |
40 virtual void OnConnectionError() override { | 40 void OnConnectionError() override { |
41 base::MessageLoop::current()->QuitWhenIdle(); | 41 base::MessageLoop::current()->QuitWhenIdle(); |
42 } | 42 } |
43 | 43 |
44 private: | 44 private: |
45 DISALLOW_COPY_AND_ASSIGN(QuitMessageLoopErrorHandler); | 45 DISALLOW_COPY_AND_ASSIGN(QuitMessageLoopErrorHandler); |
46 }; | 46 }; |
47 | 47 |
48 class TestServiceImpl : public InterfaceImpl<TestService> { | 48 class TestServiceImpl : public InterfaceImpl<TestService> { |
49 public: | 49 public: |
50 explicit TestServiceImpl(TestContext* context) : context_(context) { | 50 explicit TestServiceImpl(TestContext* context) : context_(context) { |
51 ++context_->num_impls; | 51 ++context_->num_impls; |
52 } | 52 } |
53 | 53 |
54 virtual ~TestServiceImpl() { --context_->num_impls; } | 54 ~TestServiceImpl() override { --context_->num_impls; } |
55 | 55 |
56 virtual void OnConnectionError() override { | 56 void OnConnectionError() override { |
57 if (!base::MessageLoop::current()->is_running()) | 57 if (!base::MessageLoop::current()->is_running()) |
58 return; | 58 return; |
59 base::MessageLoop::current()->Quit(); | 59 base::MessageLoop::current()->Quit(); |
60 } | 60 } |
61 | 61 |
62 // TestService implementation: | 62 // TestService implementation: |
63 virtual void Test(const String& test_string) override { | 63 void Test(const String& test_string) override { |
64 context_->last_test_string = test_string; | 64 context_->last_test_string = test_string; |
65 client()->AckTest(); | 65 client()->AckTest(); |
66 } | 66 } |
67 | 67 |
68 private: | 68 private: |
69 TestContext* context_; | 69 TestContext* context_; |
70 }; | 70 }; |
71 | 71 |
72 class TestClientImpl : public TestClient { | 72 class TestClientImpl : public TestClient { |
73 public: | 73 public: |
74 explicit TestClientImpl(TestServicePtr service) | 74 explicit TestClientImpl(TestServicePtr service) |
75 : service_(service.Pass()), quit_after_ack_(false) { | 75 : service_(service.Pass()), quit_after_ack_(false) { |
76 service_.set_client(this); | 76 service_.set_client(this); |
77 } | 77 } |
78 | 78 |
79 virtual ~TestClientImpl() { service_.reset(); } | 79 ~TestClientImpl() override { service_.reset(); } |
80 | 80 |
81 virtual void AckTest() override { | 81 void AckTest() override { |
82 if (quit_after_ack_) | 82 if (quit_after_ack_) |
83 base::MessageLoop::current()->Quit(); | 83 base::MessageLoop::current()->Quit(); |
84 } | 84 } |
85 | 85 |
86 void Test(std::string test_string) { | 86 void Test(std::string test_string) { |
87 quit_after_ack_ = true; | 87 quit_after_ack_ = true; |
88 service_->Test(test_string); | 88 service_->Test(test_string); |
89 } | 89 } |
90 | 90 |
91 private: | 91 private: |
92 TestServicePtr service_; | 92 TestServicePtr service_; |
93 bool quit_after_ack_; | 93 bool quit_after_ack_; |
94 DISALLOW_COPY_AND_ASSIGN(TestClientImpl); | 94 DISALLOW_COPY_AND_ASSIGN(TestClientImpl); |
95 }; | 95 }; |
96 | 96 |
97 class TestApplicationLoader : public ApplicationLoader, | 97 class TestApplicationLoader : public ApplicationLoader, |
98 public ApplicationDelegate, | 98 public ApplicationDelegate, |
99 public InterfaceFactory<TestService> { | 99 public InterfaceFactory<TestService> { |
100 public: | 100 public: |
101 TestApplicationLoader() : context_(NULL), num_loads_(0) {} | 101 TestApplicationLoader() : context_(NULL), num_loads_(0) {} |
102 | 102 |
103 virtual ~TestApplicationLoader() { | 103 ~TestApplicationLoader() override { |
104 if (context_) | 104 if (context_) |
105 ++context_->num_loader_deletes; | 105 ++context_->num_loader_deletes; |
106 test_app_.reset(NULL); | 106 test_app_.reset(NULL); |
107 } | 107 } |
108 | 108 |
109 void set_context(TestContext* context) { context_ = context; } | 109 void set_context(TestContext* context) { context_ = context; } |
110 int num_loads() const { return num_loads_; } | 110 int num_loads() const { return num_loads_; } |
111 const std::vector<std::string>& GetArgs() const { return test_app_->args(); } | 111 const std::vector<std::string>& GetArgs() const { return test_app_->args(); } |
112 | 112 |
113 private: | 113 private: |
114 // ApplicationLoader implementation. | 114 // ApplicationLoader implementation. |
115 virtual void Load(ApplicationManager* manager, | 115 void Load(ApplicationManager* manager, |
116 const GURL& url, | 116 const GURL& url, |
117 scoped_refptr<LoadCallbacks> callbacks) override { | 117 scoped_refptr<LoadCallbacks> callbacks) override { |
118 ++num_loads_; | 118 ++num_loads_; |
119 test_app_.reset( | 119 test_app_.reset( |
120 new ApplicationImpl(this, callbacks->RegisterApplication().Pass())); | 120 new ApplicationImpl(this, callbacks->RegisterApplication().Pass())); |
121 } | 121 } |
122 | 122 |
123 virtual void OnApplicationError(ApplicationManager* manager, | 123 void OnApplicationError(ApplicationManager* manager, |
124 const GURL& url) override {} | 124 const GURL& url) override {} |
125 | 125 |
126 // ApplicationDelegate implementation. | 126 // ApplicationDelegate implementation. |
127 virtual bool ConfigureIncomingConnection( | 127 bool ConfigureIncomingConnection(ApplicationConnection* connection) override { |
128 ApplicationConnection* connection) override { | |
129 connection->AddService(this); | 128 connection->AddService(this); |
130 return true; | 129 return true; |
131 } | 130 } |
132 | 131 |
133 // InterfaceFactory implementation. | 132 // InterfaceFactory implementation. |
134 virtual void Create(ApplicationConnection* connection, | 133 void Create(ApplicationConnection* connection, |
135 InterfaceRequest<TestService> request) override { | 134 InterfaceRequest<TestService> request) override { |
136 BindToRequest(new TestServiceImpl(context_), &request); | 135 BindToRequest(new TestServiceImpl(context_), &request); |
137 } | 136 } |
138 | 137 |
139 scoped_ptr<ApplicationImpl> test_app_; | 138 scoped_ptr<ApplicationImpl> test_app_; |
140 TestContext* context_; | 139 TestContext* context_; |
141 int num_loads_; | 140 int num_loads_; |
142 DISALLOW_COPY_AND_ASSIGN(TestApplicationLoader); | 141 DISALLOW_COPY_AND_ASSIGN(TestApplicationLoader); |
143 }; | 142 }; |
144 | 143 |
145 class TesterContext { | 144 class TesterContext { |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
237 base::MessageLoop* loop_; | 236 base::MessageLoop* loop_; |
238 }; | 237 }; |
239 | 238 |
240 // Used to test that the requestor url will be correctly passed. | 239 // Used to test that the requestor url will be correctly passed. |
241 class TestAImpl : public InterfaceImpl<TestA> { | 240 class TestAImpl : public InterfaceImpl<TestA> { |
242 public: | 241 public: |
243 TestAImpl(ApplicationConnection* connection, TesterContext* test_context) | 242 TestAImpl(ApplicationConnection* connection, TesterContext* test_context) |
244 : test_context_(test_context) { | 243 : test_context_(test_context) { |
245 connection->ConnectToApplication(kTestBURLString)->ConnectToService(&b_); | 244 connection->ConnectToApplication(kTestBURLString)->ConnectToService(&b_); |
246 } | 245 } |
247 virtual ~TestAImpl() { | 246 ~TestAImpl() override { |
248 test_context_->IncrementNumADeletes(); | 247 test_context_->IncrementNumADeletes(); |
249 if (base::MessageLoop::current()->is_running()) | 248 if (base::MessageLoop::current()->is_running()) |
250 Quit(); | 249 Quit(); |
251 } | 250 } |
252 | 251 |
253 private: | 252 private: |
254 virtual void CallB() override { | 253 void CallB() override { |
255 b_->B(base::Bind(&TestAImpl::Quit, base::Unretained(this))); | 254 b_->B(base::Bind(&TestAImpl::Quit, base::Unretained(this))); |
256 } | 255 } |
257 | 256 |
258 virtual void CallCFromB() override { | 257 void CallCFromB() override { |
259 b_->CallC(base::Bind(&TestAImpl::Quit, base::Unretained(this))); | 258 b_->CallC(base::Bind(&TestAImpl::Quit, base::Unretained(this))); |
260 } | 259 } |
261 | 260 |
262 void Quit() { | 261 void Quit() { |
263 base::MessageLoop::current()->Quit(); | 262 base::MessageLoop::current()->Quit(); |
264 test_context_->set_a_called_quit(); | 263 test_context_->set_a_called_quit(); |
265 test_context_->QuitSoon(); | 264 test_context_->QuitSoon(); |
266 } | 265 } |
267 | 266 |
268 TesterContext* test_context_; | 267 TesterContext* test_context_; |
269 TestBPtr b_; | 268 TestBPtr b_; |
270 }; | 269 }; |
271 | 270 |
272 class TestBImpl : public InterfaceImpl<TestB> { | 271 class TestBImpl : public InterfaceImpl<TestB> { |
273 public: | 272 public: |
274 TestBImpl(ApplicationConnection* connection, TesterContext* test_context) | 273 TestBImpl(ApplicationConnection* connection, TesterContext* test_context) |
275 : test_context_(test_context) { | 274 : test_context_(test_context) { |
276 connection->ConnectToService(&c_); | 275 connection->ConnectToService(&c_); |
277 } | 276 } |
278 | 277 |
279 virtual ~TestBImpl() { | 278 ~TestBImpl() override { |
280 test_context_->IncrementNumBDeletes(); | 279 test_context_->IncrementNumBDeletes(); |
281 if (base::MessageLoop::current()->is_running()) | 280 if (base::MessageLoop::current()->is_running()) |
282 base::MessageLoop::current()->Quit(); | 281 base::MessageLoop::current()->Quit(); |
283 test_context_->QuitSoon(); | 282 test_context_->QuitSoon(); |
284 } | 283 } |
285 | 284 |
286 private: | 285 private: |
287 virtual void B(const mojo::Callback<void()>& callback) override { | 286 void B(const mojo::Callback<void()>& callback) override { |
288 test_context_->IncrementNumBCalls(); | 287 test_context_->IncrementNumBCalls(); |
289 callback.Run(); | 288 callback.Run(); |
290 } | 289 } |
291 | 290 |
292 virtual void CallC(const mojo::Callback<void()>& callback) override { | 291 void CallC(const mojo::Callback<void()>& callback) override { |
293 test_context_->IncrementNumBCalls(); | 292 test_context_->IncrementNumBCalls(); |
294 c_->C(callback); | 293 c_->C(callback); |
295 } | 294 } |
296 | 295 |
297 TesterContext* test_context_; | 296 TesterContext* test_context_; |
298 TestCPtr c_; | 297 TestCPtr c_; |
299 }; | 298 }; |
300 | 299 |
301 class TestCImpl : public InterfaceImpl<TestC> { | 300 class TestCImpl : public InterfaceImpl<TestC> { |
302 public: | 301 public: |
303 TestCImpl(ApplicationConnection* connection, TesterContext* test_context) | 302 TestCImpl(ApplicationConnection* connection, TesterContext* test_context) |
304 : test_context_(test_context) {} | 303 : test_context_(test_context) {} |
305 | 304 |
306 virtual ~TestCImpl() { test_context_->IncrementNumCDeletes(); } | 305 ~TestCImpl() override { test_context_->IncrementNumCDeletes(); } |
307 | 306 |
308 private: | 307 private: |
309 virtual void C(const mojo::Callback<void()>& callback) override { | 308 void C(const mojo::Callback<void()>& callback) override { |
310 test_context_->IncrementNumCCalls(); | 309 test_context_->IncrementNumCCalls(); |
311 callback.Run(); | 310 callback.Run(); |
312 } | 311 } |
313 TesterContext* test_context_; | 312 TesterContext* test_context_; |
314 }; | 313 }; |
315 | 314 |
316 class Tester : public ApplicationDelegate, | 315 class Tester : public ApplicationDelegate, |
317 public ApplicationLoader, | 316 public ApplicationLoader, |
318 public InterfaceFactory<TestA>, | 317 public InterfaceFactory<TestA>, |
319 public InterfaceFactory<TestB>, | 318 public InterfaceFactory<TestB>, |
320 public InterfaceFactory<TestC> { | 319 public InterfaceFactory<TestC> { |
321 public: | 320 public: |
322 Tester(TesterContext* context, const std::string& requestor_url) | 321 Tester(TesterContext* context, const std::string& requestor_url) |
323 : context_(context), requestor_url_(requestor_url) {} | 322 : context_(context), requestor_url_(requestor_url) {} |
324 virtual ~Tester() {} | 323 ~Tester() override {} |
325 | 324 |
326 private: | 325 private: |
327 virtual void Load(ApplicationManager* manager, | 326 void Load(ApplicationManager* manager, |
328 const GURL& url, | 327 const GURL& url, |
329 scoped_refptr<LoadCallbacks> callbacks) override { | 328 scoped_refptr<LoadCallbacks> callbacks) override { |
330 app_.reset( | 329 app_.reset( |
331 new ApplicationImpl(this, callbacks->RegisterApplication().Pass())); | 330 new ApplicationImpl(this, callbacks->RegisterApplication().Pass())); |
332 } | 331 } |
333 | 332 |
334 virtual void OnApplicationError(ApplicationManager* manager, | 333 void OnApplicationError(ApplicationManager* manager, |
335 const GURL& url) override {} | 334 const GURL& url) override {} |
336 | 335 |
337 virtual bool ConfigureIncomingConnection( | 336 bool ConfigureIncomingConnection(ApplicationConnection* connection) override { |
338 ApplicationConnection* connection) override { | |
339 if (!requestor_url_.empty() && | 337 if (!requestor_url_.empty() && |
340 requestor_url_ != connection->GetRemoteApplicationURL()) { | 338 requestor_url_ != connection->GetRemoteApplicationURL()) { |
341 context_->set_tester_called_quit(); | 339 context_->set_tester_called_quit(); |
342 context_->QuitSoon(); | 340 context_->QuitSoon(); |
343 base::MessageLoop::current()->Quit(); | 341 base::MessageLoop::current()->Quit(); |
344 return false; | 342 return false; |
345 } | 343 } |
346 // If we're coming from A, then add B, otherwise A. | 344 // If we're coming from A, then add B, otherwise A. |
347 if (connection->GetRemoteApplicationURL() == kTestAURLString) | 345 if (connection->GetRemoteApplicationURL() == kTestAURLString) |
348 connection->AddService<TestB>(this); | 346 connection->AddService<TestB>(this); |
349 else | 347 else |
350 connection->AddService<TestA>(this); | 348 connection->AddService<TestA>(this); |
351 return true; | 349 return true; |
352 } | 350 } |
353 | 351 |
354 virtual bool ConfigureOutgoingConnection( | 352 bool ConfigureOutgoingConnection(ApplicationConnection* connection) override { |
355 ApplicationConnection* connection) override { | |
356 // If we're connecting to B, then add C. | 353 // If we're connecting to B, then add C. |
357 if (connection->GetRemoteApplicationURL() == kTestBURLString) | 354 if (connection->GetRemoteApplicationURL() == kTestBURLString) |
358 connection->AddService<TestC>(this); | 355 connection->AddService<TestC>(this); |
359 return true; | 356 return true; |
360 } | 357 } |
361 | 358 |
362 virtual void Create(ApplicationConnection* connection, | 359 void Create(ApplicationConnection* connection, |
363 InterfaceRequest<TestA> request) override { | 360 InterfaceRequest<TestA> request) override { |
364 BindToRequest(new TestAImpl(connection, context_), &request); | 361 BindToRequest(new TestAImpl(connection, context_), &request); |
365 } | 362 } |
366 | 363 |
367 virtual void Create(ApplicationConnection* connection, | 364 void Create(ApplicationConnection* connection, |
368 InterfaceRequest<TestB> request) override { | 365 InterfaceRequest<TestB> request) override { |
369 BindToRequest(new TestBImpl(connection, context_), &request); | 366 BindToRequest(new TestBImpl(connection, context_), &request); |
370 } | 367 } |
371 | 368 |
372 virtual void Create(ApplicationConnection* connection, | 369 void Create(ApplicationConnection* connection, |
373 InterfaceRequest<TestC> request) override { | 370 InterfaceRequest<TestC> request) override { |
374 BindToRequest(new TestCImpl(connection, context_), &request); | 371 BindToRequest(new TestCImpl(connection, context_), &request); |
375 } | 372 } |
376 | 373 |
377 TesterContext* context_; | 374 TesterContext* context_; |
378 scoped_ptr<ApplicationImpl> app_; | 375 scoped_ptr<ApplicationImpl> app_; |
379 std::string requestor_url_; | 376 std::string requestor_url_; |
380 }; | 377 }; |
381 | 378 |
382 class TestServiceInterceptor : public ApplicationManager::Interceptor { | 379 class TestServiceInterceptor : public ApplicationManager::Interceptor { |
383 public: | 380 public: |
384 TestServiceInterceptor() : call_count_(0) {} | 381 TestServiceInterceptor() : call_count_(0) {} |
385 | 382 |
386 virtual ServiceProviderPtr OnConnectToClient( | 383 ServiceProviderPtr OnConnectToClient( |
387 const GURL& url, | 384 const GURL& url, |
388 ServiceProviderPtr service_provider) override { | 385 ServiceProviderPtr service_provider) override { |
389 ++call_count_; | 386 ++call_count_; |
390 url_ = url; | 387 url_ = url; |
391 return service_provider.Pass(); | 388 return service_provider.Pass(); |
392 } | 389 } |
393 | 390 |
394 std::string url_spec() const { | 391 std::string url_spec() const { |
395 if (!url_.is_valid()) | 392 if (!url_.is_valid()) |
396 return "invalid url"; | 393 return "invalid url"; |
(...skipping 277 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
674 std::string url("test:test3"); | 671 std::string url("test:test3"); |
675 TestServicePtr test_service; | 672 TestServicePtr test_service; |
676 application_manager_->ConnectToService(GURL(url), &test_service); | 673 application_manager_->ConnectToService(GURL(url), &test_service); |
677 | 674 |
678 EXPECT_EQ(1, interceptor.call_count()); | 675 EXPECT_EQ(1, interceptor.call_count()); |
679 EXPECT_EQ(url, interceptor.url_spec()); | 676 EXPECT_EQ(url, interceptor.url_spec()); |
680 EXPECT_EQ(1, default_loader->num_loads()); | 677 EXPECT_EQ(1, default_loader->num_loads()); |
681 } | 678 } |
682 | 679 |
683 } // namespace mojo | 680 } // namespace mojo |
OLD | NEW |