OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #ifndef BASE_WIN_SCOPED_COM_INITIALIZER_H_ | 5 #ifndef BASE_WIN_SCOPED_COM_INITIALIZER_H_ |
6 #define BASE_WIN_SCOPED_COM_INITIALIZER_H_ | 6 #define BASE_WIN_SCOPED_COM_INITIALIZER_H_ |
7 | 7 |
| 8 #include <objbase.h> |
| 9 |
8 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
9 #include "base/logging.h" | 11 #include "base/logging.h" |
10 #include "build/build_config.h" | 12 #include "build/build_config.h" |
11 | 13 |
12 #if defined(OS_WIN) | |
13 | |
14 #include <objbase.h> | |
15 | |
16 namespace base { | 14 namespace base { |
17 namespace win { | 15 namespace win { |
18 | 16 |
19 // Initializes COM in the constructor (STA or MTA), and uninitializes COM in the | 17 // Initializes COM in the constructor (STA or MTA), and uninitializes COM in the |
20 // destructor. | 18 // destructor. |
21 class ScopedCOMInitializer { | 19 class ScopedCOMInitializer { |
22 public: | 20 public: |
23 // Enum value provided to initialize the thread as an MTA instead of STA. | 21 // Enum value provided to initialize the thread as an MTA instead of STA. |
24 enum SelectMTA { kMTA }; | 22 enum SelectMTA { kMTA }; |
25 | 23 |
26 // Constructor for STA initialization. | 24 // Constructor for STA initialization. |
27 ScopedCOMInitializer() { | 25 ScopedCOMInitializer() { |
28 Initialize(COINIT_APARTMENTTHREADED); | 26 Initialize(COINIT_APARTMENTTHREADED); |
29 } | 27 } |
30 | 28 |
31 // Constructor for MTA initialization. | 29 // Constructor for MTA initialization. |
32 explicit ScopedCOMInitializer(SelectMTA mta) { | 30 explicit ScopedCOMInitializer(SelectMTA mta) { |
33 Initialize(COINIT_MULTITHREADED); | 31 Initialize(COINIT_MULTITHREADED); |
34 } | 32 } |
35 | 33 |
36 ~ScopedCOMInitializer() { | 34 ~ScopedCOMInitializer() { |
37 #ifndef NDEBUG | 35 #ifndef NDEBUG |
38 // Using the windows API directly to avoid dependency on platform_thread. | 36 // Using the windows API directly to avoid dependency on platform_thread. |
39 DCHECK_EQ(GetCurrentThreadId(), thread_id_); | 37 DCHECK_EQ(GetCurrentThreadId(), thread_id_); |
40 #endif | 38 #endif |
41 if (SUCCEEDED(hr_)) | 39 if (succeeded()) |
42 CoUninitialize(); | 40 CoUninitialize(); |
43 } | 41 } |
44 | 42 |
45 bool succeeded() const { return SUCCEEDED(hr_); } | 43 bool succeeded() const { return SUCCEEDED(hr_); } |
46 | 44 |
47 private: | 45 private: |
48 void Initialize(COINIT init) { | 46 void Initialize(COINIT init) { |
49 #ifndef NDEBUG | 47 #ifndef NDEBUG |
50 thread_id_ = GetCurrentThreadId(); | 48 thread_id_ = GetCurrentThreadId(); |
51 #endif | 49 #endif |
52 hr_ = CoInitializeEx(NULL, init); | 50 hr_ = CoInitializeEx(NULL, init); |
53 #ifndef NDEBUG | 51 #ifndef NDEBUG |
54 switch (hr_) { | 52 if (hr_ == S_FALSE) |
55 case S_FALSE: | 53 LOG(ERROR) << "Multiple CoInitialize() calls for thread " << thread_id_; |
56 LOG(ERROR) << "Multiple CoInitialize() called for thread " | 54 else |
57 << thread_id_; | 55 DCHECK_NE(RPC_E_CHANGED_MODE, hr_) << "Invalid COM thread model change"; |
58 break; | |
59 case RPC_E_CHANGED_MODE: | |
60 DCHECK(false) << "Invalid COM thread model change"; | |
61 break; | |
62 default: | |
63 break; | |
64 } | |
65 #endif | 56 #endif |
66 } | 57 } |
67 | 58 |
68 HRESULT hr_; | 59 HRESULT hr_; |
69 #ifndef NDEBUG | 60 #ifndef NDEBUG |
70 // In debug builds we use this variable to catch a potential bug where a | 61 // In debug builds we use this variable to catch a potential bug where a |
71 // ScopedCOMInitializer instance is deleted on a different thread than it | 62 // ScopedCOMInitializer instance is deleted on a different thread than it |
72 // was initially created on. If that ever happens it can have bad | 63 // was initially created on. If that ever happens it can have bad |
73 // consequences and the cause can be tricky to track down. | 64 // consequences and the cause can be tricky to track down. |
74 DWORD thread_id_; | 65 DWORD thread_id_; |
75 #endif | 66 #endif |
76 | 67 |
77 DISALLOW_COPY_AND_ASSIGN(ScopedCOMInitializer); | 68 DISALLOW_COPY_AND_ASSIGN(ScopedCOMInitializer); |
78 }; | 69 }; |
79 | 70 |
80 } // namespace win | 71 } // namespace win |
81 } // namespace base | 72 } // namespace base |
82 | 73 |
83 #else | |
84 | |
85 namespace base { | |
86 namespace win { | |
87 | |
88 // Do-nothing class for other platforms. | |
89 class ScopedCOMInitializer { | |
90 public: | |
91 enum SelectMTA { kMTA }; | |
92 ScopedCOMInitializer() {} | |
93 explicit ScopedCOMInitializer(SelectMTA mta) {} | |
94 ~ScopedCOMInitializer() {} | |
95 | |
96 bool succeeded() const { return true; } | |
97 | |
98 private: | |
99 DISALLOW_COPY_AND_ASSIGN(ScopedCOMInitializer); | |
100 }; | |
101 | |
102 } // namespace win | |
103 } // namespace base | |
104 | |
105 #endif | |
106 | |
107 #endif // BASE_WIN_SCOPED_COM_INITIALIZER_H_ | 74 #endif // BASE_WIN_SCOPED_COM_INITIALIZER_H_ |
OLD | NEW |