Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(83)

Unified Diff: remoting/base/auto_thread.cc

Issue 11348087: Add AutoThread types for Windows that initialize COM. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Add missing OS checks. Created 8 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « remoting/base/auto_thread.h ('k') | remoting/base/auto_thread_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: remoting/base/auto_thread.cc
diff --git a/remoting/base/auto_thread.cc b/remoting/base/auto_thread.cc
index 324ccc36c4289ebe8cfa44f4329f0e3dae388d90..9f513783bff3e9c304eca079f2eef7af8638b558 100644
--- a/remoting/base/auto_thread.cc
+++ b/remoting/base/auto_thread.cc
@@ -12,11 +12,34 @@
#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> CreateComInitializer(
+ 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;
// Used to receive the AutoThreadTaskRunner for the thread.
@@ -37,12 +60,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,8 +71,28 @@ 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),
+#if defined(OS_WIN)
+ com_init_type_(COM_INIT_NONE),
+#endif
thread_(0),
name_(name),
was_quit_properly_(false) {
@@ -60,6 +100,9 @@ AutoThread::AutoThread(const char* name)
AutoThread::AutoThread(const char* name, AutoThreadTaskRunner* joiner)
: startup_data_(NULL),
+#if defined(OS_WIN)
+ com_init_type_(COM_INIT_NONE),
+#endif
thread_(0),
name_(name),
was_quit_properly_(false),
@@ -78,6 +121,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 +149,12 @@ 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) {
+ DCHECK_EQ(com_init_type_, COM_INIT_NONE);
+ com_init_type_ = com_init_type;
}
+#endif
void AutoThread::QuitThread(
scoped_refptr<base::SingleThreadTaskRunner> task_runner) {
@@ -150,6 +199,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(
+ CreateComInitializer(com_init_type_));
+#endif
+
message_loop.Run();
// Assert that MessageLoop::Quit was called by AutoThread::QuitThread.
« no previous file with comments | « remoting/base/auto_thread.h ('k') | remoting/base/auto_thread_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698