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