[email protected] | 051236f | 2010-03-12 22:06:14 | [diff] [blame] | 1 | // Copyright (c) 2010 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. |
| 4 | |
| 5 | #include "chrome_frame/urlmon_bind_status_callback.h" |
| 6 | |
[email protected] | 97965e1 | 2010-04-09 00:51:10 | [diff] [blame] | 7 | #include <mshtml.h> |
[email protected] | fb716b30 | 2010-03-26 02:45:20 | [diff] [blame] | 8 | #include <shlguid.h> |
| 9 | |
[email protected] | 051236f | 2010-03-12 22:06:14 | [diff] [blame] | 10 | #include "base/logging.h" |
| 11 | #include "base/string_util.h" |
[email protected] | d3451d83 | 2010-10-01 11:17:37 | [diff] [blame^] | 12 | #include "base/stringprintf.h" |
[email protected] | 97965e1 | 2010-04-09 00:51:10 | [diff] [blame] | 13 | #include "base/utf_string_conversions.h" |
[email protected] | 70277f6 | 2010-04-15 01:39:26 | [diff] [blame] | 14 | #include "chrome_frame/bind_context_info.h" |
[email protected] | e7dd2154 | 2010-05-07 21:59:44 | [diff] [blame] | 15 | #include "chrome_frame/exception_barrier.h" |
[email protected] | 97965e1 | 2010-04-09 00:51:10 | [diff] [blame] | 16 | #include "chrome_frame/urlmon_moniker.h" |
| 17 | #include "chrome_tab.h" // NOLINT |
[email protected] | 051236f | 2010-03-12 22:06:14 | [diff] [blame] | 18 | |
[email protected] | 051236f | 2010-03-12 22:06:14 | [diff] [blame] | 19 | |
[email protected] | 97965e1 | 2010-04-09 00:51:10 | [diff] [blame] | 20 | // A helper to given feed data to the specified |bscb| using |
| 21 | // CacheStream instance. |
| 22 | HRESULT CacheStream::BSCBFeedData(IBindStatusCallback* bscb, const char* data, |
| 23 | size_t size, CLIPFORMAT clip_format, |
[email protected] | de5bc35 | 2010-04-16 01:38:48 | [diff] [blame] | 24 | size_t flags, bool eof) { |
[email protected] | 97965e1 | 2010-04-09 00:51:10 | [diff] [blame] | 25 | if (!bscb) { |
| 26 | NOTREACHED() << "invalid IBindStatusCallback"; |
| 27 | return E_INVALIDARG; |
[email protected] | 051236f | 2010-03-12 22:06:14 | [diff] [blame] | 28 | } |
| 29 | |
[email protected] | 97965e1 | 2010-04-09 00:51:10 | [diff] [blame] | 30 | // We can't use a CComObjectStackEx here since mshtml will hold |
| 31 | // onto the stream pointer. |
| 32 | CComObject<CacheStream>* cache_stream = NULL; |
| 33 | HRESULT hr = CComObject<CacheStream>::CreateInstance(&cache_stream); |
[email protected] | 051236f | 2010-03-12 22:06:14 | [diff] [blame] | 34 | if (FAILED(hr)) { |
| 35 | NOTREACHED(); |
| 36 | return hr; |
| 37 | } |
| 38 | |
[email protected] | f1917ed | 2010-05-13 20:21:08 | [diff] [blame] | 39 | scoped_refptr<CacheStream> cache_ref = cache_stream; |
| 40 | hr = cache_stream->Initialize(data, size, eof); |
| 41 | if (FAILED(hr)) |
| 42 | return hr; |
[email protected] | 051236f | 2010-03-12 22:06:14 | [diff] [blame] | 43 | |
[email protected] | 97965e1 | 2010-04-09 00:51:10 | [diff] [blame] | 44 | FORMATETC format_etc = { clip_format, NULL, DVASPECT_CONTENT, -1, |
| 45 | TYMED_ISTREAM }; |
| 46 | STGMEDIUM medium = {0}; |
| 47 | medium.tymed = TYMED_ISTREAM; |
| 48 | medium.pstm = cache_stream; |
| 49 | |
| 50 | hr = bscb->OnDataAvailable(flags, size, &format_etc, &medium); |
[email protected] | 97965e1 | 2010-04-09 00:51:10 | [diff] [blame] | 51 | return hr; |
[email protected] | 051236f | 2010-03-12 22:06:14 | [diff] [blame] | 52 | } |
| 53 | |
[email protected] | f1917ed | 2010-05-13 20:21:08 | [diff] [blame] | 54 | HRESULT CacheStream::Initialize(const char* cache, size_t size, bool eof) { |
[email protected] | 97965e1 | 2010-04-09 00:51:10 | [diff] [blame] | 55 | position_ = 0; |
[email protected] | de5bc35 | 2010-04-16 01:38:48 | [diff] [blame] | 56 | eof_ = eof; |
[email protected] | f1917ed | 2010-05-13 20:21:08 | [diff] [blame] | 57 | |
| 58 | HRESULT hr = S_OK; |
| 59 | cache_.reset(new char[size]); |
| 60 | if (cache_.get()) { |
| 61 | memcpy(cache_.get(), cache, size); |
| 62 | size_ = size; |
| 63 | } else { |
| 64 | DLOG(ERROR) << "failed to allocate cache stream."; |
| 65 | hr = E_OUTOFMEMORY; |
| 66 | } |
| 67 | |
| 68 | return hr; |
[email protected] | 051236f | 2010-03-12 22:06:14 | [diff] [blame] | 69 | } |
| 70 | |
[email protected] | 97965e1 | 2010-04-09 00:51:10 | [diff] [blame] | 71 | // Read is the only call that we expect. Return E_PENDING if there |
| 72 | // is no more data to serve. Otherwise this will result in a |
| 73 | // read with 0 bytes indicating that no more data is available. |
| 74 | STDMETHODIMP CacheStream::Read(void* pv, ULONG cb, ULONG* read) { |
| 75 | if (!pv || !read) |
| 76 | return E_INVALIDARG; |
[email protected] | 051236f | 2010-03-12 22:06:14 | [diff] [blame] | 77 | |
[email protected] | 642c15e | 2010-05-13 20:56:12 | [diff] [blame] | 78 | if (!cache_.get()) { |
| 79 | *read = 0; |
| 80 | return S_FALSE; |
| 81 | } |
[email protected] | f1917ed | 2010-05-13 20:21:08 | [diff] [blame] | 82 | |
[email protected] | 97965e1 | 2010-04-09 00:51:10 | [diff] [blame] | 83 | // Default to E_PENDING to signal that this is a partial data. |
[email protected] | de5bc35 | 2010-04-16 01:38:48 | [diff] [blame] | 84 | HRESULT hr = eof_ ? S_FALSE : E_PENDING; |
[email protected] | 97965e1 | 2010-04-09 00:51:10 | [diff] [blame] | 85 | if (position_ < size_) { |
| 86 | *read = std::min(size_ - position_, size_t(cb)); |
[email protected] | f1917ed | 2010-05-13 20:21:08 | [diff] [blame] | 87 | memcpy(pv, cache_ .get() + position_, *read); |
[email protected] | 97965e1 | 2010-04-09 00:51:10 | [diff] [blame] | 88 | position_ += *read; |
| 89 | hr = S_OK; |
[email protected] | 051236f | 2010-03-12 22:06:14 | [diff] [blame] | 90 | } |
[email protected] | 051236f | 2010-03-12 22:06:14 | [diff] [blame] | 91 | |
| 92 | return hr; |
| 93 | } |
| 94 | |
[email protected] | 97965e1 | 2010-04-09 00:51:10 | [diff] [blame] | 95 | |
| 96 | ///////////////////////////////////////////////////////////////////// |
| 97 | |
[email protected] | 040b800f | 2010-05-05 23:46:30 | [diff] [blame] | 98 | HRESULT SniffData::InitializeCache(const std::wstring& url) { |
[email protected] | 97965e1 | 2010-04-09 00:51:10 | [diff] [blame] | 99 | url_ = url; |
| 100 | renderer_type_ = UNDETERMINED; |
| 101 | |
[email protected] | 040b800f | 2010-05-05 23:46:30 | [diff] [blame] | 102 | const int kInitialSize = 4 * 1024; // 4K |
| 103 | HGLOBAL mem = GlobalAlloc(0, kInitialSize); |
| 104 | DCHECK(mem) << "GlobalAlloc failed: " << GetLastError(); |
| 105 | |
| 106 | HRESULT hr = CreateStreamOnHGlobal(mem, TRUE, cache_.Receive()); |
[email protected] | b31b9ff | 2010-05-07 18:29:24 | [diff] [blame] | 107 | if (SUCCEEDED(hr)) { |
| 108 | ULARGE_INTEGER size = {0}; |
| 109 | cache_->SetSize(size); |
| 110 | } else { |
| 111 | DLOG(ERROR) << "CreateStreamOnHGlobal failed: " << hr; |
| 112 | } |
| 113 | |
[email protected] | 040b800f | 2010-05-05 23:46:30 | [diff] [blame] | 114 | return hr; |
[email protected] | 051236f | 2010-03-12 22:06:14 | [diff] [blame] | 115 | } |
| 116 | |
[email protected] | 97965e1 | 2010-04-09 00:51:10 | [diff] [blame] | 117 | HRESULT SniffData::ReadIntoCache(IStream* stream, bool force_determination) { |
| 118 | if (!stream) { |
| 119 | NOTREACHED(); |
| 120 | return E_INVALIDARG; |
[email protected] | 051236f | 2010-03-12 22:06:14 | [diff] [blame] | 121 | } |
[email protected] | 051236f | 2010-03-12 22:06:14 | [diff] [blame] | 122 | |
| 123 | HRESULT hr = S_OK; |
[email protected] | 97965e1 | 2010-04-09 00:51:10 | [diff] [blame] | 124 | while (SUCCEEDED(hr)) { |
| 125 | const size_t kChunkSize = 4 * 1024; |
| 126 | char buffer[kChunkSize]; |
| 127 | DWORD read = 0; |
| 128 | hr = stream->Read(buffer, sizeof(buffer), &read); |
| 129 | if (read) { |
| 130 | DWORD written = 0; |
| 131 | cache_->Write(buffer, read, &written); |
| 132 | size_ += written; |
[email protected] | fb716b30 | 2010-03-26 02:45:20 | [diff] [blame] | 133 | } |
| 134 | |
[email protected] | 97965e1 | 2010-04-09 00:51:10 | [diff] [blame] | 135 | if ((S_FALSE == hr) || !read) |
| 136 | break; |
[email protected] | 051236f | 2010-03-12 22:06:14 | [diff] [blame] | 137 | } |
| 138 | |
[email protected] | 37a3a4b | 2010-04-14 22:37:40 | [diff] [blame] | 139 | bool last_chance = force_determination || (size() >= kMaxSniffSize); |
[email protected] | de5bc35 | 2010-04-16 01:38:48 | [diff] [blame] | 140 | eof_ = force_determination; |
[email protected] | 37a3a4b | 2010-04-14 22:37:40 | [diff] [blame] | 141 | DetermineRendererType(last_chance); |
[email protected] | 051236f | 2010-03-12 22:06:14 | [diff] [blame] | 142 | return hr; |
| 143 | } |
| 144 | |
[email protected] | 97965e1 | 2010-04-09 00:51:10 | [diff] [blame] | 145 | HRESULT SniffData::DrainCache(IBindStatusCallback* bscb, DWORD bscf, |
| 146 | CLIPFORMAT clip_format) { |
| 147 | if (!is_cache_valid()) { |
| 148 | return S_OK; |
| 149 | } |
| 150 | |
| 151 | // Ideally we could just use the cache_ IStream implementation but |
| 152 | // can't use it here since we have to return E_PENDING for the |
| 153 | // last call |
| 154 | HGLOBAL memory = NULL; |
| 155 | HRESULT hr = GetHGlobalFromStream(cache_, &memory); |
| 156 | if (SUCCEEDED(hr) && memory) { |
| 157 | char* buffer = reinterpret_cast<char*>(GlobalLock(memory)); |
[email protected] | de5bc35 | 2010-04-16 01:38:48 | [diff] [blame] | 158 | hr = CacheStream::BSCBFeedData(bscb, buffer, size_, clip_format, bscf, |
| 159 | eof_); |
[email protected] | 97965e1 | 2010-04-09 00:51:10 | [diff] [blame] | 160 | GlobalUnlock(memory); |
| 161 | } |
| 162 | |
| 163 | size_ = 0; |
| 164 | cache_.Release(); |
| 165 | return hr; |
[email protected] | 051236f | 2010-03-12 22:06:14 | [diff] [blame] | 166 | } |
| 167 | |
[email protected] | 97965e1 | 2010-04-09 00:51:10 | [diff] [blame] | 168 | // Scan the buffer or OptIn URL list and decide if the renderer is |
[email protected] | 37a3a4b | 2010-04-14 22:37:40 | [diff] [blame] | 169 | // to be switched. Last chance means there's no more data. |
| 170 | void SniffData::DetermineRendererType(bool last_chance) { |
[email protected] | 97965e1 | 2010-04-09 00:51:10 | [diff] [blame] | 171 | if (is_undetermined()) { |
[email protected] | 37a3a4b | 2010-04-14 22:37:40 | [diff] [blame] | 172 | if (last_chance) |
| 173 | renderer_type_ = OTHER; |
[email protected] | d8e1351 | 2010-09-22 17:02:58 | [diff] [blame] | 174 | if (IsChrome(RendererTypeForUrl(url_.c_str()))) { |
[email protected] | 97965e1 | 2010-04-09 00:51:10 | [diff] [blame] | 175 | renderer_type_ = CHROME; |
| 176 | } else { |
[email protected] | 97965e1 | 2010-04-09 00:51:10 | [diff] [blame] | 177 | if (is_cache_valid() && cache_) { |
| 178 | HGLOBAL memory = NULL; |
| 179 | GetHGlobalFromStream(cache_, &memory); |
[email protected] | f0600f1b | 2010-06-08 22:40:35 | [diff] [blame] | 180 | const char* buffer = reinterpret_cast<const char*>(GlobalLock(memory)); |
[email protected] | 97965e1 | 2010-04-09 00:51:10 | [diff] [blame] | 181 | |
| 182 | std::wstring html_contents; |
| 183 | // TODO(joshia): detect and handle different content encodings |
| 184 | if (buffer && size_) { |
[email protected] | f0600f1b | 2010-06-08 22:40:35 | [diff] [blame] | 185 | UTF8ToWide(buffer, std::min(size_, kMaxSniffSize), &html_contents); |
[email protected] | 97965e1 | 2010-04-09 00:51:10 | [diff] [blame] | 186 | GlobalUnlock(memory); |
| 187 | } |
| 188 | |
| 189 | // Note that document_contents_ may have NULL characters in it. While |
| 190 | // browsers may handle this properly, we don't and will stop scanning |
| 191 | // for the XUACompat content value if we encounter one. |
| 192 | std::wstring xua_compat_content; |
| 193 | UtilGetXUACompatContentValue(html_contents, &xua_compat_content); |
| 194 | if (StrStrI(xua_compat_content.c_str(), kChromeContentPrefix)) { |
| 195 | renderer_type_ = CHROME; |
| 196 | } |
| 197 | } |
| 198 | } |
[email protected] | ffec6bf | 2010-04-09 23:53:26 | [diff] [blame] | 199 | DLOG(INFO) << __FUNCTION__ << "Url: " << url_ << |
[email protected] | d3451d83 | 2010-10-01 11:17:37 | [diff] [blame^] | 200 | base::StringPrintf("Renderer type: %s", |
| 201 | renderer_type_ == CHROME ? "CHROME" : "OTHER"); |
[email protected] | 97965e1 | 2010-04-09 00:51:10 | [diff] [blame] | 202 | } |
[email protected] | 051236f | 2010-03-12 22:06:14 | [diff] [blame] | 203 | } |
| 204 | |
[email protected] | 97965e1 | 2010-04-09 00:51:10 | [diff] [blame] | 205 | ///////////////////////////////////////////////////////////////////// |
| 206 | |
[email protected] | 74e9983a | 2010-05-14 02:27:34 | [diff] [blame] | 207 | BSCBStorageBind::BSCBStorageBind() : clip_format_(CF_NULL) { |
| 208 | } |
| 209 | |
| 210 | BSCBStorageBind::~BSCBStorageBind() { |
[email protected] | 77d7aee | 2010-05-14 20:31:55 | [diff] [blame] | 211 | std::for_each(saved_progress_.begin(), saved_progress_.end(), |
| 212 | utils::DeleteObject()); |
[email protected] | 74e9983a | 2010-05-14 02:27:34 | [diff] [blame] | 213 | } |
| 214 | |
[email protected] | 70277f6 | 2010-04-15 01:39:26 | [diff] [blame] | 215 | HRESULT BSCBStorageBind::Initialize(IMoniker* moniker, IBindCtx* bind_ctx) { |
[email protected] | d3451d83 | 2010-10-01 11:17:37 | [diff] [blame^] | 216 | DLOG(INFO) << __FUNCTION__ << me() << base::StringPrintf(" tid=%i", |
[email protected] | 051236f | 2010-03-12 22:06:14 | [diff] [blame] | 217 | PlatformThread::CurrentId()); |
| 218 | |
[email protected] | 040b800f | 2010-05-05 23:46:30 | [diff] [blame] | 219 | std::wstring url = GetActualUrlFromMoniker(moniker, bind_ctx, |
| 220 | std::wstring()); |
| 221 | HRESULT hr = data_sniffer_.InitializeCache(url); |
| 222 | if (FAILED(hr)) |
| 223 | return hr; |
| 224 | |
| 225 | hr = AttachToBind(bind_ctx); |
[email protected] | 97965e1 | 2010-04-09 00:51:10 | [diff] [blame] | 226 | if (FAILED(hr)) { |
| 227 | NOTREACHED() << __FUNCTION__ << me() << "AttachToBind error: " << hr; |
| 228 | return hr; |
[email protected] | 051236f | 2010-03-12 22:06:14 | [diff] [blame] | 229 | } |
| 230 | |
[email protected] | 97965e1 | 2010-04-09 00:51:10 | [diff] [blame] | 231 | if (!delegate()) { |
| 232 | NOTREACHED() << __FUNCTION__ << me() << "No existing callback: " << hr; |
| 233 | return E_FAIL; |
| 234 | } |
[email protected] | 051236f | 2010-03-12 22:06:14 | [diff] [blame] | 235 | |
[email protected] | 051236f | 2010-03-12 22:06:14 | [diff] [blame] | 236 | return hr; |
| 237 | } |
| 238 | |
[email protected] | 97965e1 | 2010-04-09 00:51:10 | [diff] [blame] | 239 | STDMETHODIMP BSCBStorageBind::OnProgress(ULONG progress, ULONG progress_max, |
| 240 | ULONG status_code, LPCWSTR status_text) { |
[email protected] | d3451d83 | 2010-10-01 11:17:37 | [diff] [blame^] | 241 | DLOG(INFO) << __FUNCTION__ << me() << base::StringPrintf( |
| 242 | " status=%i tid=%i %ls", status_code, PlatformThread::CurrentId(), |
| 243 | status_text); |
[email protected] | e7dd2154 | 2010-05-07 21:59:44 | [diff] [blame] | 244 | // Report all crashes in the exception handler if we wrap the callback. |
| 245 | // Note that this avoids having the VEH report a crash if an SEH earlier in |
| 246 | // the chain handles the exception. |
| 247 | ExceptionBarrier barrier; |
[email protected] | 97965e1 | 2010-04-09 00:51:10 | [diff] [blame] | 248 | |
| 249 | HRESULT hr = S_OK; |
[email protected] | b31b9ff | 2010-05-07 18:29:24 | [diff] [blame] | 250 | |
| 251 | // Remember the last redirected URL in case we get switched into |
| 252 | // chrome frame |
| 253 | if (status_code == BINDSTATUS_REDIRECTING) { |
[email protected] | 77d7aee | 2010-05-14 20:31:55 | [diff] [blame] | 254 | ScopedComPtr<BindContextInfo> info; |
| 255 | BindContextInfo::FromBindContext(bind_ctx_, info.Receive()); |
[email protected] | b31b9ff | 2010-05-07 18:29:24 | [diff] [blame] | 256 | DCHECK(info); |
| 257 | if (info) |
| 258 | info->set_url(status_text); |
| 259 | } |
| 260 | |
| 261 | if (ShouldCacheProgress(status_code)) { |
[email protected] | 74e9983a | 2010-05-14 02:27:34 | [diff] [blame] | 262 | saved_progress_.push_back(new Progress(progress, progress_max, status_code, |
| 263 | status_text)); |
[email protected] | 97965e1 | 2010-04-09 00:51:10 | [diff] [blame] | 264 | } else { |
| 265 | hr = CallbackImpl::OnProgress(progress, progress_max, status_code, |
| 266 | status_text); |
| 267 | } |
| 268 | |
| 269 | return hr; |
| 270 | } |
| 271 | |
| 272 | // Refer to urlmon_moniker.h for explanation of how things work. |
| 273 | STDMETHODIMP BSCBStorageBind::OnDataAvailable(DWORD flags, DWORD size, |
| 274 | FORMATETC* format_etc, |
| 275 | STGMEDIUM* stgmed) { |
[email protected] | d3451d83 | 2010-10-01 11:17:37 | [diff] [blame^] | 276 | DLOG(INFO) << __FUNCTION__ << base::StringPrintf(" tid=%i", |
[email protected] | 051236f | 2010-03-12 22:06:14 | [diff] [blame] | 277 | PlatformThread::CurrentId()); |
[email protected] | e7dd2154 | 2010-05-07 21:59:44 | [diff] [blame] | 278 | // Report all crashes in the exception handler if we wrap the callback. |
| 279 | // Note that this avoids having the VEH report a crash if an SEH earlier in |
| 280 | // the chain handles the exception. |
| 281 | ExceptionBarrier barrier; |
[email protected] | 3ee6112 | 2010-04-14 00:35:52 | [diff] [blame] | 282 | // Do not touch anything other than text/html. |
[email protected] | 3ee6112 | 2010-04-14 00:35:52 | [diff] [blame] | 283 | bool is_interesting = (format_etc && stgmed && stgmed->pstm && |
[email protected] | 2b3102be | 2010-04-21 20:07:35 | [diff] [blame] | 284 | stgmed->tymed == TYMED_ISTREAM && |
| 285 | IsTextHtmlClipFormat(format_etc->cfFormat)); |
[email protected] | 3ee6112 | 2010-04-14 00:35:52 | [diff] [blame] | 286 | |
| 287 | if (!is_interesting) { |
| 288 | // Play back report progress so far. |
| 289 | MayPlayBack(flags); |
[email protected] | 97965e1 | 2010-04-09 00:51:10 | [diff] [blame] | 290 | return CallbackImpl::OnDataAvailable(flags, size, format_etc, stgmed); |
| 291 | } |
| 292 | |
| 293 | HRESULT hr = S_OK; |
| 294 | if (!clip_format_) |
| 295 | clip_format_ = format_etc->cfFormat; |
| 296 | |
| 297 | if (data_sniffer_.is_undetermined()) { |
| 298 | bool force_determination = !!(flags & |
| 299 | (BSCF_LASTDATANOTIFICATION | BSCF_DATAFULLYAVAILABLE)); |
| 300 | hr = data_sniffer_.ReadIntoCache(stgmed->pstm, force_determination); |
| 301 | // If we don't have sufficient data to determine renderer type |
| 302 | // wait for the next data notification. |
| 303 | if (data_sniffer_.is_undetermined()) |
| 304 | return S_OK; |
| 305 | } |
| 306 | |
| 307 | DCHECK(!data_sniffer_.is_undetermined()); |
| 308 | |
| 309 | if (data_sniffer_.is_cache_valid()) { |
| 310 | hr = MayPlayBack(flags); |
| 311 | DCHECK(!data_sniffer_.is_cache_valid()); |
| 312 | } else { |
| 313 | hr = CallbackImpl::OnDataAvailable(flags, size, format_etc, stgmed); |
| 314 | } |
[email protected] | 051236f | 2010-03-12 22:06:14 | [diff] [blame] | 315 | return hr; |
| 316 | } |
| 317 | |
[email protected] | 97965e1 | 2010-04-09 00:51:10 | [diff] [blame] | 318 | STDMETHODIMP BSCBStorageBind::OnStopBinding(HRESULT hresult, LPCWSTR error) { |
[email protected] | d3451d83 | 2010-10-01 11:17:37 | [diff] [blame^] | 319 | DLOG(INFO) << __FUNCTION__ << base::StringPrintf(" tid=%i", |
[email protected] | 97965e1 | 2010-04-09 00:51:10 | [diff] [blame] | 320 | PlatformThread::CurrentId()); |
[email protected] | e7dd2154 | 2010-05-07 21:59:44 | [diff] [blame] | 321 | // Report all crashes in the exception handler if we wrap the callback. |
| 322 | // Note that this avoids having the VEH report a crash if an SEH earlier in |
| 323 | // the chain handles the exception. |
| 324 | ExceptionBarrier barrier; |
| 325 | |
[email protected] | 97965e1 | 2010-04-09 00:51:10 | [diff] [blame] | 326 | HRESULT hr = MayPlayBack(BSCF_LASTDATANOTIFICATION); |
[email protected] | ffec6bf | 2010-04-09 23:53:26 | [diff] [blame] | 327 | hr = CallbackImpl::OnStopBinding(hresult, error); |
| 328 | ReleaseBind(); |
| 329 | return hr; |
[email protected] | 051236f | 2010-03-12 22:06:14 | [diff] [blame] | 330 | } |
| 331 | |
[email protected] | 97965e1 | 2010-04-09 00:51:10 | [diff] [blame] | 332 | // Play back the cached data to the delegate. Normally this would happen |
| 333 | // when we have read enough data to determine the renderer. In this case |
| 334 | // we first play back the data from the cache and then go into a 'pass |
| 335 | // through' mode. In some cases we may end up getting OnStopBinding |
| 336 | // before we get a chance to determine. Also it's possible that the |
| 337 | // BindToStorage call will return before OnStopBinding is sent. Hence |
| 338 | // This is called from 3 places and it's important to maintain the |
| 339 | // exact sequence of calls. |
| 340 | // Once the data is played back, calling this again is a no op. |
| 341 | HRESULT BSCBStorageBind::MayPlayBack(DWORD flags) { |
| 342 | // Force renderer type determination if not already done since |
| 343 | // we want to play back data now. |
[email protected] | 37a3a4b | 2010-04-14 22:37:40 | [diff] [blame] | 344 | data_sniffer_.DetermineRendererType(true); |
[email protected] | 97965e1 | 2010-04-09 00:51:10 | [diff] [blame] | 345 | DCHECK(!data_sniffer_.is_undetermined()); |
| 346 | |
| 347 | HRESULT hr = S_OK; |
| 348 | if (data_sniffer_.is_chrome()) { |
| 349 | // Remember clip format. If we are switching to chrome, then in order |
| 350 | // to make mshtml return INET_E_TERMINATED_BIND and reissue navigation |
| 351 | // with the same bind context, we have to return a mime type that is |
| 352 | // special cased by mshtml. |
| 353 | static const CLIPFORMAT kMagicClipFormat = |
| 354 | RegisterClipboardFormat(CFSTR_MIME_MPEG); |
| 355 | clip_format_ = kMagicClipFormat; |
| 356 | } else { |
| 357 | if (!saved_progress_.empty()) { |
[email protected] | 77d7aee | 2010-05-14 20:31:55 | [diff] [blame] | 358 | for (ProgressVector::iterator i = saved_progress_.begin(); |
[email protected] | 74e9983a | 2010-05-14 02:27:34 | [diff] [blame] | 359 | i != saved_progress_.end(); i++) { |
| 360 | Progress* p = (*i); |
[email protected] | 77d7aee | 2010-05-14 20:31:55 | [diff] [blame] | 361 | // We don't really expect a race condition here but just for sake |
| 362 | // of completeness we check. |
| 363 | if (p) { |
| 364 | (*i) = NULL; |
| 365 | CallbackImpl::OnProgress(p->progress(), p->progress_max(), |
| 366 | p->status_code(), p->status_text()); |
| 367 | delete p; |
| 368 | } |
[email protected] | 97965e1 | 2010-04-09 00:51:10 | [diff] [blame] | 369 | } |
| 370 | saved_progress_.clear(); |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | if (data_sniffer_.is_cache_valid()) { |
[email protected] | 3ee6112 | 2010-04-14 00:35:52 | [diff] [blame] | 375 | if (data_sniffer_.is_chrome()) { |
[email protected] | 77d7aee | 2010-05-14 20:31:55 | [diff] [blame] | 376 | ScopedComPtr<BindContextInfo> info; |
| 377 | BindContextInfo::FromBindContext(bind_ctx_, info.Receive()); |
[email protected] | 70277f6 | 2010-04-15 01:39:26 | [diff] [blame] | 378 | DCHECK(info); |
| 379 | if (info) { |
| 380 | info->SetToSwitch(data_sniffer_.cache_); |
[email protected] | 3ee6112 | 2010-04-14 00:35:52 | [diff] [blame] | 381 | } |
[email protected] | 3ee6112 | 2010-04-14 00:35:52 | [diff] [blame] | 382 | } |
| 383 | |
[email protected] | 97965e1 | 2010-04-09 00:51:10 | [diff] [blame] | 384 | hr = data_sniffer_.DrainCache(delegate(), |
| 385 | flags | BSCF_FIRSTDATANOTIFICATION, clip_format_); |
[email protected] | c8c547e | 2010-04-12 20:40:15 | [diff] [blame] | 386 | DLOG_IF(WARNING, INET_E_TERMINATED_BIND != hr) << __FUNCTION__ << |
| 387 | " mshtml OnDataAvailable returned: " << std::hex << hr; |
[email protected] | 97965e1 | 2010-04-09 00:51:10 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | return hr; |
[email protected] | 2b3102be | 2010-04-21 20:07:35 | [diff] [blame] | 391 | } |
| 392 | |
[email protected] | b31b9ff | 2010-05-07 18:29:24 | [diff] [blame] | 393 | // We cache and suppress sending progress notifications till |
| 394 | // we get the first OnDataAvailable. This is to prevent |
| 395 | // mshtml from making up its mind about the mime type. |
| 396 | // However, this is the invasive part of the patch and |
| 397 | // could trip other software that's due to mistimed progress |
| 398 | // notifications. It is probably not a good idea to hide redirects |
| 399 | // and some cookie notifications. |
| 400 | // |
| 401 | // We only need to suppress data notifications like |
| 402 | // BINDSTATUS_MIMETYPEAVAILABLE, |
| 403 | // BINDSTATUS_CACHEFILENAMEAVAILABLE etc. |
| 404 | // |
| 405 | // This is an atempt to reduce the exposure by starting to |
| 406 | // cache only when we receive one of the interesting progress |
| 407 | // notification. |
| 408 | bool BSCBStorageBind::ShouldCacheProgress(unsigned long status_code) const { |
| 409 | // We need to cache progress notifications only if we haven't yet figured |
| 410 | // out which way the request is going. |
| 411 | if (data_sniffer_.is_undetermined()) { |
| 412 | // If we are already caching then continue. |
| 413 | if (!saved_progress_.empty()) |
| 414 | return true; |
| 415 | // Start caching only if we see one of the interesting progress |
| 416 | // notifications. |
| 417 | switch (status_code) { |
| 418 | case BINDSTATUS_BEGINDOWNLOADDATA: |
| 419 | case BINDSTATUS_DOWNLOADINGDATA: |
| 420 | case BINDSTATUS_USINGCACHEDCOPY: |
| 421 | case BINDSTATUS_MIMETYPEAVAILABLE: |
| 422 | case BINDSTATUS_CACHEFILENAMEAVAILABLE: |
| 423 | case BINDSTATUS_SERVER_MIMETYPEAVAILABLE: |
| 424 | return true; |
| 425 | default: |
| 426 | break; |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | return false; |
| 431 | } |
| 432 | |