| OLD | NEW |
| (Empty) |
| 1 // Copyright 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 #include "omaha/goopdate/cocreate_async.h" | |
| 17 #include "base/basictypes.h" | |
| 18 #include "base/debug.h" | |
| 19 #include "omaha/base/scope_guard.h" | |
| 20 #include "base/scoped_ptr_address.h" | |
| 21 #include "omaha/base/system.h" | |
| 22 #include "omaha/base/thread_pool_callback.h" | |
| 23 #include "omaha/base/vistautil.h" | |
| 24 #include "omaha/goopdate/goopdate.h" | |
| 25 | |
| 26 namespace omaha { | |
| 27 | |
| 28 CoCreateAsync::CoCreateAsync() : StdMarshalInfo(true) { | |
| 29 } | |
| 30 | |
| 31 STDMETHODIMP CoCreateAsync::createOmahaMachineServerAsync( | |
| 32 BSTR origin_url, | |
| 33 BOOL create_elevated, | |
| 34 ICoCreateAsyncStatus** status) { | |
| 35 CORE_LOG(L3, (L"[CoCreateAsync::createOmahaMachineServerAsync][%s][%d]", | |
| 36 origin_url, create_elevated)); | |
| 37 ASSERT1(status); | |
| 38 ASSERT1(origin_url && wcslen(origin_url)); | |
| 39 *status = NULL; | |
| 40 | |
| 41 if (create_elevated && | |
| 42 !vista_util::IsVistaOrLater() && !vista_util::IsUserAdmin()) { | |
| 43 return E_ACCESSDENIED; | |
| 44 } | |
| 45 | |
| 46 typedef CComObject<CoCreateAsyncStatus> ComObjectAsyncStatus; | |
| 47 scoped_ptr<ComObjectAsyncStatus> async_status; | |
| 48 HRESULT hr = ComObjectAsyncStatus::CreateInstance(address(async_status)); | |
| 49 if (FAILED(hr)) { | |
| 50 return hr; | |
| 51 } | |
| 52 | |
| 53 hr = async_status->CreateOmahaMachineServerAsync(origin_url, create_elevated); | |
| 54 if (FAILED(hr)) { | |
| 55 return hr; | |
| 56 } | |
| 57 | |
| 58 hr = async_status->QueryInterface(status); | |
| 59 if (FAILED(hr)) { | |
| 60 return hr; | |
| 61 } | |
| 62 | |
| 63 async_status.release(); | |
| 64 return S_OK; | |
| 65 } | |
| 66 | |
| 67 CoCreateAsyncStatus::CoCreateAsyncStatus() : is_done_(false), hr_(E_PENDING) { | |
| 68 } | |
| 69 | |
| 70 HRESULT CoCreateAsyncStatus::CreateOmahaMachineServerAsync( | |
| 71 BSTR origin_url, | |
| 72 BOOL create_elevated) { | |
| 73 // Create a thread pool work item for deferred execution of the CoCreate. The | |
| 74 // thread pool owns this call back object. | |
| 75 typedef ThreadPoolCallBack2<CoCreateAsyncStatus, | |
| 76 const CString, | |
| 77 BOOL> CallBack; | |
| 78 scoped_ptr<CallBack> | |
| 79 callback(new CallBack(this, | |
| 80 &CoCreateAsyncStatus::CreateOmahaMachineServer, | |
| 81 origin_url, | |
| 82 create_elevated)); | |
| 83 HRESULT hr = Goopdate::Instance().QueueUserWorkItem(callback.get(), | |
| 84 WT_EXECUTELONGFUNCTION); | |
| 85 if (FAILED(hr)) { | |
| 86 CORE_LOG(LE, (_T("[QueueUserWorkItem failed][0x%x]"), hr)); | |
| 87 return hr; | |
| 88 } | |
| 89 | |
| 90 VERIFY1(thread_started_gate_.Wait(INFINITE)); | |
| 91 | |
| 92 callback.release(); | |
| 93 return S_OK; | |
| 94 } | |
| 95 | |
| 96 void CoCreateAsyncStatus::CreateOmahaMachineServer(const CString origin_url, | |
| 97 BOOL create_elevated) { | |
| 98 CORE_LOG(L3, (_T("[CoCreateAsyncStatus::CreateOmahaMachineServer][%s][%d]"), | |
| 99 origin_url, create_elevated)); | |
| 100 AddRef(); | |
| 101 ON_SCOPE_EXIT_OBJ(*this, &CoCreateAsyncStatus::Release); | |
| 102 | |
| 103 VERIFY1(thread_started_gate_.Open()); | |
| 104 | |
| 105 HRESULT hr = E_FAIL; | |
| 106 CComPtr<IDispatch> ptr; | |
| 107 | |
| 108 // Since the values of hr and ptr are being modified after the scope guard, | |
| 109 // the variables are passed by reference instead of by values using ByRef. | |
| 110 ON_SCOPE_EXIT_OBJ(*this, | |
| 111 &CoCreateAsyncStatus::SetCreateInstanceResults, | |
| 112 ByRef(hr), | |
| 113 ByRef(ptr)); | |
| 114 | |
| 115 scoped_co_init init_com_apt(COINIT_MULTITHREADED); | |
| 116 hr = init_com_apt.hresult(); | |
| 117 if (FAILED(hr)) { | |
| 118 CORE_LOG(LE, (_T("[init_com_apt failed][0x%x]"), hr)); | |
| 119 return; | |
| 120 } | |
| 121 | |
| 122 CComPtr<IGoogleUpdate3WebSecurity> security; | |
| 123 REFCLSID clsid(__uuidof(GoogleUpdate3WebMachineClass)); | |
| 124 hr = create_elevated ? | |
| 125 System::CoCreateInstanceAsAdmin(NULL, clsid, IID_PPV_ARGS(&security)) : | |
| 126 ::CoCreateInstance(clsid, NULL, CLSCTX_ALL, IID_PPV_ARGS(&security)); | |
| 127 if (FAILED(hr)) { | |
| 128 CORE_LOG(LE, (_T("[CoCreate failed][0x%x]"), hr)); | |
| 129 return; | |
| 130 } | |
| 131 | |
| 132 hr = security->setOriginURL(CComBSTR(origin_url)); | |
| 133 if (FAILED(hr)) { | |
| 134 CORE_LOG(LE, (_T("[setOriginURL failed][0x%x]"), hr)); | |
| 135 return; | |
| 136 } | |
| 137 | |
| 138 hr = security.QueryInterface(&ptr); | |
| 139 if (FAILED(hr)) { | |
| 140 CORE_LOG(LE, (_T("[QueryInterface failed][0x%x]"), hr)); | |
| 141 return; | |
| 142 } | |
| 143 } | |
| 144 | |
| 145 void CoCreateAsyncStatus::SetCreateInstanceResults( | |
| 146 const HRESULT& hr, | |
| 147 const CComPtr<IDispatch>& ptr) { | |
| 148 CORE_LOG(L3, (_T("[SetCreateInstanceResults][0x%x][0x%p]"), hr, ptr)); | |
| 149 Lock(); | |
| 150 ON_SCOPE_EXIT_OBJ(*this, &CoCreateAsyncStatus::Unlock); | |
| 151 | |
| 152 hr_ = hr; | |
| 153 ptr_ = ptr; | |
| 154 is_done_ = true; | |
| 155 } | |
| 156 | |
| 157 // ICoCreateAsyncStatus. | |
| 158 STDMETHODIMP CoCreateAsyncStatus::get_isDone(VARIANT_BOOL* is_done) { | |
| 159 Lock(); | |
| 160 ON_SCOPE_EXIT_OBJ(*this, &CoCreateAsyncStatus::Unlock); | |
| 161 | |
| 162 ASSERT1(is_done); | |
| 163 | |
| 164 *is_done = is_done_ ? VARIANT_TRUE : VARIANT_FALSE; | |
| 165 CORE_LOG(L3, (_T("[get_isDone][%d]"), is_done_)); | |
| 166 return S_OK; | |
| 167 } | |
| 168 | |
| 169 STDMETHODIMP CoCreateAsyncStatus::get_completionHResult(LONG* hr) { | |
| 170 Lock(); | |
| 171 ON_SCOPE_EXIT_OBJ(*this, &CoCreateAsyncStatus::Unlock); | |
| 172 | |
| 173 ASSERT1(hr); | |
| 174 | |
| 175 *hr = hr_; | |
| 176 CORE_LOG(L3, (_T("[get_completionHResult][0x%x]"), hr_)); | |
| 177 return S_OK; | |
| 178 } | |
| 179 | |
| 180 STDMETHODIMP CoCreateAsyncStatus::get_createdInstance(IDispatch** instance) { | |
| 181 Lock(); | |
| 182 ON_SCOPE_EXIT_OBJ(*this, &CoCreateAsyncStatus::Unlock); | |
| 183 | |
| 184 ASSERT1(instance); | |
| 185 | |
| 186 ptr_.CopyTo(instance); | |
| 187 CORE_LOG(L3, (_T("[get_createdInstance][0x%p]"), *instance)); | |
| 188 return S_OK; | |
| 189 } | |
| 190 | |
| 191 } // namespace omaha | |
| 192 | |
| OLD | NEW |