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