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