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 "content/browser/loader/resource_handler.h"
13 #include "content/public/browser/download_interrupt_reasons.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"
23 class ByteStreamReader
;
24 class ByteStreamWriter
;
25 class DownloadRequestHandle
;
26 class PowerSaveBlocker
;
27 struct DownloadCreateInfo
;
29 // Forwards data to the download thread.
30 class CONTENT_EXPORT DownloadResourceHandler
31 : public ResourceHandler
,
32 public base::SupportsWeakPtr
<DownloadResourceHandler
> {
34 struct DownloadTabInfo
;
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(
44 net::URLRequest
* request
,
45 const DownloadUrlParameters::OnStartedCallback
& started_cb
,
46 scoped_ptr
<DownloadSaveInfo
> save_info
);
48 bool OnRequestRedirected(const net::RedirectInfo
& redirect_info
,
49 ResourceResponse
* response
,
50 bool* defer
) override
;
52 // Send the download creation information to the download thread.
53 bool OnResponseStarted(ResourceResponse
* response
, bool* defer
) override
;
55 // Pass-through implementation.
56 bool OnWillStart(const GURL
& url
, bool* defer
) override
;
58 // Pass-through implementation.
59 bool OnBeforeNetworkStart(const GURL
& url
, bool* defer
) override
;
61 // Create a new buffer, which will be handed to the download thread for file
62 // writing and deletion.
63 bool OnWillRead(scoped_refptr
<net::IOBuffer
>* buf
,
65 int min_size
) override
;
67 bool OnReadCompleted(int bytes_read
, bool* defer
) override
;
69 void OnResponseCompleted(const net::URLRequestStatus
& status
,
70 const std::string
& security_info
,
71 bool* defer
) override
;
73 // N/A to this flavor of DownloadHandler.
74 void OnDataDownloaded(int bytes_downloaded
) override
;
79 // May result in this object being deleted by its owner.
82 std::string
DebugString() const;
85 ~DownloadResourceHandler() override
;
87 // Arrange for started_cb_ to be called on the UI thread with the
88 // below values, nulling out started_cb_. Should only be called
90 void CallStartedCB(DownloadItem
* item
,
91 DownloadInterruptReason interrupt_reason
);
94 // This is read only on the IO thread, but may only
95 // be called on the UI thread.
96 DownloadUrlParameters::OnStartedCallback started_cb_
;
97 scoped_ptr
<DownloadSaveInfo
> save_info_
;
99 // Stores information about the download that must be acquired on the UI
100 // thread before StartOnUIThread is called.
101 // Created on IO thread, but only accessed on UI thread. |tab_info_| holds
102 // the pointer until we pass it off to StartOnUIThread or DeleteSoon.
103 // Marked as a scoped_ptr<> to indicate ownership of the structure, but
104 // deletion must occur on the IO thread.
105 scoped_ptr
<DownloadTabInfo
> tab_info_
;
108 scoped_refptr
<net::IOBuffer
> read_buffer_
; // From URLRequest.
109 scoped_ptr
<ByteStreamWriter
> stream_writer_
; // To rest of system.
111 // Keeps the system from sleeping while this ResourceHandler is alive. If the
112 // system enters power saving mode while a request is alive, it can cause the
113 // request to fail and the associated download will be interrupted.
114 scoped_ptr
<PowerSaveBlocker
> power_save_blocker_
;
116 // The following are used to collect stats.
117 base::TimeTicks download_start_time_
;
118 base::TimeTicks last_read_time_
;
119 base::TimeTicks last_stream_pause_time_
;
120 base::TimeDelta total_pause_time_
;
121 size_t last_buffer_size_
;
128 bool on_response_started_called_
;
130 static const int kReadBufSize
= 32768; // bytes
131 static const int kThrottleTimeMs
= 200; // milliseconds
133 DISALLOW_COPY_AND_ASSIGN(DownloadResourceHandler
);
136 } // namespace content
138 #endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_RESOURCE_HANDLER_H_