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_
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_interrupt_reasons.h"
15 #include "content/public/browser/download_manager.h"
16 #include "content/public/browser/download_save_info.h"
17 #include "content/public/browser/download_url_parameters.h"
24 class ByteStreamReader
;
25 class ByteStreamWriter
;
26 class DownloadRequestHandle
;
27 class PowerSaveBlocker
;
28 struct DownloadCreateInfo
;
30 // Forwards data to the download thread.
31 class CONTENT_EXPORT DownloadResourceHandler
32 : public ResourceHandler
,
33 public base::SupportsWeakPtr
<DownloadResourceHandler
> {
35 struct DownloadTabInfo
;
37 // Size of the buffer used between the DownloadResourceHandler and the
38 // downstream receiver of its output.
39 static const int kDownloadByteStreamSize
;
41 // started_cb will be called exactly once on the UI thread.
42 // |id| should be invalid if the id should be automatically assigned.
43 DownloadResourceHandler(
45 net::URLRequest
* request
,
46 const DownloadUrlParameters::OnStartedCallback
& started_cb
,
47 scoped_ptr
<DownloadSaveInfo
> save_info
);
49 bool OnUploadProgress(uint64 position
, uint64 size
) override
;
51 bool OnRequestRedirected(const net::RedirectInfo
& redirect_info
,
52 ResourceResponse
* response
,
53 bool* defer
) override
;
55 // Send the download creation information to the download thread.
56 bool OnResponseStarted(ResourceResponse
* response
, bool* defer
) override
;
58 // Pass-through implementation.
59 bool OnWillStart(const GURL
& url
, bool* defer
) override
;
61 // Pass-through implementation.
62 bool OnBeforeNetworkStart(const GURL
& url
, bool* defer
) override
;
64 // Create a new buffer, which will be handed to the download thread for file
65 // writing and deletion.
66 bool OnWillRead(scoped_refptr
<net::IOBuffer
>* buf
,
68 int min_size
) override
;
70 bool OnReadCompleted(int bytes_read
, bool* defer
) override
;
72 void OnResponseCompleted(const net::URLRequestStatus
& status
,
73 const std::string
& security_info
,
74 bool* defer
) override
;
76 // N/A to this flavor of DownloadHandler.
77 void OnDataDownloaded(int bytes_downloaded
) override
;
82 // May result in this object being deleted by its owner.
85 std::string
DebugString() const;
88 ~DownloadResourceHandler() override
;
90 // Arrange for started_cb_ to be called on the UI thread with the
91 // below values, nulling out started_cb_. Should only be called
93 void CallStartedCB(DownloadItem
* item
,
94 DownloadInterruptReason interrupt_reason
);
97 // This is read only on the IO thread, but may only
98 // be called on the UI thread.
99 DownloadUrlParameters::OnStartedCallback started_cb_
;
100 scoped_ptr
<DownloadSaveInfo
> save_info_
;
102 // Stores information about the download that must be acquired on the UI
103 // thread before StartOnUIThread is called.
104 // Created on IO thread, but only accessed on UI thread. |tab_info_| holds
105 // the pointer until we pass it off to StartOnUIThread or DeleteSoon.
106 DownloadTabInfo
* tab_info_
;
109 scoped_refptr
<net::IOBuffer
> read_buffer_
; // From URLRequest.
110 scoped_ptr
<ByteStreamWriter
> stream_writer_
; // To rest of system.
112 // Keeps the system from sleeping while this ResourceHandler is alive. If the
113 // system enters power saving mode while a request is alive, it can cause the
114 // request to fail and the associated download will be interrupted.
115 scoped_ptr
<PowerSaveBlocker
> power_save_blocker_
;
117 // The following are used to collect stats.
118 base::TimeTicks download_start_time_
;
119 base::TimeTicks last_read_time_
;
120 base::TimeTicks last_stream_pause_time_
;
121 base::TimeDelta total_pause_time_
;
122 size_t last_buffer_size_
;
129 bool on_response_started_called_
;
131 static const int kReadBufSize
= 32768; // bytes
132 static const int kThrottleTimeMs
= 200; // milliseconds
134 DISALLOW_COPY_AND_ASSIGN(DownloadResourceHandler
);
137 } // namespace content
139 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_RESOURCE_HANDLER_H_