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