OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2009 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 #ifndef CHROME_FRAME_URLMON_URL_REQUEST_H_ |
| 6 #define CHROME_FRAME_URLMON_URL_REQUEST_H_ |
| 7 |
| 8 #include <urlmon.h> |
| 9 #include <atlbase.h> |
| 10 #include <atlcom.h> |
| 11 |
| 12 #include <algorithm> |
| 13 |
| 14 #include "base/lock.h" |
| 15 #include "base/platform_thread.h" |
| 16 #include "base/scoped_comptr_win.h" |
| 17 #include "chrome_frame/plugin_url_request.h" |
| 18 |
| 19 #include "net/base/net_errors.h" |
| 20 #include "net/base/upload_data.h" |
| 21 |
| 22 class UrlmonUrlRequest |
| 23 : public CComObjectRootEx<CComSingleThreadModel>, |
| 24 public PluginUrlRequest, |
| 25 public IServiceProviderImpl<UrlmonUrlRequest>, |
| 26 public IBindStatusCallback, |
| 27 public IHttpNegotiate, |
| 28 public IAuthenticate, |
| 29 public IHttpSecurity { |
| 30 public: |
| 31 UrlmonUrlRequest(); |
| 32 ~UrlmonUrlRequest(); |
| 33 |
| 34 BEGIN_COM_MAP(UrlmonUrlRequest) |
| 35 COM_INTERFACE_ENTRY(IHttpNegotiate) |
| 36 COM_INTERFACE_ENTRY(IServiceProvider) |
| 37 COM_INTERFACE_ENTRY(IBindStatusCallback) |
| 38 COM_INTERFACE_ENTRY(IWindowForBindingUI) |
| 39 COM_INTERFACE_ENTRY(IAuthenticate) |
| 40 COM_INTERFACE_ENTRY(IHttpSecurity) |
| 41 END_COM_MAP() |
| 42 |
| 43 BEGIN_SERVICE_MAP(UrlmonUrlRequest) |
| 44 SERVICE_ENTRY(IID_IHttpNegotiate); |
| 45 END_SERVICE_MAP() |
| 46 |
| 47 // PluginUrlRequest implementation |
| 48 virtual bool Start(); |
| 49 virtual void Stop(); |
| 50 virtual bool Read(int bytes_to_read); |
| 51 |
| 52 // IBindStatusCallback implementation |
| 53 STDMETHOD(OnStartBinding)(DWORD reserved, IBinding* binding); |
| 54 STDMETHOD(GetPriority)(LONG* priority); |
| 55 STDMETHOD(OnLowResource)(DWORD reserved); |
| 56 STDMETHOD(OnProgress)(ULONG progress, ULONG max_progress, |
| 57 ULONG status_code, LPCWSTR status_text); |
| 58 STDMETHOD(OnStopBinding)(HRESULT result, LPCWSTR error); |
| 59 STDMETHOD(GetBindInfo)(DWORD* bind_flags, BINDINFO* bind_info); |
| 60 STDMETHOD(OnDataAvailable)(DWORD flags, DWORD size, FORMATETC* formatetc, |
| 61 STGMEDIUM* storage); |
| 62 STDMETHOD(OnObjectAvailable)(REFIID iid, IUnknown* object); |
| 63 |
| 64 // IHttpNegotiate implementation |
| 65 STDMETHOD(BeginningTransaction)(const wchar_t* url, |
| 66 const wchar_t* current_headers, DWORD reserved, |
| 67 wchar_t** additional_headers); |
| 68 STDMETHOD(OnResponse)(DWORD dwResponseCode, const wchar_t* response_headers, |
| 69 const wchar_t* request_headers, wchar_t** additional_headers); |
| 70 |
| 71 // IWindowForBindingUI implementation. This interface is used typically to |
| 72 // query the window handle which URLMON uses as the parent of error dialogs. |
| 73 STDMETHOD(GetWindow)(REFGUID guid_reason, HWND* parent_window); |
| 74 |
| 75 // IAuthenticate implementation. Used to return the parent window for the |
| 76 // dialog displayed by IE for authenticating with a proxy. |
| 77 STDMETHOD(Authenticate)(HWND* parent_window, LPWSTR* user_name, |
| 78 LPWSTR* password); |
| 79 |
| 80 // IHttpSecurity implementation. |
| 81 STDMETHOD(OnSecurityProblem)(DWORD problem); |
| 82 |
| 83 HRESULT ConnectToExistingMoniker(IMoniker* moniker, IBindCtx* context, |
| 84 const std::wstring& url); |
| 85 |
| 86 void set_parent_window(HWND parent_window) { |
| 87 parent_window_ = parent_window; |
| 88 } |
| 89 |
| 90 protected: |
| 91 static const size_t kCopyChunkSize = 32 * 1024; |
| 92 |
| 93 // A fake stream class to make it easier to copy received data using |
| 94 // IStream::CopyTo instead of allocating temporary buffers and keeping |
| 95 // track of data copied so far. |
| 96 class SendStream |
| 97 : public CComObjectRoot, |
| 98 public IStream { |
| 99 public: |
| 100 SendStream() { |
| 101 } |
| 102 |
| 103 BEGIN_COM_MAP(SendStream) |
| 104 COM_INTERFACE_ENTRY(IStream) |
| 105 COM_INTERFACE_ENTRY(ISequentialStream) |
| 106 END_COM_MAP() |
| 107 |
| 108 void Initialize(UrlmonUrlRequest* request) { |
| 109 request_ = request; |
| 110 } |
| 111 |
| 112 STDMETHOD(Read)(void* pv, ULONG cb, ULONG* read) { |
| 113 DCHECK(false) << __FUNCTION__; |
| 114 return E_NOTIMPL; |
| 115 } |
| 116 |
| 117 STDMETHOD(Write)(const void * buffer, ULONG size, ULONG* size_written) { |
| 118 DCHECK(request_); |
| 119 int size_to_write = static_cast<int>( |
| 120 std::min(static_cast<ULONG>(MAXINT), size)); |
| 121 request_->OnReadComplete(buffer, size_to_write); |
| 122 if (size_written) |
| 123 *size_written = size_to_write; |
| 124 return S_OK; |
| 125 } |
| 126 |
| 127 STDMETHOD(Seek)(LARGE_INTEGER move, DWORD origin, ULARGE_INTEGER* new_pos) { |
| 128 DCHECK(false) << __FUNCTION__; |
| 129 return E_NOTIMPL; |
| 130 } |
| 131 |
| 132 STDMETHOD(SetSize)(ULARGE_INTEGER new_size) { |
| 133 DCHECK(false) << __FUNCTION__; |
| 134 return E_NOTIMPL; |
| 135 } |
| 136 |
| 137 STDMETHOD(CopyTo)(IStream* stream, ULARGE_INTEGER cb, ULARGE_INTEGER* read, |
| 138 ULARGE_INTEGER* written) { |
| 139 DCHECK(false) << __FUNCTION__; |
| 140 return E_NOTIMPL; |
| 141 } |
| 142 |
| 143 STDMETHOD(Commit)(DWORD flags) { |
| 144 DCHECK(false) << __FUNCTION__; |
| 145 return E_NOTIMPL; |
| 146 } |
| 147 |
| 148 STDMETHOD(Revert)() { |
| 149 DCHECK(false) << __FUNCTION__; |
| 150 return E_NOTIMPL; |
| 151 } |
| 152 |
| 153 STDMETHOD(LockRegion)(ULARGE_INTEGER offset, ULARGE_INTEGER cb, |
| 154 DWORD type) { |
| 155 DCHECK(false) << __FUNCTION__; |
| 156 return E_NOTIMPL; |
| 157 } |
| 158 |
| 159 STDMETHOD(UnlockRegion)(ULARGE_INTEGER offset, ULARGE_INTEGER cb, |
| 160 DWORD type) { |
| 161 DCHECK(false) << __FUNCTION__; |
| 162 return E_NOTIMPL; |
| 163 } |
| 164 |
| 165 STDMETHOD(Stat)(STATSTG *pstatstg, DWORD grfStatFlag) { |
| 166 return E_NOTIMPL; |
| 167 } |
| 168 |
| 169 STDMETHOD(Clone)(IStream** stream) { |
| 170 DCHECK(false) << __FUNCTION__; |
| 171 return E_NOTIMPL; |
| 172 } |
| 173 |
| 174 protected: |
| 175 scoped_refptr<UrlmonUrlRequest> request_; |
| 176 DISALLOW_COPY_AND_ASSIGN(SendStream); |
| 177 }; |
| 178 |
| 179 // Manage data caching. Note: this class supports cache |
| 180 // size less than 2GB |
| 181 class Cache { |
| 182 public: |
| 183 bool Create(); |
| 184 |
| 185 // Adds data to the end of the cache. |
| 186 bool Append(IStream* source, size_t* bytes_copied); |
| 187 |
| 188 // Reads from the cache. |
| 189 bool Read(IStream* dest, size_t size, size_t* bytes_copied); |
| 190 |
| 191 size_t Size(); |
| 192 size_t CurrentPos(); |
| 193 size_t SizeRemaining(); |
| 194 void Clear(); |
| 195 bool is_valid() const { |
| 196 return (stream_ != NULL); |
| 197 } |
| 198 |
| 199 protected: |
| 200 ScopedComPtr<IStream> stream_; |
| 201 char read_buffer_[kCopyChunkSize]; |
| 202 }; |
| 203 |
| 204 HRESULT StartAsyncDownload(); |
| 205 void EndRequest(); |
| 206 |
| 207 int GetHttpResponseStatus() const; |
| 208 |
| 209 static net::Error HresultToNetError(HRESULT hr); |
| 210 |
| 211 private: |
| 212 std::wstring redirect_url_; |
| 213 int redirect_status_; |
| 214 ScopedComPtr<IBinding> binding_; |
| 215 ScopedComPtr<IMoniker> moniker_; |
| 216 ScopedComPtr<IBindCtx> bind_context_; |
| 217 Cache cached_data_; |
| 218 size_t pending_read_size_; |
| 219 URLRequestStatus status_; |
| 220 |
| 221 uint64 post_data_len_; |
| 222 |
| 223 PlatformThreadId thread_; |
| 224 bool is_request_started_; |
| 225 static int instance_count_; |
| 226 HWND parent_window_; |
| 227 DISALLOW_COPY_AND_ASSIGN(UrlmonUrlRequest); |
| 228 }; |
| 229 |
| 230 #endif // CHROME_FRAME_URLMON_URL_REQUEST_H_ |
| 231 |
OLD | NEW |