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 "mojo/shell/shell_test_helper.h" | 5 #include "mojo/shell/shell_test_helper.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/message_loop/message_loop.h" | |
10 #include "mojo/service_manager/service_loader.h" | |
11 #include "mojo/shell/context.h" | |
12 #include "mojo/shell/init.h" | 9 #include "mojo/shell/init.h" |
13 | 10 |
14 namespace mojo { | 11 namespace mojo { |
15 namespace shell { | 12 namespace shell { |
16 | 13 |
17 // State used on the background thread. Be careful, this is created on the main | |
18 // thread than passed to the shell thread. Destruction happens on the shell | |
19 // thread. | |
20 struct ShellTestHelper::State { | |
21 scoped_ptr<Context> context; | |
22 scoped_ptr<ServiceManager::TestAPI> test_api; | |
23 ScopedMessagePipeHandle service_provider_handle; | |
24 }; | |
25 | |
26 namespace { | |
27 | |
28 void StartShellOnShellThread(ShellTestHelper::State* state) { | |
29 state->context.reset(new Context); | |
30 state->test_api.reset( | |
31 new ServiceManager::TestAPI(state->context->service_manager())); | |
32 state->service_provider_handle = state->test_api->GetServiceProviderHandle(); | |
33 } | |
34 | |
35 void SetLoaderForURLOnShellThread(ShellTestHelper::State* state, | |
36 scoped_ptr<ServiceLoader> loader, | |
37 const GURL& url) { | |
38 state->context->service_manager()->SetLoaderForURL(loader.Pass(), url); | |
39 } | |
40 | |
41 } // namespace | |
42 | |
43 class ShellTestHelper::TestServiceProvider : public ServiceProvider { | 14 class ShellTestHelper::TestServiceProvider : public ServiceProvider { |
44 public: | 15 public: |
45 TestServiceProvider() {} | 16 TestServiceProvider() {} |
46 virtual ~TestServiceProvider() {} | 17 virtual ~TestServiceProvider() {} |
47 | 18 |
48 // ServiceProvider: | 19 // ServiceProvider: |
49 virtual void ConnectToService( | 20 virtual void ConnectToService( |
50 const mojo::String& url, | 21 const mojo::String& url, |
51 const mojo::String& name, | 22 const mojo::String& name, |
52 ScopedMessagePipeHandle client_handle) OVERRIDE {} | 23 ScopedMessagePipeHandle client_handle) OVERRIDE {} |
53 | 24 |
54 private: | 25 private: |
55 DISALLOW_COPY_AND_ASSIGN(TestServiceProvider); | 26 DISALLOW_COPY_AND_ASSIGN(TestServiceProvider); |
56 }; | 27 }; |
57 | 28 |
58 ShellTestHelper::ShellTestHelper() | 29 ShellTestHelper::ShellTestHelper() { |
59 : service_provider_thread_("shell_test_helper"), | |
60 state_(NULL) { | |
61 base::CommandLine::Init(0, NULL); | 30 base::CommandLine::Init(0, NULL); |
62 mojo::shell::InitializeLogging(); | 31 mojo::shell::InitializeLogging(); |
63 } | 32 } |
64 | 33 |
65 ShellTestHelper::~ShellTestHelper() { | 34 ShellTestHelper::~ShellTestHelper() { |
66 if (state_) { | |
67 // |state_| contains data created on the background thread. Destroy it | |
68 // there so that there aren't any race conditions. | |
69 service_provider_thread_.message_loop()->DeleteSoon(FROM_HERE, state_); | |
70 state_ = NULL; | |
71 } | |
72 } | 35 } |
73 | 36 |
74 void ShellTestHelper::Init() { | 37 void ShellTestHelper::Init() { |
75 DCHECK(!state_); | 38 context_.reset(new Context); |
76 state_ = new State; | 39 test_api_.reset(new ServiceManager::TestAPI(context_->service_manager())); |
77 service_provider_thread_.Start(); | 40 local_service_provider_.reset(new TestServiceProvider); |
78 base::MessageLoopProxy* message_loop_proxy = | 41 service_provider_.Bind(test_api_->GetServiceProviderHandle().Pass()); |
79 service_provider_thread_.message_loop()->message_loop_proxy(); | 42 service_provider_.set_client(local_service_provider_.get()); |
80 message_loop_proxy->PostTaskAndReply( | |
81 FROM_HERE, | |
82 base::Bind(&StartShellOnShellThread, state_), | |
83 base::Bind(&ShellTestHelper::OnServiceProviderStarted, | |
84 base::Unretained(this))); | |
85 run_loop_.reset(new base::RunLoop); | |
86 run_loop_->Run(); | |
87 } | 43 } |
88 | 44 |
89 void ShellTestHelper::SetLoaderForURL(scoped_ptr<ServiceLoader> loader, | 45 void ShellTestHelper::SetLoaderForURL(scoped_ptr<ServiceLoader> loader, |
90 const GURL& url) { | 46 const GURL& url) { |
91 service_provider_thread_.message_loop()->message_loop_proxy()->PostTask( | 47 context_->service_manager()->SetLoaderForURL(loader.Pass(), url); |
92 FROM_HERE, | |
93 base::Bind(&SetLoaderForURLOnShellThread, state_, base::Passed(&loader), | |
94 url)); | |
95 } | |
96 | |
97 void ShellTestHelper::OnServiceProviderStarted() { | |
98 DCHECK(state_); | |
99 local_service_provider_.reset(new TestServiceProvider); | |
100 service_provider_.Bind(state_->service_provider_handle.Pass()); | |
101 service_provider_.set_client(local_service_provider_.get()); | |
102 run_loop_->Quit(); | |
103 } | 48 } |
104 | 49 |
105 } // namespace shell | 50 } // namespace shell |
106 } // namespace mojo | 51 } // namespace mojo |
OLD | NEW |