OLD | NEW |
| (Empty) |
1 // Copyright 2009 Google Inc. | |
2 // | |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
4 // you may not use this file except in compliance with the License. | |
5 // You may obtain a copy of the License at | |
6 // | |
7 // http://www.apache.org/licenses/LICENSE-2.0 | |
8 // | |
9 // Unless required by applicable law or agreed to in writing, software | |
10 // distributed under the License is distributed on an "AS IS" BASIS, | |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
12 // See the License for the specific language governing permissions and | |
13 // limitations under the License. | |
14 // ======================================================================== | |
15 | |
16 // TODO(omaha): rename the file to match the name of the class. | |
17 | |
18 #ifndef OMAHA_GOOPDATE_COM_WRAPPER_CREATOR_H_ | |
19 #define OMAHA_GOOPDATE_COM_WRAPPER_CREATOR_H_ | |
20 | |
21 #include <windows.h> | |
22 #include <atlbase.h> | |
23 #include <atlcom.h> | |
24 #include "base/basictypes.h" | |
25 #include "base/scoped_ptr.h" | |
26 #include "base/scoped_ptr_address.h" | |
27 #include "base/utils.h" | |
28 #include "goopdate/omaha3_idl.h" | |
29 #include "omaha/goopdate/model_object.h" | |
30 #include "third_party/bar/shared_ptr.h" | |
31 | |
32 namespace omaha { | |
33 | |
34 class AppBundle; | |
35 typedef shared_ptr<AppBundle> ControllingPtr; | |
36 | |
37 // Generalizes the creation of COM wrappers for a given class T. | |
38 // It requires: | |
39 // * The wrapper class TWrapper derives from ComWrapper | |
40 // * The wrapped class T provides access to model instance | |
41 template <typename TWrapper, typename T> | |
42 class ComWrapper : public CComObjectRootEx<CComObjectThreadModel> { | |
43 public: | |
44 | |
45 static HRESULT Create(const ControllingPtr& controlling_ptr, | |
46 T* t, IDispatch** t_wrapper) { | |
47 ASSERT1(t); | |
48 ASSERT1(t_wrapper); | |
49 | |
50 ASSERT1(IsModelLockedByCaller(t->model())); | |
51 | |
52 scoped_ptr<TComObject> t_com_object; | |
53 HRESULT hr = TComObject::CreateInstance(address(t_com_object)); | |
54 if (FAILED(hr)) { | |
55 return hr; | |
56 } | |
57 | |
58 hr = t_com_object->QueryInterface(t_wrapper); | |
59 if (FAILED(hr)) { | |
60 return hr; | |
61 } | |
62 | |
63 t_com_object->model_ = t->model(); | |
64 t_com_object->controlling_ptr_ = controlling_ptr; | |
65 t_com_object->wrapped_obj_ = t; | |
66 | |
67 t_com_object.release(); | |
68 return S_OK; | |
69 } | |
70 | |
71 protected: | |
72 ComWrapper() : model_(NULL), wrapped_obj_(NULL) {} | |
73 | |
74 ~ComWrapper() {} | |
75 | |
76 void FinalRelease() { | |
77 controlling_ptr_.reset(); | |
78 wrapped_obj_ = NULL; | |
79 } | |
80 | |
81 const Model* model() const { | |
82 return omaha::interlocked_exchange_pointer(&model_, model_); | |
83 } | |
84 | |
85 const ControllingPtr& controlling_ptr() const { | |
86 ASSERT1(IsModelLockedByCaller(wrapped_obj_->model())); | |
87 return controlling_ptr_; | |
88 } | |
89 | |
90 T* wrapped_obj() { | |
91 return omaha::interlocked_exchange_pointer(&wrapped_obj_, wrapped_obj_); | |
92 } | |
93 | |
94 private: | |
95 | |
96 typedef CComObject<TWrapper> TComObject; | |
97 | |
98 // The pointer is written and read from multiple threads and it is written | |
99 // to with the same value by the atomic pointer exchange, hence the volatile | |
100 // and mutable cv qualifiers respectively. | |
101 mutable Model* volatile model_; | |
102 | |
103 ControllingPtr controlling_ptr_; | |
104 | |
105 T* wrapped_obj_; | |
106 | |
107 DISALLOW_COPY_AND_ASSIGN(ComWrapper); | |
108 }; | |
109 | |
110 } // namespace omaha | |
111 | |
112 #endif // OMAHA_GOOPDATE_COM_WRAPPER_CREATOR_H_ | |
113 | |
OLD | NEW |