Upstream version 10.39.225.0
[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/basictypes.h"
9 #include "base/callback.h"
10 #include "base/compiler_specific.h"
11 #include "base/files/file.h"
12 #include "base/files/file_path.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/memory/weak_ptr.h"
16 #include "content/browser/loader/layered_resource_handler.h"
17 #include "content/browser/loader/temporary_file_stream.h"
18 #include "content/common/content_export.h"
19 #include "net/url_request/url_request.h"
20 #include "net/url_request/url_request_status.h"
21 #include "url/gurl.h"
22
23 namespace net {
24 class FileStream;
25 class GrowableIOBuffer;
26 }
27
28 namespace storage {
29 class ShareableFileReference;
30 }
31
32 namespace content {
33
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
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 class CONTENT_EXPORT RedirectToFileResourceHandler
40     : public LayeredResourceHandler {
41  public:
42   typedef base::Callback<void(const CreateTemporaryFileStreamCallback&)>
43       CreateTemporaryFileStreamFunction;
44
45   // Create a RedirectToFileResourceHandler for |request| which wraps
46   // |next_handler|.
47   RedirectToFileResourceHandler(scoped_ptr<ResourceHandler> next_handler,
48                                 net::URLRequest* request);
49   virtual ~RedirectToFileResourceHandler();
50
51   // Replace the CreateTemporaryFileStream implementation with a mocked one for
52   // testing purposes. The function should create a net::FileStream and a
53   // ShareableFileReference and then asynchronously pass them to the
54   // CreateTemporaryFileStreamCallback.
55   void SetCreateTemporaryFileStreamFunctionForTesting(
56       const CreateTemporaryFileStreamFunction& create_temporary_file_stream);
57
58   // LayeredResourceHandler implementation:
59   virtual bool OnResponseStarted(ResourceResponse* response,
60                                  bool* defer) OVERRIDE;
61   virtual bool OnWillStart(const GURL& url, bool* defer) OVERRIDE;
62   virtual bool OnWillRead(scoped_refptr<net::IOBuffer>* buf,
63                           int* buf_size,
64                           int min_size) OVERRIDE;
65   virtual bool OnReadCompleted(int bytes_read, bool* defer) OVERRIDE;
66   virtual void OnResponseCompleted(const net::URLRequestStatus& status,
67                                    const std::string& security_info,
68                                    bool* defer) OVERRIDE;
69
70  private:
71   void DidCreateTemporaryFile(base::File::Error error_code,
72                               scoped_ptr<net::FileStream> file_stream,
73                               storage::ShareableFileReference* deletable_file);
74
75   // Called by RedirectToFileResourceHandler::Writer.
76   void DidWriteToFile(int result);
77
78   bool WriteMore();
79   bool BufIsFull() const;
80   void ResumeIfDeferred();
81
82   CreateTemporaryFileStreamFunction create_temporary_file_stream_;
83
84   // We allocate a single, fixed-size IO buffer (buf_) used to read from the
85   // network (buf_write_pending_ is true while the system is copying data into
86   // buf_), and then write this buffer out to disk (write_callback_pending_ is
87   // true while writing to disk).  Reading from the network is suspended while
88   // the buffer is full (BufIsFull returns true).  The write_cursor_ member
89   // tracks the offset into buf_ that we are writing to disk.
90
91   scoped_refptr<net::GrowableIOBuffer> buf_;
92   bool buf_write_pending_;
93   int write_cursor_;
94
95   // Helper writer object which maintains references to the net::FileStream and
96   // storage::ShareableFileReference. This is maintained separately so that,
97   // on Windows, the temporary file isn't deleted until after it is closed.
98   class Writer;
99   Writer* writer_;
100
101   // |next_buffer_size_| is the size of the buffer to be allocated on the next
102   // OnWillRead() call.  We exponentially grow the size of the buffer allocated
103   // when our owner fills our buffers. On the first OnWillRead() call, we
104   // allocate a buffer of 32k and double it in OnReadCompleted() if the buffer
105   // was filled, up to a maximum size of 512k.
106   int next_buffer_size_;
107
108   bool did_defer_;
109
110   bool completed_during_write_;
111   GURL will_start_url_;
112   net::URLRequestStatus completed_status_;
113   std::string completed_security_info_;
114
115   base::WeakPtrFactory<RedirectToFileResourceHandler> weak_factory_;
116
117   DISALLOW_COPY_AND_ASSIGN(RedirectToFileResourceHandler);
118 };
119
120 }  // namespace content
121
122 #endif  // CONTENT_BROWSER_LOADER_REDIRECT_TO_FILE_RESOURCE_HANDLER_H_