OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef CHROME_FRAME_URLMON_URL_REQUEST_PRIVATE_H_ |
| 6 #define CHROME_FRAME_URLMON_URL_REQUEST_PRIVATE_H_ |
| 7 |
| 8 #include <atlbase.h> |
| 9 #include <atlcom.h> |
| 10 #include <string> |
| 11 #include <vector> |
| 12 |
| 13 #include "net/base/net_errors.h" |
| 14 #include "net/http/http_response_headers.h" |
| 15 #include "net/url_request/url_request_status.h" |
| 16 |
| 17 class UrlmonUrlRequest |
| 18 : public CComObjectRootEx<CComMultiThreadModel>, |
| 19 public PluginUrlRequest, |
| 20 public IServiceProviderImpl<UrlmonUrlRequest>, |
| 21 public IBindStatusCallback, |
| 22 public IHttpNegotiate, |
| 23 public IAuthenticate, |
| 24 public IHttpSecurity { |
| 25 public: |
| 26 static int instance_count_; |
| 27 virtual bool Start(); |
| 28 virtual void Stop(); |
| 29 virtual bool Read(int bytes_to_read); |
| 30 |
| 31 // Special function needed by ActiveDocument::Load() |
| 32 HRESULT ConnectToExistingMoniker(IMoniker* moniker, IBindCtx* context, |
| 33 const std::wstring& url); |
| 34 |
| 35 // Used from "OnDownloadRequestInHost". |
| 36 void StealMoniker(IMoniker** moniker); |
| 37 |
| 38 // Parent Window for UrlMon error dialogs |
| 39 void set_parent_window(HWND parent_window) { |
| 40 parent_window_ = parent_window; |
| 41 } |
| 42 |
| 43 protected: |
| 44 UrlmonUrlRequest(); |
| 45 ~UrlmonUrlRequest(); |
| 46 |
| 47 BEGIN_COM_MAP(UrlmonUrlRequest) |
| 48 COM_INTERFACE_ENTRY(IHttpNegotiate) |
| 49 COM_INTERFACE_ENTRY(IServiceProvider) |
| 50 COM_INTERFACE_ENTRY(IBindStatusCallback) |
| 51 COM_INTERFACE_ENTRY(IWindowForBindingUI) |
| 52 COM_INTERFACE_ENTRY(IAuthenticate) |
| 53 COM_INTERFACE_ENTRY(IHttpSecurity) |
| 54 END_COM_MAP() |
| 55 |
| 56 BEGIN_SERVICE_MAP(UrlmonUrlRequest) |
| 57 SERVICE_ENTRY(IID_IHttpNegotiate); |
| 58 END_SERVICE_MAP() |
| 59 |
| 60 |
| 61 // IBindStatusCallback implementation |
| 62 STDMETHOD(OnStartBinding)(DWORD reserved, IBinding* binding); |
| 63 STDMETHOD(GetPriority)(LONG* priority); |
| 64 STDMETHOD(OnLowResource)(DWORD reserved); |
| 65 STDMETHOD(OnProgress)(ULONG progress, ULONG max_progress, |
| 66 ULONG status_code, LPCWSTR status_text); |
| 67 STDMETHOD(OnStopBinding)(HRESULT result, LPCWSTR error); |
| 68 STDMETHOD(GetBindInfo)(DWORD* bind_flags, BINDINFO* bind_info); |
| 69 STDMETHOD(OnDataAvailable)(DWORD flags, DWORD size, FORMATETC* formatetc, |
| 70 STGMEDIUM* storage); |
| 71 STDMETHOD(OnObjectAvailable)(REFIID iid, IUnknown* object); |
| 72 |
| 73 // IHttpNegotiate implementation |
| 74 STDMETHOD(BeginningTransaction)(const wchar_t* url, |
| 75 const wchar_t* current_headers, DWORD reserved, |
| 76 wchar_t** additional_headers); |
| 77 STDMETHOD(OnResponse)(DWORD dwResponseCode, const wchar_t* response_headers, |
| 78 const wchar_t* request_headers, wchar_t** additional_headers); |
| 79 |
| 80 // IWindowForBindingUI implementation. This interface is used typically to |
| 81 // query the window handle which URLMON uses as the parent of error dialogs. |
| 82 STDMETHOD(GetWindow)(REFGUID guid_reason, HWND* parent_window); |
| 83 |
| 84 // IAuthenticate implementation. Used to return the parent window for the |
| 85 // dialog displayed by IE for authenticating with a proxy. |
| 86 STDMETHOD(Authenticate)(HWND* parent_window, LPWSTR* user_name, |
| 87 LPWSTR* password); |
| 88 |
| 89 // IHttpSecurity implementation. |
| 90 STDMETHOD(OnSecurityProblem)(DWORD problem); |
| 91 |
| 92 protected: |
| 93 void ReleaseBindings(); |
| 94 |
| 95 static const size_t kCopyChunkSize = 32 * 1024; |
| 96 // A fake stream class to make it easier to copy received data using |
| 97 // IStream::CopyTo instead of allocating temporary buffers and keeping |
| 98 // track of data copied so far. |
| 99 class SendStream : public CComObjectRoot, public IStream { |
| 100 public: |
| 101 SendStream() { |
| 102 } |
| 103 |
| 104 BEGIN_COM_MAP(SendStream) |
| 105 COM_INTERFACE_ENTRY(IStream) |
| 106 COM_INTERFACE_ENTRY(ISequentialStream) |
| 107 END_COM_MAP() |
| 108 |
| 109 void Initialize(UrlmonUrlRequest* request) { |
| 110 request_ = request; |
| 111 } |
| 112 |
| 113 STDMETHOD(Write)(const void * buffer, ULONG size, ULONG* size_written); |
| 114 STDMETHOD(Read)(void* pv, ULONG cb, ULONG* read) { |
| 115 DCHECK(false) << __FUNCTION__; |
| 116 return E_NOTIMPL; |
| 117 } |
| 118 |
| 119 STDMETHOD(Seek)(LARGE_INTEGER move, DWORD origin, ULARGE_INTEGER* new_pos) { |
| 120 DCHECK(false) << __FUNCTION__; |
| 121 return E_NOTIMPL; |
| 122 } |
| 123 |
| 124 STDMETHOD(SetSize)(ULARGE_INTEGER new_size) { |
| 125 DCHECK(false) << __FUNCTION__; |
| 126 return E_NOTIMPL; |
| 127 } |
| 128 |
| 129 STDMETHOD(CopyTo)(IStream* stream, ULARGE_INTEGER cb, ULARGE_INTEGER* read, |
| 130 ULARGE_INTEGER* written) { |
| 131 DCHECK(false) << __FUNCTION__; |
| 132 return E_NOTIMPL; |
| 133 } |
| 134 |
| 135 STDMETHOD(Commit)(DWORD flags) { |
| 136 DCHECK(false) << __FUNCTION__; |
| 137 return E_NOTIMPL; |
| 138 } |
| 139 |
| 140 STDMETHOD(Revert)() { |
| 141 DCHECK(false) << __FUNCTION__; |
| 142 return E_NOTIMPL; |
| 143 } |
| 144 |
| 145 STDMETHOD(LockRegion)(ULARGE_INTEGER offset, ULARGE_INTEGER cb, |
| 146 DWORD type) { |
| 147 DCHECK(false) << __FUNCTION__; |
| 148 return E_NOTIMPL; |
| 149 } |
| 150 |
| 151 STDMETHOD(UnlockRegion)(ULARGE_INTEGER offset, ULARGE_INTEGER cb, |
| 152 DWORD type) { |
| 153 DCHECK(false) << __FUNCTION__; |
| 154 return E_NOTIMPL; |
| 155 } |
| 156 |
| 157 STDMETHOD(Stat)(STATSTG *pstatstg, DWORD grfStatFlag) { |
| 158 return E_NOTIMPL; |
| 159 } |
| 160 |
| 161 STDMETHOD(Clone)(IStream** stream) { |
| 162 DCHECK(false) << __FUNCTION__; |
| 163 return E_NOTIMPL; |
| 164 } |
| 165 |
| 166 protected: |
| 167 scoped_refptr<UrlmonUrlRequest> request_; |
| 168 DISALLOW_COPY_AND_ASSIGN(SendStream); |
| 169 }; |
| 170 |
| 171 // Manage data caching. Note: this class supports cache |
| 172 // size less than 2GB |
| 173 class Cache { |
| 174 public: |
| 175 // Adds data to the end of the cache. |
| 176 bool Append(IStream* source, size_t* bytes_copied); |
| 177 |
| 178 // Reads from the cache. |
| 179 bool Read(IStream* dest, size_t size, size_t* bytes_copied); |
| 180 |
| 181 // Returns the size of the cache. |
| 182 size_t Size() const; |
| 183 |
| 184 // Returns true if the cache has valid data. |
| 185 bool is_valid() const { |
| 186 return Size() != 0; |
| 187 } |
| 188 |
| 189 protected: |
| 190 std::vector<byte> cache_; |
| 191 char read_buffer_[kCopyChunkSize]; |
| 192 }; |
| 193 |
| 194 HRESULT StartAsyncDownload(); |
| 195 void NotifyDelegateAndDie(); |
| 196 int GetHttpResponseStatus() const; |
| 197 std::string GetHttpHeaders() const; |
| 198 static net::Error HresultToNetError(HRESULT hr); |
| 199 |
| 200 private: |
| 201 // This class simplifies tracking the progress of operation. We have 3 main |
| 202 // states: DONE, WORKING and ABORTING. |
| 203 // When in [DONE] or [ABORTING] state, there is additional information |
| 204 // about the result of operation. |
| 205 // Start(), SetRedirected(), Cancel() and Done() methods trigger the state |
| 206 // change. See comments bellow. |
| 207 class Status { |
| 208 public: |
| 209 enum State {DONE, ABORTING, WORKING}; |
| 210 struct Redirection { |
| 211 Redirection() : http_code(0) { } |
| 212 int http_code; |
| 213 std::string utf8_url; |
| 214 }; |
| 215 |
| 216 Status() : state_(Status::DONE) { |
| 217 } |
| 218 |
| 219 State get_state() const { |
| 220 return state_; |
| 221 } |
| 222 |
| 223 // Switch from [DONE] to [WORKING]. |
| 224 void Start() { |
| 225 DCHECK_EQ(state_, DONE); |
| 226 state_ = WORKING; |
| 227 } |
| 228 |
| 229 // Save redirection information and switch to [ABORTING] state. |
| 230 // Assumes binding_->Abort() will be called! |
| 231 void SetRedirected(int http_code, const std::string& utf8_url) { |
| 232 DCHECK_EQ(state_, WORKING); |
| 233 DCHECK_EQ(result_.status(), URLRequestStatus::SUCCESS); |
| 234 redirect_.utf8_url = utf8_url; |
| 235 |
| 236 // At times we receive invalid redirect codes like 0, 200, etc. We |
| 237 // default to 302 in this case. |
| 238 redirect_.http_code = http_code; |
| 239 if (!net::HttpResponseHeaders::IsRedirectResponseCode(http_code)) |
| 240 redirect_.http_code = 302; |
| 241 |
| 242 state_ = ABORTING; |
| 243 } |
| 244 |
| 245 // Set the result as URLRequestStatus::CANCELED. |
| 246 // Switch to [ABORTING] state (if not already in that state). |
| 247 void Cancel() { |
| 248 if (state_ == DONE) |
| 249 return; |
| 250 |
| 251 if (state_ == WORKING) { |
| 252 state_ = ABORTING; |
| 253 } else { |
| 254 // state_ == ABORTING |
| 255 redirect_.http_code = 0; |
| 256 redirect_.utf8_url.clear(); |
| 257 } |
| 258 |
| 259 set_result(URLRequestStatus::CANCELED, 0); |
| 260 } |
| 261 |
| 262 void Done() { |
| 263 state_ = DONE; |
| 264 } |
| 265 |
| 266 bool was_redirected() const { |
| 267 return redirect_.http_code != 0; |
| 268 } |
| 269 |
| 270 const Redirection& get_redirection() const { |
| 271 return redirect_; |
| 272 } |
| 273 |
| 274 const URLRequestStatus& get_result() const { |
| 275 return result_; |
| 276 } |
| 277 |
| 278 void set_result(URLRequestStatus::Status status, int os_error) { |
| 279 result_.set_status(status); |
| 280 result_.set_os_error(os_error); |
| 281 } |
| 282 |
| 283 void set_result(HRESULT hr) { |
| 284 result_.set_status(FAILED(hr)? URLRequestStatus::FAILED: |
| 285 URLRequestStatus::SUCCESS); |
| 286 result_.set_os_error(HresultToNetError(hr)); |
| 287 } |
| 288 |
| 289 private: |
| 290 Redirection redirect_; |
| 291 State state_; |
| 292 URLRequestStatus result_; |
| 293 }; |
| 294 |
| 295 Status status_; |
| 296 ScopedComPtr<IBinding> binding_; |
| 297 ScopedComPtr<IMoniker> moniker_; |
| 298 ScopedComPtr<IBindCtx> bind_context_; |
| 299 Cache cached_data_; |
| 300 size_t pending_read_size_; |
| 301 PlatformThreadId thread_; |
| 302 HWND parent_window_; |
| 303 |
| 304 DISALLOW_COPY_AND_ASSIGN(UrlmonUrlRequest); |
| 305 }; |
| 306 |
| 307 #endif // CHROME_FRAME_URLMON_URL_REQUEST_PRIVATE_H_ |
OLD | NEW |