blob: 8fb6ebd5519f384bfbeaf9e3f10d10c8c35e6f1d [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]97965e12010-04-09 00:51:1015#include "chrome_frame/urlmon_moniker.h"
16#include "chrome_tab.h" // NOLINT
[email protected]051236f2010-03-12 22:06:1417
[email protected]051236f2010-03-12 22:06:1418
[email protected]97965e12010-04-09 00:51:1019// A helper to given feed data to the specified |bscb| using
20// CacheStream instance.
21HRESULT CacheStream::BSCBFeedData(IBindStatusCallback* bscb, const char* data,
22 size_t size, CLIPFORMAT clip_format,
[email protected]de5bc352010-04-16 01:38:4823 size_t flags, bool eof) {
[email protected]97965e12010-04-09 00:51:1024 if (!bscb) {
25 NOTREACHED() << "invalid IBindStatusCallback";
26 return E_INVALIDARG;
[email protected]051236f2010-03-12 22:06:1427 }
28
[email protected]97965e12010-04-09 00:51:1029 // 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]051236f2010-03-12 22:06:1433 if (FAILED(hr)) {
34 NOTREACHED();
35 return hr;
36 }
37
[email protected]97965e12010-04-09 00:51:1038 cache_stream->AddRef();
[email protected]de5bc352010-04-16 01:38:4839 cache_stream->Initialize(data, size, eof);
[email protected]051236f2010-03-12 22:06:1440
[email protected]97965e12010-04-09 00:51:1041 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]051236f2010-03-12 22:06:1451}
52
[email protected]de5bc352010-04-16 01:38:4853void CacheStream::Initialize(const char* cache, size_t size, bool eof) {
[email protected]97965e12010-04-09 00:51:1054 cache_ = cache;
55 size_ = size;
56 position_ = 0;
[email protected]de5bc352010-04-16 01:38:4857 eof_ = eof;
[email protected]051236f2010-03-12 22:06:1458}
59
[email protected]97965e12010-04-09 00:51:1060// 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.
63STDMETHODIMP CacheStream::Read(void* pv, ULONG cb, ULONG* read) {
64 if (!pv || !read)
65 return E_INVALIDARG;
[email protected]051236f2010-03-12 22:06:1466
[email protected]97965e12010-04-09 00:51:1067 // Default to E_PENDING to signal that this is a partial data.
[email protected]de5bc352010-04-16 01:38:4868 HRESULT hr = eof_ ? S_FALSE : E_PENDING;
[email protected]97965e12010-04-09 00:51:1069 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]051236f2010-03-12 22:06:1474 }
[email protected]051236f2010-03-12 22:06:1475
76 return hr;
77}
78
[email protected]97965e12010-04-09 00:51:1079
80/////////////////////////////////////////////////////////////////////
81
[email protected]040b800f2010-05-05 23:46:3082HRESULT SniffData::InitializeCache(const std::wstring& url) {
[email protected]97965e12010-04-09 00:51:1083 url_ = url;
84 renderer_type_ = UNDETERMINED;
85
[email protected]040b800f2010-05-05 23:46:3086 const int kInitialSize = 4 * 1024; // 4K
87 HGLOBAL mem = GlobalAlloc(0, kInitialSize);
88 DCHECK(mem) << "GlobalAlloc failed: " << GetLastError();
89
90 HRESULT hr = CreateStreamOnHGlobal(mem, TRUE, cache_.Receive());
[email protected]b31b9ff2010-05-07 18:29:2491 if (SUCCEEDED(hr)) {
92 ULARGE_INTEGER size = {0};
93 cache_->SetSize(size);
94 } else {
95 DLOG(ERROR) << "CreateStreamOnHGlobal failed: " << hr;
96 }
97
[email protected]040b800f2010-05-05 23:46:3098 return hr;
[email protected]051236f2010-03-12 22:06:1499}
100
[email protected]97965e12010-04-09 00:51:10101HRESULT SniffData::ReadIntoCache(IStream* stream, bool force_determination) {
102 if (!stream) {
103 NOTREACHED();
104 return E_INVALIDARG;
[email protected]051236f2010-03-12 22:06:14105 }
[email protected]051236f2010-03-12 22:06:14106
107 HRESULT hr = S_OK;
[email protected]97965e12010-04-09 00:51:10108 while (SUCCEEDED(hr)) {
109 const size_t kChunkSize = 4 * 1024;
110 char buffer[kChunkSize];
111 DWORD read = 0;
112 hr = stream->Read(buffer, sizeof(buffer), &read);
113 if (read) {
114 DWORD written = 0;
115 cache_->Write(buffer, read, &written);
116 size_ += written;
[email protected]fb716b302010-03-26 02:45:20117 }
118
[email protected]97965e12010-04-09 00:51:10119 if ((S_FALSE == hr) || !read)
120 break;
[email protected]051236f2010-03-12 22:06:14121 }
122
[email protected]37a3a4b2010-04-14 22:37:40123 bool last_chance = force_determination || (size() >= kMaxSniffSize);
[email protected]de5bc352010-04-16 01:38:48124 eof_ = force_determination;
[email protected]37a3a4b2010-04-14 22:37:40125 DetermineRendererType(last_chance);
[email protected]051236f2010-03-12 22:06:14126 return hr;
127}
128
[email protected]97965e12010-04-09 00:51:10129HRESULT SniffData::DrainCache(IBindStatusCallback* bscb, DWORD bscf,
130 CLIPFORMAT clip_format) {
131 if (!is_cache_valid()) {
132 return S_OK;
133 }
134
135 // Ideally we could just use the cache_ IStream implementation but
136 // can't use it here since we have to return E_PENDING for the
137 // last call
138 HGLOBAL memory = NULL;
139 HRESULT hr = GetHGlobalFromStream(cache_, &memory);
140 if (SUCCEEDED(hr) && memory) {
141 char* buffer = reinterpret_cast<char*>(GlobalLock(memory));
[email protected]de5bc352010-04-16 01:38:48142 hr = CacheStream::BSCBFeedData(bscb, buffer, size_, clip_format, bscf,
143 eof_);
[email protected]97965e12010-04-09 00:51:10144 GlobalUnlock(memory);
145 }
146
147 size_ = 0;
148 cache_.Release();
149 return hr;
[email protected]051236f2010-03-12 22:06:14150}
151
[email protected]97965e12010-04-09 00:51:10152// Scan the buffer or OptIn URL list and decide if the renderer is
[email protected]37a3a4b2010-04-14 22:37:40153// to be switched. Last chance means there's no more data.
154void SniffData::DetermineRendererType(bool last_chance) {
[email protected]97965e12010-04-09 00:51:10155 if (is_undetermined()) {
[email protected]37a3a4b2010-04-14 22:37:40156 if (last_chance)
157 renderer_type_ = OTHER;
[email protected]97965e12010-04-09 00:51:10158 if (IsOptInUrl(url_.c_str())) {
159 renderer_type_ = CHROME;
160 } else {
[email protected]97965e12010-04-09 00:51:10161 if (is_cache_valid() && cache_) {
162 HGLOBAL memory = NULL;
163 GetHGlobalFromStream(cache_, &memory);
164 char* buffer = reinterpret_cast<char*>(GlobalLock(memory));
165
166 std::wstring html_contents;
167 // TODO(joshia): detect and handle different content encodings
168 if (buffer && size_) {
169 UTF8ToWide(buffer, size_, &html_contents);
170 GlobalUnlock(memory);
171 }
172
173 // Note that document_contents_ may have NULL characters in it. While
174 // browsers may handle this properly, we don't and will stop scanning
175 // for the XUACompat content value if we encounter one.
176 std::wstring xua_compat_content;
177 UtilGetXUACompatContentValue(html_contents, &xua_compat_content);
178 if (StrStrI(xua_compat_content.c_str(), kChromeContentPrefix)) {
179 renderer_type_ = CHROME;
180 }
181 }
182 }
[email protected]ffec6bf2010-04-09 23:53:26183 DLOG(INFO) << __FUNCTION__ << "Url: " << url_ <<
184 StringPrintf("Renderer type: %s",
185 renderer_type_ == CHROME ? "CHROME" : "OTHER");
[email protected]97965e12010-04-09 00:51:10186 }
[email protected]051236f2010-03-12 22:06:14187}
188
[email protected]97965e12010-04-09 00:51:10189/////////////////////////////////////////////////////////////////////
190
[email protected]70277f62010-04-15 01:39:26191HRESULT BSCBStorageBind::Initialize(IMoniker* moniker, IBindCtx* bind_ctx) {
[email protected]051236f2010-03-12 22:06:14192 DLOG(INFO) << __FUNCTION__ << me() << StringPrintf(" tid=%i",
193 PlatformThread::CurrentId());
194
[email protected]040b800f2010-05-05 23:46:30195 std::wstring url = GetActualUrlFromMoniker(moniker, bind_ctx,
196 std::wstring());
197 HRESULT hr = data_sniffer_.InitializeCache(url);
198 if (FAILED(hr))
199 return hr;
200
201 hr = AttachToBind(bind_ctx);
[email protected]97965e12010-04-09 00:51:10202 if (FAILED(hr)) {
203 NOTREACHED() << __FUNCTION__ << me() << "AttachToBind error: " << hr;
204 return hr;
[email protected]051236f2010-03-12 22:06:14205 }
206
[email protected]97965e12010-04-09 00:51:10207 if (!delegate()) {
208 NOTREACHED() << __FUNCTION__ << me() << "No existing callback: " << hr;
209 return E_FAIL;
210 }
[email protected]051236f2010-03-12 22:06:14211
[email protected]051236f2010-03-12 22:06:14212 return hr;
213}
214
[email protected]97965e12010-04-09 00:51:10215STDMETHODIMP BSCBStorageBind::OnProgress(ULONG progress, ULONG progress_max,
216 ULONG status_code, LPCWSTR status_text) {
217 DLOG(INFO) << __FUNCTION__ << me() << StringPrintf(" status=%i tid=%i %ls",
218 status_code, PlatformThread::CurrentId(), status_text);
219
220 HRESULT hr = S_OK;
[email protected]b31b9ff2010-05-07 18:29:24221
222 // Remember the last redirected URL in case we get switched into
223 // chrome frame
224 if (status_code == BINDSTATUS_REDIRECTING) {
225 scoped_refptr<BindContextInfo> info =
226 BindContextInfo::FromBindContext(bind_ctx_);
227 DCHECK(info);
228 if (info)
229 info->set_url(status_text);
230 }
231
232 if (ShouldCacheProgress(status_code)) {
[email protected]97965e12010-04-09 00:51:10233 Progress new_progress = { progress, progress_max, status_code,
234 status_text ? status_text : std::wstring() };
235 saved_progress_.push_back(new_progress);
236 } else {
237 hr = CallbackImpl::OnProgress(progress, progress_max, status_code,
238 status_text);
239 }
240
241 return hr;
242}
243
244// Refer to urlmon_moniker.h for explanation of how things work.
245STDMETHODIMP BSCBStorageBind::OnDataAvailable(DWORD flags, DWORD size,
246 FORMATETC* format_etc,
247 STGMEDIUM* stgmed) {
248 DLOG(INFO) << __FUNCTION__ << StringPrintf(" tid=%i",
[email protected]051236f2010-03-12 22:06:14249 PlatformThread::CurrentId());
[email protected]97965e12010-04-09 00:51:10250
[email protected]3ee61122010-04-14 00:35:52251 // Do not touch anything other than text/html.
[email protected]3ee61122010-04-14 00:35:52252 bool is_interesting = (format_etc && stgmed && stgmed->pstm &&
[email protected]2b3102be2010-04-21 20:07:35253 stgmed->tymed == TYMED_ISTREAM &&
254 IsTextHtmlClipFormat(format_etc->cfFormat));
[email protected]3ee61122010-04-14 00:35:52255
256 if (!is_interesting) {
257 // Play back report progress so far.
258 MayPlayBack(flags);
[email protected]97965e12010-04-09 00:51:10259 return CallbackImpl::OnDataAvailable(flags, size, format_etc, stgmed);
260 }
261
262 HRESULT hr = S_OK;
263 if (!clip_format_)
264 clip_format_ = format_etc->cfFormat;
265
266 if (data_sniffer_.is_undetermined()) {
267 bool force_determination = !!(flags &
268 (BSCF_LASTDATANOTIFICATION | BSCF_DATAFULLYAVAILABLE));
269 hr = data_sniffer_.ReadIntoCache(stgmed->pstm, force_determination);
270 // If we don't have sufficient data to determine renderer type
271 // wait for the next data notification.
272 if (data_sniffer_.is_undetermined())
273 return S_OK;
274 }
275
276 DCHECK(!data_sniffer_.is_undetermined());
277
278 if (data_sniffer_.is_cache_valid()) {
279 hr = MayPlayBack(flags);
280 DCHECK(!data_sniffer_.is_cache_valid());
281 } else {
282 hr = CallbackImpl::OnDataAvailable(flags, size, format_etc, stgmed);
283 }
[email protected]051236f2010-03-12 22:06:14284 return hr;
285}
286
[email protected]97965e12010-04-09 00:51:10287STDMETHODIMP BSCBStorageBind::OnStopBinding(HRESULT hresult, LPCWSTR error) {
288 DLOG(INFO) << __FUNCTION__ << StringPrintf(" tid=%i",
289 PlatformThread::CurrentId());
290 HRESULT hr = MayPlayBack(BSCF_LASTDATANOTIFICATION);
[email protected]ffec6bf2010-04-09 23:53:26291 hr = CallbackImpl::OnStopBinding(hresult, error);
292 ReleaseBind();
293 return hr;
[email protected]051236f2010-03-12 22:06:14294}
295
[email protected]97965e12010-04-09 00:51:10296// Play back the cached data to the delegate. Normally this would happen
297// when we have read enough data to determine the renderer. In this case
298// we first play back the data from the cache and then go into a 'pass
299// through' mode. In some cases we may end up getting OnStopBinding
300// before we get a chance to determine. Also it's possible that the
301// BindToStorage call will return before OnStopBinding is sent. Hence
302// This is called from 3 places and it's important to maintain the
303// exact sequence of calls.
304// Once the data is played back, calling this again is a no op.
305HRESULT BSCBStorageBind::MayPlayBack(DWORD flags) {
306 // Force renderer type determination if not already done since
307 // we want to play back data now.
[email protected]37a3a4b2010-04-14 22:37:40308 data_sniffer_.DetermineRendererType(true);
[email protected]97965e12010-04-09 00:51:10309 DCHECK(!data_sniffer_.is_undetermined());
310
311 HRESULT hr = S_OK;
312 if (data_sniffer_.is_chrome()) {
313 // Remember clip format. If we are switching to chrome, then in order
314 // to make mshtml return INET_E_TERMINATED_BIND and reissue navigation
315 // with the same bind context, we have to return a mime type that is
316 // special cased by mshtml.
317 static const CLIPFORMAT kMagicClipFormat =
318 RegisterClipboardFormat(CFSTR_MIME_MPEG);
319 clip_format_ = kMagicClipFormat;
320 } else {
321 if (!saved_progress_.empty()) {
322 for (std::vector<Progress>::iterator i = saved_progress_.begin();
323 i != saved_progress_.end(); i++) {
324 const wchar_t* status_text = i->status_text_.empty() ?
325 NULL : i->status_text_.c_str();
326 CallbackImpl::OnProgress(i->progress_, i->progress_max_,
327 i->status_code_, status_text);
328 }
329 saved_progress_.clear();
330 }
331 }
332
333 if (data_sniffer_.is_cache_valid()) {
[email protected]3ee61122010-04-14 00:35:52334 if (data_sniffer_.is_chrome()) {
[email protected]70277f62010-04-15 01:39:26335 scoped_refptr<BindContextInfo> info =
336 BindContextInfo::FromBindContext(bind_ctx_);
337 DCHECK(info);
338 if (info) {
339 info->SetToSwitch(data_sniffer_.cache_);
[email protected]3ee61122010-04-14 00:35:52340 }
[email protected]3ee61122010-04-14 00:35:52341 }
342
[email protected]97965e12010-04-09 00:51:10343 hr = data_sniffer_.DrainCache(delegate(),
344 flags | BSCF_FIRSTDATANOTIFICATION, clip_format_);
[email protected]c8c547e2010-04-12 20:40:15345 DLOG_IF(WARNING, INET_E_TERMINATED_BIND != hr) << __FUNCTION__ <<
346 " mshtml OnDataAvailable returned: " << std::hex << hr;
[email protected]97965e12010-04-09 00:51:10347 }
348
349 return hr;
[email protected]2b3102be2010-04-21 20:07:35350}
351
[email protected]b31b9ff2010-05-07 18:29:24352// We cache and suppress sending progress notifications till
353// we get the first OnDataAvailable. This is to prevent
354// mshtml from making up its mind about the mime type.
355// However, this is the invasive part of the patch and
356// could trip other software that's due to mistimed progress
357// notifications. It is probably not a good idea to hide redirects
358// and some cookie notifications.
359//
360// We only need to suppress data notifications like
361// BINDSTATUS_MIMETYPEAVAILABLE,
362// BINDSTATUS_CACHEFILENAMEAVAILABLE etc.
363//
364// This is an atempt to reduce the exposure by starting to
365// cache only when we receive one of the interesting progress
366// notification.
367bool BSCBStorageBind::ShouldCacheProgress(unsigned long status_code) const {
368 // We need to cache progress notifications only if we haven't yet figured
369 // out which way the request is going.
370 if (data_sniffer_.is_undetermined()) {
371 // If we are already caching then continue.
372 if (!saved_progress_.empty())
373 return true;
374 // Start caching only if we see one of the interesting progress
375 // notifications.
376 switch (status_code) {
377 case BINDSTATUS_BEGINDOWNLOADDATA:
378 case BINDSTATUS_DOWNLOADINGDATA:
379 case BINDSTATUS_USINGCACHEDCOPY:
380 case BINDSTATUS_MIMETYPEAVAILABLE:
381 case BINDSTATUS_CACHEFILENAMEAVAILABLE:
382 case BINDSTATUS_SERVER_MIMETYPEAVAILABLE:
383 return true;
384 default:
385 break;
386 }
387 }
388
389 return false;
390}
391