blob: 851edee29eca10869a460095680bd8745fc35d1e [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
82bool SniffData::InitializeCache(const std::wstring& url) {
83 url_ = url;
84 renderer_type_ = UNDETERMINED;
85
[email protected]3ee61122010-04-14 00:35:5286 HRESULT hr = CreateStreamOnHGlobal(NULL, TRUE, cache_.Receive());
[email protected]97965e12010-04-09 00:51:1087 if (FAILED(hr)) {
[email protected]97965e12010-04-09 00:51:1088 NOTREACHED();
89 return false;
90 }
[email protected]97965e12010-04-09 00:51:1091 return true;
[email protected]051236f2010-03-12 22:06:1492}
93
[email protected]97965e12010-04-09 00:51:1094HRESULT SniffData::ReadIntoCache(IStream* stream, bool force_determination) {
95 if (!stream) {
96 NOTREACHED();
97 return E_INVALIDARG;
[email protected]051236f2010-03-12 22:06:1498 }
[email protected]051236f2010-03-12 22:06:1499
100 HRESULT hr = S_OK;
[email protected]97965e12010-04-09 00:51:10101 while (SUCCEEDED(hr)) {
102 const size_t kChunkSize = 4 * 1024;
103 char buffer[kChunkSize];
104 DWORD read = 0;
105 hr = stream->Read(buffer, sizeof(buffer), &read);
106 if (read) {
107 DWORD written = 0;
108 cache_->Write(buffer, read, &written);
109 size_ += written;
[email protected]fb716b302010-03-26 02:45:20110 }
111
[email protected]97965e12010-04-09 00:51:10112 if ((S_FALSE == hr) || !read)
113 break;
[email protected]051236f2010-03-12 22:06:14114 }
115
[email protected]37a3a4b2010-04-14 22:37:40116 bool last_chance = force_determination || (size() >= kMaxSniffSize);
[email protected]de5bc352010-04-16 01:38:48117 eof_ = force_determination;
[email protected]37a3a4b2010-04-14 22:37:40118 DetermineRendererType(last_chance);
[email protected]051236f2010-03-12 22:06:14119 return hr;
120}
121
[email protected]97965e12010-04-09 00:51:10122HRESULT 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));
[email protected]de5bc352010-04-16 01:38:48135 hr = CacheStream::BSCBFeedData(bscb, buffer, size_, clip_format, bscf,
136 eof_);
[email protected]97965e12010-04-09 00:51:10137 GlobalUnlock(memory);
138 }
139
140 size_ = 0;
141 cache_.Release();
142 return hr;
[email protected]051236f2010-03-12 22:06:14143}
144
[email protected]97965e12010-04-09 00:51:10145// Scan the buffer or OptIn URL list and decide if the renderer is
[email protected]37a3a4b2010-04-14 22:37:40146// to be switched. Last chance means there's no more data.
147void SniffData::DetermineRendererType(bool last_chance) {
[email protected]97965e12010-04-09 00:51:10148 if (is_undetermined()) {
[email protected]37a3a4b2010-04-14 22:37:40149 if (last_chance)
150 renderer_type_ = OTHER;
[email protected]97965e12010-04-09 00:51:10151 if (IsOptInUrl(url_.c_str())) {
152 renderer_type_ = CHROME;
153 } else {
[email protected]97965e12010-04-09 00:51:10154 if (is_cache_valid() && cache_) {
155 HGLOBAL memory = NULL;
156 GetHGlobalFromStream(cache_, &memory);
157 char* buffer = reinterpret_cast<char*>(GlobalLock(memory));
158
159 std::wstring html_contents;
160 // TODO(joshia): detect and handle different content encodings
161 if (buffer && size_) {
162 UTF8ToWide(buffer, size_, &html_contents);
163 GlobalUnlock(memory);
164 }
165
166 // Note that document_contents_ may have NULL characters in it. While
167 // browsers may handle this properly, we don't and will stop scanning
168 // for the XUACompat content value if we encounter one.
169 std::wstring xua_compat_content;
170 UtilGetXUACompatContentValue(html_contents, &xua_compat_content);
171 if (StrStrI(xua_compat_content.c_str(), kChromeContentPrefix)) {
172 renderer_type_ = CHROME;
173 }
174 }
175 }
[email protected]ffec6bf2010-04-09 23:53:26176 DLOG(INFO) << __FUNCTION__ << "Url: " << url_ <<
177 StringPrintf("Renderer type: %s",
178 renderer_type_ == CHROME ? "CHROME" : "OTHER");
[email protected]97965e12010-04-09 00:51:10179 }
[email protected]051236f2010-03-12 22:06:14180}
181
[email protected]97965e12010-04-09 00:51:10182/////////////////////////////////////////////////////////////////////
183
[email protected]70277f62010-04-15 01:39:26184HRESULT BSCBStorageBind::Initialize(IMoniker* moniker, IBindCtx* bind_ctx) {
[email protected]051236f2010-03-12 22:06:14185 DLOG(INFO) << __FUNCTION__ << me() << StringPrintf(" tid=%i",
186 PlatformThread::CurrentId());
187
[email protected]97965e12010-04-09 00:51:10188 HRESULT hr = AttachToBind(bind_ctx);
189 if (FAILED(hr)) {
190 NOTREACHED() << __FUNCTION__ << me() << "AttachToBind error: " << hr;
191 return hr;
[email protected]051236f2010-03-12 22:06:14192 }
193
[email protected]97965e12010-04-09 00:51:10194 if (!delegate()) {
195 NOTREACHED() << __FUNCTION__ << me() << "No existing callback: " << hr;
196 return E_FAIL;
197 }
[email protected]051236f2010-03-12 22:06:14198
[email protected]97965e12010-04-09 00:51:10199 std::wstring url = GetActualUrlFromMoniker(moniker, bind_ctx,
200 std::wstring());
201 data_sniffer_.InitializeCache(url);
[email protected]051236f2010-03-12 22:06:14202 return hr;
203}
204
[email protected]97965e12010-04-09 00:51:10205STDMETHODIMP 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);
[email protected]29c32f902010-04-20 23:27:29215 if (status_code == BINDSTATUS_REDIRECTING) {
216 scoped_refptr<BindContextInfo> info =
217 BindContextInfo::FromBindContext(bind_ctx_);
218 DCHECK(info);
219 info->set_url(status_text);
220 }
[email protected]97965e12010-04-09 00:51:10221 } else {
222 hr = CallbackImpl::OnProgress(progress, progress_max, status_code,
223 status_text);
224 }
225
226 return hr;
227}
228
229// Refer to urlmon_moniker.h for explanation of how things work.
230STDMETHODIMP BSCBStorageBind::OnDataAvailable(DWORD flags, DWORD size,
231 FORMATETC* format_etc,
232 STGMEDIUM* stgmed) {
233 DLOG(INFO) << __FUNCTION__ << StringPrintf(" tid=%i",
[email protected]051236f2010-03-12 22:06:14234 PlatformThread::CurrentId());
[email protected]97965e12010-04-09 00:51:10235
[email protected]3ee61122010-04-14 00:35:52236 // Do not touch anything other than text/html.
[email protected]3ee61122010-04-14 00:35:52237 bool is_interesting = (format_etc && stgmed && stgmed->pstm &&
[email protected]2b3102be2010-04-21 20:07:35238 stgmed->tymed == TYMED_ISTREAM &&
239 IsTextHtmlClipFormat(format_etc->cfFormat));
[email protected]3ee61122010-04-14 00:35:52240
241 if (!is_interesting) {
242 // Play back report progress so far.
243 MayPlayBack(flags);
[email protected]97965e12010-04-09 00:51:10244 return CallbackImpl::OnDataAvailable(flags, size, format_etc, stgmed);
245 }
246
247 HRESULT hr = S_OK;
248 if (!clip_format_)
249 clip_format_ = format_etc->cfFormat;
250
251 if (data_sniffer_.is_undetermined()) {
252 bool force_determination = !!(flags &
253 (BSCF_LASTDATANOTIFICATION | BSCF_DATAFULLYAVAILABLE));
254 hr = data_sniffer_.ReadIntoCache(stgmed->pstm, force_determination);
255 // If we don't have sufficient data to determine renderer type
256 // wait for the next data notification.
257 if (data_sniffer_.is_undetermined())
258 return S_OK;
259 }
260
261 DCHECK(!data_sniffer_.is_undetermined());
262
263 if (data_sniffer_.is_cache_valid()) {
264 hr = MayPlayBack(flags);
265 DCHECK(!data_sniffer_.is_cache_valid());
266 } else {
267 hr = CallbackImpl::OnDataAvailable(flags, size, format_etc, stgmed);
268 }
[email protected]051236f2010-03-12 22:06:14269 return hr;
270}
271
[email protected]97965e12010-04-09 00:51:10272STDMETHODIMP BSCBStorageBind::OnStopBinding(HRESULT hresult, LPCWSTR error) {
273 DLOG(INFO) << __FUNCTION__ << StringPrintf(" tid=%i",
274 PlatformThread::CurrentId());
275 HRESULT hr = MayPlayBack(BSCF_LASTDATANOTIFICATION);
[email protected]ffec6bf2010-04-09 23:53:26276 hr = CallbackImpl::OnStopBinding(hresult, error);
277 ReleaseBind();
278 return hr;
[email protected]051236f2010-03-12 22:06:14279}
280
[email protected]97965e12010-04-09 00:51:10281// Play back the cached data to the delegate. Normally this would happen
282// when we have read enough data to determine the renderer. In this case
283// we first play back the data from the cache and then go into a 'pass
284// through' mode. In some cases we may end up getting OnStopBinding
285// before we get a chance to determine. Also it's possible that the
286// BindToStorage call will return before OnStopBinding is sent. Hence
287// This is called from 3 places and it's important to maintain the
288// exact sequence of calls.
289// Once the data is played back, calling this again is a no op.
290HRESULT BSCBStorageBind::MayPlayBack(DWORD flags) {
291 // Force renderer type determination if not already done since
292 // we want to play back data now.
[email protected]37a3a4b2010-04-14 22:37:40293 data_sniffer_.DetermineRendererType(true);
[email protected]97965e12010-04-09 00:51:10294 DCHECK(!data_sniffer_.is_undetermined());
295
296 HRESULT hr = S_OK;
297 if (data_sniffer_.is_chrome()) {
298 // Remember clip format. If we are switching to chrome, then in order
299 // to make mshtml return INET_E_TERMINATED_BIND and reissue navigation
300 // with the same bind context, we have to return a mime type that is
301 // special cased by mshtml.
302 static const CLIPFORMAT kMagicClipFormat =
303 RegisterClipboardFormat(CFSTR_MIME_MPEG);
304 clip_format_ = kMagicClipFormat;
305 } else {
306 if (!saved_progress_.empty()) {
307 for (std::vector<Progress>::iterator i = saved_progress_.begin();
308 i != saved_progress_.end(); i++) {
309 const wchar_t* status_text = i->status_text_.empty() ?
310 NULL : i->status_text_.c_str();
311 CallbackImpl::OnProgress(i->progress_, i->progress_max_,
312 i->status_code_, status_text);
313 }
314 saved_progress_.clear();
315 }
316 }
317
318 if (data_sniffer_.is_cache_valid()) {
[email protected]3ee61122010-04-14 00:35:52319 if (data_sniffer_.is_chrome()) {
[email protected]70277f62010-04-15 01:39:26320 scoped_refptr<BindContextInfo> info =
321 BindContextInfo::FromBindContext(bind_ctx_);
322 DCHECK(info);
323 if (info) {
324 info->SetToSwitch(data_sniffer_.cache_);
[email protected]3ee61122010-04-14 00:35:52325 }
[email protected]3ee61122010-04-14 00:35:52326 }
327
[email protected]97965e12010-04-09 00:51:10328 hr = data_sniffer_.DrainCache(delegate(),
329 flags | BSCF_FIRSTDATANOTIFICATION, clip_format_);
[email protected]c8c547e2010-04-12 20:40:15330 DLOG_IF(WARNING, INET_E_TERMINATED_BIND != hr) << __FUNCTION__ <<
331 " mshtml OnDataAvailable returned: " << std::hex << hr;
[email protected]97965e12010-04-09 00:51:10332 }
333
334 return hr;
[email protected]2b3102be2010-04-21 20:07:35335}
336