Chromium Code Reviews| Index: content/browser/loader/redirect_to_file_resource_handler.h |
| diff --git a/content/browser/loader/redirect_to_file_resource_handler.h b/content/browser/loader/redirect_to_file_resource_handler.h |
| index f83dd5456fa77ab81dbd3eea76fcb2b0ee90e84b..12ac0d65943b1ffed1b05f277fd4cb6bfe8d2aa9 100644 |
| --- a/content/browser/loader/redirect_to_file_resource_handler.h |
| +++ b/content/browser/loader/redirect_to_file_resource_handler.h |
| @@ -5,6 +5,9 @@ |
| #ifndef CONTENT_BROWSER_LOADER_REDIRECT_TO_FILE_RESOURCE_HANDLER_H_ |
| #define CONTENT_BROWSER_LOADER_REDIRECT_TO_FILE_RESOURCE_HANDLER_H_ |
| +#include "base/basictypes.h" |
| +#include "base/callback.h" |
| +#include "base/compiler_specific.h" |
| #include "base/files/file.h" |
| #include "base/files/file_path.h" |
| #include "base/memory/ref_counted.h" |
| @@ -12,8 +15,11 @@ |
| #include "base/memory/weak_ptr.h" |
| #include "base/platform_file.h" |
| #include "content/browser/loader/layered_resource_handler.h" |
| +#include "content/browser/loader/temporary_file_stream.h" |
| +#include "content/common/content_export.h" |
| #include "net/url_request/url_request.h" |
| #include "net/url_request/url_request_status.h" |
| +#include "url/gurl.h" |
| namespace net { |
| class FileStream; |
| @@ -25,19 +31,34 @@ class ShareableFileReference; |
| } |
| namespace content { |
| -class ResourceDispatcherHostImpl; |
| -// Redirects network data to a file. This is intended to be layered in front |
| -// of either the AsyncResourceHandler or the SyncResourceHandler. |
| -class RedirectToFileResourceHandler : public LayeredResourceHandler { |
| +// Redirects network data to a file. This is intended to be layered in front of |
| +// either the AsyncResourceHandler or the SyncResourceHandler. The downstream |
| +// resource handler does not see OnWillRead or OnReadCompleted calls. Instead, |
| +// the ResourceResponse contains the path to a temporary file and |
| +// OnDataDownloaded is called as the file downloads. |
| +// |
| +// The temporary is vended through a delegate interface. |
|
darin (slow to review)
2014/03/11 05:15:58
nit: "The temporary [file]..."
davidben
2014/03/11 19:50:04
Oh whoops, that comment's no longer accurate anywa
|
| +class CONTENT_EXPORT RedirectToFileResourceHandler |
| + : public LayeredResourceHandler { |
| public: |
| - RedirectToFileResourceHandler( |
| - scoped_ptr<ResourceHandler> next_handler, |
| - net::URLRequest* request, |
| - ResourceDispatcherHostImpl* resource_dispatcher_host); |
| + typedef base::Callback<void(const CreateTemporaryFileStreamCallback&)> |
| + CreateTemporaryFileStreamFunction; |
| + |
| + // Create a RedirectToFileResourceHandler for |request| which wraps |
| + // |next_handler|. |
| + RedirectToFileResourceHandler(scoped_ptr<ResourceHandler> next_handler, |
| + net::URLRequest* request); |
| virtual ~RedirectToFileResourceHandler(); |
| - // ResourceHandler implementation: |
| + // Replace the CreateTemporaryFileStream implementation with a mocked one for |
| + // testing purposes. The function should create a net::FileStream and a |
| + // ShareableFileReference and then asynchronously pass them to the |
| + // CreateTemporaryFileStreamCallback. |
| + void SetCreateTemporaryFileStreamFunctionForTesting( |
| + const CreateTemporaryFileStreamFunction& create_temporary_file_stream); |
| + |
| + // LayeredResourceHandler implementation: |
| virtual bool OnResponseStarted(int request_id, |
| ResourceResponse* response, |
| bool* defer) OVERRIDE; |
| @@ -57,17 +78,19 @@ class RedirectToFileResourceHandler : public LayeredResourceHandler { |
| bool* defer) OVERRIDE; |
| private: |
| - void DidCreateTemporaryFile(base::File::Error error_code, |
| - base::PassPlatformFile file_handle, |
| - const base::FilePath& file_path); |
| + void DidCreateTemporaryFile( |
| + base::File::Error error_code, |
| + scoped_ptr<net::FileStream> file_stream, |
| + webkit_blob::ShareableFileReference* deletable_file); |
| + |
| + // Called by RedirectToFileResourceHandler::Writer. |
| void DidWriteToFile(int result); |
| + |
| bool WriteMore(); |
| bool BufIsFull() const; |
| void ResumeIfDeferred(); |
| - base::WeakPtrFactory<RedirectToFileResourceHandler> weak_factory_; |
| - |
| - ResourceDispatcherHostImpl* host_; |
| + CreateTemporaryFileStreamFunction create_temporary_file_stream_; |
| // We allocate a single, fixed-size IO buffer (buf_) used to read from the |
| // network (buf_write_pending_ is true while the system is copying data into |
| @@ -80,8 +103,11 @@ class RedirectToFileResourceHandler : public LayeredResourceHandler { |
| bool buf_write_pending_; |
| int write_cursor_; |
| - scoped_ptr<net::FileStream> file_stream_; |
| - bool write_callback_pending_; |
| + // Helper writer object which maintains references to the net::FileStream and |
| + // webkit_blob::ShareableFileReference. This is maintained separately so that, |
| + // on Windows, the temporary file isn't deleted until after it is closed. |
| + class Writer; |
| + Writer* writer_; |
| // |next_buffer_size_| is the size of the buffer to be allocated on the next |
| // OnWillRead() call. We exponentially grow the size of the buffer allocated |
| @@ -90,16 +116,15 @@ class RedirectToFileResourceHandler : public LayeredResourceHandler { |
| // was filled, up to a maximum size of 512k. |
| int next_buffer_size_; |
| - // We create a ShareableFileReference that's deletable for the temp |
| - // file created as a result of the download. |
| - scoped_refptr<webkit_blob::ShareableFileReference> deletable_file_; |
| - |
| - bool did_defer_ ; |
| + bool did_defer_; |
| bool completed_during_write_; |
| + GURL will_start_url_; |
| net::URLRequestStatus completed_status_; |
| std::string completed_security_info_; |
| + base::WeakPtrFactory<RedirectToFileResourceHandler> weak_factory_; |
| + |
| DISALLOW_COPY_AND_ASSIGN(RedirectToFileResourceHandler); |
| }; |