Chromium Code Reviews| 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 CONTENT_BROWSER_LOADER_REDIRECT_TO_FILE_RESOURCE_HANDLER_H_ | 5 #ifndef CONTENT_BROWSER_LOADER_REDIRECT_TO_FILE_RESOURCE_HANDLER_H_ |
| 6 #define CONTENT_BROWSER_LOADER_REDIRECT_TO_FILE_RESOURCE_HANDLER_H_ | 6 #define CONTENT_BROWSER_LOADER_REDIRECT_TO_FILE_RESOURCE_HANDLER_H_ |
| 7 | 7 |
| 8 #include "base/callback_forward.h" | |
| 8 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
| 9 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
| 10 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/memory/weak_ptr.h" | 12 #include "base/memory/weak_ptr.h" |
| 12 #include "base/platform_file.h" | 13 #include "base/platform_file.h" |
| 13 #include "content/browser/loader/layered_resource_handler.h" | 14 #include "content/browser/loader/layered_resource_handler.h" |
| 14 #include "net/url_request/url_request.h" | 15 #include "net/url_request/url_request.h" |
| 15 #include "net/url_request/url_request_status.h" | 16 #include "net/url_request/url_request_status.h" |
| 16 | 17 |
| 17 namespace net { | 18 namespace net { |
| 18 class FileStream; | 19 class FileStream; |
| 19 class GrowableIOBuffer; | 20 class GrowableIOBuffer; |
| 20 } | 21 } |
| 21 | 22 |
| 22 namespace webkit_blob { | 23 namespace webkit_blob { |
| 23 class ShareableFileReference; | 24 class ShareableFileReference; |
| 24 } | 25 } |
| 25 | 26 |
| 26 namespace content { | 27 namespace content { |
| 27 class ResourceDispatcherHostImpl; | |
| 28 | 28 |
| 29 // Redirects network data to a file. This is intended to be layered in front | 29 // A factory for creating the temporary stream. This removes the dependency on |
| 30 // of either the AsyncResourceHandler or the SyncResourceHandler. | 30 // ResourceDispatcherHost and gives a hook for mocking in errors in unit tests. |
| 31 class RedirectToFileResourceHandler : public LayeredResourceHandler { | 31 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.
| |
| 32 public: | |
| 33 typedef base::Callback< | |
| 34 void(base::PlatformFileError, | |
| 35 scoped_ptr<net::FileStream>, | |
| 36 scoped_refptr<webkit_blob::ShareableFileReference>)> Callback; | |
| 37 | |
| 38 virtual ~TemporaryFileStreamFactory() {} | |
| 39 | |
| 40 // Creates a temporary file for a request. |callback| is called asynchronously | |
| 41 // with a net::FileStream to read from and a ShareableFileReference. The | |
| 42 // caller should retain a reference to the ShareableFileReference until it is | |
| 43 // done writing to it. When all references to the ShareableFileReference are | |
| 44 // released, the temporary is deleted. The factory itself may retain its own | |
| 45 // reference to the file. | |
| 46 // | |
| 47 // If there is an error in creating the temporary, |callback| is called with a | |
| 48 // base::PlatformFileError. | |
| 49 virtual void CreateTemporary(int child_id, | |
| 50 int request_id, | |
| 51 const Callback& callback) = 0; | |
| 52 }; | |
| 53 | |
| 54 // Redirects network data to a file. This is intended to be layered in front of | |
| 55 // either the AsyncResourceHandler or the SyncResourceHandler. The downstream | |
| 56 // resource handler does not see OnWillRead or OnReadCompleted calls. Instead, | |
| 57 // the ResourceResponse contains the path to a temporary file and | |
| 58 // OnDataDownloaded is called as the file downloads. | |
| 59 // | |
| 60 // The temporary is vended through a TemporaryFileStreamFactory interface. | |
| 61 class CONTENT_EXPORT RedirectToFileResourceHandler | |
| 62 : public LayeredResourceHandler { | |
| 32 public: | 63 public: |
| 33 RedirectToFileResourceHandler( | 64 RedirectToFileResourceHandler( |
| 34 scoped_ptr<ResourceHandler> next_handler, | 65 scoped_ptr<ResourceHandler> next_handler, |
| 35 net::URLRequest* request, | 66 net::URLRequest* request, |
| 36 ResourceDispatcherHostImpl* resource_dispatcher_host); | 67 TemporaryFileStreamFactory* file_stream_factory); |
|
mmenke
2013/12/04 20:55:48
Should mention lifetime requirements here.
davidben
2013/12/04 22:44:04
Done.
| |
| 37 virtual ~RedirectToFileResourceHandler(); | 68 virtual ~RedirectToFileResourceHandler(); |
| 38 | 69 |
| 39 // ResourceHandler implementation: | 70 // LayeredResourceHandler implementation: |
| 40 virtual bool OnResponseStarted(int request_id, | 71 virtual bool OnResponseStarted(int request_id, |
| 41 ResourceResponse* response, | 72 ResourceResponse* response, |
| 42 bool* defer) OVERRIDE; | 73 bool* defer) OVERRIDE; |
| 43 virtual bool OnWillStart(int request_id, | 74 virtual bool OnWillStart(int request_id, |
| 44 const GURL& url, | 75 const GURL& url, |
| 45 bool* defer) OVERRIDE; | 76 bool* defer) OVERRIDE; |
| 46 virtual bool OnWillRead(int request_id, | 77 virtual bool OnWillRead(int request_id, |
| 47 scoped_refptr<net::IOBuffer>* buf, | 78 scoped_refptr<net::IOBuffer>* buf, |
| 48 int* buf_size, | 79 int* buf_size, |
| 49 int min_size) OVERRIDE; | 80 int min_size) OVERRIDE; |
| 50 virtual bool OnReadCompleted(int request_id, | 81 virtual bool OnReadCompleted(int request_id, |
| 51 int bytes_read, | 82 int bytes_read, |
| 52 bool* defer) OVERRIDE; | 83 bool* defer) OVERRIDE; |
| 53 virtual void OnResponseCompleted(int request_id, | 84 virtual void OnResponseCompleted(int request_id, |
| 54 const net::URLRequestStatus& status, | 85 const net::URLRequestStatus& status, |
| 55 const std::string& security_info, | 86 const std::string& security_info, |
| 56 bool* defer) OVERRIDE; | 87 bool* defer) OVERRIDE; |
| 57 | 88 |
| 89 // Called by RedirectToFileResourceHandler::Writer. | |
| 90 void DidWriteToFile(int result); | |
| 91 | |
| 58 private: | 92 private: |
| 59 void DidCreateTemporaryFile(base::PlatformFileError error_code, | 93 void DidCreateTemporaryFile( |
| 60 base::PassPlatformFile file_handle, | 94 base::PlatformFileError error_code, |
| 61 const base::FilePath& file_path); | 95 scoped_ptr<net::FileStream> file_stream, |
| 62 void DidWriteToFile(int result); | 96 scoped_refptr<webkit_blob::ShareableFileReference> deletable_file); |
| 63 bool WriteMore(); | 97 bool WriteMore(); |
| 64 bool BufIsFull() const; | 98 bool BufIsFull() const; |
| 65 void ResumeIfDeferred(); | 99 void ResumeIfDeferred(); |
| 66 | 100 |
| 67 base::WeakPtrFactory<RedirectToFileResourceHandler> weak_factory_; | 101 TemporaryFileStreamFactory* file_stream_factory_; |
| 68 | |
| 69 ResourceDispatcherHostImpl* host_; | |
| 70 | 102 |
| 71 // We allocate a single, fixed-size IO buffer (buf_) used to read from the | 103 // We allocate a single, fixed-size IO buffer (buf_) used to read from the |
| 72 // network (buf_write_pending_ is true while the system is copying data into | 104 // network (buf_write_pending_ is true while the system is copying data into |
| 73 // buf_), and then write this buffer out to disk (write_callback_pending_ is | 105 // buf_), and then write this buffer out to disk (write_callback_pending_ is |
| 74 // true while writing to disk). Reading from the network is suspended while | 106 // true while writing to disk). Reading from the network is suspended while |
| 75 // the buffer is full (BufIsFull returns true). The write_cursor_ member | 107 // the buffer is full (BufIsFull returns true). The write_cursor_ member |
| 76 // tracks the offset into buf_ that we are writing to disk. | 108 // tracks the offset into buf_ that we are writing to disk. |
| 77 | 109 |
| 78 scoped_refptr<net::GrowableIOBuffer> buf_; | 110 scoped_refptr<net::GrowableIOBuffer> buf_; |
| 79 bool buf_write_pending_; | 111 bool buf_write_pending_; |
| 80 int write_cursor_; | 112 int write_cursor_; |
| 81 | 113 |
| 82 scoped_ptr<net::FileStream> file_stream_; | 114 // Helper writer object which maintains references to the net::FileStream and |
| 83 bool write_callback_pending_; | 115 // webkit_blob::ShareableFileReference. This is maintained separately so that, |
| 116 // on Windows, the temporary file isn't deleted until after it is closed. | |
| 117 class Writer; | |
| 118 scoped_ptr<Writer> writer_; | |
| 84 | 119 |
| 85 // |next_buffer_size_| is the size of the buffer to be allocated on the next | 120 // |next_buffer_size_| is the size of the buffer to be allocated on the next |
| 86 // OnWillRead() call. We exponentially grow the size of the buffer allocated | 121 // OnWillRead() call. We exponentially grow the size of the buffer allocated |
| 87 // when our owner fills our buffers. On the first OnWillRead() call, we | 122 // when our owner fills our buffers. On the first OnWillRead() call, we |
| 88 // allocate a buffer of 32k and double it in OnReadCompleted() if the buffer | 123 // allocate a buffer of 32k and double it in OnReadCompleted() if the buffer |
| 89 // was filled, up to a maximum size of 512k. | 124 // was filled, up to a maximum size of 512k. |
| 90 int next_buffer_size_; | 125 int next_buffer_size_; |
| 91 | 126 |
| 92 // We create a ShareableFileReference that's deletable for the temp | 127 bool did_defer_; |
| 93 // file created as a result of the download. | |
| 94 scoped_refptr<webkit_blob::ShareableFileReference> deletable_file_; | |
| 95 | |
| 96 bool did_defer_ ; | |
| 97 | 128 |
| 98 bool completed_during_write_; | 129 bool completed_during_write_; |
| 130 GURL will_start_url_; | |
| 99 net::URLRequestStatus completed_status_; | 131 net::URLRequestStatus completed_status_; |
| 100 std::string completed_security_info_; | 132 std::string completed_security_info_; |
| 101 | 133 |
| 134 base::WeakPtrFactory<RedirectToFileResourceHandler> weak_factory_; | |
| 135 | |
| 102 DISALLOW_COPY_AND_ASSIGN(RedirectToFileResourceHandler); | 136 DISALLOW_COPY_AND_ASSIGN(RedirectToFileResourceHandler); |
| 103 }; | 137 }; |
| 104 | 138 |
| 105 } // namespace content | 139 } // namespace content |
| 106 | 140 |
| 107 #endif // CONTENT_BROWSER_LOADER_REDIRECT_TO_FILE_RESOURCE_HANDLER_H_ | 141 #endif // CONTENT_BROWSER_LOADER_REDIRECT_TO_FILE_RESOURCE_HANDLER_H_ |
| OLD | NEW |