[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] | 97965e1 | 2010-04-09 00:51:10 | [diff] [blame] | 15 | #include "chrome_frame/urlmon_moniker.h" |
| 16 | #include "chrome_tab.h" // NOLINT |
[email protected] | 051236f | 2010-03-12 22:06:14 | [diff] [blame] | 17 | |
[email protected] | 051236f | 2010-03-12 22:06:14 | [diff] [blame] | 18 | |
[email protected] | 97965e1 | 2010-04-09 00:51:10 | [diff] [blame] | 19 | // A helper to given feed data to the specified |bscb| using |
| 20 | // CacheStream instance. |
| 21 | HRESULT CacheStream::BSCBFeedData(IBindStatusCallback* bscb, const char* data, |
| 22 | size_t size, CLIPFORMAT clip_format, |
[email protected] | de5bc35 | 2010-04-16 01:38:48 | [diff] [blame] | 23 | size_t flags, bool eof) { |
[email protected] | 97965e1 | 2010-04-09 00:51:10 | [diff] [blame] | 24 | if (!bscb) { |
| 25 | NOTREACHED() << "invalid IBindStatusCallback"; |
| 26 | return E_INVALIDARG; |
[email protected] | 051236f | 2010-03-12 22:06:14 | [diff] [blame] | 27 | } |
| 28 | |
[email protected] | 97965e1 | 2010-04-09 00:51:10 | [diff] [blame] | 29 | // We can't use a CComObjectStackEx here since mshtml will hold |
| 30 | // onto the stream pointer. |
| 31 | CComObject<CacheStream>* cache_stream = NULL; |
| 32 | HRESULT hr = CComObject<CacheStream>::CreateInstance(&cache_stream); |
[email protected] | 051236f | 2010-03-12 22:06:14 | [diff] [blame] | 33 | if (FAILED(hr)) { |
| 34 | NOTREACHED(); |
| 35 | return hr; |
| 36 | } |
| 37 | |
[email protected] | 97965e1 | 2010-04-09 00:51:10 | [diff] [blame] | 38 | cache_stream->AddRef(); |
[email protected] | de5bc35 | 2010-04-16 01:38:48 | [diff] [blame] | 39 | cache_stream->Initialize(data, size, eof); |
[email protected] | 051236f | 2010-03-12 22:06:14 | [diff] [blame] | 40 | |
[email protected] | 97965e1 | 2010-04-09 00:51:10 | [diff] [blame] | 41 | FORMATETC format_etc = { clip_format, NULL, DVASPECT_CONTENT, -1, |
| 42 | TYMED_ISTREAM }; |
| 43 | STGMEDIUM medium = {0}; |
| 44 | medium.tymed = TYMED_ISTREAM; |
| 45 | medium.pstm = cache_stream; |
| 46 | |
| 47 | hr = bscb->OnDataAvailable(flags, size, &format_etc, &medium); |
| 48 | |
| 49 | cache_stream->Release(); |
| 50 | return hr; |
[email protected] | 051236f | 2010-03-12 22:06:14 | [diff] [blame] | 51 | } |
| 52 | |
[email protected] | de5bc35 | 2010-04-16 01:38:48 | [diff] [blame] | 53 | void CacheStream::Initialize(const char* cache, size_t size, bool eof) { |
[email protected] | 97965e1 | 2010-04-09 00:51:10 | [diff] [blame] | 54 | cache_ = cache; |
| 55 | size_ = size; |
| 56 | position_ = 0; |
[email protected] | de5bc35 | 2010-04-16 01:38:48 | [diff] [blame] | 57 | eof_ = eof; |
[email protected] | 051236f | 2010-03-12 22:06:14 | [diff] [blame] | 58 | } |
| 59 | |
[email protected] | 97965e1 | 2010-04-09 00:51:10 | [diff] [blame] | 60 | // Read is the only call that we expect. Return E_PENDING if there |
| 61 | // is no more data to serve. Otherwise this will result in a |
| 62 | // read with 0 bytes indicating that no more data is available. |
| 63 | STDMETHODIMP CacheStream::Read(void* pv, ULONG cb, ULONG* read) { |
| 64 | if (!pv || !read) |
| 65 | return E_INVALIDARG; |
[email protected] | 051236f | 2010-03-12 22:06:14 | [diff] [blame] | 66 | |
[email protected] | 97965e1 | 2010-04-09 00:51:10 | [diff] [blame] | 67 | // Default to E_PENDING to signal that this is a partial data. |
[email protected] | de5bc35 | 2010-04-16 01:38:48 | [diff] [blame] | 68 | HRESULT hr = eof_ ? S_FALSE : E_PENDING; |
[email protected] | 97965e1 | 2010-04-09 00:51:10 | [diff] [blame] | 69 | if (position_ < size_) { |
| 70 | *read = std::min(size_ - position_, size_t(cb)); |
| 71 | memcpy(pv, cache_ + position_, *read); |
| 72 | position_ += *read; |
| 73 | hr = S_OK; |
[email protected] | 051236f | 2010-03-12 22:06:14 | [diff] [blame] | 74 | } |
[email protected] | 051236f | 2010-03-12 22:06:14 | [diff] [blame] | 75 | |
| 76 | return hr; |
| 77 | } |
| 78 | |
[email protected] | 97965e1 | 2010-04-09 00:51:10 | [diff] [blame] | 79 | |
| 80 | ///////////////////////////////////////////////////////////////////// |
| 81 | |
| 82 | bool SniffData::InitializeCache(const std::wstring& url) { |
| 83 | url_ = url; |
| 84 | renderer_type_ = UNDETERMINED; |
| 85 | |
[email protected] | 3ee6112 | 2010-04-14 00:35:52 | [diff] [blame] | 86 | HRESULT hr = CreateStreamOnHGlobal(NULL, TRUE, cache_.Receive()); |
[email protected] | 97965e1 | 2010-04-09 00:51:10 | [diff] [blame] | 87 | if (FAILED(hr)) { |
[email protected] | 97965e1 | 2010-04-09 00:51:10 | [diff] [blame] | 88 | NOTREACHED(); |
| 89 | return false; |
| 90 | } |
[email protected] | 97965e1 | 2010-04-09 00:51:10 | [diff] [blame] | 91 | return true; |
[email protected] | 051236f | 2010-03-12 22:06:14 | [diff] [blame] | 92 | } |
| 93 | |
[email protected] | 97965e1 | 2010-04-09 00:51:10 | [diff] [blame] | 94 | HRESULT SniffData::ReadIntoCache(IStream* stream, bool force_determination) { |
| 95 | if (!stream) { |
| 96 | NOTREACHED(); |
| 97 | return E_INVALIDARG; |
[email protected] | 051236f | 2010-03-12 22:06:14 | [diff] [blame] | 98 | } |
[email protected] | 051236f | 2010-03-12 22:06:14 | [diff] [blame] | 99 | |
| 100 | HRESULT hr = S_OK; |
[email protected] | 97965e1 | 2010-04-09 00:51:10 | [diff] [blame] | 101 | while (SUCCEEDED(hr)) { |
| 102 | const size_t kChunkSize = 4 * 1024; |
| 103 | char buffer[kChunkSize]; |
| 104 | DWORD read = 0; |
| 105 | hr = stream->Read(buffer, sizeof(buffer), &read); |
| 106 | if (read) { |
| 107 | DWORD written = 0; |
| 108 | cache_->Write(buffer, read, &written); |
| 109 | size_ += written; |
[email protected] | fb716b30 | 2010-03-26 02:45:20 | [diff] [blame] | 110 | } |
| 111 | |
[email protected] | 97965e1 | 2010-04-09 00:51:10 | [diff] [blame] | 112 | if ((S_FALSE == hr) || !read) |
| 113 | break; |
[email protected] | 051236f | 2010-03-12 22:06:14 | [diff] [blame] | 114 | } |
| 115 | |
[email protected] | 37a3a4b | 2010-04-14 22:37:40 | [diff] [blame] | 116 | bool last_chance = force_determination || (size() >= kMaxSniffSize); |
[email protected] | de5bc35 | 2010-04-16 01:38:48 | [diff] [blame] | 117 | eof_ = force_determination; |
[email protected] | 37a3a4b | 2010-04-14 22:37:40 | [diff] [blame] | 118 | DetermineRendererType(last_chance); |
[email protected] | 051236f | 2010-03-12 22:06:14 | [diff] [blame] | 119 | return hr; |
| 120 | } |
| 121 | |
[email protected] | 97965e1 | 2010-04-09 00:51:10 | [diff] [blame] | 122 | HRESULT SniffData::DrainCache(IBindStatusCallback* bscb, DWORD bscf, |
| 123 | CLIPFORMAT clip_format) { |
| 124 | if (!is_cache_valid()) { |
| 125 | return S_OK; |
| 126 | } |
| 127 | |
| 128 | // Ideally we could just use the cache_ IStream implementation but |
| 129 | // can't use it here since we have to return E_PENDING for the |
| 130 | // last call |
| 131 | HGLOBAL memory = NULL; |
| 132 | HRESULT hr = GetHGlobalFromStream(cache_, &memory); |
| 133 | if (SUCCEEDED(hr) && memory) { |
| 134 | char* buffer = reinterpret_cast<char*>(GlobalLock(memory)); |
[email protected] | de5bc35 | 2010-04-16 01:38:48 | [diff] [blame] | 135 | hr = CacheStream::BSCBFeedData(bscb, buffer, size_, clip_format, bscf, |
| 136 | eof_); |
[email protected] | 97965e1 | 2010-04-09 00:51:10 | [diff] [blame] | 137 | GlobalUnlock(memory); |
| 138 | } |
| 139 | |
| 140 | size_ = 0; |
| 141 | cache_.Release(); |
| 142 | return hr; |
[email protected] | 051236f | 2010-03-12 22:06:14 | [diff] [blame] | 143 | } |
| 144 | |
[email protected] | 97965e1 | 2010-04-09 00:51:10 | [diff] [blame] | 145 | // Scan the buffer or OptIn URL list and decide if the renderer is |
[email protected] | 37a3a4b | 2010-04-14 22:37:40 | [diff] [blame] | 146 | // to be switched. Last chance means there's no more data. |
| 147 | void SniffData::DetermineRendererType(bool last_chance) { |
[email protected] | 97965e1 | 2010-04-09 00:51:10 | [diff] [blame] | 148 | if (is_undetermined()) { |
[email protected] | 37a3a4b | 2010-04-14 22:37:40 | [diff] [blame] | 149 | if (last_chance) |
| 150 | renderer_type_ = OTHER; |
[email protected] | 97965e1 | 2010-04-09 00:51:10 | [diff] [blame] | 151 | if (IsOptInUrl(url_.c_str())) { |
| 152 | renderer_type_ = CHROME; |
| 153 | } else { |
[email protected] | 97965e1 | 2010-04-09 00:51:10 | [diff] [blame] | 154 | if (is_cache_valid() && cache_) { |
| 155 | HGLOBAL memory = NULL; |
| 156 | GetHGlobalFromStream(cache_, &memory); |
| 157 | char* buffer = reinterpret_cast<char*>(GlobalLock(memory)); |
| 158 | |
| 159 | std::wstring html_contents; |
| 160 | // TODO(joshia): detect and handle different content encodings |
| 161 | if (buffer && size_) { |
| 162 | UTF8ToWide(buffer, size_, &html_contents); |
| 163 | GlobalUnlock(memory); |
| 164 | } |
| 165 | |
| 166 | // Note that document_contents_ may have NULL characters in it. While |
| 167 | // browsers may handle this properly, we don't and will stop scanning |
| 168 | // for the XUACompat content value if we encounter one. |
| 169 | std::wstring xua_compat_content; |
| 170 | UtilGetXUACompatContentValue(html_contents, &xua_compat_content); |
| 171 | if (StrStrI(xua_compat_content.c_str(), kChromeContentPrefix)) { |
| 172 | renderer_type_ = CHROME; |
| 173 | } |
| 174 | } |
| 175 | } |
[email protected] | ffec6bf | 2010-04-09 23:53:26 | [diff] [blame] | 176 | DLOG(INFO) << __FUNCTION__ << "Url: " << url_ << |
| 177 | StringPrintf("Renderer type: %s", |
| 178 | renderer_type_ == CHROME ? "CHROME" : "OTHER"); |
[email protected] | 97965e1 | 2010-04-09 00:51:10 | [diff] [blame] | 179 | } |
[email protected] | 051236f | 2010-03-12 22:06:14 | [diff] [blame] | 180 | } |
| 181 | |
[email protected] | 97965e1 | 2010-04-09 00:51:10 | [diff] [blame] | 182 | ///////////////////////////////////////////////////////////////////// |
| 183 | |
[email protected] | 70277f6 | 2010-04-15 01:39:26 | [diff] [blame] | 184 | HRESULT BSCBStorageBind::Initialize(IMoniker* moniker, IBindCtx* bind_ctx) { |
[email protected] | 051236f | 2010-03-12 22:06:14 | [diff] [blame] | 185 | DLOG(INFO) << __FUNCTION__ << me() << StringPrintf(" tid=%i", |
| 186 | PlatformThread::CurrentId()); |
| 187 | |
[email protected] | 97965e1 | 2010-04-09 00:51:10 | [diff] [blame] | 188 | HRESULT hr = AttachToBind(bind_ctx); |
| 189 | if (FAILED(hr)) { |
| 190 | NOTREACHED() << __FUNCTION__ << me() << "AttachToBind error: " << hr; |
| 191 | return hr; |
[email protected] | 051236f | 2010-03-12 22:06:14 | [diff] [blame] | 192 | } |
| 193 | |
[email protected] | 97965e1 | 2010-04-09 00:51:10 | [diff] [blame] | 194 | if (!delegate()) { |
| 195 | NOTREACHED() << __FUNCTION__ << me() << "No existing callback: " << hr; |
| 196 | return E_FAIL; |
| 197 | } |
[email protected] | 051236f | 2010-03-12 22:06:14 | [diff] [blame] | 198 | |
[email protected] | 97965e1 | 2010-04-09 00:51:10 | [diff] [blame] | 199 | std::wstring url = GetActualUrlFromMoniker(moniker, bind_ctx, |
| 200 | std::wstring()); |
| 201 | data_sniffer_.InitializeCache(url); |
[email protected] | 051236f | 2010-03-12 22:06:14 | [diff] [blame] | 202 | return hr; |
| 203 | } |
| 204 | |
[email protected] | 97965e1 | 2010-04-09 00:51:10 | [diff] [blame] | 205 | STDMETHODIMP BSCBStorageBind::OnProgress(ULONG progress, ULONG progress_max, |
| 206 | ULONG status_code, LPCWSTR status_text) { |
| 207 | DLOG(INFO) << __FUNCTION__ << me() << StringPrintf(" status=%i tid=%i %ls", |
| 208 | status_code, PlatformThread::CurrentId(), status_text); |
| 209 | |
| 210 | HRESULT hr = S_OK; |
| 211 | if (data_sniffer_.is_undetermined()) { |
| 212 | Progress new_progress = { progress, progress_max, status_code, |
| 213 | status_text ? status_text : std::wstring() }; |
| 214 | saved_progress_.push_back(new_progress); |
[email protected] | 29c32f90 | 2010-04-20 23:27:29 | [diff] [blame] | 215 | if (status_code == BINDSTATUS_REDIRECTING) { |
| 216 | scoped_refptr<BindContextInfo> info = |
| 217 | BindContextInfo::FromBindContext(bind_ctx_); |
| 218 | DCHECK(info); |
| 219 | info->set_url(status_text); |
| 220 | } |
[email protected] | 97965e1 | 2010-04-09 00:51:10 | [diff] [blame] | 221 | } else { |
| 222 | hr = CallbackImpl::OnProgress(progress, progress_max, status_code, |
| 223 | status_text); |
| 224 | } |
| 225 | |
| 226 | return hr; |
| 227 | } |
| 228 | |
| 229 | // Refer to urlmon_moniker.h for explanation of how things work. |
| 230 | STDMETHODIMP BSCBStorageBind::OnDataAvailable(DWORD flags, DWORD size, |
| 231 | FORMATETC* format_etc, |
| 232 | STGMEDIUM* stgmed) { |
| 233 | DLOG(INFO) << __FUNCTION__ << StringPrintf(" tid=%i", |
[email protected] | 051236f | 2010-03-12 22:06:14 | [diff] [blame] | 234 | PlatformThread::CurrentId()); |
[email protected] | 97965e1 | 2010-04-09 00:51:10 | [diff] [blame] | 235 | |
[email protected] | 3ee6112 | 2010-04-14 00:35:52 | [diff] [blame] | 236 | // Do not touch anything other than text/html. |
[email protected] | 3ee6112 | 2010-04-14 00:35:52 | [diff] [blame] | 237 | bool is_interesting = (format_etc && stgmed && stgmed->pstm && |
[email protected] | 2b3102be | 2010-04-21 20:07:35 | [diff] [blame^] | 238 | stgmed->tymed == TYMED_ISTREAM && |
| 239 | IsTextHtmlClipFormat(format_etc->cfFormat)); |
[email protected] | 3ee6112 | 2010-04-14 00:35:52 | [diff] [blame] | 240 | |
| 241 | if (!is_interesting) { |
| 242 | // Play back report progress so far. |
| 243 | MayPlayBack(flags); |
[email protected] | 97965e1 | 2010-04-09 00:51:10 | [diff] [blame] | 244 | return CallbackImpl::OnDataAvailable(flags, size, format_etc, stgmed); |
| 245 | } |
| 246 | |
| 247 | HRESULT hr = S_OK; |
| 248 | if (!clip_format_) |
| 249 | clip_format_ = format_etc->cfFormat; |
| 250 | |
| 251 | if (data_sniffer_.is_undetermined()) { |
| 252 | bool force_determination = !!(flags & |
| 253 | (BSCF_LASTDATANOTIFICATION | BSCF_DATAFULLYAVAILABLE)); |
| 254 | hr = data_sniffer_.ReadIntoCache(stgmed->pstm, force_determination); |
| 255 | // If we don't have sufficient data to determine renderer type |
| 256 | // wait for the next data notification. |
| 257 | if (data_sniffer_.is_undetermined()) |
| 258 | return S_OK; |
| 259 | } |
| 260 | |
| 261 | DCHECK(!data_sniffer_.is_undetermined()); |
| 262 | |
| 263 | if (data_sniffer_.is_cache_valid()) { |
| 264 | hr = MayPlayBack(flags); |
| 265 | DCHECK(!data_sniffer_.is_cache_valid()); |
| 266 | } else { |
| 267 | hr = CallbackImpl::OnDataAvailable(flags, size, format_etc, stgmed); |
| 268 | } |
[email protected] | 051236f | 2010-03-12 22:06:14 | [diff] [blame] | 269 | return hr; |
| 270 | } |
| 271 | |
[email protected] | 97965e1 | 2010-04-09 00:51:10 | [diff] [blame] | 272 | STDMETHODIMP BSCBStorageBind::OnStopBinding(HRESULT hresult, LPCWSTR error) { |
| 273 | DLOG(INFO) << __FUNCTION__ << StringPrintf(" tid=%i", |
| 274 | PlatformThread::CurrentId()); |
| 275 | HRESULT hr = MayPlayBack(BSCF_LASTDATANOTIFICATION); |
[email protected] | ffec6bf | 2010-04-09 23:53:26 | [diff] [blame] | 276 | hr = CallbackImpl::OnStopBinding(hresult, error); |
| 277 | ReleaseBind(); |
| 278 | return hr; |
[email protected] | 051236f | 2010-03-12 22:06:14 | [diff] [blame] | 279 | } |
| 280 | |
[email protected] | 97965e1 | 2010-04-09 00:51:10 | [diff] [blame] | 281 | // Play back the cached data to the delegate. Normally this would happen |
| 282 | // when we have read enough data to determine the renderer. In this case |
| 283 | // we first play back the data from the cache and then go into a 'pass |
| 284 | // through' mode. In some cases we may end up getting OnStopBinding |
| 285 | // before we get a chance to determine. Also it's possible that the |
| 286 | // BindToStorage call will return before OnStopBinding is sent. Hence |
| 287 | // This is called from 3 places and it's important to maintain the |
| 288 | // exact sequence of calls. |
| 289 | // Once the data is played back, calling this again is a no op. |
| 290 | HRESULT BSCBStorageBind::MayPlayBack(DWORD flags) { |
| 291 | // Force renderer type determination if not already done since |
| 292 | // we want to play back data now. |
[email protected] | 37a3a4b | 2010-04-14 22:37:40 | [diff] [blame] | 293 | data_sniffer_.DetermineRendererType(true); |
[email protected] | 97965e1 | 2010-04-09 00:51:10 | [diff] [blame] | 294 | DCHECK(!data_sniffer_.is_undetermined()); |
| 295 | |
| 296 | HRESULT hr = S_OK; |
| 297 | if (data_sniffer_.is_chrome()) { |
| 298 | // Remember clip format. If we are switching to chrome, then in order |
| 299 | // to make mshtml return INET_E_TERMINATED_BIND and reissue navigation |
| 300 | // with the same bind context, we have to return a mime type that is |
| 301 | // special cased by mshtml. |
| 302 | static const CLIPFORMAT kMagicClipFormat = |
| 303 | RegisterClipboardFormat(CFSTR_MIME_MPEG); |
| 304 | clip_format_ = kMagicClipFormat; |
| 305 | } else { |
| 306 | if (!saved_progress_.empty()) { |
| 307 | for (std::vector<Progress>::iterator i = saved_progress_.begin(); |
| 308 | i != saved_progress_.end(); i++) { |
| 309 | const wchar_t* status_text = i->status_text_.empty() ? |
| 310 | NULL : i->status_text_.c_str(); |
| 311 | CallbackImpl::OnProgress(i->progress_, i->progress_max_, |
| 312 | i->status_code_, status_text); |
| 313 | } |
| 314 | saved_progress_.clear(); |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | if (data_sniffer_.is_cache_valid()) { |
[email protected] | 3ee6112 | 2010-04-14 00:35:52 | [diff] [blame] | 319 | if (data_sniffer_.is_chrome()) { |
[email protected] | 70277f6 | 2010-04-15 01:39:26 | [diff] [blame] | 320 | scoped_refptr<BindContextInfo> info = |
| 321 | BindContextInfo::FromBindContext(bind_ctx_); |
| 322 | DCHECK(info); |
| 323 | if (info) { |
| 324 | info->SetToSwitch(data_sniffer_.cache_); |
[email protected] | 3ee6112 | 2010-04-14 00:35:52 | [diff] [blame] | 325 | } |
[email protected] | 3ee6112 | 2010-04-14 00:35:52 | [diff] [blame] | 326 | } |
| 327 | |
[email protected] | 97965e1 | 2010-04-09 00:51:10 | [diff] [blame] | 328 | hr = data_sniffer_.DrainCache(delegate(), |
| 329 | flags | BSCF_FIRSTDATANOTIFICATION, clip_format_); |
[email protected] | c8c547e | 2010-04-12 20:40:15 | [diff] [blame] | 330 | DLOG_IF(WARNING, INET_E_TERMINATED_BIND != hr) << __FUNCTION__ << |
| 331 | " mshtml OnDataAvailable returned: " << std::hex << hr; |
[email protected] | 97965e1 | 2010-04-09 00:51:10 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | return hr; |
[email protected] | 2b3102be | 2010-04-21 20:07:35 | [diff] [blame^] | 335 | } |
| 336 | |