| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #ifndef NET_URL_REQUEST_URL_FETCHER_CORE_H_ | 5 #ifndef NET_URL_REQUEST_URL_FETCHER_CORE_H_ |
| 6 #define NET_URL_REQUEST_URL_FETCHER_CORE_H_ | 6 #define NET_URL_REQUEST_URL_FETCHER_CORE_H_ |
| 7 | 7 |
| 8 #include <set> | 8 #include <set> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 84 void SetURLRequestUserData( | 84 void SetURLRequestUserData( |
| 85 const void* key, | 85 const void* key, |
| 86 const URLFetcher::CreateDataCallback& create_data_callback); | 86 const URLFetcher::CreateDataCallback& create_data_callback); |
| 87 void SetStopOnRedirect(bool stop_on_redirect); | 87 void SetStopOnRedirect(bool stop_on_redirect); |
| 88 void SetAutomaticallyRetryOn5xx(bool retry); | 88 void SetAutomaticallyRetryOn5xx(bool retry); |
| 89 void SetMaxRetries(int max_retries); | 89 void SetMaxRetries(int max_retries); |
| 90 int GetMaxRetries() const; | 90 int GetMaxRetries() const; |
| 91 base::TimeDelta GetBackoffDelay() const; | 91 base::TimeDelta GetBackoffDelay() const; |
| 92 void SaveResponseToFileAtPath( | 92 void SaveResponseToFileAtPath( |
| 93 const FilePath& file_path, | 93 const FilePath& file_path, |
| 94 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner); | 94 scoped_refptr<base::TaskRunner> file_task_runner); |
| 95 void SaveResponseToTemporaryFile( | 95 void SaveResponseToTemporaryFile( |
| 96 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner); | 96 scoped_refptr<base::TaskRunner> file_task_runner); |
| 97 HttpResponseHeaders* GetResponseHeaders() const; | 97 HttpResponseHeaders* GetResponseHeaders() const; |
| 98 HostPortPair GetSocketAddress() const; | 98 HostPortPair GetSocketAddress() const; |
| 99 bool WasFetchedViaProxy() const; | 99 bool WasFetchedViaProxy() const; |
| 100 const GURL& GetOriginalURL() const; | 100 const GURL& GetOriginalURL() const; |
| 101 const GURL& GetURL() const; | 101 const GURL& GetURL() const; |
| 102 const URLRequestStatus& GetStatus() const; | 102 const URLRequestStatus& GetStatus() const; |
| 103 int GetResponseCode() const; | 103 int GetResponseCode() const; |
| 104 const ResponseCookies& GetCookies() const; | 104 const ResponseCookies& GetCookies() const; |
| 105 bool FileErrorOccurred(base::PlatformFileError* out_error_code) const; | 105 bool FileErrorOccurred(base::PlatformFileError* out_error_code) const; |
| 106 // Reports that the received content was malformed (i.e. failed parsing | 106 // Reports that the received content was malformed (i.e. failed parsing |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 155 std::set<URLFetcherCore*> fetchers_; | 155 std::set<URLFetcherCore*> fetchers_; |
| 156 | 156 |
| 157 DISALLOW_COPY_AND_ASSIGN(Registry); | 157 DISALLOW_COPY_AND_ASSIGN(Registry); |
| 158 }; | 158 }; |
| 159 | 159 |
| 160 // Class FileWriter encapsulates all state involved in writing | 160 // Class FileWriter encapsulates all state involved in writing |
| 161 // response bytes to a file. It is only used if | 161 // response bytes to a file. It is only used if |
| 162 // |URLFetcherCore::response_destination_| == TEMP_FILE || | 162 // |URLFetcherCore::response_destination_| == TEMP_FILE || |
| 163 // |URLFetcherCore::response_destination_| == PERMANENT_FILE. Each | 163 // |URLFetcherCore::response_destination_| == PERMANENT_FILE. Each |
| 164 // instance of FileWriter is owned by a URLFetcherCore, which | 164 // instance of FileWriter is owned by a URLFetcherCore, which |
| 165 // manages its lifetime and never transfers ownership. While | 165 // manages its lifetime and never transfers ownership. All file operations |
| 166 // writing to a file, all function calls happen on the IO thread. | 166 // happen on |file_task_runner_|. |
| 167 class FileWriter { | 167 class FileWriter { |
| 168 public: | 168 public: |
| 169 FileWriter(URLFetcherCore* core, | 169 FileWriter(URLFetcherCore* core, |
| 170 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner); | 170 scoped_refptr<base::TaskRunner> file_task_runner); |
| 171 ~FileWriter(); | 171 ~FileWriter(); |
| 172 | 172 |
| 173 void CreateFileAtPath(const FilePath& file_path); | 173 void CreateFileAtPath(const FilePath& file_path); |
| 174 void CreateTempFile(); | 174 void CreateTempFile(); |
| 175 | 175 |
| 176 // Record |num_bytes_| response bytes in |core_->buffer_| to the file. | 176 // Record |num_bytes_| response bytes in |core_->buffer_| to the file. |
| 177 void WriteBuffer(int num_bytes); | 177 void WriteBuffer(int num_bytes); |
| 178 | 178 |
| 179 // Called when a write has been done. Continues writing if there are | 179 // Called when a write has been done. Continues writing if there are |
| 180 // any more bytes to write. Otherwise, initiates a read in core_. | 180 // any more bytes to write. Otherwise, initiates a read in core_. |
| 181 void ContinueWrite(base::PlatformFileError error_code, int bytes_written); | 181 void ContinueWrite(base::PlatformFileError error_code, int bytes_written); |
| 182 | 182 |
| 183 // Drop ownership of the file at |file_path_|. | 183 // Drop ownership of the file at |file_path_|. |
| 184 // This class will not delete it or write to it again. | 184 // This class will not delete it or write to it again. |
| 185 void DisownFile(); | 185 void DisownFile(); |
| 186 | 186 |
| 187 // Close the file if it is open. | 187 // Close the file if it is open. |
| 188 void CloseFileAndCompleteRequest(); | 188 void CloseFileAndCompleteRequest(); |
| 189 | 189 |
| 190 // Remove the file if we have created one. | 190 // Close the file if it is open and then delete it. |
| 191 void RemoveFile(); | 191 void CloseAndDeleteFile(); |
| 192 | 192 |
| 193 const FilePath& file_path() const { return file_path_; } | 193 const FilePath& file_path() const { return file_path_; } |
| 194 int64 total_bytes_written() { return total_bytes_written_; } | 194 int64 total_bytes_written() { return total_bytes_written_; } |
| 195 base::PlatformFileError error_code() const { return error_code_; } | 195 base::PlatformFileError error_code() const { return error_code_; } |
| 196 | 196 |
| 197 private: | 197 private: |
| 198 // Callback which gets the result of a permanent file creation. | 198 // Callback which gets the result of a permanent file creation. |
| 199 void DidCreateFile(const FilePath& file_path, | 199 void DidCreateFile(const FilePath& file_path, |
| 200 base::PlatformFileError error_code, | 200 base::PlatformFileError error_code, |
| 201 base::PassPlatformFile file_handle, | 201 base::PassPlatformFile file_handle, |
| 202 bool created); | 202 bool created); |
| 203 // Callback which gets the result of a temporary file creation. | 203 // Callback which gets the result of a temporary file creation. |
| 204 void DidCreateTempFile(base::PlatformFileError error_code, | 204 void DidCreateTempFile(base::PlatformFileError error_code, |
| 205 base::PassPlatformFile file_handle, | 205 base::PassPlatformFile file_handle, |
| 206 const FilePath& file_path); | 206 const FilePath& file_path); |
| 207 // This method is used to implement DidCreateFile and DidCreateTempFile. | 207 // This method is used to implement DidCreateFile and DidCreateTempFile. |
| 208 void DidCreateFileInternal(const FilePath& file_path, | 208 void DidCreateFileInternal(const FilePath& file_path, |
| 209 base::PlatformFileError error_code, | 209 base::PlatformFileError error_code, |
| 210 base::PassPlatformFile file_handle); | 210 base::PassPlatformFile file_handle); |
| 211 | 211 |
| 212 // Callback which gets the result of closing the file. | 212 // Callback which gets the result of closing the file. |
| 213 void DidCloseFile(base::PlatformFileError error); | 213 void DidCloseFile(base::PlatformFileError error); |
| 214 | 214 |
| 215 // Callback which gets the result of closing the file. Deletes the file if |
| 216 // it has been created. |
| 217 void DeleteFile(base::PlatformFileError error_code); |
| 218 |
| 215 // The URLFetcherCore which instantiated this class. | 219 // The URLFetcherCore which instantiated this class. |
| 216 URLFetcherCore* core_; | 220 URLFetcherCore* core_; |
| 217 | 221 |
| 218 // The last error encountered on a file operation. base::PLATFORM_FILE_OK | 222 // The last error encountered on a file operation. base::PLATFORM_FILE_OK |
| 219 // if no error occurred. | 223 // if no error occurred. |
| 220 base::PlatformFileError error_code_; | 224 base::PlatformFileError error_code_; |
| 221 | 225 |
| 222 // Callbacks are created for use with base::FileUtilProxy. | 226 // Callbacks are created for use with base::FileUtilProxy. |
| 223 base::WeakPtrFactory<URLFetcherCore::FileWriter> weak_factory_; | 227 base::WeakPtrFactory<URLFetcherCore::FileWriter> weak_factory_; |
| 224 | 228 |
| 225 // Task runner for the thread on which file operations should happen. | 229 // Task runner on which file operations should happen. |
| 226 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner_; | 230 scoped_refptr<base::TaskRunner> file_task_runner_; |
| 227 | 231 |
| 228 // Path to the file. This path is empty when there is no file. | 232 // Path to the file. This path is empty when there is no file. |
| 229 FilePath file_path_; | 233 FilePath file_path_; |
| 230 | 234 |
| 231 // Handle to the file. | 235 // Handle to the file. |
| 232 base::PlatformFile file_handle_; | 236 base::PlatformFile file_handle_; |
| 233 | 237 |
| 234 // We always append to the file. Track the total number of bytes | 238 // We always append to the file. Track the total number of bytes |
| 235 // written, so that writes know the offset to give. | 239 // written, so that writes know the offset to give. |
| 236 int64 total_bytes_written_; | 240 int64 total_bytes_written_; |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 294 | 298 |
| 295 URLFetcher* fetcher_; // Corresponding fetcher object | 299 URLFetcher* fetcher_; // Corresponding fetcher object |
| 296 GURL original_url_; // The URL we were asked to fetch | 300 GURL original_url_; // The URL we were asked to fetch |
| 297 GURL url_; // The URL we eventually wound up at | 301 GURL url_; // The URL we eventually wound up at |
| 298 URLFetcher::RequestType request_type_; // What type of request is this? | 302 URLFetcher::RequestType request_type_; // What type of request is this? |
| 299 URLRequestStatus status_; // Status of the request | 303 URLRequestStatus status_; // Status of the request |
| 300 URLFetcherDelegate* delegate_; // Object to notify on completion | 304 URLFetcherDelegate* delegate_; // Object to notify on completion |
| 301 scoped_refptr<base::SingleThreadTaskRunner> delegate_task_runner_; | 305 scoped_refptr<base::SingleThreadTaskRunner> delegate_task_runner_; |
| 302 // Task runner for the creating thread. | 306 // Task runner for the creating thread. |
| 303 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner_; | 307 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner_; |
| 304 // Task runner for the thread | 308 // Task runner for file access. |
| 305 // on which the request IO happens. | 309 scoped_refptr<base::TaskRunner> file_task_runner_; |
| 306 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner_; | |
| 307 // Task runner for the thread | 310 // Task runner for the thread |
| 308 // on which file access happens. | 311 // on which file access happens. |
| 309 scoped_ptr<URLRequest> request_; // The actual request this wraps | 312 scoped_ptr<URLRequest> request_; // The actual request this wraps |
| 310 int load_flags_; // Flags for the load operation | 313 int load_flags_; // Flags for the load operation |
| 311 int response_code_; // HTTP status code for the request | 314 int response_code_; // HTTP status code for the request |
| 312 std::string data_; // Results of the request, when we are | 315 std::string data_; // Results of the request, when we are |
| 313 // storing the response as a string. | 316 // storing the response as a string. |
| 314 scoped_refptr<IOBuffer> buffer_; | 317 scoped_refptr<IOBuffer> buffer_; |
| 315 // Read buffer | 318 // Read buffer |
| 316 scoped_refptr<URLRequestContextGetter> request_context_getter_; | 319 scoped_refptr<URLRequestContextGetter> request_context_getter_; |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 401 base::debug::StackTrace stack_trace_; | 404 base::debug::StackTrace stack_trace_; |
| 402 | 405 |
| 403 static base::LazyInstance<Registry> g_registry; | 406 static base::LazyInstance<Registry> g_registry; |
| 404 | 407 |
| 405 DISALLOW_COPY_AND_ASSIGN(URLFetcherCore); | 408 DISALLOW_COPY_AND_ASSIGN(URLFetcherCore); |
| 406 }; | 409 }; |
| 407 | 410 |
| 408 } // namespace net | 411 } // namespace net |
| 409 | 412 |
| 410 #endif // NET_URL_REQUEST_URL_FETCHER_CORE_H_ | 413 #endif // NET_URL_REQUEST_URL_FETCHER_CORE_H_ |
| OLD | NEW |