| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2006-2008 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 NET_URL_REQUEST_URL_REQUEST_INET_JOB_H__ | |
| 6 #define NET_URL_REQUEST_URL_REQUEST_INET_JOB_H__ | |
| 7 | |
| 8 #include <windows.h> | |
| 9 #include <wininet.h> | |
| 10 | |
| 11 #include "base/lock.h" | |
| 12 #include "net/url_request/url_request.h" | |
| 13 #include "net/url_request/url_request_job.h" | |
| 14 | |
| 15 namespace net { | |
| 16 class AuthData; | |
| 17 } | |
| 18 | |
| 19 class MessageLoop; | |
| 20 | |
| 21 // For all WinInet-based URL requests | |
| 22 class URLRequestInetJob : public URLRequestJob { | |
| 23 public: | |
| 24 URLRequestInetJob(URLRequest* request); | |
| 25 | |
| 26 virtual void SetExtraRequestHeaders(const std::string& headers) { | |
| 27 extra_request_headers_ = headers; | |
| 28 } | |
| 29 | |
| 30 virtual void Kill(); | |
| 31 virtual bool ReadRawData(net::IOBuffer* buf, int buf_size, int *bytes_read); | |
| 32 | |
| 33 // URLRequestJob Authentication methods | |
| 34 virtual void SetAuth(const std::wstring& username, | |
| 35 const std::wstring& password); | |
| 36 virtual void CancelAuth(); | |
| 37 | |
| 38 // A structure holding the result and error code of an asynchronous IO. | |
| 39 // This is a copy of INTERNET_ASYNC_RESULT. | |
| 40 struct AsyncResult { | |
| 41 DWORD_PTR dwResult; | |
| 42 DWORD dwError; | |
| 43 }; | |
| 44 | |
| 45 // A virtual method to handle WinInet callbacks. If this class | |
| 46 // issues asynchronous IO, it will need to override this method | |
| 47 // to receive completions of those asynchronous IOs. The class | |
| 48 // must track whether it has an async IO outstanding, and if it | |
| 49 // does not it must call the base class' OnIOComplete. | |
| 50 virtual void OnIOComplete(const AsyncResult& result) = 0; | |
| 51 | |
| 52 // Used internally to setup the OnIOComplete call. Public because this | |
| 53 // is called from the Windows procedure, and we don't want to make it a | |
| 54 // friend so we can avoid the Windows headers for this header file. | |
| 55 void CallOnIOComplete(const AsyncResult& result); | |
| 56 | |
| 57 HINTERNET request_handle() const { return request_handle_; } | |
| 58 | |
| 59 protected: | |
| 60 virtual ~URLRequestInetJob(); | |
| 61 | |
| 62 // Called by this class and subclasses to send or resend this request. | |
| 63 virtual void SendRequest() = 0; | |
| 64 | |
| 65 // Calls InternetReadFile(Ex) depending on the derived class. | |
| 66 // Returns ERROR_SUCCESS on success, or else a standard Windows error code | |
| 67 // on failure (from GetLastError()). | |
| 68 virtual int CallInternetRead(char* dest, int dest_size, int *bytes_read) = 0; | |
| 69 | |
| 70 // After the base class calls CallInternetRead and the result is available, | |
| 71 // it will call this method to get the number of received bytes. | |
| 72 virtual bool GetReadBytes(const AsyncResult& result, int* bytes_read) = 0; | |
| 73 | |
| 74 // Called by this class and subclasses whenever a WinInet call fails. This | |
| 75 // method returns true if the error just means that we have to wait for | |
| 76 // OnIOComplete to be called. | |
| 77 bool ProcessRequestError(int error); | |
| 78 | |
| 79 // Cleans up the connection, if necessary, and closes the connection and | |
| 80 // request handles. May be called multiple times, it will be a NOP if | |
| 81 // there is nothing to do. | |
| 82 void CleanupConnection(); | |
| 83 | |
| 84 // Closes the given handle. | |
| 85 void CleanupHandle(HINTERNET handle); | |
| 86 | |
| 87 // Returns the global handle to the internet (NOT the same as the connection | |
| 88 // or request handle below) | |
| 89 static HINTERNET GetTheInternet(); | |
| 90 | |
| 91 // Makes appropriate asynch call to re-send a request based on | |
| 92 // dynamic scheme type and user action at authentication prompt | |
| 93 //(OK or Cancel) | |
| 94 virtual void OnCancelAuth() = 0; | |
| 95 virtual void OnSetAuth() = 0; | |
| 96 | |
| 97 // Handle of the connection for this request. This handle is created | |
| 98 // by subclasses that create the connection according to their requirements. | |
| 99 // It will be automatically destroyed by this class when the connection is | |
| 100 // being closed. See also 'request_handle_' | |
| 101 HINTERNET connection_handle_; | |
| 102 | |
| 103 // Handle of the specific request created by subclasses to meet their own | |
| 104 // requirements. This handle has a more narrow scope than the connection | |
| 105 // handle. If non-null, it will be automatically destroyed by this class | |
| 106 // when the connection is being closed. It will be destroyed before the | |
| 107 // connection handle. | |
| 108 HINTERNET request_handle_; | |
| 109 | |
| 110 // The last error that occurred. Used by ContinueDespiteLastError to adjust | |
| 111 // the request's load_flags to ignore this error. | |
| 112 DWORD last_error_; | |
| 113 | |
| 114 // Any extra request headers (\n-delimited) that should be included in the | |
| 115 // request. | |
| 116 std::string extra_request_headers_; | |
| 117 | |
| 118 // Authentication information. | |
| 119 scoped_refptr<net::AuthData> proxy_auth_; | |
| 120 scoped_refptr<net::AuthData> server_auth_; | |
| 121 | |
| 122 private: | |
| 123 | |
| 124 // One-time global state setup | |
| 125 static void InitializeTheInternet(const std::string& user_agent); | |
| 126 | |
| 127 // Runs on some background thread (called by WinInet) | |
| 128 static void CALLBACK URLRequestStatusCallback(HINTERNET handle, | |
| 129 DWORD_PTR job_id, | |
| 130 DWORD status, | |
| 131 LPVOID status_info, | |
| 132 DWORD status_info_len); | |
| 133 | |
| 134 static HINTERNET the_internet_; | |
| 135 #ifndef NDEBUG | |
| 136 static MessageLoop* my_message_loop_; // Used to sanity-check that all | |
| 137 // requests are made on the same | |
| 138 // thread | |
| 139 #endif | |
| 140 | |
| 141 // true if waiting for OnIOComplete to be called | |
| 142 bool is_waiting_; | |
| 143 | |
| 144 // debugging state - is there a read already in progress | |
| 145 bool read_in_progress_; | |
| 146 | |
| 147 // The result and error code of asynchronous IO. It is modified by the | |
| 148 // status callback functions on asynchronous IO completion and passed to | |
| 149 // CallOnIOComplete. Since there is at most one pending IO, the object | |
| 150 // can reuse the async_result_ member for all its asynchronous IOs. | |
| 151 AsyncResult async_result_; | |
| 152 | |
| 153 Lock loop_lock_; | |
| 154 MessageLoop* loop_; | |
| 155 | |
| 156 DISALLOW_EVIL_CONSTRUCTORS(URLRequestInetJob); | |
| 157 }; | |
| 158 | |
| 159 #endif // NET_URL_REQUEST_URL_REQUEST_INET_JOB_H__ | |
| OLD | NEW |