[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] | 37a3a4b | 2010-04-14 22:37:40 | [diff] [blame^] | 115 | bool last_chance = force_determination || (size() >= kMaxSniffSize); |
| 116 | DetermineRendererType(last_chance); |
[email protected] | 051236f | 2010-03-12 22:06:14 | [diff] [blame] | 117 | return hr; |
| 118 | } |
| 119 | |
[email protected] | 97965e1 | 2010-04-09 00:51:10 | [diff] [blame] | 120 | HRESULT SniffData::DrainCache(IBindStatusCallback* bscb, DWORD bscf, |
| 121 | CLIPFORMAT clip_format) { |
| 122 | if (!is_cache_valid()) { |
| 123 | return S_OK; |
| 124 | } |
| 125 | |
| 126 | // Ideally we could just use the cache_ IStream implementation but |
| 127 | // can't use it here since we have to return E_PENDING for the |
| 128 | // last call |
| 129 | HGLOBAL memory = NULL; |
| 130 | HRESULT hr = GetHGlobalFromStream(cache_, &memory); |
| 131 | if (SUCCEEDED(hr) && memory) { |
| 132 | char* buffer = reinterpret_cast<char*>(GlobalLock(memory)); |
| 133 | hr = CacheStream::BSCBFeedData(bscb, buffer, size_, clip_format, bscf); |
| 134 | GlobalUnlock(memory); |
| 135 | } |
| 136 | |
| 137 | size_ = 0; |
| 138 | cache_.Release(); |
| 139 | return hr; |
[email protected] | 051236f | 2010-03-12 22:06:14 | [diff] [blame] | 140 | } |
| 141 | |
[email protected] | 97965e1 | 2010-04-09 00:51:10 | [diff] [blame] | 142 | // Scan the buffer or OptIn URL list and decide if the renderer is |
[email protected] | 37a3a4b | 2010-04-14 22:37:40 | [diff] [blame^] | 143 | // to be switched. Last chance means there's no more data. |
| 144 | void SniffData::DetermineRendererType(bool last_chance) { |
[email protected] | 97965e1 | 2010-04-09 00:51:10 | [diff] [blame] | 145 | if (is_undetermined()) { |
[email protected] | 37a3a4b | 2010-04-14 22:37:40 | [diff] [blame^] | 146 | if (last_chance) |
| 147 | renderer_type_ = OTHER; |
[email protected] | 97965e1 | 2010-04-09 00:51:10 | [diff] [blame] | 148 | if (IsOptInUrl(url_.c_str())) { |
| 149 | renderer_type_ = CHROME; |
| 150 | } else { |
[email protected] | 97965e1 | 2010-04-09 00:51:10 | [diff] [blame] | 151 | if (is_cache_valid() && cache_) { |
| 152 | HGLOBAL memory = NULL; |
| 153 | GetHGlobalFromStream(cache_, &memory); |
| 154 | char* buffer = reinterpret_cast<char*>(GlobalLock(memory)); |
| 155 | |
| 156 | std::wstring html_contents; |
| 157 | // TODO(joshia): detect and handle different content encodings |
| 158 | if (buffer && size_) { |
| 159 | UTF8ToWide(buffer, size_, &html_contents); |
| 160 | GlobalUnlock(memory); |
| 161 | } |
| 162 | |
| 163 | // Note that document_contents_ may have NULL characters in it. While |
| 164 | // browsers may handle this properly, we don't and will stop scanning |
| 165 | // for the XUACompat content value if we encounter one. |
| 166 | std::wstring xua_compat_content; |
| 167 | UtilGetXUACompatContentValue(html_contents, &xua_compat_content); |
| 168 | if (StrStrI(xua_compat_content.c_str(), kChromeContentPrefix)) { |
| 169 | renderer_type_ = CHROME; |
| 170 | } |
| 171 | } |
| 172 | } |
[email protected] | ffec6bf | 2010-04-09 23:53:26 | [diff] [blame] | 173 | DLOG(INFO) << __FUNCTION__ << "Url: " << url_ << |
| 174 | StringPrintf("Renderer type: %s", |
| 175 | renderer_type_ == CHROME ? "CHROME" : "OTHER"); |
[email protected] | 97965e1 | 2010-04-09 00:51:10 | [diff] [blame] | 176 | } |
[email protected] | 051236f | 2010-03-12 22:06:14 | [diff] [blame] | 177 | } |
| 178 | |
[email protected] | 97965e1 | 2010-04-09 00:51:10 | [diff] [blame] | 179 | ///////////////////////////////////////////////////////////////////// |
| 180 | |
[email protected] | 3ee6112 | 2010-04-14 00:35:52 | [diff] [blame] | 181 | HRESULT BSCBStorageBind::Initialize(IMoniker* moniker, IBindCtx* bind_ctx, |
| 182 | bool no_cache) { |
[email protected] | 051236f | 2010-03-12 22:06:14 | [diff] [blame] | 183 | DLOG(INFO) << __FUNCTION__ << me() << StringPrintf(" tid=%i", |
| 184 | PlatformThread::CurrentId()); |
| 185 | |
[email protected] | 97965e1 | 2010-04-09 00:51:10 | [diff] [blame] | 186 | HRESULT hr = AttachToBind(bind_ctx); |
| 187 | if (FAILED(hr)) { |
| 188 | NOTREACHED() << __FUNCTION__ << me() << "AttachToBind error: " << hr; |
| 189 | return hr; |
[email protected] | 051236f | 2010-03-12 22:06:14 | [diff] [blame] | 190 | } |
| 191 | |
[email protected] | 97965e1 | 2010-04-09 00:51:10 | [diff] [blame] | 192 | if (!delegate()) { |
| 193 | NOTREACHED() << __FUNCTION__ << me() << "No existing callback: " << hr; |
| 194 | return E_FAIL; |
| 195 | } |
[email protected] | 051236f | 2010-03-12 22:06:14 | [diff] [blame] | 196 | |
[email protected] | 97965e1 | 2010-04-09 00:51:10 | [diff] [blame] | 197 | std::wstring url = GetActualUrlFromMoniker(moniker, bind_ctx, |
| 198 | std::wstring()); |
| 199 | data_sniffer_.InitializeCache(url); |
[email protected] | 3ee6112 | 2010-04-14 00:35:52 | [diff] [blame] | 200 | no_cache_ = no_cache; |
[email protected] | 051236f | 2010-03-12 22:06:14 | [diff] [blame] | 201 | return hr; |
| 202 | } |
| 203 | |
[email protected] | 97965e1 | 2010-04-09 00:51:10 | [diff] [blame] | 204 | STDMETHODIMP BSCBStorageBind::OnProgress(ULONG progress, ULONG progress_max, |
| 205 | ULONG status_code, LPCWSTR status_text) { |
| 206 | DLOG(INFO) << __FUNCTION__ << me() << StringPrintf(" status=%i tid=%i %ls", |
| 207 | status_code, PlatformThread::CurrentId(), status_text); |
| 208 | |
| 209 | HRESULT hr = S_OK; |
| 210 | if (data_sniffer_.is_undetermined()) { |
| 211 | Progress new_progress = { progress, progress_max, status_code, |
| 212 | status_text ? status_text : std::wstring() }; |
| 213 | saved_progress_.push_back(new_progress); |
| 214 | } else { |
| 215 | hr = CallbackImpl::OnProgress(progress, progress_max, status_code, |
| 216 | status_text); |
| 217 | } |
| 218 | |
| 219 | return hr; |
| 220 | } |
| 221 | |
| 222 | // Refer to urlmon_moniker.h for explanation of how things work. |
| 223 | STDMETHODIMP BSCBStorageBind::OnDataAvailable(DWORD flags, DWORD size, |
| 224 | FORMATETC* format_etc, |
| 225 | STGMEDIUM* stgmed) { |
| 226 | DLOG(INFO) << __FUNCTION__ << StringPrintf(" tid=%i", |
[email protected] | 051236f | 2010-03-12 22:06:14 | [diff] [blame] | 227 | PlatformThread::CurrentId()); |
[email protected] | 97965e1 | 2010-04-09 00:51:10 | [diff] [blame] | 228 | |
[email protected] | 3ee6112 | 2010-04-14 00:35:52 | [diff] [blame] | 229 | // Do not touch anything other than text/html. |
| 230 | const CLIPFORMAT text_html = RegisterClipboardFormat(CFSTR_MIME_HTML); |
| 231 | bool is_interesting = (format_etc && stgmed && stgmed->pstm && |
| 232 | (stgmed->tymed == TYMED_ISTREAM) && (text_html == format_etc->cfFormat)); |
| 233 | |
| 234 | if (!is_interesting) { |
| 235 | // Play back report progress so far. |
| 236 | MayPlayBack(flags); |
[email protected] | 97965e1 | 2010-04-09 00:51:10 | [diff] [blame] | 237 | return CallbackImpl::OnDataAvailable(flags, size, format_etc, stgmed); |
| 238 | } |
| 239 | |
| 240 | HRESULT hr = S_OK; |
| 241 | if (!clip_format_) |
| 242 | clip_format_ = format_etc->cfFormat; |
| 243 | |
| 244 | if (data_sniffer_.is_undetermined()) { |
| 245 | bool force_determination = !!(flags & |
| 246 | (BSCF_LASTDATANOTIFICATION | BSCF_DATAFULLYAVAILABLE)); |
| 247 | hr = data_sniffer_.ReadIntoCache(stgmed->pstm, force_determination); |
| 248 | // If we don't have sufficient data to determine renderer type |
| 249 | // wait for the next data notification. |
| 250 | if (data_sniffer_.is_undetermined()) |
| 251 | return S_OK; |
| 252 | } |
| 253 | |
| 254 | DCHECK(!data_sniffer_.is_undetermined()); |
| 255 | |
| 256 | if (data_sniffer_.is_cache_valid()) { |
| 257 | hr = MayPlayBack(flags); |
| 258 | DCHECK(!data_sniffer_.is_cache_valid()); |
| 259 | } else { |
| 260 | hr = CallbackImpl::OnDataAvailable(flags, size, format_etc, stgmed); |
| 261 | } |
[email protected] | 051236f | 2010-03-12 22:06:14 | [diff] [blame] | 262 | return hr; |
| 263 | } |
| 264 | |
[email protected] | 97965e1 | 2010-04-09 00:51:10 | [diff] [blame] | 265 | STDMETHODIMP BSCBStorageBind::OnStopBinding(HRESULT hresult, LPCWSTR error) { |
| 266 | DLOG(INFO) << __FUNCTION__ << StringPrintf(" tid=%i", |
| 267 | PlatformThread::CurrentId()); |
| 268 | HRESULT hr = MayPlayBack(BSCF_LASTDATANOTIFICATION); |
[email protected] | ffec6bf | 2010-04-09 23:53:26 | [diff] [blame] | 269 | hr = CallbackImpl::OnStopBinding(hresult, error); |
| 270 | ReleaseBind(); |
| 271 | return hr; |
[email protected] | 051236f | 2010-03-12 22:06:14 | [diff] [blame] | 272 | } |
| 273 | |
[email protected] | 97965e1 | 2010-04-09 00:51:10 | [diff] [blame] | 274 | // Play back the cached data to the delegate. Normally this would happen |
| 275 | // when we have read enough data to determine the renderer. In this case |
| 276 | // we first play back the data from the cache and then go into a 'pass |
| 277 | // through' mode. In some cases we may end up getting OnStopBinding |
| 278 | // before we get a chance to determine. Also it's possible that the |
| 279 | // BindToStorage call will return before OnStopBinding is sent. Hence |
| 280 | // This is called from 3 places and it's important to maintain the |
| 281 | // exact sequence of calls. |
| 282 | // Once the data is played back, calling this again is a no op. |
| 283 | HRESULT BSCBStorageBind::MayPlayBack(DWORD flags) { |
| 284 | // Force renderer type determination if not already done since |
| 285 | // we want to play back data now. |
[email protected] | 37a3a4b | 2010-04-14 22:37:40 | [diff] [blame^] | 286 | data_sniffer_.DetermineRendererType(true); |
[email protected] | 97965e1 | 2010-04-09 00:51:10 | [diff] [blame] | 287 | DCHECK(!data_sniffer_.is_undetermined()); |
| 288 | |
| 289 | HRESULT hr = S_OK; |
| 290 | if (data_sniffer_.is_chrome()) { |
| 291 | // Remember clip format. If we are switching to chrome, then in order |
| 292 | // to make mshtml return INET_E_TERMINATED_BIND and reissue navigation |
| 293 | // with the same bind context, we have to return a mime type that is |
| 294 | // special cased by mshtml. |
| 295 | static const CLIPFORMAT kMagicClipFormat = |
| 296 | RegisterClipboardFormat(CFSTR_MIME_MPEG); |
| 297 | clip_format_ = kMagicClipFormat; |
| 298 | } else { |
| 299 | if (!saved_progress_.empty()) { |
| 300 | for (std::vector<Progress>::iterator i = saved_progress_.begin(); |
| 301 | i != saved_progress_.end(); i++) { |
| 302 | const wchar_t* status_text = i->status_text_.empty() ? |
| 303 | NULL : i->status_text_.c_str(); |
| 304 | CallbackImpl::OnProgress(i->progress_, i->progress_max_, |
| 305 | i->status_code_, status_text); |
| 306 | } |
| 307 | saved_progress_.clear(); |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | if (data_sniffer_.is_cache_valid()) { |
[email protected] | 3ee6112 | 2010-04-14 00:35:52 | [diff] [blame] | 312 | if (data_sniffer_.is_chrome()) { |
| 313 | ScopedComPtr<IStream> cache; |
| 314 | if (no_cache_) { |
| 315 | // This flag is set by BindToObject indicating taht we don't need |
| 316 | // to cache as we'll be able to read data from the bind later. |
| 317 | CreateStreamOnHGlobal(NULL, TRUE, cache.Receive()); |
| 318 | } else { |
| 319 | // Binding began with BindToStorage and the data cann't be read |
| 320 | // back so pass on the data read so far. |
| 321 | cache = data_sniffer_.cache_; |
| 322 | } |
| 323 | DCHECK(cache); |
| 324 | NavigationManager::SetForSwitch(bind_ctx_, cache); |
| 325 | } |
| 326 | |
[email protected] | 97965e1 | 2010-04-09 00:51:10 | [diff] [blame] | 327 | hr = data_sniffer_.DrainCache(delegate(), |
| 328 | flags | BSCF_FIRSTDATANOTIFICATION, clip_format_); |
[email protected] | c8c547e | 2010-04-12 20:40:15 | [diff] [blame] | 329 | DLOG_IF(WARNING, INET_E_TERMINATED_BIND != hr) << __FUNCTION__ << |
| 330 | " mshtml OnDataAvailable returned: " << std::hex << hr; |
[email protected] | 97965e1 | 2010-04-09 00:51:10 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | return hr; |
[email protected] | c8c547e | 2010-04-12 20:40:15 | [diff] [blame] | 334 | } |