OLD | NEW |
| (Empty) |
1 // Copyright 2009-2010 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 #ifndef OMAHA_GOOPDATE_GOOGLE_UPDATE3_H_ | |
17 #define OMAHA_GOOPDATE_GOOGLE_UPDATE3_H_ | |
18 | |
19 #include <atlbase.h> | |
20 #include <atlcom.h> | |
21 #include <atlstr.h> | |
22 #include <vector> | |
23 #include "goopdate/omaha3_idl.h" | |
24 #include "base/scoped_ptr.h" | |
25 #include "omaha/base/atlregmapex.h" | |
26 #include "omaha/base/constants.h" | |
27 #include "omaha/base/error.h" | |
28 #include "omaha/base/exception_barrier.h" | |
29 #include "omaha/base/preprocessor_fun.h" | |
30 #include "omaha/base/user_rights.h" | |
31 #include "omaha/base/utils.h" | |
32 #include "omaha/common/goopdate_utils.h" | |
33 #include "omaha/goopdate/com_proxy.h" | |
34 #include "omaha/goopdate/model.h" | |
35 #include "omaha/goopdate/non_localized_resource.h" | |
36 #include "omaha/goopdate/worker.h" | |
37 #include "third_party/bar/shared_ptr.h" | |
38 | |
39 namespace omaha { | |
40 | |
41 // The ATL Singleton Class Factory does not work very well if errors happen in | |
42 // CreateInstance(), the server continues running. This is because the module | |
43 // count is not incremented or decremented. This class fixes the issue so that | |
44 // on error, the server shuts down as expected. | |
45 template <class T> | |
46 class SingletonClassFactory : public CComClassFactorySingleton<T> { | |
47 public: | |
48 SingletonClassFactory() {} | |
49 virtual ~SingletonClassFactory() {} | |
50 | |
51 STDMETHOD(CreateInstance)(LPUNKNOWN unk, REFIID iid, void** obj) { | |
52 HRESULT hr = CComClassFactorySingleton<T>::CreateInstance(unk, iid, obj); | |
53 if (FAILED(hr)) { | |
54 CORE_LOG(LE, (_T("[SingletonClassFactory::CreateInstance failed][0x%x]") | |
55 _T("[pulsing module count]"), hr)); | |
56 LockServer(TRUE); | |
57 LockServer(FALSE); | |
58 | |
59 return hr; | |
60 } | |
61 | |
62 return hr; | |
63 } | |
64 | |
65 private: | |
66 DISALLOW_COPY_AND_ASSIGN(SingletonClassFactory); | |
67 }; | |
68 | |
69 template <bool machine, const TCHAR* const progid, const GUID& clsid, | |
70 UINT registry_resid, const TCHAR* const hkroot> | |
71 struct Update3COMClassMode { | |
72 static bool is_machine() { return machine; } | |
73 static const TCHAR* const prog_id() { return progid; } | |
74 static GUID class_id() { return clsid; } | |
75 static UINT registry_res_id() { return registry_resid; } | |
76 static const TCHAR* const hk_root() { return hkroot; } | |
77 }; | |
78 | |
79 #pragma warning(push) | |
80 // C4640: construction of local static object is not thread-safe | |
81 #pragma warning(disable : 4640) | |
82 // C4505: unreferenced IUnknown local functions have been removed | |
83 #pragma warning(disable : 4505) | |
84 | |
85 template <typename T> | |
86 class ATL_NO_VTABLE Update3COMClass | |
87 : public CComObjectRootEx<CComObjectThreadModel>, | |
88 public CComCoClass<Update3COMClass<T> >, | |
89 public IDispatchImpl<IGoogleUpdate3, | |
90 &__uuidof(IGoogleUpdate3), | |
91 &CAtlModule::m_libid, | |
92 kMajorTypeLibVersion, | |
93 kMinorTypeLibVersion>, | |
94 public StdMarshalInfo { | |
95 public: | |
96 typedef Update3COMClass<T> Update3COMClassT; | |
97 typedef SingletonClassFactory<Update3COMClassT> SingletonClassFactoryT; | |
98 | |
99 Update3COMClass() : StdMarshalInfo(T::is_machine()), model_(NULL) {} | |
100 virtual ~Update3COMClass() {} | |
101 | |
102 DECLARE_CLASSFACTORY_EX(SingletonClassFactoryT) | |
103 DECLARE_NOT_AGGREGATABLE(Update3COMClassT) | |
104 DECLARE_PROTECT_FINAL_CONSTRUCT() | |
105 | |
106 DECLARE_REGISTRY_RESOURCEID_EX(T::registry_res_id()) | |
107 | |
108 BEGIN_REGISTRY_MAP() | |
109 REGMAP_ENTRY(_T("HKROOT"), T::hk_root()) | |
110 REGMAP_EXE_MODULE(_T("MODULE")) | |
111 REGMAP_ENTRY(_T("VERSION"), _T("1.0")) | |
112 REGMAP_ENTRY(_T("PROGID"), T::prog_id()) | |
113 REGMAP_ENTRY(_T("DESCRIPTION"), _T("Update3COMClass")) | |
114 REGMAP_UUID(_T("CLSID"), T::class_id()) | |
115 END_REGISTRY_MAP() | |
116 | |
117 BEGIN_COM_MAP(Update3COMClassT) | |
118 COM_INTERFACE_ENTRY(IGoogleUpdate3) | |
119 COM_INTERFACE_ENTRY(IDispatch) | |
120 COM_INTERFACE_ENTRY(IStdMarshalInfo) | |
121 END_COM_MAP() | |
122 | |
123 STDMETHODIMP get_Count(long* count) { // NOLINT | |
124 ASSERT1(count); | |
125 ExceptionBarrier barrier; | |
126 | |
127 __mutexScope(model()->lock()); | |
128 *count = model()->GetNumberOfAppBundles(); | |
129 | |
130 return S_OK; | |
131 } | |
132 | |
133 STDMETHODIMP get_Item(long index, IDispatch** app_bundle_wrapper) { // NOLINT | |
134 ASSERT1(app_bundle_wrapper); | |
135 ExceptionBarrier barrier; | |
136 | |
137 if (::IsUserAnAdmin() && !UserRights::VerifyCallerIsAdmin()) { | |
138 CORE_LOG(LE, (_T("[User is not an admin]"))); | |
139 return E_ACCESSDENIED; | |
140 } | |
141 | |
142 __mutexScope(model()->lock()); | |
143 | |
144 const size_t num_app_bundles(model()->GetNumberOfAppBundles()); | |
145 if (index < 0 || static_cast<size_t>(index) >= num_app_bundles) { | |
146 return HRESULT_FROM_WIN32(ERROR_INVALID_INDEX); | |
147 } | |
148 shared_ptr<AppBundle> app_bundle(model()->GetAppBundle(index)); | |
149 return AppBundleWrapper::Create(app_bundle->controlling_ptr(), | |
150 app_bundle.get(), | |
151 app_bundle_wrapper); | |
152 } | |
153 | |
154 // Creates an AppBundle object and its corresponding COM wrapper. | |
155 STDMETHODIMP createAppBundle(IDispatch** app_bundle_wrapper) { | |
156 ASSERT1(app_bundle_wrapper); | |
157 ExceptionBarrier barrier; | |
158 | |
159 __mutexScope(model()->lock()); | |
160 | |
161 shared_ptr<AppBundle> app_bundle(model()->CreateAppBundle(T::is_machine())); | |
162 return AppBundleWrapper::Create(app_bundle->controlling_ptr(), | |
163 app_bundle.get(), | |
164 app_bundle_wrapper); | |
165 } | |
166 | |
167 HRESULT FinalConstruct() { | |
168 CORE_LOG(L2, (_T("[Update3COMClass::FinalConstruct]"))); | |
169 | |
170 HRESULT hr = InitializeWorker(); | |
171 if (FAILED(hr)) { | |
172 CORE_LOG(LE, (_T("[InitializeWorker failed][0x%x]"), hr)); | |
173 return hr; | |
174 } | |
175 | |
176 omaha::interlocked_exchange_pointer(&model_, Worker::Instance().model()); | |
177 ASSERT1(model()); | |
178 | |
179 return S_OK; | |
180 } | |
181 | |
182 void FinalRelease() { | |
183 CORE_LOG(L2, (_T("[Update3COMClass::FinalRelease]"))); | |
184 } | |
185 | |
186 private: | |
187 static HRESULT InitializeWorker() { | |
188 static LLock lock; | |
189 static bool is_initialized = false; | |
190 | |
191 __mutexScope(lock); | |
192 | |
193 if (is_initialized) { | |
194 return S_OK; | |
195 } | |
196 | |
197 CORE_LOG(L2, (_T("[InitializeWorker][%d]"), T::is_machine())); | |
198 | |
199 HRESULT hr = Worker::Instance().Initialize(T::is_machine()); | |
200 if (FAILED(hr)) { | |
201 return hr; | |
202 } | |
203 | |
204 is_initialized = true; | |
205 return S_OK; | |
206 } | |
207 | |
208 Model* model() { | |
209 return omaha::interlocked_exchange_pointer(&model_, model_); | |
210 } | |
211 | |
212 // C++ root of the object model. Not owned by this instance. | |
213 mutable Model* volatile model_; | |
214 | |
215 DISALLOW_COPY_AND_ASSIGN(Update3COMClass); | |
216 }; | |
217 | |
218 #pragma warning(pop) | |
219 | |
220 extern TCHAR kHKRootUser[]; | |
221 extern TCHAR kHKRootMachine[]; | |
222 extern TCHAR kHKRootService[]; | |
223 extern TCHAR kProgIDUpdate3COMClassUserLocal[]; | |
224 extern TCHAR kProgIDUpdate3COMClassMachineLocal[]; | |
225 extern TCHAR kProgIDUpdate3COMClassServiceLocal[]; | |
226 | |
227 typedef Update3COMClassMode<false, | |
228 kProgIDUpdate3COMClassUserLocal, | |
229 __uuidof(GoogleUpdate3UserClass), | |
230 IDR_LOCAL_SERVER_RGS, | |
231 kHKRootUser> Update3COMClassModeUser; | |
232 | |
233 typedef Update3COMClassMode<true, | |
234 kProgIDUpdate3COMClassServiceLocal, | |
235 __uuidof(GoogleUpdate3ServiceClass), | |
236 IDR_LOCAL_SERVICE_RGS, | |
237 kHKRootService> Update3COMClassModeService; | |
238 | |
239 typedef Update3COMClass<Update3COMClassModeUser> Update3COMClassUser; | |
240 typedef Update3COMClass<Update3COMClassModeService> Update3COMClassService; | |
241 | |
242 } // namespace omaha | |
243 | |
244 #endif // OMAHA_GOOPDATE_GOOGLE_UPDATE3_H_ | |
OLD | NEW |