Chromium Code Reviews| Index: remoting/base/auto_thread.cc |
| diff --git a/remoting/base/auto_thread.cc b/remoting/base/auto_thread.cc |
| index 324ccc36c4289ebe8cfa44f4329f0e3dae388d90..4979fd79f68c79b30bc4dbb8c9a9945ff40614f7 100644 |
| --- a/remoting/base/auto_thread.cc |
| +++ b/remoting/base/auto_thread.cc |
| @@ -12,12 +12,38 @@ |
| #include "base/synchronization/waitable_event.h" |
| #include "remoting/base/auto_thread_task_runner.h" |
| +#if defined(OS_WIN) |
| +#include "base/win/scoped_com_initializer.h" |
| +#endif |
| + |
| namespace remoting { |
| +namespace { |
| + |
| +#if defined(OS_WIN) |
| +scoped_ptr<base::win::ScopedCOMInitializer> CreateComInitializerWithType( |
|
alexeypa (please no reviews)
2012/11/26 19:16:13
nit: CreateComInitializer()
Wez
2012/11/27 02:22:27
Done.
|
| + AutoThread::ComInitType type) { |
| + scoped_ptr<base::win::ScopedCOMInitializer> initializer; |
| + if (type == AutoThread::COM_INIT_MTA) { |
| + initializer.reset(new base::win::ScopedCOMInitializer( |
| + base::win::ScopedCOMInitializer::kMTA)); |
| + } else if (type == AutoThread::COM_INIT_STA) { |
| + initializer.reset(new base::win::ScopedCOMInitializer()); |
| + } |
| + return initializer.Pass(); |
| +} |
| +#endif |
| + |
| +} |
| + |
| // Used to pass data to ThreadMain. This structure is allocated on the stack |
| // from within StartWithType. |
| struct AutoThread::StartupData { |
| + // Fields describing the desired thread behaviour. |
| MessageLoop::Type loop_type; |
| +#if defined(OS_WIN) |
| + ComInitType com_init_type; |
| +#endif |
|
Wez
2012/11/22 21:57:15
note-to-self: Remove this.
Wez
2012/11/27 02:22:27
Done.
|
| // Used to receive the AutoThreadTaskRunner for the thread. |
| scoped_refptr<AutoThreadTaskRunner> task_runner; |
| @@ -37,12 +63,9 @@ scoped_refptr<AutoThreadTaskRunner> AutoThread::CreateWithType( |
| MessageLoop::Type type) { |
| AutoThread* thread = new AutoThread(name, joiner); |
| scoped_refptr<AutoThreadTaskRunner> task_runner = thread->StartWithType(type); |
| - if (task_runner.get()) { |
| - return task_runner; |
| - } else { |
| + if (!task_runner) |
| delete thread; |
| - return NULL; |
| - } |
| + return task_runner; |
| } |
| // static |
| @@ -51,6 +74,23 @@ scoped_refptr<AutoThreadTaskRunner> AutoThread::Create( |
| return CreateWithType(name, joiner, MessageLoop::TYPE_DEFAULT); |
| } |
| +#if defined(OS_WIN) |
| +// static |
| +scoped_refptr<AutoThreadTaskRunner> AutoThread::CreateWithLoopAndComInitTypes( |
| + const char* name, |
| + scoped_refptr<AutoThreadTaskRunner> joiner, |
| + MessageLoop::Type loop_type, |
| + ComInitType com_init_type) { |
| + AutoThread* thread = new AutoThread(name, joiner); |
| + thread->SetComInitType(com_init_type); |
| + scoped_refptr<AutoThreadTaskRunner> task_runner = |
| + thread->StartWithType(loop_type); |
| + if (!task_runner) |
| + delete thread; |
| + return task_runner; |
| +} |
| +#endif |
| + |
| AutoThread::AutoThread(const char* name) |
| : startup_data_(NULL), |
|
alexeypa (please no reviews)
2012/11/26 19:16:13
Initialize |com_init_type_| here.
Wez
2012/11/27 02:22:27
Done.
|
| thread_(0), |
| @@ -78,6 +118,9 @@ AutoThread::~AutoThread() { |
| scoped_refptr<AutoThreadTaskRunner> |
| AutoThread::StartWithType(MessageLoop::Type type) { |
| DCHECK(!thread_); |
| +#if defined(OS_WIN) |
| + DCHECK(com_init_type_ != COM_INIT_STA || type == MessageLoop::TYPE_UI); |
| +#endif |
| StartupData startup_data(type); |
| startup_data_ = &startup_data; |
| @@ -103,9 +146,11 @@ AutoThread::StartWithType(MessageLoop::Type type) { |
| return startup_data.task_runner; |
| } |
| -scoped_refptr<AutoThreadTaskRunner> AutoThread::Start() { |
| - return StartWithType(MessageLoop::TYPE_DEFAULT); |
| +#if defined(OS_WIN) |
| +void AutoThread::SetComInitType(ComInitType com_init_type) { |
| + com_init_type_ = com_init_type; |
|
alexeypa (please no reviews)
2012/11/26 19:16:13
nit: Add DCHECK_EQ(com_init_type_, COM_INIT_NONE)
Wez
2012/11/27 02:22:27
Done.
|
| } |
| +#endif |
| void AutoThread::QuitThread( |
| scoped_refptr<base::SingleThreadTaskRunner> task_runner) { |
| @@ -150,6 +195,12 @@ void AutoThread::ThreadMain() { |
| // startup_data_ can't be touched anymore since the starting thread is now |
| // unlocked. |
| +#if defined(OS_WIN) |
| + // Initialize COM on the thread, if requested. |
| + scoped_ptr<base::win::ScopedCOMInitializer> com_initializer( |
| + CreateComInitializerWithType(com_init_type_)); |
| +#endif |
| + |
| message_loop.Run(); |
| // Assert that MessageLoop::Quit was called by AutoThread::QuitThread. |