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 // Redirects network data to a file. This is intended to be layered in front of |
30 // of either the AsyncResourceHandler or the SyncResourceHandler. | 30 // either the AsyncResourceHandler or the SyncResourceHandler. The downstream |
31 class RedirectToFileResourceHandler : public LayeredResourceHandler { | 31 // resource handler does not see OnWillRead or OnReadCompleted calls. Instead, |
32 // the ResourceResponse contains the path to a temporary file and | |
33 // OnDataDownloaded is called as the file downloads. | |
34 // | |
35 // The temporary is vended through a delegate interface. | |
36 class CONTENT_EXPORT RedirectToFileResourceHandler | |
mmenke
2013/12/05 21:34:03
Should include content_export.h
davidben
2013/12/05 22:43:20
Done.
| |
37 : public LayeredResourceHandler { | |
32 public: | 38 public: |
39 typedef base::Callback< | |
mmenke
2013/12/05 21:34:03
Should include callback.h or callback_forward.h.
davidben
2013/12/05 22:43:20
It's already there, but...
| |
40 void(base::PlatformFileError, | |
41 scoped_ptr<net::FileStream>, | |
42 scoped_refptr<webkit_blob::ShareableFileReference>)> | |
43 CreateTemporaryCallback; | |
mmenke
2013/12/05 21:34:03
I'm unable to find anything that's quite this ugly
davidben
2013/12/05 22:43:20
Actually deleting it altogether. It's gone now and
| |
44 | |
45 | |
mmenke
2013/12/05 21:34:03
nit: Remove extra blank line.
davidben
2013/12/05 22:43:20
Done.
| |
46 // A delegate class for vending temporary files. | |
47 class Delegate { | |
48 public: | |
49 virtual ~Delegate() {} | |
50 | |
51 // Creates a temporary file for a request. DidCreateTemporaryFile is called | |
52 // on |handler| with a net::FileStream to read from and a | |
53 // ShareableFileReference. The caller should retain a reference to the | |
54 // ShareableFileReference until it is done writing to it. When all | |
55 // references to the ShareableFileReference are released, the temporary is | |
56 // deleted. The factory itself may retain its own reference to the file. | |
57 // | |
58 // If there is an error in creating the temporary, DidCreateTemporaryFile is | |
59 // called with a base::PlatformFileError. | |
60 virtual void CreateTemporary( | |
61 int child_id, | |
62 int request_id, | |
63 base::WeakPtr<RedirectToFileResourceHandler> handler) = 0; | |
64 }; | |
65 | |
66 // Create a RedirectToFileResourceHandler for |request| which wraps | |
67 // |next_handler|. |delegate| must outlive the handler. | |
33 RedirectToFileResourceHandler( | 68 RedirectToFileResourceHandler( |
34 scoped_ptr<ResourceHandler> next_handler, | 69 scoped_ptr<ResourceHandler> next_handler, |
35 net::URLRequest* request, | 70 net::URLRequest* request, |
36 ResourceDispatcherHostImpl* resource_dispatcher_host); | 71 Delegate* delegate); |
37 virtual ~RedirectToFileResourceHandler(); | 72 virtual ~RedirectToFileResourceHandler(); |
38 | 73 |
39 // ResourceHandler implementation: | 74 // LayeredResourceHandler implementation: |
40 virtual bool OnResponseStarted(int request_id, | 75 virtual bool OnResponseStarted(int request_id, |
41 ResourceResponse* response, | 76 ResourceResponse* response, |
42 bool* defer) OVERRIDE; | 77 bool* defer) OVERRIDE; |
43 virtual bool OnWillStart(int request_id, | 78 virtual bool OnWillStart(int request_id, |
44 const GURL& url, | 79 const GURL& url, |
45 bool* defer) OVERRIDE; | 80 bool* defer) OVERRIDE; |
46 virtual bool OnWillRead(int request_id, | 81 virtual bool OnWillRead(int request_id, |
47 scoped_refptr<net::IOBuffer>* buf, | 82 scoped_refptr<net::IOBuffer>* buf, |
48 int* buf_size, | 83 int* buf_size, |
49 int min_size) OVERRIDE; | 84 int min_size) OVERRIDE; |
50 virtual bool OnReadCompleted(int request_id, | 85 virtual bool OnReadCompleted(int request_id, |
51 int bytes_read, | 86 int bytes_read, |
52 bool* defer) OVERRIDE; | 87 bool* defer) OVERRIDE; |
53 virtual void OnResponseCompleted(int request_id, | 88 virtual void OnResponseCompleted(int request_id, |
54 const net::URLRequestStatus& status, | 89 const net::URLRequestStatus& status, |
55 const std::string& security_info, | 90 const std::string& security_info, |
56 bool* defer) OVERRIDE; | 91 bool* defer) OVERRIDE; |
57 | 92 |
93 // Called by RedirectToFileResourceHandler::Writer. | |
94 void DidWriteToFile(int result); | |
mmenke
2013/12/05 21:34:03
This can be private, since inner classes are frien
davidben
2013/12/05 22:43:20
Done. Huh, I didn't actually know that. Handy.
| |
95 | |
96 // Called by the delegate. | |
97 void DidCreateTemporaryFile( | |
98 base::PlatformFileError error_code, | |
99 scoped_ptr<net::FileStream> file_stream, | |
100 webkit_blob::ShareableFileReference* deletable_file); | |
101 | |
58 private: | 102 private: |
59 void DidCreateTemporaryFile(base::PlatformFileError error_code, | |
60 base::PassPlatformFile file_handle, | |
61 const base::FilePath& file_path); | |
62 void DidWriteToFile(int result); | |
63 bool WriteMore(); | 103 bool WriteMore(); |
64 bool BufIsFull() const; | 104 bool BufIsFull() const; |
65 void ResumeIfDeferred(); | 105 void ResumeIfDeferred(); |
66 | 106 |
67 base::WeakPtrFactory<RedirectToFileResourceHandler> weak_factory_; | 107 Delegate* delegate_; |
68 | |
69 ResourceDispatcherHostImpl* host_; | |
70 | 108 |
71 // We allocate a single, fixed-size IO buffer (buf_) used to read from the | 109 // 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 | 110 // 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 | 111 // 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 | 112 // true while writing to disk). Reading from the network is suspended while |
75 // the buffer is full (BufIsFull returns true). The write_cursor_ member | 113 // the buffer is full (BufIsFull returns true). The write_cursor_ member |
76 // tracks the offset into buf_ that we are writing to disk. | 114 // tracks the offset into buf_ that we are writing to disk. |
77 | 115 |
78 scoped_refptr<net::GrowableIOBuffer> buf_; | 116 scoped_refptr<net::GrowableIOBuffer> buf_; |
79 bool buf_write_pending_; | 117 bool buf_write_pending_; |
80 int write_cursor_; | 118 int write_cursor_; |
81 | 119 |
82 scoped_ptr<net::FileStream> file_stream_; | 120 // Helper writer object which maintains references to the net::FileStream and |
83 bool write_callback_pending_; | 121 // webkit_blob::ShareableFileReference. This is maintained separately so that, |
122 // on Windows, the temporary file isn't deleted until after it is closed. | |
123 class Writer; | |
124 scoped_ptr<Writer> writer_; | |
84 | 125 |
85 // |next_buffer_size_| is the size of the buffer to be allocated on the next | 126 // |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 | 127 // OnWillRead() call. We exponentially grow the size of the buffer allocated |
87 // when our owner fills our buffers. On the first OnWillRead() call, we | 128 // 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 | 129 // allocate a buffer of 32k and double it in OnReadCompleted() if the buffer |
89 // was filled, up to a maximum size of 512k. | 130 // was filled, up to a maximum size of 512k. |
90 int next_buffer_size_; | 131 int next_buffer_size_; |
91 | 132 |
92 // We create a ShareableFileReference that's deletable for the temp | 133 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 | 134 |
98 bool completed_during_write_; | 135 bool completed_during_write_; |
136 GURL will_start_url_; | |
99 net::URLRequestStatus completed_status_; | 137 net::URLRequestStatus completed_status_; |
100 std::string completed_security_info_; | 138 std::string completed_security_info_; |
101 | 139 |
140 base::WeakPtrFactory<RedirectToFileResourceHandler> weak_factory_; | |
141 | |
102 DISALLOW_COPY_AND_ASSIGN(RedirectToFileResourceHandler); | 142 DISALLOW_COPY_AND_ASSIGN(RedirectToFileResourceHandler); |
mmenke
2013/12/05 21:34:03
While you're fixing this file, should add includes
davidben
2013/12/05 22:43:20
Done. I kinda wonder if we should just have a sing
mmenke
2013/12/06 16:15:39
Indeed. Even just having one include the other wo
| |
103 }; | 143 }; |
104 | 144 |
105 } // namespace content | 145 } // namespace content |
106 | 146 |
107 #endif // CONTENT_BROWSER_LOADER_REDIRECT_TO_FILE_RESOURCE_HANDLER_H_ | 147 #endif // CONTENT_BROWSER_LOADER_REDIRECT_TO_FILE_RESOURCE_HANDLER_H_ |
OLD | NEW |