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 3d62835e8888d734a8e144356177d6a1faa87ea2..9d14cea62148519a632a36aa2c30cc777068dcdb 100644 |
--- a/content/browser/loader/redirect_to_file_resource_handler.h |
+++ b/content/browser/loader/redirect_to_file_resource_handler.h |
@@ -5,6 +5,7 @@ |
#ifndef CONTENT_BROWSER_LOADER_REDIRECT_TO_FILE_RESOURCE_HANDLER_H_ |
#define CONTENT_BROWSER_LOADER_REDIRECT_TO_FILE_RESOURCE_HANDLER_H_ |
+#include "base/callback_forward.h" |
#include "base/files/file_path.h" |
#include "base/memory/ref_counted.h" |
#include "base/memory/scoped_ptr.h" |
@@ -24,19 +25,49 @@ 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 { |
+// A factory for creating the temporary stream. This removes the dependency on |
+// ResourceDispatcherHost and gives a hook for mocking in errors in unit tests. |
+class TemporaryFileStreamFactory { |
mmenke
2013/12/04 20:55:48
This should be an inner class for RedirectToFileRe
davidben
2013/12/04 22:44:04
Done.
|
+ public: |
+ typedef base::Callback< |
+ void(base::PlatformFileError, |
+ scoped_ptr<net::FileStream>, |
+ scoped_refptr<webkit_blob::ShareableFileReference>)> Callback; |
+ |
+ virtual ~TemporaryFileStreamFactory() {} |
+ |
+ // Creates a temporary file for a request. |callback| is called asynchronously |
+ // with a net::FileStream to read from and a ShareableFileReference. The |
+ // caller should retain a reference to the ShareableFileReference until it is |
+ // done writing to it. When all references to the ShareableFileReference are |
+ // released, the temporary is deleted. The factory itself may retain its own |
+ // reference to the file. |
+ // |
+ // If there is an error in creating the temporary, |callback| is called with a |
+ // base::PlatformFileError. |
+ virtual void CreateTemporary(int child_id, |
+ int request_id, |
+ const Callback& callback) = 0; |
+}; |
+ |
+// 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 TemporaryFileStreamFactory interface. |
+class CONTENT_EXPORT RedirectToFileResourceHandler |
+ : public LayeredResourceHandler { |
public: |
RedirectToFileResourceHandler( |
scoped_ptr<ResourceHandler> next_handler, |
net::URLRequest* request, |
- ResourceDispatcherHostImpl* resource_dispatcher_host); |
+ TemporaryFileStreamFactory* file_stream_factory); |
mmenke
2013/12/04 20:55:48
Should mention lifetime requirements here.
davidben
2013/12/04 22:44:04
Done.
|
virtual ~RedirectToFileResourceHandler(); |
- // ResourceHandler implementation: |
+ // LayeredResourceHandler implementation: |
virtual bool OnResponseStarted(int request_id, |
ResourceResponse* response, |
bool* defer) OVERRIDE; |
@@ -55,18 +86,19 @@ class RedirectToFileResourceHandler : public LayeredResourceHandler { |
const std::string& security_info, |
bool* defer) OVERRIDE; |
- private: |
- void DidCreateTemporaryFile(base::PlatformFileError error_code, |
- base::PassPlatformFile file_handle, |
- const base::FilePath& file_path); |
+ // Called by RedirectToFileResourceHandler::Writer. |
void DidWriteToFile(int result); |
+ |
+ private: |
+ void DidCreateTemporaryFile( |
+ base::PlatformFileError error_code, |
+ scoped_ptr<net::FileStream> file_stream, |
+ scoped_refptr<webkit_blob::ShareableFileReference> deletable_file); |
bool WriteMore(); |
bool BufIsFull() const; |
void ResumeIfDeferred(); |
- base::WeakPtrFactory<RedirectToFileResourceHandler> weak_factory_; |
- |
- ResourceDispatcherHostImpl* host_; |
+ TemporaryFileStreamFactory* file_stream_factory_; |
// 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 |
@@ -79,8 +111,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; |
+ scoped_ptr<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 |
@@ -89,16 +124,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); |
}; |