blob: bfc41f9ec39fd565d14b78926cbf042d75287196 [file] [log] [blame]
[email protected]051236f2010-03-12 22:06:141// 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]97965e12010-04-09 00:51:107#include <mshtml.h>
[email protected]fb716b302010-03-26 02:45:208#include <shlguid.h>
9
[email protected]051236f2010-03-12 22:06:1410#include "base/logging.h"
11#include "base/string_util.h"
[email protected]d3451d832010-10-01 11:17:3712#include "base/stringprintf.h"
[email protected]97965e12010-04-09 00:51:1013#include "base/utf_string_conversions.h"
[email protected]70277f62010-04-15 01:39:2614#include "chrome_frame/bind_context_info.h"
[email protected]e7dd21542010-05-07 21:59:4415#include "chrome_frame/exception_barrier.h"
[email protected]97965e12010-04-09 00:51:1016#include "chrome_frame/urlmon_moniker.h"
17#include "chrome_tab.h" // NOLINT
[email protected]051236f2010-03-12 22:06:1418
[email protected]051236f2010-03-12 22:06:1419
[email protected]97965e12010-04-09 00:51:1020// A helper to given feed data to the specified |bscb| using
21// CacheStream instance.
22HRESULT CacheStream::BSCBFeedData(IBindStatusCallback* bscb, const char* data,
23 size_t size, CLIPFORMAT clip_format,
[email protected]de5bc352010-04-16 01:38:4824 size_t flags, bool eof) {
[email protected]97965e12010-04-09 00:51:1025 if (!bscb) {
26 NOTREACHED() << "invalid IBindStatusCallback";
27 return E_INVALIDARG;
[email protected]051236f2010-03-12 22:06:1428 }
29
[email protected]97965e12010-04-09 00:51:1030 // 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]051236f2010-03-12 22:06:1434 if (FAILED(hr)) {
35 NOTREACHED();
36 return hr;
37 }
38
[email protected]f1917ed2010-05-13 20:21:0839 scoped_refptr<CacheStream> cache_ref = cache_stream;
40 hr = cache_stream->Initialize(data, size, eof);
41 if (FAILED(hr))
42 return hr;
[email protected]051236f2010-03-12 22:06:1443
[email protected]97965e12010-04-09 00:51:1044 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]97965e12010-04-09 00:51:1051 return hr;
[email protected]051236f2010-03-12 22:06:1452}
53
[email protected]f1917ed2010-05-13 20:21:0854HRESULT CacheStream::Initialize(const char* cache, size_t size, bool eof) {
[email protected]97965e12010-04-09 00:51:1055 position_ = 0;
[email protected]de5bc352010-04-16 01:38:4856 eof_ = eof;
[email protected]f1917ed2010-05-13 20:21:0857
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]051236f2010-03-12 22:06:1469}
70
[email protected]97965e12010-04-09 00:51:1071// 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.
74STDMETHODIMP CacheStream::Read(void* pv, ULONG cb, ULONG* read) {
75 if (!pv || !read)
76 return E_INVALIDARG;
[email protected]051236f2010-03-12 22:06:1477
[email protected]642c15e2010-05-13 20:56:1278 if (!cache_.get()) {
79 *read = 0;
80 return S_FALSE;
81 }
[email protected]f1917ed2010-05-13 20:21:0882
[email protected]97965e12010-04-09 00:51:1083 // Default to E_PENDING to signal that this is a partial data.
[email protected]de5bc352010-04-16 01:38:4884 HRESULT hr = eof_ ? S_FALSE : E_PENDING;
[email protected]97965e12010-04-09 00:51:1085 if (position_ < size_) {
86 *read = std::min(size_ - position_, size_t(cb));
[email protected]f1917ed2010-05-13 20:21:0887 memcpy(pv, cache_ .get() + position_, *read);
[email protected]97965e12010-04-09 00:51:1088 position_ += *read;
89 hr = S_OK;
[email protected]051236f2010-03-12 22:06:1490 }
[email protected]051236f2010-03-12 22:06:1491
92 return hr;
93}
94
[email protected]97965e12010-04-09 00:51:1095
96/////////////////////////////////////////////////////////////////////
97
[email protected]040b800f2010-05-05 23:46:3098HRESULT SniffData::InitializeCache(const std::wstring& url) {
[email protected]97965e12010-04-09 00:51:1099 url_ = url;
100 renderer_type_ = UNDETERMINED;
101
[email protected]040b800f2010-05-05 23:46:30102 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]b31b9ff2010-05-07 18:29:24107 if (SUCCEEDED(hr)) {
108 ULARGE_INTEGER size = {0};
109 cache_->SetSize(size);
110 } else {
111 DLOG(ERROR) << "CreateStreamOnHGlobal failed: " << hr;
112 }
113
[email protected]040b800f2010-05-05 23:46:30114 return hr;
[email protected]051236f2010-03-12 22:06:14115}
116
[email protected]97965e12010-04-09 00:51:10117HRESULT SniffData::ReadIntoCache(IStream* stream, bool force_determination) {
118 if (!stream) {
119 NOTREACHED();
120 return E_INVALIDARG;
[email protected]051236f2010-03-12 22:06:14121 }
[email protected]051236f2010-03-12 22:06:14122
123 HRESULT hr = S_OK;
[email protected]97965e12010-04-09 00:51:10124 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]fb716b302010-03-26 02:45:20133 }
134
[email protected]97965e12010-04-09 00:51:10135 if ((S_FALSE == hr) || !read)
136 break;
[email protected]051236f2010-03-12 22:06:14137 }
138
[email protected]37a3a4b2010-04-14 22:37:40139 bool last_chance = force_determination || (size() >= kMaxSniffSize);
[email protected]de5bc352010-04-16 01:38:48140 eof_ = force_determination;
[email protected]37a3a4b2010-04-14 22:37:40141 DetermineRendererType(last_chance);
[email protected]051236f2010-03-12 22:06:14142 return hr;
143}
144
[email protected]97965e12010-04-09 00:51:10145HRESULT 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]de5bc352010-04-16 01:38:48158 hr = CacheStream::BSCBFeedData(bscb, buffer, size_, clip_format, bscf,
159 eof_);
[email protected]97965e12010-04-09 00:51:10160 GlobalUnlock(memory);
161 }
162
163 size_ = 0;
164 cache_.Release();
165 return hr;
[email protected]051236f2010-03-12 22:06:14166}
167
[email protected]97965e12010-04-09 00:51:10168// Scan the buffer or OptIn URL list and decide if the renderer is
[email protected]37a3a4b2010-04-14 22:37:40169// to be switched. Last chance means there's no more data.
170void SniffData::DetermineRendererType(bool last_chance) {
[email protected]97965e12010-04-09 00:51:10171 if (is_undetermined()) {
[email protected]37a3a4b2010-04-14 22:37:40172 if (last_chance)
173 renderer_type_ = OTHER;
[email protected]d8e13512010-09-22 17:02:58174 if (IsChrome(RendererTypeForUrl(url_.c_str()))) {
[email protected]97965e12010-04-09 00:51:10175 renderer_type_ = CHROME;
176 } else {
[email protected]97965e12010-04-09 00:51:10177 if (is_cache_valid() && cache_) {
178 HGLOBAL memory = NULL;
179 GetHGlobalFromStream(cache_, &memory);
[email protected]f0600f1b2010-06-08 22:40:35180 const char* buffer = reinterpret_cast<const char*>(GlobalLock(memory));
[email protected]97965e12010-04-09 00:51:10181
182 std::wstring html_contents;
183 // TODO(joshia): detect and handle different content encodings
184 if (buffer && size_) {
[email protected]f0600f1b2010-06-08 22:40:35185 UTF8ToWide(buffer, std::min(size_, kMaxSniffSize), &html_contents);
[email protected]97965e12010-04-09 00:51:10186 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]ffec6bf2010-04-09 23:53:26199 DLOG(INFO) << __FUNCTION__ << "Url: " << url_ <<
[email protected]d3451d832010-10-01 11:17:37200 base::StringPrintf("Renderer type: %s",
201 renderer_type_ == CHROME ? "CHROME" : "OTHER");
[email protected]97965e12010-04-09 00:51:10202 }
[email protected]051236f2010-03-12 22:06:14203}
204
[email protected]97965e12010-04-09 00:51:10205/////////////////////////////////////////////////////////////////////
206
[email protected]74e9983a2010-05-14 02:27:34207BSCBStorageBind::BSCBStorageBind() : clip_format_(CF_NULL) {
208}
209
210BSCBStorageBind::~BSCBStorageBind() {
[email protected]77d7aee2010-05-14 20:31:55211 std::for_each(saved_progress_.begin(), saved_progress_.end(),
212 utils::DeleteObject());
[email protected]74e9983a2010-05-14 02:27:34213}
214
[email protected]70277f62010-04-15 01:39:26215HRESULT BSCBStorageBind::Initialize(IMoniker* moniker, IBindCtx* bind_ctx) {
[email protected]d3451d832010-10-01 11:17:37216 DLOG(INFO) << __FUNCTION__ << me() << base::StringPrintf(" tid=%i",
[email protected]051236f2010-03-12 22:06:14217 PlatformThread::CurrentId());
218
[email protected]040b800f2010-05-05 23:46:30219 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]97965e12010-04-09 00:51:10226 if (FAILED(hr)) {
227 NOTREACHED() << __FUNCTION__ << me() << "AttachToBind error: " << hr;
228 return hr;
[email protected]051236f2010-03-12 22:06:14229 }
230
[email protected]97965e12010-04-09 00:51:10231 if (!delegate()) {
232 NOTREACHED() << __FUNCTION__ << me() << "No existing callback: " << hr;
233 return E_FAIL;
234 }
[email protected]051236f2010-03-12 22:06:14235
[email protected]051236f2010-03-12 22:06:14236 return hr;
237}
238
[email protected]97965e12010-04-09 00:51:10239STDMETHODIMP BSCBStorageBind::OnProgress(ULONG progress, ULONG progress_max,
240 ULONG status_code, LPCWSTR status_text) {
[email protected]d3451d832010-10-01 11:17:37241 DLOG(INFO) << __FUNCTION__ << me() << base::StringPrintf(
242 " status=%i tid=%i %ls", status_code, PlatformThread::CurrentId(),
243 status_text);
[email protected]e7dd21542010-05-07 21:59:44244 // 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]97965e12010-04-09 00:51:10248
249 HRESULT hr = S_OK;
[email protected]b31b9ff2010-05-07 18:29:24250
251 // Remember the last redirected URL in case we get switched into
252 // chrome frame
253 if (status_code == BINDSTATUS_REDIRECTING) {
[email protected]77d7aee2010-05-14 20:31:55254 ScopedComPtr<BindContextInfo> info;
255 BindContextInfo::FromBindContext(bind_ctx_, info.Receive());
[email protected]b31b9ff2010-05-07 18:29:24256 DCHECK(info);
257 if (info)
258 info->set_url(status_text);
259 }
260
261 if (ShouldCacheProgress(status_code)) {
[email protected]74e9983a2010-05-14 02:27:34262 saved_progress_.push_back(new Progress(progress, progress_max, status_code,
263 status_text));
[email protected]97965e12010-04-09 00:51:10264 } 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.
273STDMETHODIMP BSCBStorageBind::OnDataAvailable(DWORD flags, DWORD size,
274 FORMATETC* format_etc,
275 STGMEDIUM* stgmed) {
[email protected]d3451d832010-10-01 11:17:37276 DLOG(INFO) << __FUNCTION__ << base::StringPrintf(" tid=%i",
[email protected]051236f2010-03-12 22:06:14277 PlatformThread::CurrentId());
[email protected]e7dd21542010-05-07 21:59:44278 // 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]3ee61122010-04-14 00:35:52282 // Do not touch anything other than text/html.
[email protected]3ee61122010-04-14 00:35:52283 bool is_interesting = (format_etc && stgmed && stgmed->pstm &&
[email protected]2b3102be2010-04-21 20:07:35284 stgmed->tymed == TYMED_ISTREAM &&
285 IsTextHtmlClipFormat(format_etc->cfFormat));
[email protected]3ee61122010-04-14 00:35:52286
287 if (!is_interesting) {
288 // Play back report progress so far.
289 MayPlayBack(flags);
[email protected]97965e12010-04-09 00:51:10290 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]051236f2010-03-12 22:06:14315 return hr;
316}
317
[email protected]97965e12010-04-09 00:51:10318STDMETHODIMP BSCBStorageBind::OnStopBinding(HRESULT hresult, LPCWSTR error) {
[email protected]d3451d832010-10-01 11:17:37319 DLOG(INFO) << __FUNCTION__ << base::StringPrintf(" tid=%i",
[email protected]97965e12010-04-09 00:51:10320 PlatformThread::CurrentId());
[email protected]e7dd21542010-05-07 21:59:44321 // 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]97965e12010-04-09 00:51:10326 HRESULT hr = MayPlayBack(BSCF_LASTDATANOTIFICATION);
[email protected]ffec6bf2010-04-09 23:53:26327 hr = CallbackImpl::OnStopBinding(hresult, error);
328 ReleaseBind();
329 return hr;
[email protected]051236f2010-03-12 22:06:14330}
331
[email protected]97965e12010-04-09 00:51:10332// 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.
341HRESULT BSCBStorageBind::MayPlayBack(DWORD flags) {
342 // Force renderer type determination if not already done since
343 // we want to play back data now.
[email protected]37a3a4b2010-04-14 22:37:40344 data_sniffer_.DetermineRendererType(true);
[email protected]97965e12010-04-09 00:51:10345 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]77d7aee2010-05-14 20:31:55358 for (ProgressVector::iterator i = saved_progress_.begin();
[email protected]74e9983a2010-05-14 02:27:34359 i != saved_progress_.end(); i++) {
360 Progress* p = (*i);
[email protected]77d7aee2010-05-14 20:31:55361 // 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]97965e12010-04-09 00:51:10369 }
370 saved_progress_.clear();
371 }
372 }
373
374 if (data_sniffer_.is_cache_valid()) {
[email protected]3ee61122010-04-14 00:35:52375 if (data_sniffer_.is_chrome()) {
[email protected]77d7aee2010-05-14 20:31:55376 ScopedComPtr<BindContextInfo> info;
377 BindContextInfo::FromBindContext(bind_ctx_, info.Receive());
[email protected]70277f62010-04-15 01:39:26378 DCHECK(info);
379 if (info) {
380 info->SetToSwitch(data_sniffer_.cache_);
[email protected]3ee61122010-04-14 00:35:52381 }
[email protected]3ee61122010-04-14 00:35:52382 }
383
[email protected]97965e12010-04-09 00:51:10384 hr = data_sniffer_.DrainCache(delegate(),
385 flags | BSCF_FIRSTDATANOTIFICATION, clip_format_);
[email protected]c8c547e2010-04-12 20:40:15386 DLOG_IF(WARNING, INET_E_TERMINATED_BIND != hr) << __FUNCTION__ <<
387 " mshtml OnDataAvailable returned: " << std::hex << hr;
[email protected]97965e12010-04-09 00:51:10388 }
389
390 return hr;
[email protected]2b3102be2010-04-21 20:07:35391}
392
[email protected]b31b9ff2010-05-07 18:29:24393// 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.
408bool 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