OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "mojo/shell/shell_test_helper.h" |
| 6 |
| 7 #include "base/command_line.h" |
| 8 #include "base/logging.h" |
| 9 #include "base/message_loop/message_loop.h" |
| 10 #include "mojo/shell/context.h" |
| 11 #include "mojo/shell/init.h" |
| 12 |
| 13 namespace mojo { |
| 14 namespace shell { |
| 15 |
| 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 |
| 18 // thread. |
| 19 struct ShellTestHelper::State { |
| 20 scoped_ptr<Context> context; |
| 21 ScopedShellHandle shell_handle; |
| 22 }; |
| 23 |
| 24 namespace { |
| 25 |
| 26 class TestServiceLoader : public ServiceLoader { |
| 27 public: |
| 28 explicit TestServiceLoader(ScopedShellHandle* handle) : handle_(handle) {} |
| 29 virtual ~TestServiceLoader() {} |
| 30 |
| 31 // ServiceLoader: |
| 32 virtual void LoadService(ServiceManager* manager, |
| 33 const GURL& url, |
| 34 ScopedShellHandle service_handle) OVERRIDE { |
| 35 // Bounce the handle back to the main thread. |
| 36 *handle_ = service_handle.Pass(); |
| 37 } |
| 38 |
| 39 virtual void OnServiceError(ServiceManager* manager, |
| 40 const GURL& url) OVERRIDE { |
| 41 CHECK(false); |
| 42 } |
| 43 |
| 44 private: |
| 45 ScopedShellHandle* handle_; |
| 46 |
| 47 DISALLOW_COPY_AND_ASSIGN(TestServiceLoader); |
| 48 }; |
| 49 |
| 50 void StartShellOnShellThread(ShellTestHelper::State* state) { |
| 51 state->context.reset(new Context); |
| 52 MessagePipe pipe; |
| 53 state->context->service_manager()->set_default_loader( |
| 54 scoped_ptr<ServiceLoader>(new TestServiceLoader(&state->shell_handle))); |
| 55 state->context->service_manager()->Connect(GURL("test:test"), |
| 56 pipe.handle0.Pass()); |
| 57 // Destroy TestServiceLoader as it's no longer needed. |
| 58 state->context->service_manager()->set_default_loader( |
| 59 scoped_ptr<ServiceLoader>()); |
| 60 } |
| 61 |
| 62 } // namespace |
| 63 |
| 64 class ShellTestHelper::TestShellClient : public ShellClient { |
| 65 public: |
| 66 TestShellClient() {} |
| 67 virtual ~TestShellClient() {} |
| 68 |
| 69 // ShellClient: |
| 70 virtual void AcceptConnection( |
| 71 const mojo::String& url, |
| 72 ScopedMessagePipeHandle client_handle) OVERRIDE { |
| 73 } |
| 74 |
| 75 private: |
| 76 DISALLOW_COPY_AND_ASSIGN(TestShellClient); |
| 77 }; |
| 78 |
| 79 ShellTestHelper::ShellTestHelper() |
| 80 : shell_thread_("Test Shell Thread"), |
| 81 state_(NULL) { |
| 82 CommandLine::Init(0, NULL); |
| 83 mojo::shell::InitializeLogging(); |
| 84 } |
| 85 |
| 86 ShellTestHelper::~ShellTestHelper() { |
| 87 if (state_) { |
| 88 // |state_| contains data created on the background thread. Destroy it |
| 89 // there so that there aren't any race conditions. |
| 90 shell_thread_.message_loop()->DeleteSoon(FROM_HERE, state_); |
| 91 state_ = NULL; |
| 92 } |
| 93 } |
| 94 |
| 95 void ShellTestHelper::Init() { |
| 96 DCHECK(!state_); |
| 97 state_ = new State; |
| 98 shell_thread_.Start(); |
| 99 shell_thread_.message_loop()->message_loop_proxy()->PostTaskAndReply( |
| 100 FROM_HERE, |
| 101 base::Bind(&StartShellOnShellThread, state_), |
| 102 base::Bind(&ShellTestHelper::OnShellStarted, base::Unretained(this))); |
| 103 run_loop_.reset(new base::RunLoop); |
| 104 run_loop_->Run(); |
| 105 } |
| 106 |
| 107 void ShellTestHelper::OnShellStarted() { |
| 108 DCHECK(state_); |
| 109 shell_client_.reset(new TestShellClient); |
| 110 shell_.reset(state_->shell_handle.Pass(), shell_client_.get()); |
| 111 run_loop_->Quit(); |
| 112 } |
| 113 |
| 114 } // namespace shell |
| 115 } // namespace mojo |
OLD | NEW |