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