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
3 // found in the LICENSE file.
5 #ifndef CONTENT_BROWSER_LOADER_REDIRECT_TO_FILE_RESOURCE_HANDLER_H_
6 #define CONTENT_BROWSER_LOADER_REDIRECT_TO_FILE_RESOURCE_HANDLER_H_
8 #include "base/files/file_path.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/memory/weak_ptr.h"
12 #include "base/platform_file.h"
13 #include "content/browser/loader/layered_resource_handler.h"
14 #include "net/url_request/url_request.h"
15 #include "net/url_request/url_request_status.h"
19 class GrowableIOBuffer;
22 namespace webkit_blob {
23 class ShareableFileReference;
27 class ResourceDispatcherHostImpl;
29 // Redirects network data to a file. This is intended to be layered in front
30 // of either the AsyncResourceHandler or the SyncResourceHandler.
31 class RedirectToFileResourceHandler : public LayeredResourceHandler {
33 RedirectToFileResourceHandler(
34 scoped_ptr<ResourceHandler> next_handler,
35 net::URLRequest* request,
36 ResourceDispatcherHostImpl* resource_dispatcher_host);
37 virtual ~RedirectToFileResourceHandler();
39 // ResourceHandler implementation:
40 virtual bool OnResponseStarted(int request_id,
41 ResourceResponse* response,
42 bool* defer) OVERRIDE;
43 virtual bool OnWillStart(int request_id,
45 bool* defer) OVERRIDE;
46 virtual bool OnWillRead(int request_id,
47 scoped_refptr<net::IOBuffer>* buf,
49 int min_size) OVERRIDE;
50 virtual bool OnReadCompleted(int request_id,
52 bool* defer) OVERRIDE;
53 virtual bool OnResponseCompleted(int request_id,
54 const net::URLRequestStatus& status,
55 const std::string& security_info) OVERRIDE;
58 void DidCreateTemporaryFile(base::PlatformFileError error_code,
59 base::PassPlatformFile file_handle,
60 const base::FilePath& file_path);
61 void DidWriteToFile(int result);
63 bool BufIsFull() const;
64 void ResumeIfDeferred();
66 base::WeakPtrFactory<RedirectToFileResourceHandler> weak_factory_;
68 ResourceDispatcherHostImpl* host_;
70 // We allocate a single, fixed-size IO buffer (buf_) used to read from the
71 // network (buf_write_pending_ is true while the system is copying data into
72 // buf_), and then write this buffer out to disk (write_callback_pending_ is
73 // true while writing to disk). Reading from the network is suspended while
74 // the buffer is full (BufIsFull returns true). The write_cursor_ member
75 // tracks the offset into buf_ that we are writing to disk.
77 scoped_refptr<net::GrowableIOBuffer> buf_;
78 bool buf_write_pending_;
81 scoped_ptr<net::FileStream> file_stream_;
82 bool write_callback_pending_;
84 // |next_buffer_size_| is the size of the buffer to be allocated on the next
85 // OnWillRead() call. We exponentially grow the size of the buffer allocated
86 // when our owner fills our buffers. On the first OnWillRead() call, we
87 // allocate a buffer of 32k and double it in OnReadCompleted() if the buffer
88 // was filled, up to a maximum size of 512k.
89 int next_buffer_size_;
91 // We create a ShareableFileReference that's deletable for the temp
92 // file created as a result of the download.
93 scoped_refptr<webkit_blob::ShareableFileReference> deletable_file_;
97 bool completed_during_write_;
98 net::URLRequestStatus completed_status_;
99 std::string completed_security_info_;
101 DISALLOW_COPY_AND_ASSIGN(RedirectToFileResourceHandler);
104 } // namespace content
106 #endif // CONTENT_BROWSER_LOADER_REDIRECT_TO_FILE_RESOURCE_HANDLER_H_