blob: 1f39a08c0df296d3d0c962b5ca9e6b6f3a8c4403 [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]97965e12010-04-09 00:51:1012#include "base/utf_string_conversions.h"
[email protected]051236f2010-03-12 22:06:1413
[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]f1917ed2010-05-13 20:21:0878 if (!cache_.get())
79 return E_FAIL;
80
[email protected]97965e12010-04-09 00:51:1081 // Default to E_PENDING to signal that this is a partial data.
[email protected]de5bc352010-04-16 01:38:4882 HRESULT hr = eof_ ? S_FALSE : E_PENDING;
[email protected]97965e12010-04-09 00:51:1083 if (position_ < size_) {
84 *read = std::min(size_ - position_, size_t(cb));
[email protected]f1917ed2010-05-13 20:21:0885 memcpy(pv, cache_ .get() + position_, *read);
[email protected]97965e12010-04-09 00:51:1086 position_ += *read;
87 hr = S_OK;
[email protected]051236f2010-03-12 22:06:1488 }
[email protected]051236f2010-03-12 22:06:1489
90 return hr;
91}
92
[email protected]97965e12010-04-09 00:51:1093
94/////////////////////////////////////////////////////////////////////
95
[email protected]040b800f2010-05-05 23:46:3096HRESULT SniffData::InitializeCache(const std::wstring& url) {
[email protected]97965e12010-04-09 00:51:1097 url_ = url;
98 renderer_type_ = UNDETERMINED;
99
[email protected]040b800f2010-05-05 23:46:30100 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]b31b9ff2010-05-07 18:29:24105 if (SUCCEEDED(hr)) {
106 ULARGE_INTEGER size = {0};
107 cache_->SetSize(size);
108 } else {
109 DLOG(ERROR) << "CreateStreamOnHGlobal failed: " << hr;
110 }
111
[email protected]040b800f2010-05-05 23:46:30112 return hr;
[email protected]051236f2010-03-12 22:06:14113}
114
[email protected]97965e12010-04-09 00:51:10115HRESULT SniffData::ReadIntoCache(IStream* stream, bool force_determination) {
116 if (!stream) {
117 NOTREACHED();
118 return E_INVALIDARG;
[email protected]051236f2010-03-12 22:06:14119 }
[email protected]051236f2010-03-12 22:06:14120
121 HRESULT hr = S_OK;
[email protected]97965e12010-04-09 00:51:10122 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]fb716b302010-03-26 02:45:20131 }
132
[email protected]97965e12010-04-09 00:51:10133 if ((S_FALSE == hr) || !read)
134 break;
[email protected]051236f2010-03-12 22:06:14135 }
136
[email protected]37a3a4b2010-04-14 22:37:40137 bool last_chance = force_determination || (size() >= kMaxSniffSize);
[email protected]de5bc352010-04-16 01:38:48138 eof_ = force_determination;
[email protected]37a3a4b2010-04-14 22:37:40139 DetermineRendererType(last_chance);
[email protected]051236f2010-03-12 22:06:14140 return hr;
141}
142
[email protected]97965e12010-04-09 00:51:10143HRESULT 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]de5bc352010-04-16 01:38:48156 hr = CacheStream::BSCBFeedData(bscb, buffer, size_, clip_format, bscf,
157 eof_);
[email protected]97965e12010-04-09 00:51:10158 GlobalUnlock(memory);
159 }
160
161 size_ = 0;
162 cache_.Release();
163 return hr;
[email protected]051236f2010-03-12 22:06:14164}
165
[email protected]97965e12010-04-09 00:51:10166// Scan the buffer or OptIn URL list and decide if the renderer is
[email protected]37a3a4b2010-04-14 22:37:40167// to be switched. Last chance means there's no more data.
168void SniffData::DetermineRendererType(bool last_chance) {
[email protected]97965e12010-04-09 00:51:10169 if (is_undetermined()) {
[email protected]37a3a4b2010-04-14 22:37:40170 if (last_chance)
171 renderer_type_ = OTHER;
[email protected]97965e12010-04-09 00:51:10172 if (IsOptInUrl(url_.c_str())) {
173 renderer_type_ = CHROME;
174 } else {
[email protected]97965e12010-04-09 00:51:10175 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]ffec6bf2010-04-09 23:53:26197 DLOG(INFO) << __FUNCTION__ << "Url: " << url_ <<
198 StringPrintf("Renderer type: %s",
199 renderer_type_ == CHROME ? "CHROME" : "OTHER");
[email protected]97965e12010-04-09 00:51:10200 }
[email protected]051236f2010-03-12 22:06:14201}
202
[email protected]97965e12010-04-09 00:51:10203/////////////////////////////////////////////////////////////////////
204
[email protected]70277f62010-04-15 01:39:26205HRESULT BSCBStorageBind::Initialize(IMoniker* moniker, IBindCtx* bind_ctx) {
[email protected]051236f2010-03-12 22:06:14206 DLOG(INFO) << __FUNCTION__ << me() << StringPrintf(" tid=%i",
207 PlatformThread::CurrentId());
208
[email protected]040b800f2010-05-05 23:46:30209 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]97965e12010-04-09 00:51:10216 if (FAILED(hr)) {
217 NOTREACHED() << __FUNCTION__ << me() << "AttachToBind error: " << hr;
218 return hr;
[email protected]051236f2010-03-12 22:06:14219 }
220
[email protected]97965e12010-04-09 00:51:10221 if (!delegate()) {
222 NOTREACHED() << __FUNCTION__ << me() << "No existing callback: " << hr;
223 return E_FAIL;
224 }
[email protected]051236f2010-03-12 22:06:14225
[email protected]051236f2010-03-12 22:06:14226 return hr;
227}
228
[email protected]97965e12010-04-09 00:51:10229STDMETHODIMP 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]e7dd21542010-05-07 21:59:44233 // 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]97965e12010-04-09 00:51:10237
238 HRESULT hr = S_OK;
[email protected]b31b9ff2010-05-07 18:29:24239
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]97965e12010-04-09 00:51:10251 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.
263STDMETHODIMP BSCBStorageBind::OnDataAvailable(DWORD flags, DWORD size,
264 FORMATETC* format_etc,
265 STGMEDIUM* stgmed) {
266 DLOG(INFO) << __FUNCTION__ << StringPrintf(" tid=%i",
[email protected]051236f2010-03-12 22:06:14267 PlatformThread::CurrentId());
[email protected]e7dd21542010-05-07 21:59:44268 // 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]3ee61122010-04-14 00:35:52272 // Do not touch anything other than text/html.
[email protected]3ee61122010-04-14 00:35:52273 bool is_interesting = (format_etc && stgmed && stgmed->pstm &&
[email protected]2b3102be2010-04-21 20:07:35274 stgmed->tymed == TYMED_ISTREAM &&
275 IsTextHtmlClipFormat(format_etc->cfFormat));
[email protected]3ee61122010-04-14 00:35:52276
277 if (!is_interesting) {
278 // Play back report progress so far.
279 MayPlayBack(flags);
[email protected]97965e12010-04-09 00:51:10280 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]051236f2010-03-12 22:06:14305 return hr;
306}
307
[email protected]97965e12010-04-09 00:51:10308STDMETHODIMP BSCBStorageBind::OnStopBinding(HRESULT hresult, LPCWSTR error) {
309 DLOG(INFO) << __FUNCTION__ << StringPrintf(" tid=%i",
310 PlatformThread::CurrentId());
[email protected]e7dd21542010-05-07 21:59:44311 // 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]97965e12010-04-09 00:51:10316 HRESULT hr = MayPlayBack(BSCF_LASTDATANOTIFICATION);
[email protected]ffec6bf2010-04-09 23:53:26317 hr = CallbackImpl::OnStopBinding(hresult, error);
318 ReleaseBind();
319 return hr;
[email protected]051236f2010-03-12 22:06:14320}
321
[email protected]97965e12010-04-09 00:51:10322// 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.
331HRESULT BSCBStorageBind::MayPlayBack(DWORD flags) {
332 // Force renderer type determination if not already done since
333 // we want to play back data now.
[email protected]37a3a4b2010-04-14 22:37:40334 data_sniffer_.DetermineRendererType(true);
[email protected]97965e12010-04-09 00:51:10335 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]3ee61122010-04-14 00:35:52360 if (data_sniffer_.is_chrome()) {
[email protected]70277f62010-04-15 01:39:26361 scoped_refptr<BindContextInfo> info =
362 BindContextInfo::FromBindContext(bind_ctx_);
363 DCHECK(info);
364 if (info) {
365 info->SetToSwitch(data_sniffer_.cache_);
[email protected]3ee61122010-04-14 00:35:52366 }
[email protected]3ee61122010-04-14 00:35:52367 }
368
[email protected]97965e12010-04-09 00:51:10369 hr = data_sniffer_.DrainCache(delegate(),
370 flags | BSCF_FIRSTDATANOTIFICATION, clip_format_);
[email protected]c8c547e2010-04-12 20:40:15371 DLOG_IF(WARNING, INET_E_TERMINATED_BIND != hr) << __FUNCTION__ <<
372 " mshtml OnDataAvailable returned: " << std::hex << hr;
[email protected]97965e12010-04-09 00:51:10373 }
374
375 return hr;
[email protected]2b3102be2010-04-21 20:07:35376}
377
[email protected]b31b9ff2010-05-07 18:29:24378// 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.
393bool 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