OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome_frame/urlmon_bind_status_callback.h" | 5 #include "chrome_frame/urlmon_bind_status_callback.h" |
6 | 6 |
7 #include <mshtml.h> | 7 #include <mshtml.h> |
8 #include <shlguid.h> | 8 #include <shlguid.h> |
9 | 9 |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/string_util.h" | 11 #include "base/string_util.h" |
12 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
13 | 13 |
14 #include "chrome_frame/bind_context_info.h" | 14 #include "chrome_frame/bind_context_info.h" |
15 #include "chrome_frame/urlmon_moniker.h" | 15 #include "chrome_frame/urlmon_moniker.h" |
16 #include "chrome_tab.h" // NOLINT | 16 #include "chrome_tab.h" // NOLINT |
17 | 17 |
18 | 18 |
19 // A helper to given feed data to the specified |bscb| using | 19 // A helper to given feed data to the specified |bscb| using |
20 // CacheStream instance. | 20 // CacheStream instance. |
21 HRESULT CacheStream::BSCBFeedData(IBindStatusCallback* bscb, const char* data, | 21 HRESULT CacheStream::BSCBFeedData(IBindStatusCallback* bscb, const char* data, |
22 size_t size, CLIPFORMAT clip_format, | 22 size_t size, CLIPFORMAT clip_format, |
23 size_t flags) { | 23 size_t flags, bool eof) { |
24 if (!bscb) { | 24 if (!bscb) { |
25 NOTREACHED() << "invalid IBindStatusCallback"; | 25 NOTREACHED() << "invalid IBindStatusCallback"; |
26 return E_INVALIDARG; | 26 return E_INVALIDARG; |
27 } | 27 } |
28 | 28 |
29 // We can't use a CComObjectStackEx here since mshtml will hold | 29 // We can't use a CComObjectStackEx here since mshtml will hold |
30 // onto the stream pointer. | 30 // onto the stream pointer. |
31 CComObject<CacheStream>* cache_stream = NULL; | 31 CComObject<CacheStream>* cache_stream = NULL; |
32 HRESULT hr = CComObject<CacheStream>::CreateInstance(&cache_stream); | 32 HRESULT hr = CComObject<CacheStream>::CreateInstance(&cache_stream); |
33 if (FAILED(hr)) { | 33 if (FAILED(hr)) { |
34 NOTREACHED(); | 34 NOTREACHED(); |
35 return hr; | 35 return hr; |
36 } | 36 } |
37 | 37 |
38 cache_stream->AddRef(); | 38 cache_stream->AddRef(); |
39 cache_stream->Initialize(data, size); | 39 cache_stream->Initialize(data, size, eof); |
40 | 40 |
41 FORMATETC format_etc = { clip_format, NULL, DVASPECT_CONTENT, -1, | 41 FORMATETC format_etc = { clip_format, NULL, DVASPECT_CONTENT, -1, |
42 TYMED_ISTREAM }; | 42 TYMED_ISTREAM }; |
43 STGMEDIUM medium = {0}; | 43 STGMEDIUM medium = {0}; |
44 medium.tymed = TYMED_ISTREAM; | 44 medium.tymed = TYMED_ISTREAM; |
45 medium.pstm = cache_stream; | 45 medium.pstm = cache_stream; |
46 | 46 |
47 hr = bscb->OnDataAvailable(flags, size, &format_etc, &medium); | 47 hr = bscb->OnDataAvailable(flags, size, &format_etc, &medium); |
48 | 48 |
49 cache_stream->Release(); | 49 cache_stream->Release(); |
50 return hr; | 50 return hr; |
51 } | 51 } |
52 | 52 |
53 void CacheStream::Initialize(const char* cache, size_t size) { | 53 void CacheStream::Initialize(const char* cache, size_t size, bool eof) { |
54 cache_ = cache; | 54 cache_ = cache; |
55 size_ = size; | 55 size_ = size; |
56 position_ = 0; | 56 position_ = 0; |
| 57 eof_ = eof; |
57 } | 58 } |
58 | 59 |
59 // Read is the only call that we expect. Return E_PENDING if there | 60 // Read is the only call that we expect. Return E_PENDING if there |
60 // is no more data to serve. Otherwise this will result in a | 61 // is no more data to serve. Otherwise this will result in a |
61 // read with 0 bytes indicating that no more data is available. | 62 // read with 0 bytes indicating that no more data is available. |
62 STDMETHODIMP CacheStream::Read(void* pv, ULONG cb, ULONG* read) { | 63 STDMETHODIMP CacheStream::Read(void* pv, ULONG cb, ULONG* read) { |
63 if (!pv || !read) | 64 if (!pv || !read) |
64 return E_INVALIDARG; | 65 return E_INVALIDARG; |
65 | 66 |
66 // Default to E_PENDING to signal that this is a partial data. | 67 // Default to E_PENDING to signal that this is a partial data. |
67 HRESULT hr = E_PENDING; | 68 HRESULT hr = eof_ ? S_FALSE : E_PENDING; |
68 if (position_ < size_) { | 69 if (position_ < size_) { |
69 *read = std::min(size_ - position_, size_t(cb)); | 70 *read = std::min(size_ - position_, size_t(cb)); |
70 memcpy(pv, cache_ + position_, *read); | 71 memcpy(pv, cache_ + position_, *read); |
71 position_ += *read; | 72 position_ += *read; |
72 hr = S_OK; | 73 hr = S_OK; |
73 } | 74 } |
74 | 75 |
75 return hr; | 76 return hr; |
76 } | 77 } |
77 | 78 |
(...skipping 29 matching lines...) Expand all Loading... |
107 DWORD written = 0; | 108 DWORD written = 0; |
108 cache_->Write(buffer, read, &written); | 109 cache_->Write(buffer, read, &written); |
109 size_ += written; | 110 size_ += written; |
110 } | 111 } |
111 | 112 |
112 if ((S_FALSE == hr) || !read) | 113 if ((S_FALSE == hr) || !read) |
113 break; | 114 break; |
114 } | 115 } |
115 | 116 |
116 bool last_chance = force_determination || (size() >= kMaxSniffSize); | 117 bool last_chance = force_determination || (size() >= kMaxSniffSize); |
| 118 eof_ = force_determination; |
117 DetermineRendererType(last_chance); | 119 DetermineRendererType(last_chance); |
118 return hr; | 120 return hr; |
119 } | 121 } |
120 | 122 |
121 HRESULT SniffData::DrainCache(IBindStatusCallback* bscb, DWORD bscf, | 123 HRESULT SniffData::DrainCache(IBindStatusCallback* bscb, DWORD bscf, |
122 CLIPFORMAT clip_format) { | 124 CLIPFORMAT clip_format) { |
123 if (!is_cache_valid()) { | 125 if (!is_cache_valid()) { |
124 return S_OK; | 126 return S_OK; |
125 } | 127 } |
126 | 128 |
127 // Ideally we could just use the cache_ IStream implementation but | 129 // Ideally we could just use the cache_ IStream implementation but |
128 // can't use it here since we have to return E_PENDING for the | 130 // can't use it here since we have to return E_PENDING for the |
129 // last call | 131 // last call |
130 HGLOBAL memory = NULL; | 132 HGLOBAL memory = NULL; |
131 HRESULT hr = GetHGlobalFromStream(cache_, &memory); | 133 HRESULT hr = GetHGlobalFromStream(cache_, &memory); |
132 if (SUCCEEDED(hr) && memory) { | 134 if (SUCCEEDED(hr) && memory) { |
133 char* buffer = reinterpret_cast<char*>(GlobalLock(memory)); | 135 char* buffer = reinterpret_cast<char*>(GlobalLock(memory)); |
134 hr = CacheStream::BSCBFeedData(bscb, buffer, size_, clip_format, bscf); | 136 hr = CacheStream::BSCBFeedData(bscb, buffer, size_, clip_format, bscf, |
| 137 eof_); |
135 GlobalUnlock(memory); | 138 GlobalUnlock(memory); |
136 } | 139 } |
137 | 140 |
138 size_ = 0; | 141 size_ = 0; |
139 cache_.Release(); | 142 cache_.Release(); |
140 return hr; | 143 return hr; |
141 } | 144 } |
142 | 145 |
143 // Scan the buffer or OptIn URL list and decide if the renderer is | 146 // Scan the buffer or OptIn URL list and decide if the renderer is |
144 // to be switched. Last chance means there's no more data. | 147 // to be switched. Last chance means there's no more data. |
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
318 } | 321 } |
319 | 322 |
320 hr = data_sniffer_.DrainCache(delegate(), | 323 hr = data_sniffer_.DrainCache(delegate(), |
321 flags | BSCF_FIRSTDATANOTIFICATION, clip_format_); | 324 flags | BSCF_FIRSTDATANOTIFICATION, clip_format_); |
322 DLOG_IF(WARNING, INET_E_TERMINATED_BIND != hr) << __FUNCTION__ << | 325 DLOG_IF(WARNING, INET_E_TERMINATED_BIND != hr) << __FUNCTION__ << |
323 " mshtml OnDataAvailable returned: " << std::hex << hr; | 326 " mshtml OnDataAvailable returned: " << std::hex << hr; |
324 } | 327 } |
325 | 328 |
326 return hr; | 329 return hr; |
327 } | 330 } |
OLD | NEW |