OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 #pragma once | 7 #pragma once |
8 | 8 |
9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
10 #include "build/build_config.h" | 10 #include "build/build_config.h" |
11 | 11 |
12 #if defined(OS_WIN) | 12 #if defined(OS_WIN) |
13 | 13 |
14 #include <objbase.h> | 14 #include <objbase.h> |
15 | 15 |
16 namespace base { | 16 namespace base { |
17 namespace win { | 17 namespace win { |
18 | 18 |
19 // Initializes COM in the constructor (STA), and uninitializes COM in the | 19 // Initializes COM in the constructor (STA or MTA), and uninitializes COM in the |
20 // destructor. | 20 // destructor. |
21 class ScopedCOMInitializer { | 21 class ScopedCOMInitializer { |
22 public: | 22 public: |
| 23 // Enum value provided to initialize the thread as an MTA instead of STA. |
| 24 enum SelectMTA { kMTA }; |
| 25 |
| 26 // Constructor for STA initialization. |
23 ScopedCOMInitializer() : hr_(CoInitialize(NULL)) { | 27 ScopedCOMInitializer() : hr_(CoInitialize(NULL)) { |
24 } | 28 } |
25 | 29 |
| 30 // Constructor for MTA initialization. |
| 31 explicit ScopedCOMInitializer(SelectMTA mta) |
| 32 : hr_(CoInitializeEx(NULL, COINIT_MULTITHREADED)) { |
| 33 } |
| 34 |
26 ScopedCOMInitializer::~ScopedCOMInitializer() { | 35 ScopedCOMInitializer::~ScopedCOMInitializer() { |
27 if (SUCCEEDED(hr_)) | 36 if (SUCCEEDED(hr_)) |
28 CoUninitialize(); | 37 CoUninitialize(); |
29 } | 38 } |
30 | 39 |
| 40 bool succeeded() const { return SUCCEEDED(hr_); } |
| 41 |
31 private: | 42 private: |
32 HRESULT hr_; | 43 HRESULT hr_; |
33 | 44 |
34 DISALLOW_COPY_AND_ASSIGN(ScopedCOMInitializer); | 45 DISALLOW_COPY_AND_ASSIGN(ScopedCOMInitializer); |
35 }; | 46 }; |
36 | 47 |
37 } // namespace win | 48 } // namespace win |
38 } // namespace base | 49 } // namespace base |
39 | 50 |
40 #else | 51 #else |
41 | 52 |
42 namespace base { | 53 namespace base { |
43 namespace win { | 54 namespace win { |
44 | 55 |
45 // Do-nothing class for other platforms. | 56 // Do-nothing class for other platforms. |
46 class ScopedCOMInitializer { | 57 class ScopedCOMInitializer { |
47 public: | 58 public: |
| 59 enum SelectMTA { kMTA }; |
48 ScopedCOMInitializer() {} | 60 ScopedCOMInitializer() {} |
| 61 explicit ScopedCOMInitializer(SelectMTA mta) {} |
49 ~ScopedCOMInitializer() {} | 62 ~ScopedCOMInitializer() {} |
50 | 63 |
| 64 bool succeeded() const { return true; } |
| 65 |
51 private: | 66 private: |
52 DISALLOW_COPY_AND_ASSIGN(ScopedCOMInitializer); | 67 DISALLOW_COPY_AND_ASSIGN(ScopedCOMInitializer); |
53 }; | 68 }; |
54 | 69 |
55 } // namespace win | 70 } // namespace win |
56 } // namespace base | 71 } // namespace base |
57 | 72 |
58 #endif | 73 #endif |
59 | 74 |
60 #endif // BASE_WIN_SCOPED_COM_INITIALIZER_H_ | 75 #endif // BASE_WIN_SCOPED_COM_INITIALIZER_H_ |
OLD | NEW |