| OLD | NEW |
| (Empty) |
| 1 // Copyright 2007-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 // IBindStatusCallback interface. | |
| 17 | |
| 18 #include "omaha/net/bind_status_callback.h" | |
| 19 #include <wininet.h> | |
| 20 #include "omaha/base/debug.h" | |
| 21 #include "omaha/base/error.h" | |
| 22 #include "omaha/base/logging.h" | |
| 23 #include "omaha/base/utils.h" | |
| 24 | |
| 25 namespace omaha { | |
| 26 | |
| 27 HRESULT QueryHttpInfo(IWinInetHttpInfo* http_info, DWORD query, CString* info) { | |
| 28 CORE_LOG(L3, (_T("[QueryHttpInfo][%d]"), query)); | |
| 29 ASSERT1(http_info); | |
| 30 ASSERT1(query); | |
| 31 ASSERT1(info); | |
| 32 | |
| 33 info->Empty(); | |
| 34 DWORD size = 0; | |
| 35 DWORD flags = 0; | |
| 36 HRESULT hr = http_info->QueryInfo(query, 0, &size, &flags, 0); | |
| 37 CORE_LOG(L3, (_T("[http_info->QueryInfo][0x%x][%d]"), hr, size)); | |
| 38 if (FAILED(hr)) { | |
| 39 return hr; | |
| 40 } | |
| 41 | |
| 42 CStringA buf; | |
| 43 hr = http_info->QueryInfo(query, CStrBufA(buf, size), &size, &flags, 0); | |
| 44 CORE_LOG(L3, (_T("[http_info->QueryInfo][0x%x][%d]"), hr, size)); | |
| 45 if (FAILED(hr)) { | |
| 46 return hr; | |
| 47 } | |
| 48 | |
| 49 CORE_LOG(L3, (_T("[QueryHttpInfo success][%d][%s]"), query, CA2T(buf))); | |
| 50 *info = buf; | |
| 51 return S_OK; | |
| 52 } | |
| 53 | |
| 54 BindStatusCallback::BindStatusCallback() | |
| 55 : http_verb_(BINDVERB_GET), | |
| 56 post_data_byte_count_(0), | |
| 57 response_code_(0) { | |
| 58 } | |
| 59 | |
| 60 HRESULT BindStatusCallback::Send(BSTR url, | |
| 61 BSTR post_data, | |
| 62 BSTR request_headers, | |
| 63 VARIANT response_headers_needed, | |
| 64 VARIANT* response_headers, | |
| 65 DWORD* response_code, | |
| 66 BSTR* cache_filename) { | |
| 67 if (!url || !*url || !response_code || !cache_filename) { | |
| 68 return E_INVALIDARG; | |
| 69 } | |
| 70 | |
| 71 *response_code = 0; | |
| 72 *cache_filename = NULL; | |
| 73 | |
| 74 if (V_VT(&response_headers_needed) != VT_EMPTY) { | |
| 75 if ((V_VT(&response_headers_needed) != (VT_ARRAY | VT_UI4)) || | |
| 76 !response_headers) { | |
| 77 return E_INVALIDARG; | |
| 78 } | |
| 79 V_VT(response_headers) = VT_NULL; | |
| 80 response_headers_needed_ = response_headers_needed.parray; | |
| 81 if (!response_headers_needed_.GetCount()) { | |
| 82 return E_INVALIDARG; | |
| 83 } | |
| 84 } | |
| 85 | |
| 86 request_headers_ = request_headers; | |
| 87 if (!post_data) { | |
| 88 http_verb_ = BINDVERB_GET; | |
| 89 } else { | |
| 90 http_verb_ = BINDVERB_POST; | |
| 91 post_data_byte_count_ = ::SysStringByteLen(post_data); | |
| 92 reset(post_data_, ::GlobalAlloc(GPTR, post_data_byte_count_)); | |
| 93 if (!post_data_) { | |
| 94 HRESULT hr = HRESULTFromLastError(); | |
| 95 CORE_LOG(LE, (_T("[::GlobalAlloc failed][0x%x]"), hr)); | |
| 96 return hr; | |
| 97 } | |
| 98 | |
| 99 memcpy(get(post_data_), post_data, post_data_byte_count_); | |
| 100 } | |
| 101 | |
| 102 CComPtr<IBindStatusCallback> bsc(this); | |
| 103 CString filename; | |
| 104 HRESULT hr = ::URLDownloadToCacheFile(NULL, | |
| 105 url, | |
| 106 CStrBuf(filename, MAX_PATH), | |
| 107 MAX_PATH, | |
| 108 0, | |
| 109 bsc); | |
| 110 | |
| 111 if (response_headers) { | |
| 112 response_headers_.Detach(response_headers); | |
| 113 } | |
| 114 *response_code = response_code_; | |
| 115 | |
| 116 CORE_LOG(L2, (_T("[URLDownloadToCacheFile][0x%x][%s]"), hr, url)); | |
| 117 if (FAILED(hr)) { | |
| 118 return hr; | |
| 119 } | |
| 120 | |
| 121 ASSERT1(!filename.IsEmpty()); | |
| 122 CORE_LOG(L2, (_T("[BindStatusCallback::Send][cache file][%s]"), filename)); | |
| 123 *cache_filename = filename.AllocSysString(); | |
| 124 return hr; | |
| 125 } | |
| 126 | |
| 127 // IBindStatusCallback methods. | |
| 128 | |
| 129 STDMETHODIMP BindStatusCallback::OnStartBinding(DWORD, IBinding* binding) { | |
| 130 __mutexScope(lock_); | |
| 131 binding_git_.Attach(binding); | |
| 132 return S_OK; | |
| 133 } | |
| 134 | |
| 135 STDMETHODIMP BindStatusCallback::GetPriority(LONG* priority) { | |
| 136 UNREFERENCED_PARAMETER(priority); | |
| 137 return E_NOTIMPL; | |
| 138 } | |
| 139 | |
| 140 STDMETHODIMP BindStatusCallback::OnLowResource(DWORD) { | |
| 141 return E_NOTIMPL; | |
| 142 } | |
| 143 | |
| 144 STDMETHODIMP BindStatusCallback::OnProgress(ULONG, ULONG, ULONG, LPCWSTR) { | |
| 145 return E_NOTIMPL; | |
| 146 } | |
| 147 | |
| 148 STDMETHODIMP BindStatusCallback::OnStopBinding(HRESULT, LPCWSTR) { | |
| 149 CComPtr<IBinding> binding; | |
| 150 | |
| 151 __mutexBlock(lock_) { | |
| 152 if (!binding_git_) { | |
| 153 return S_OK; | |
| 154 } | |
| 155 | |
| 156 HRESULT hr = binding_git_.CopyTo(&binding); | |
| 157 VERIFY1(SUCCEEDED(binding_git_.Revoke())); | |
| 158 if (FAILED(hr)) { | |
| 159 CORE_LOG(LW, (_T("[binding_git_.CopyTo failed][0x%x]"), hr)); | |
| 160 return S_OK; | |
| 161 } | |
| 162 } | |
| 163 | |
| 164 CComQIPtr<IWinInetHttpInfo> http_info(binding); | |
| 165 if (!http_info) { | |
| 166 return S_OK; | |
| 167 } | |
| 168 | |
| 169 CString response_code_buf; | |
| 170 if (SUCCEEDED(QueryHttpInfo(http_info, | |
| 171 HTTP_QUERY_STATUS_CODE, | |
| 172 &response_code_buf)) && | |
| 173 !response_code_buf.IsEmpty()) { | |
| 174 response_code_ = _ttoi(response_code_buf); | |
| 175 } | |
| 176 | |
| 177 if (!response_headers_needed_) { | |
| 178 return S_OK; | |
| 179 } | |
| 180 int count = response_headers_needed_.GetCount(); | |
| 181 ASSERT1(count > 0); | |
| 182 int lower_bound = response_headers_needed_.GetLowerBound(); | |
| 183 int upper_bound = response_headers_needed_.GetUpperBound(); | |
| 184 | |
| 185 CComSafeArray<BSTR> response_array(count, lower_bound); | |
| 186 for (int i = lower_bound; i <= upper_bound; ++i) { | |
| 187 CString response_header_buf; | |
| 188 QueryHttpInfo(http_info, response_headers_needed_[i], &response_header_buf); | |
| 189 response_array[i] = response_header_buf.AllocSysString(); | |
| 190 } | |
| 191 | |
| 192 V_VT(&response_headers_) = VT_ARRAY | VT_BSTR; | |
| 193 V_ARRAY(&response_headers_) = response_array.Detach(); | |
| 194 return S_OK; | |
| 195 } | |
| 196 | |
| 197 STDMETHODIMP BindStatusCallback::GetBindInfo(DWORD* flags, BINDINFO* info) { | |
| 198 ASSERT1(flags); | |
| 199 ASSERT1(info); | |
| 200 *flags = 0; | |
| 201 | |
| 202 // Set up the BINDINFO data structure. | |
| 203 info->cbSize = sizeof(*info); | |
| 204 info->dwBindVerb = http_verb_; | |
| 205 info->szExtraInfo = NULL; | |
| 206 | |
| 207 // Initialize the STGMEDIUM. | |
| 208 SetZero(info->stgmedData); | |
| 209 info->grfBindInfoF = 0; | |
| 210 info->szCustomVerb = NULL; | |
| 211 | |
| 212 switch (http_verb_) { | |
| 213 case BINDVERB_POST: | |
| 214 if (post_data_) { | |
| 215 // Fill the STGMEDIUM with the data to post. Certain versions of Urlmon | |
| 216 // require TYMED_GLOBAL with GMEM_FIXED. | |
| 217 info->stgmedData.tymed = TYMED_HGLOBAL; | |
| 218 info->stgmedData.hGlobal = get(post_data_); | |
| 219 | |
| 220 // The documentation for GetBindInfo() indicates that the method could | |
| 221 // be called multiple times for the same request. We do not want to | |
| 222 // allocate global memory for each of those times. So we maintain | |
| 223 // ownership of the global memory, and pass a reference to it each time. | |
| 224 // The HGLOBAL is released on BindStatusCallback destruction. Hence we | |
| 225 // set pUnkForRelease to our IUnknown ptr. | |
| 226 info->stgmedData.pUnkForRelease = | |
| 227 static_cast<IBindStatusCallback*>(this); | |
| 228 AddRef(); | |
| 229 | |
| 230 info->cbstgmedData = post_data_byte_count_; | |
| 231 } | |
| 232 return S_OK; | |
| 233 | |
| 234 case BINDVERB_GET: | |
| 235 return S_OK; | |
| 236 | |
| 237 case BINDVERB_PUT: | |
| 238 case BINDVERB_CUSTOM: | |
| 239 default: | |
| 240 ASSERT1(false); | |
| 241 return E_FAIL; | |
| 242 } | |
| 243 } | |
| 244 | |
| 245 STDMETHODIMP BindStatusCallback::OnDataAvailable(DWORD, | |
| 246 DWORD, | |
| 247 FORMATETC*, | |
| 248 STGMEDIUM*) { | |
| 249 // The documentation does not explicitly say that E_NOTIMPL can be returned | |
| 250 // for this method. So we return S_OK. | |
| 251 return S_OK; | |
| 252 } | |
| 253 | |
| 254 STDMETHODIMP BindStatusCallback::OnObjectAvailable(REFIID, IUnknown*) { | |
| 255 // The documentation does not explicitly say that E_NOTIMPL can be returned | |
| 256 // for this method. So we return S_OK. | |
| 257 return S_OK; | |
| 258 } | |
| 259 | |
| 260 STDMETHODIMP BindStatusCallback::BeginningTransaction(LPCWSTR, | |
| 261 LPCWSTR, | |
| 262 DWORD, | |
| 263 LPWSTR* request_headers) { | |
| 264 if (!request_headers) { | |
| 265 return E_INVALIDARG; | |
| 266 } | |
| 267 *request_headers = NULL; | |
| 268 | |
| 269 if (request_headers_.IsEmpty()) { | |
| 270 return S_OK; | |
| 271 } | |
| 272 | |
| 273 int request_headers_size = request_headers_.GetLength() + 1; | |
| 274 TCHAR* additional_headers = static_cast<TCHAR*>( | |
| 275 ::CoTaskMemAlloc(request_headers_size * sizeof(TCHAR))); | |
| 276 if (!additional_headers) { | |
| 277 return E_OUTOFMEMORY; | |
| 278 } | |
| 279 | |
| 280 _tcscpy_s(additional_headers, request_headers_size, request_headers_); | |
| 281 *request_headers = additional_headers; | |
| 282 | |
| 283 return S_OK; | |
| 284 } | |
| 285 | |
| 286 STDMETHODIMP BindStatusCallback::OnResponse(DWORD response_code, | |
| 287 LPCWSTR response_headers, | |
| 288 LPCWSTR request_headers, | |
| 289 LPWSTR* additional_headers) { | |
| 290 CORE_LOG(L1, (_T("[OnResponse [%d][%s]"), response_code, response_headers)); | |
| 291 UNREFERENCED_PARAMETER(response_code); | |
| 292 UNREFERENCED_PARAMETER(response_headers); | |
| 293 UNREFERENCED_PARAMETER(request_headers); | |
| 294 if (!additional_headers) { | |
| 295 return E_INVALIDARG; | |
| 296 } | |
| 297 | |
| 298 *additional_headers = NULL; | |
| 299 return S_OK; | |
| 300 } | |
| 301 | |
| 302 HRESULT BindStatusCallback::Cancel() { | |
| 303 CComPtr<IBinding> binding; | |
| 304 | |
| 305 __mutexBlock(lock_) { | |
| 306 if (!binding_git_) { | |
| 307 return S_OK; | |
| 308 } | |
| 309 | |
| 310 HRESULT hr = binding_git_.CopyTo(&binding); | |
| 311 if (FAILED(hr)) { | |
| 312 CORE_LOG(LE, (_T("[binding_git_.CopyTo failed][0x%x]"), hr)); | |
| 313 return hr; | |
| 314 } | |
| 315 } | |
| 316 | |
| 317 return binding->Abort(); | |
| 318 } | |
| 319 | |
| 320 } // namespace omaha | |
| 321 | |
| OLD | NEW |