IndexedDBFactory now ForceCloses databases.
[chromium-blink-merge.git] / content / browser / download / download_resource_handler.h
blob138d93e73a7d70cac98671156c1147adf039586c
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_DOWNLOAD_DOWNLOAD_RESOURCE_HANDLER_H_
6 #define CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_RESOURCE_HANDLER_H_
8 #include <string>
10 #include "base/callback.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/timer/timer.h"
13 #include "content/browser/loader/resource_handler.h"
14 #include "content/public/browser/download_manager.h"
15 #include "content/public/browser/download_save_info.h"
16 #include "content/public/browser/download_url_parameters.h"
17 #include "content/public/browser/global_request_id.h"
18 #include "net/base/net_errors.h"
21 namespace net {
22 class URLRequest;
23 } // namespace net
25 namespace content {
26 class ByteStreamWriter;
27 class ByteStreamReader;
28 class DownloadRequestHandle;
29 struct DownloadCreateInfo;
31 // Forwards data to the download thread.
32 class CONTENT_EXPORT DownloadResourceHandler
33 : public ResourceHandler,
34 public base::SupportsWeakPtr<DownloadResourceHandler> {
35 public:
36 // Size of the buffer used between the DownloadResourceHandler and the
37 // downstream receiver of its output.
38 static const int kDownloadByteStreamSize;
40 // started_cb will be called exactly once on the UI thread.
41 // |id| should be invalid if the id should be automatically assigned.
42 DownloadResourceHandler(
43 uint32 id,
44 net::URLRequest* request,
45 const DownloadUrlParameters::OnStartedCallback& started_cb,
46 scoped_ptr<DownloadSaveInfo> save_info);
48 virtual bool OnUploadProgress(int request_id,
49 uint64 position,
50 uint64 size) OVERRIDE;
52 virtual bool OnRequestRedirected(int request_id,
53 const GURL& url,
54 ResourceResponse* response,
55 bool* defer) OVERRIDE;
57 // Send the download creation information to the download thread.
58 virtual bool OnResponseStarted(int request_id,
59 ResourceResponse* response,
60 bool* defer) OVERRIDE;
62 // Pass-through implementation.
63 virtual bool OnWillStart(int request_id,
64 const GURL& url,
65 bool* defer) OVERRIDE;
67 // Pass-through implementation.
68 virtual bool OnBeforeNetworkStart(int request_id,
69 const GURL& url,
70 bool* defer) OVERRIDE;
72 // Create a new buffer, which will be handed to the download thread for file
73 // writing and deletion.
74 virtual bool OnWillRead(int request_id,
75 scoped_refptr<net::IOBuffer>* buf,
76 int* buf_size,
77 int min_size) OVERRIDE;
79 virtual bool OnReadCompleted(int request_id, int bytes_read,
80 bool* defer) OVERRIDE;
82 virtual void OnResponseCompleted(int request_id,
83 const net::URLRequestStatus& status,
84 const std::string& security_info,
85 bool* defer) OVERRIDE;
87 // N/A to this flavor of DownloadHandler.
88 virtual void OnDataDownloaded(int request_id, int bytes_downloaded) OVERRIDE;
90 void PauseRequest();
91 void ResumeRequest();
92 void CancelRequest();
94 std::string DebugString() const;
96 private:
97 virtual ~DownloadResourceHandler();
99 // Arrange for started_cb_ to be called on the UI thread with the
100 // below values, nulling out started_cb_. Should only be called
101 // on the IO thread.
102 void CallStartedCB(DownloadItem* item, net::Error error);
104 uint32 download_id_;
105 // This is read only on the IO thread, but may only
106 // be called on the UI thread.
107 DownloadUrlParameters::OnStartedCallback started_cb_;
108 scoped_ptr<DownloadSaveInfo> save_info_;
110 // Data flow
111 scoped_refptr<net::IOBuffer> read_buffer_; // From URLRequest.
112 scoped_ptr<ByteStreamWriter> stream_writer_; // To rest of system.
114 // The following are used to collect stats.
115 base::TimeTicks download_start_time_;
116 base::TimeTicks last_read_time_;
117 base::TimeTicks last_stream_pause_time_;
118 base::TimeDelta total_pause_time_;
119 size_t last_buffer_size_;
120 int64 bytes_read_;
122 int pause_count_;
123 bool was_deferred_;
125 // For DCHECKing
126 bool on_response_started_called_;
128 static const int kReadBufSize = 32768; // bytes
129 static const int kThrottleTimeMs = 200; // milliseconds
131 DISALLOW_COPY_AND_ASSIGN(DownloadResourceHandler);
134 } // namespace content
136 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_RESOURCE_HANDLER_H_