blob: 68cf1755d074e85f7ce514b57ad6acb7d3b11db3 [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]97965e12010-04-09 00:51:1014#include "chrome_frame/urlmon_moniker.h"
15#include "chrome_tab.h" // NOLINT
[email protected]051236f2010-03-12 22:06:1416
[email protected]051236f2010-03-12 22:06:1417
[email protected]97965e12010-04-09 00:51:1018// A helper to given feed data to the specified |bscb| using
19// CacheStream instance.
20HRESULT 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]051236f2010-03-12 22:06:1426 }
27
[email protected]97965e12010-04-09 00:51:1028 // 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]051236f2010-03-12 22:06:1432 if (FAILED(hr)) {
33 NOTREACHED();
34 return hr;
35 }
36
[email protected]97965e12010-04-09 00:51:1037 cache_stream->AddRef();
38 cache_stream->Initialize(data, size);
[email protected]051236f2010-03-12 22:06:1439
[email protected]97965e12010-04-09 00:51:1040 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]051236f2010-03-12 22:06:1450}
51
[email protected]97965e12010-04-09 00:51:1052void CacheStream::Initialize(const char* cache, size_t size) {
53 cache_ = cache;
54 size_ = size;
55 position_ = 0;
[email protected]051236f2010-03-12 22:06:1456}
57
[email protected]97965e12010-04-09 00:51:1058// 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.
61STDMETHODIMP CacheStream::Read(void* pv, ULONG cb, ULONG* read) {
62 if (!pv || !read)
63 return E_INVALIDARG;
[email protected]051236f2010-03-12 22:06:1464
[email protected]97965e12010-04-09 00:51:1065 // 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]051236f2010-03-12 22:06:1472 }
[email protected]051236f2010-03-12 22:06:1473
74 return hr;
75}
76
[email protected]97965e12010-04-09 00:51:1077
78/////////////////////////////////////////////////////////////////////
79
80bool SniffData::InitializeCache(const std::wstring& url) {
81 url_ = url;
82 renderer_type_ = UNDETERMINED;
83
[email protected]3ee61122010-04-14 00:35:5284 HRESULT hr = CreateStreamOnHGlobal(NULL, TRUE, cache_.Receive());
[email protected]97965e12010-04-09 00:51:1085 if (FAILED(hr)) {
[email protected]97965e12010-04-09 00:51:1086 NOTREACHED();
87 return false;
88 }
89
90 return true;
[email protected]051236f2010-03-12 22:06:1491}
92
[email protected]97965e12010-04-09 00:51:1093HRESULT SniffData::ReadIntoCache(IStream* stream, bool force_determination) {
94 if (!stream) {
95 NOTREACHED();
96 return E_INVALIDARG;
[email protected]051236f2010-03-12 22:06:1497 }
[email protected]051236f2010-03-12 22:06:1498
99 HRESULT hr = S_OK;
[email protected]97965e12010-04-09 00:51:10100 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]fb716b302010-03-26 02:45:20109 }
110
[email protected]97965e12010-04-09 00:51:10111 if ((S_FALSE == hr) || !read)
112 break;
[email protected]051236f2010-03-12 22:06:14113 }
114
[email protected]37a3a4b2010-04-14 22:37:40115 bool last_chance = force_determination || (size() >= kMaxSniffSize);
116 DetermineRendererType(last_chance);
[email protected]051236f2010-03-12 22:06:14117 return hr;
118}
119
[email protected]97965e12010-04-09 00:51:10120HRESULT 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]051236f2010-03-12 22:06:14140}
141
[email protected]97965e12010-04-09 00:51:10142// Scan the buffer or OptIn URL list and decide if the renderer is
[email protected]37a3a4b2010-04-14 22:37:40143// to be switched. Last chance means there's no more data.
144void SniffData::DetermineRendererType(bool last_chance) {
[email protected]97965e12010-04-09 00:51:10145 if (is_undetermined()) {
[email protected]37a3a4b2010-04-14 22:37:40146 if (last_chance)
147 renderer_type_ = OTHER;
[email protected]97965e12010-04-09 00:51:10148 if (IsOptInUrl(url_.c_str())) {
149 renderer_type_ = CHROME;
150 } else {
[email protected]97965e12010-04-09 00:51:10151 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]ffec6bf2010-04-09 23:53:26173 DLOG(INFO) << __FUNCTION__ << "Url: " << url_ <<
174 StringPrintf("Renderer type: %s",
175 renderer_type_ == CHROME ? "CHROME" : "OTHER");
[email protected]97965e12010-04-09 00:51:10176 }
[email protected]051236f2010-03-12 22:06:14177}
178
[email protected]97965e12010-04-09 00:51:10179/////////////////////////////////////////////////////////////////////
180
[email protected]3ee61122010-04-14 00:35:52181HRESULT BSCBStorageBind::Initialize(IMoniker* moniker, IBindCtx* bind_ctx,
182 bool no_cache) {
[email protected]051236f2010-03-12 22:06:14183 DLOG(INFO) << __FUNCTION__ << me() << StringPrintf(" tid=%i",
184 PlatformThread::CurrentId());
185
[email protected]97965e12010-04-09 00:51:10186 HRESULT hr = AttachToBind(bind_ctx);
187 if (FAILED(hr)) {
188 NOTREACHED() << __FUNCTION__ << me() << "AttachToBind error: " << hr;
189 return hr;
[email protected]051236f2010-03-12 22:06:14190 }
191
[email protected]97965e12010-04-09 00:51:10192 if (!delegate()) {
193 NOTREACHED() << __FUNCTION__ << me() << "No existing callback: " << hr;
194 return E_FAIL;
195 }
[email protected]051236f2010-03-12 22:06:14196
[email protected]97965e12010-04-09 00:51:10197 std::wstring url = GetActualUrlFromMoniker(moniker, bind_ctx,
198 std::wstring());
199 data_sniffer_.InitializeCache(url);
[email protected]3ee61122010-04-14 00:35:52200 no_cache_ = no_cache;
[email protected]051236f2010-03-12 22:06:14201 return hr;
202}
203
[email protected]97965e12010-04-09 00:51:10204STDMETHODIMP 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.
223STDMETHODIMP BSCBStorageBind::OnDataAvailable(DWORD flags, DWORD size,
224 FORMATETC* format_etc,
225 STGMEDIUM* stgmed) {
226 DLOG(INFO) << __FUNCTION__ << StringPrintf(" tid=%i",
[email protected]051236f2010-03-12 22:06:14227 PlatformThread::CurrentId());
[email protected]97965e12010-04-09 00:51:10228
[email protected]3ee61122010-04-14 00:35:52229 // 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]97965e12010-04-09 00:51:10237 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]051236f2010-03-12 22:06:14262 return hr;
263}
264
[email protected]97965e12010-04-09 00:51:10265STDMETHODIMP BSCBStorageBind::OnStopBinding(HRESULT hresult, LPCWSTR error) {
266 DLOG(INFO) << __FUNCTION__ << StringPrintf(" tid=%i",
267 PlatformThread::CurrentId());
268 HRESULT hr = MayPlayBack(BSCF_LASTDATANOTIFICATION);
[email protected]ffec6bf2010-04-09 23:53:26269 hr = CallbackImpl::OnStopBinding(hresult, error);
270 ReleaseBind();
271 return hr;
[email protected]051236f2010-03-12 22:06:14272}
273
[email protected]97965e12010-04-09 00:51:10274// 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.
283HRESULT BSCBStorageBind::MayPlayBack(DWORD flags) {
284 // Force renderer type determination if not already done since
285 // we want to play back data now.
[email protected]37a3a4b2010-04-14 22:37:40286 data_sniffer_.DetermineRendererType(true);
[email protected]97965e12010-04-09 00:51:10287 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]3ee61122010-04-14 00:35:52312 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]97965e12010-04-09 00:51:10327 hr = data_sniffer_.DrainCache(delegate(),
328 flags | BSCF_FIRSTDATANOTIFICATION, clip_format_);
[email protected]c8c547e2010-04-12 20:40:15329 DLOG_IF(WARNING, INET_E_TERMINATED_BIND != hr) << __FUNCTION__ <<
330 " mshtml OnDataAvailable returned: " << std::hex << hr;
[email protected]97965e12010-04-09 00:51:10331 }
332
333 return hr;
[email protected]c8c547e2010-04-12 20:40:15334}