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" | 8 #include "base/basictypes.h" |
9 #include "base/callback.h" | 9 #include "base/callback.h" |
10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
11 #include "base/files/file.h" | 11 #include "base/files/file.h" |
12 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
14 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/scoped_ptr.h" |
15 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
16 #include "content/browser/loader/layered_resource_handler.h" | 16 #include "content/browser/loader/layered_resource_handler.h" |
17 #include "content/browser/loader/temporary_file_stream.h" | 17 #include "content/browser/loader/temporary_file_stream.h" |
18 #include "content/common/content_export.h" | 18 #include "content/common/content_export.h" |
19 #include "net/url_request/url_request.h" | 19 #include "net/url_request/url_request.h" |
20 #include "net/url_request/url_request_status.h" | 20 #include "net/url_request/url_request_status.h" |
21 #include "url/gurl.h" | 21 #include "url/gurl.h" |
22 | 22 |
23 namespace net { | 23 namespace net { |
24 class FileStream; | 24 class FileStream; |
25 class GrowableIOBuffer; | 25 class GrowableIOBuffer; |
26 } | 26 } |
27 | 27 |
28 namespace webkit_blob { | 28 namespace storage { |
29 class ShareableFileReference; | 29 class ShareableFileReference; |
30 } | 30 } |
31 | 31 |
32 namespace content { | 32 namespace content { |
33 | 33 |
34 // Redirects network data to a file. This is intended to be layered in front of | 34 // Redirects network data to a file. This is intended to be layered in front of |
35 // either the AsyncResourceHandler or the SyncResourceHandler. The downstream | 35 // either the AsyncResourceHandler or the SyncResourceHandler. The downstream |
36 // resource handler does not see OnWillRead or OnReadCompleted calls. Instead, | 36 // resource handler does not see OnWillRead or OnReadCompleted calls. Instead, |
37 // the ResourceResponse contains the path to a temporary file and | 37 // the ResourceResponse contains the path to a temporary file and |
38 // OnDataDownloaded is called as the file downloads. | 38 // OnDataDownloaded is called as the file downloads. |
(...skipping 22 matching lines...) Expand all Loading... |
61 virtual bool OnWillStart(const GURL& url, bool* defer) OVERRIDE; | 61 virtual bool OnWillStart(const GURL& url, bool* defer) OVERRIDE; |
62 virtual bool OnWillRead(scoped_refptr<net::IOBuffer>* buf, | 62 virtual bool OnWillRead(scoped_refptr<net::IOBuffer>* buf, |
63 int* buf_size, | 63 int* buf_size, |
64 int min_size) OVERRIDE; | 64 int min_size) OVERRIDE; |
65 virtual bool OnReadCompleted(int bytes_read, bool* defer) OVERRIDE; | 65 virtual bool OnReadCompleted(int bytes_read, bool* defer) OVERRIDE; |
66 virtual void OnResponseCompleted(const net::URLRequestStatus& status, | 66 virtual void OnResponseCompleted(const net::URLRequestStatus& status, |
67 const std::string& security_info, | 67 const std::string& security_info, |
68 bool* defer) OVERRIDE; | 68 bool* defer) OVERRIDE; |
69 | 69 |
70 private: | 70 private: |
71 void DidCreateTemporaryFile( | 71 void DidCreateTemporaryFile(base::File::Error error_code, |
72 base::File::Error error_code, | 72 scoped_ptr<net::FileStream> file_stream, |
73 scoped_ptr<net::FileStream> file_stream, | 73 storage::ShareableFileReference* deletable_file); |
74 webkit_blob::ShareableFileReference* deletable_file); | |
75 | 74 |
76 // Called by RedirectToFileResourceHandler::Writer. | 75 // Called by RedirectToFileResourceHandler::Writer. |
77 void DidWriteToFile(int result); | 76 void DidWriteToFile(int result); |
78 | 77 |
79 bool WriteMore(); | 78 bool WriteMore(); |
80 bool BufIsFull() const; | 79 bool BufIsFull() const; |
81 void ResumeIfDeferred(); | 80 void ResumeIfDeferred(); |
82 | 81 |
83 CreateTemporaryFileStreamFunction create_temporary_file_stream_; | 82 CreateTemporaryFileStreamFunction create_temporary_file_stream_; |
84 | 83 |
85 // We allocate a single, fixed-size IO buffer (buf_) used to read from the | 84 // We allocate a single, fixed-size IO buffer (buf_) used to read from the |
86 // network (buf_write_pending_ is true while the system is copying data into | 85 // network (buf_write_pending_ is true while the system is copying data into |
87 // buf_), and then write this buffer out to disk (write_callback_pending_ is | 86 // buf_), and then write this buffer out to disk (write_callback_pending_ is |
88 // true while writing to disk). Reading from the network is suspended while | 87 // true while writing to disk). Reading from the network is suspended while |
89 // the buffer is full (BufIsFull returns true). The write_cursor_ member | 88 // the buffer is full (BufIsFull returns true). The write_cursor_ member |
90 // tracks the offset into buf_ that we are writing to disk. | 89 // tracks the offset into buf_ that we are writing to disk. |
91 | 90 |
92 scoped_refptr<net::GrowableIOBuffer> buf_; | 91 scoped_refptr<net::GrowableIOBuffer> buf_; |
93 bool buf_write_pending_; | 92 bool buf_write_pending_; |
94 int write_cursor_; | 93 int write_cursor_; |
95 | 94 |
96 // Helper writer object which maintains references to the net::FileStream and | 95 // Helper writer object which maintains references to the net::FileStream and |
97 // webkit_blob::ShareableFileReference. This is maintained separately so that, | 96 // storage::ShareableFileReference. This is maintained separately so that, |
98 // on Windows, the temporary file isn't deleted until after it is closed. | 97 // on Windows, the temporary file isn't deleted until after it is closed. |
99 class Writer; | 98 class Writer; |
100 Writer* writer_; | 99 Writer* writer_; |
101 | 100 |
102 // |next_buffer_size_| is the size of the buffer to be allocated on the next | 101 // |next_buffer_size_| is the size of the buffer to be allocated on the next |
103 // OnWillRead() call. We exponentially grow the size of the buffer allocated | 102 // OnWillRead() call. We exponentially grow the size of the buffer allocated |
104 // when our owner fills our buffers. On the first OnWillRead() call, we | 103 // when our owner fills our buffers. On the first OnWillRead() call, we |
105 // allocate a buffer of 32k and double it in OnReadCompleted() if the buffer | 104 // allocate a buffer of 32k and double it in OnReadCompleted() if the buffer |
106 // was filled, up to a maximum size of 512k. | 105 // was filled, up to a maximum size of 512k. |
107 int next_buffer_size_; | 106 int next_buffer_size_; |
108 | 107 |
109 bool did_defer_; | 108 bool did_defer_; |
110 | 109 |
111 bool completed_during_write_; | 110 bool completed_during_write_; |
112 GURL will_start_url_; | 111 GURL will_start_url_; |
113 net::URLRequestStatus completed_status_; | 112 net::URLRequestStatus completed_status_; |
114 std::string completed_security_info_; | 113 std::string completed_security_info_; |
115 | 114 |
116 base::WeakPtrFactory<RedirectToFileResourceHandler> weak_factory_; | 115 base::WeakPtrFactory<RedirectToFileResourceHandler> weak_factory_; |
117 | 116 |
118 DISALLOW_COPY_AND_ASSIGN(RedirectToFileResourceHandler); | 117 DISALLOW_COPY_AND_ASSIGN(RedirectToFileResourceHandler); |
119 }; | 118 }; |
120 | 119 |
121 } // namespace content | 120 } // namespace content |
122 | 121 |
123 #endif // CONTENT_BROWSER_LOADER_REDIRECT_TO_FILE_RESOURCE_HANDLER_H_ | 122 #endif // CONTENT_BROWSER_LOADER_REDIRECT_TO_FILE_RESOURCE_HANDLER_H_ |
OLD | NEW |