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