Connect PPAPI IPC channels for non-SFI mode.
[chromium-blink-merge.git] / base / win / scoped_com_initializer.h
blob392c351cc7d795bfdd4d945345e637eaa08106f6
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
3 // found in the LICENSE file.
5 #ifndef BASE_WIN_SCOPED_COM_INITIALIZER_H_
6 #define BASE_WIN_SCOPED_COM_INITIALIZER_H_
8 #include <objbase.h>
10 #include "base/basictypes.h"
11 #include "base/logging.h"
12 #include "build/build_config.h"
14 namespace base {
15 namespace win {
17 // Initializes COM in the constructor (STA or MTA), and uninitializes COM in the
18 // destructor.
19 class ScopedCOMInitializer {
20 public:
21 // Enum value provided to initialize the thread as an MTA instead of STA.
22 enum SelectMTA { kMTA };
24 // Constructor for STA initialization.
25 ScopedCOMInitializer() {
26 Initialize(COINIT_APARTMENTTHREADED);
29 // Constructor for MTA initialization.
30 explicit ScopedCOMInitializer(SelectMTA mta) {
31 Initialize(COINIT_MULTITHREADED);
34 ~ScopedCOMInitializer() {
35 #ifndef NDEBUG
36 // Using the windows API directly to avoid dependency on platform_thread.
37 DCHECK_EQ(GetCurrentThreadId(), thread_id_);
38 #endif
39 if (succeeded())
40 CoUninitialize();
43 bool succeeded() const { return SUCCEEDED(hr_); }
45 private:
46 void Initialize(COINIT init) {
47 #ifndef NDEBUG
48 thread_id_ = GetCurrentThreadId();
49 #endif
50 hr_ = CoInitializeEx(NULL, init);
51 #ifndef NDEBUG
52 if (hr_ == S_FALSE)
53 LOG(ERROR) << "Multiple CoInitialize() calls for thread " << thread_id_;
54 else
55 DCHECK_NE(RPC_E_CHANGED_MODE, hr_) << "Invalid COM thread model change";
56 #endif
59 HRESULT hr_;
60 #ifndef NDEBUG
61 // In debug builds we use this variable to catch a potential bug where a
62 // ScopedCOMInitializer instance is deleted on a different thread than it
63 // was initially created on. If that ever happens it can have bad
64 // consequences and the cause can be tricky to track down.
65 DWORD thread_id_;
66 #endif
68 DISALLOW_COPY_AND_ASSIGN(ScopedCOMInitializer);
71 } // namespace win
72 } // namespace base
74 #endif // BASE_WIN_SCOPED_COM_INITIALIZER_H_