summaryrefslogtreecommitdiff
path: root/chromium/content/browser/download/url_downloader.cc
blob: b178a1d4b2654f52099ef953e1b179b18ed5554a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/browser/download/url_downloader.h"

#include "base/callback_helpers.h"
#include "base/location.h"
#include "base/macros.h"
#include "base/threading/sequenced_task_runner_handle.h"
#include "content/browser/byte_stream.h"
#include "content/browser/download/download_create_info.h"
#include "content/browser/download/download_manager_impl.h"
#include "content/browser/download/download_request_handle.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/download_interrupt_reasons.h"
#include "content/public/browser/download_save_info.h"
#include "net/base/io_buffer.h"
#include "net/base/load_flags.h"
#include "net/base/net_errors.h"
#include "net/http/http_response_headers.h"
#include "net/http/http_status_code.h"
#include "ui/base/page_transition_types.h"

namespace content {

class UrlDownloader::RequestHandle : public DownloadRequestHandleInterface {
 public:
  RequestHandle(base::WeakPtr<UrlDownloader> downloader,
                base::WeakPtr<DownloadManagerImpl> download_manager_impl,
                scoped_refptr<base::SequencedTaskRunner> downloader_task_runner)
      : downloader_(downloader),
        download_manager_impl_(download_manager_impl),
        downloader_task_runner_(downloader_task_runner) {}
  RequestHandle(RequestHandle&& other)
      : downloader_(std::move(other.downloader_)),
        download_manager_impl_(std::move(other.download_manager_impl_)),
        downloader_task_runner_(std::move(other.downloader_task_runner_)) {}
  RequestHandle& operator=(RequestHandle&& other) {
    downloader_ = std::move(other.downloader_);
    download_manager_impl_ = std::move(other.download_manager_impl_);
    downloader_task_runner_ = std::move(other.downloader_task_runner_);
    return *this;
  }

  // DownloadRequestHandleInterface
  WebContents* GetWebContents() const override { return nullptr; }
  DownloadManager* GetDownloadManager() const override {
    return download_manager_impl_ ? download_manager_impl_.get() : nullptr;
  }
  void PauseRequest() const override {
    downloader_task_runner_->PostTask(
        FROM_HERE, base::Bind(&UrlDownloader::PauseRequest, downloader_));
  }
  void ResumeRequest() const override {
    downloader_task_runner_->PostTask(
        FROM_HERE, base::Bind(&UrlDownloader::ResumeRequest, downloader_));
  }
  void CancelRequest() const override {
    downloader_task_runner_->PostTask(
        FROM_HERE, base::Bind(&UrlDownloader::CancelRequest, downloader_));
  }
  std::string DebugString() const override { return std::string(); }

 private:
  base::WeakPtr<UrlDownloader> downloader_;
  base::WeakPtr<DownloadManagerImpl> download_manager_impl_;
  scoped_refptr<base::SequencedTaskRunner> downloader_task_runner_;

  DISALLOW_COPY_AND_ASSIGN(RequestHandle);
};

// static
scoped_ptr<UrlDownloader> UrlDownloader::BeginDownload(
    base::WeakPtr<DownloadManagerImpl> download_manager,
    scoped_ptr<net::URLRequest> request,
    const Referrer& referrer,
    bool prefer_cache,
    scoped_ptr<DownloadSaveInfo> save_info,
    uint32_t download_id,
    const DownloadUrlParameters::OnStartedCallback& started_callback) {
  if (!referrer.url.is_valid())
    request->SetReferrer(std::string());
  else
    request->SetReferrer(referrer.url.spec());

  int extra_load_flags = net::LOAD_NORMAL;
  if (prefer_cache) {
    // If there is upload data attached, only retrieve from cache because there
    // is no current mechanism to prompt the user for their consent for a
    // re-post. For GETs, try to retrieve data from the cache and skip
    // validating the entry if present.
    if (request->get_upload() != NULL)
      extra_load_flags |= net::LOAD_ONLY_FROM_CACHE;
    else
      extra_load_flags |= net::LOAD_PREFERRING_CACHE;
  } else {
    extra_load_flags |= net::LOAD_DISABLE_CACHE;
  }
  request->SetLoadFlags(request->load_flags() | extra_load_flags);

  if (request->url().SchemeIs(url::kBlobScheme))
    return nullptr;

  // From this point forward, the |UrlDownloader| is responsible for
  // |started_callback|.
  scoped_ptr<UrlDownloader> downloader(
      new UrlDownloader(std::move(request), download_manager,
                        std::move(save_info), download_id, started_callback));
  downloader->Start();

  return downloader;
}

UrlDownloader::UrlDownloader(
    scoped_ptr<net::URLRequest> request,
    base::WeakPtr<DownloadManagerImpl> manager,
    scoped_ptr<DownloadSaveInfo> save_info,
    uint32_t download_id,
    const DownloadUrlParameters::OnStartedCallback& on_started_callback)
    : request_(std::move(request)),
      manager_(manager),
      download_id_(download_id),
      on_started_callback_(on_started_callback),
      handler_(
          request_.get(),
          std::move(save_info),
          base::Bind(&UrlDownloader::ResumeReading, base::Unretained(this))),
      weak_ptr_factory_(this) {}

UrlDownloader::~UrlDownloader() {
  CallStartedCallbackOnFailure(DOWNLOAD_INTERRUPT_REASON_USER_CANCELED);
}

void UrlDownloader::Start() {
  DCHECK(!request_->is_pending());

  if (!request_->status().is_success())
    return;

  request_->set_delegate(this);
  request_->Start();
}

void UrlDownloader::OnReceivedRedirect(net::URLRequest* request,
                                       const net::RedirectInfo& redirect_info,
                                       bool* defer_redirect) {
  DVLOG(1) << "OnReceivedRedirect: " << request_->url().spec();
  request_->CancelWithError(net::ERR_ABORTED);
}

void UrlDownloader::OnResponseStarted(net::URLRequest* request) {
  DVLOG(1) << "OnResponseStarted: " << request_->url().spec();

  if (!request_->status().is_success()) {
    ResponseCompleted();
    return;
  }

  scoped_ptr<DownloadCreateInfo> create_info;
  scoped_ptr<ByteStreamReader> stream_reader;

  handler_.OnResponseStarted(&create_info, &stream_reader);

  create_info->download_id = download_id_;
  create_info->request_handle.reset(
      new RequestHandle(weak_ptr_factory_.GetWeakPtr(), manager_,
                        base::SequencedTaskRunnerHandle::Get()));
  BrowserThread::PostTask(
      BrowserThread::UI, FROM_HERE,
      base::Bind(&DownloadManagerImpl::StartDownload, manager_,
                 base::Passed(&create_info), base::Passed(&stream_reader),
                 base::ResetAndReturn(&on_started_callback_)));

  if (request_->status().is_success())
    StartReading(false);  // Read the first chunk.
  else
    ResponseCompleted();
}

void UrlDownloader::StartReading(bool is_continuation) {
  int bytes_read;

  // Make sure we track the buffer in at least one place.  This ensures it gets
  // deleted even in the case the request has already finished its job and
  // doesn't use the buffer.
  scoped_refptr<net::IOBuffer> buf;
  int buf_size;
  if (!handler_.OnWillRead(&buf, &buf_size, -1)) {
    request_->CancelWithError(net::ERR_ABORTED);
    base::SequencedTaskRunnerHandle::Get()->PostTask(
        FROM_HERE, base::Bind(&UrlDownloader::ResponseCompleted,
                              weak_ptr_factory_.GetWeakPtr()));
    return;
  }

  DCHECK(buf.get());
  DCHECK(buf_size > 0);

  request_->Read(buf.get(), buf_size, &bytes_read);

  // If IO is pending, wait for the URLRequest to call OnReadCompleted.
  if (request_->status().is_io_pending())
    return;

  if (!is_continuation || bytes_read <= 0) {
    OnReadCompleted(request_.get(), bytes_read);
  } else {
    // Else, trigger OnReadCompleted asynchronously to avoid starving the IO
    // thread in case the URLRequest can provide data synchronously.
    base::SequencedTaskRunnerHandle::Get()->PostTask(
        FROM_HERE,
        base::Bind(&UrlDownloader::OnReadCompleted,
                   weak_ptr_factory_.GetWeakPtr(), request_.get(), bytes_read));
  }
}

void UrlDownloader::OnReadCompleted(net::URLRequest* request, int bytes_read) {
  DVLOG(1) << "OnReadCompleted: \"" << request_->url().spec() << "\""
           << " bytes_read = " << bytes_read;

  // bytes_read == -1 always implies an error.
  if (bytes_read == -1 || !request_->status().is_success()) {
    ResponseCompleted();
    return;
  }

  DCHECK(bytes_read >= 0);
  DCHECK(request_->status().is_success());

  bool defer = false;
  if (!handler_.OnReadCompleted(bytes_read, &defer)) {
    request_->CancelWithError(net::ERR_ABORTED);
    return;
  } else if (defer) {
    return;
  }

  if (!request_->status().is_success())
    return;

  if (bytes_read > 0) {
    StartReading(true);  // Read the next chunk.
  } else {
    // URLRequest reported an EOF. Call ResponseCompleted.
    DCHECK_EQ(0, bytes_read);
    ResponseCompleted();
  }
}

void UrlDownloader::ResponseCompleted() {
  DVLOG(1) << "ResponseCompleted: " << request_->url().spec();

  handler_.OnResponseCompleted(request_->status());
  BrowserThread::PostTask(
      BrowserThread::UI, FROM_HERE,
      base::Bind(&DownloadManagerImpl::RemoveUrlDownloader, manager_, this));
}

void UrlDownloader::ResumeReading() {
  if (request_->status().is_success()) {
    StartReading(false);  // Read the next chunk (OK to complete synchronously).
  } else {
    ResponseCompleted();
  }
}

void UrlDownloader::CallStartedCallbackOnFailure(
    DownloadInterruptReason result) {
  if (on_started_callback_.is_null())
    return;
  BrowserThread::PostTask(
      BrowserThread::UI, FROM_HERE,
      base::Bind(base::ResetAndReturn(&on_started_callback_), nullptr, result));
}

void UrlDownloader::PauseRequest() {
  handler_.PauseRequest();
}

void UrlDownloader::ResumeRequest() {
  handler_.ResumeRequest();
}

void UrlDownloader::CancelRequest() {
  BrowserThread::PostTask(
      BrowserThread::UI, FROM_HERE,
      base::Bind(&DownloadManagerImpl::RemoveUrlDownloader, manager_, this));
}

}  // namespace content