- add sources.
[platform/framework/web/crosswalk.git] / src / content / browser / loader / redirect_to_file_resource_handler.h
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.
4
5 #ifndef CONTENT_BROWSER_LOADER_REDIRECT_TO_FILE_RESOURCE_HANDLER_H_
6 #define CONTENT_BROWSER_LOADER_REDIRECT_TO_FILE_RESOURCE_HANDLER_H_
7
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"
16
17 namespace net {
18 class FileStream;
19 class GrowableIOBuffer;
20 }
21
22 namespace webkit_blob {
23 class ShareableFileReference;
24 }
25
26 namespace content {
27 class ResourceDispatcherHostImpl;
28
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 {
32  public:
33   RedirectToFileResourceHandler(
34       scoped_ptr<ResourceHandler> next_handler,
35       net::URLRequest* request,
36       ResourceDispatcherHostImpl* resource_dispatcher_host);
37   virtual ~RedirectToFileResourceHandler();
38
39   // ResourceHandler implementation:
40   virtual bool OnResponseStarted(int request_id,
41                                  ResourceResponse* response,
42                                  bool* defer) OVERRIDE;
43   virtual bool OnWillStart(int request_id,
44                            const GURL& url,
45                            bool* defer) OVERRIDE;
46   virtual bool OnWillRead(int request_id,
47                           scoped_refptr<net::IOBuffer>* buf,
48                           int* buf_size,
49                           int min_size) OVERRIDE;
50   virtual bool OnReadCompleted(int request_id,
51                                int bytes_read,
52                                bool* defer) OVERRIDE;
53   virtual bool OnResponseCompleted(int request_id,
54                                    const net::URLRequestStatus& status,
55                                    const std::string& security_info) OVERRIDE;
56
57  private:
58   void DidCreateTemporaryFile(base::PlatformFileError error_code,
59                               base::PassPlatformFile file_handle,
60                               const base::FilePath& file_path);
61   void DidWriteToFile(int result);
62   bool WriteMore();
63   bool BufIsFull() const;
64   void ResumeIfDeferred();
65
66   base::WeakPtrFactory<RedirectToFileResourceHandler> weak_factory_;
67
68   ResourceDispatcherHostImpl* host_;
69
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.
76
77   scoped_refptr<net::GrowableIOBuffer> buf_;
78   bool buf_write_pending_;
79   int write_cursor_;
80
81   scoped_ptr<net::FileStream> file_stream_;
82   bool write_callback_pending_;
83
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_;
90
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_;
94
95   bool did_defer_ ;
96
97   bool completed_during_write_;
98   net::URLRequestStatus completed_status_;
99   std::string completed_security_info_;
100
101   DISALLOW_COPY_AND_ASSIGN(RedirectToFileResourceHandler);
102 };
103
104 }  // namespace content
105
106 #endif  // CONTENT_BROWSER_LOADER_REDIRECT_TO_FILE_RESOURCE_HANDLER_H_