blob: ef3ec8f5057901314b32b831ca11e1a56fc8c93b [file] [log] [blame]
[email protected]27a112c2011-01-06 04:19:301// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]e3c404b2008-12-23 01:07:322// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]a0ce3282011-08-19 20:49:525#ifndef CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_RESOURCE_HANDLER_H_
6#define CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_RESOURCE_HANDLER_H_
[email protected]32b76ef2010-07-26 23:08:247#pragma once
[email protected]e3c404b2008-12-23 01:07:328
9#include <string>
10
[email protected]8e3ae68c2011-09-16 22:15:4711#include "base/callback.h"
[email protected]3b63f8f42011-03-28 01:54:1512#include "base/memory/scoped_ptr.h"
[email protected]92b24c12009-12-10 20:04:3513#include "base/timer.h"
[email protected]71bf3f5e2011-08-15 21:05:2214#include "content/browser/download/download_file.h"
[email protected]eda58402011-09-21 19:32:0215#include "content/browser/download/download_id.h"
[email protected]5de634712011-03-02 00:20:1916#include "content/browser/renderer_host/global_request_id.h"
17#include "content/browser/renderer_host/resource_handler.h"
[email protected]e3c404b2008-12-23 01:07:3218
[email protected]92b24c12009-12-10 20:04:3519class DownloadFileManager;
20class ResourceDispatcherHost;
[email protected]e3c404b2008-12-23 01:07:3221struct DownloadBuffer;
22
[email protected]edfe7fab2010-11-28 13:11:5223namespace net {
24class URLRequest;
25} // namespace net
26
[email protected]e3c404b2008-12-23 01:07:3227// Forwards data to the download thread.
[email protected]287b86b2011-02-26 00:11:3528class DownloadResourceHandler : public ResourceHandler {
[email protected]e3c404b2008-12-23 01:07:3229 public:
[email protected]eda58402011-09-21 19:32:0230 typedef base::Callback<void(DownloadId, net::Error)>
[email protected]8e3ae68c2011-09-16 22:15:4731 OnStartedCallback;
32
33 // started_cb will be called exactly once.
[email protected]c2475df2011-09-07 23:52:2234 DownloadResourceHandler(ResourceDispatcherHost* rdh,
35 int render_process_host_id,
36 int render_view_id,
37 int request_id,
38 const GURL& url,
[email protected]eda58402011-09-21 19:32:0239 DownloadId dl_id,
[email protected]c2475df2011-09-07 23:52:2240 DownloadFileManager* download_file_manager,
41 net::URLRequest* request,
42 bool save_as,
[email protected]8e3ae68c2011-09-16 22:15:4743 const OnStartedCallback& started_cb,
[email protected]c2475df2011-09-07 23:52:2244 const DownloadSaveInfo& save_info);
[email protected]e3c404b2008-12-23 01:07:3245
[email protected]78994ab02010-12-08 18:06:4446 virtual bool OnUploadProgress(int request_id, uint64 position, uint64 size);
[email protected]afd832c2010-03-02 04:53:3147
[email protected]e3c404b2008-12-23 01:07:3248 // Not needed, as this event handler ought to be the final resource.
[email protected]78994ab02010-12-08 18:06:4449 virtual bool OnRequestRedirected(int request_id, const GURL& url,
50 ResourceResponse* response, bool* defer);
[email protected]e3c404b2008-12-23 01:07:3251
52 // Send the download creation information to the download thread.
[email protected]78994ab02010-12-08 18:06:4453 virtual bool OnResponseStarted(int request_id, ResourceResponse* response);
[email protected]e3c404b2008-12-23 01:07:3254
[email protected]afd832c2010-03-02 04:53:3155 // Pass-through implementation.
[email protected]78994ab02010-12-08 18:06:4456 virtual bool OnWillStart(int request_id, const GURL& url, bool* defer);
[email protected]afd832c2010-03-02 04:53:3157
[email protected]e3c404b2008-12-23 01:07:3258 // Create a new buffer, which will be handed to the download thread for file
59 // writing and deletion.
[email protected]78994ab02010-12-08 18:06:4460 virtual bool OnWillRead(int request_id, net::IOBuffer** buf, int* buf_size,
61 int min_size);
[email protected]e3c404b2008-12-23 01:07:3262
[email protected]78994ab02010-12-08 18:06:4463 virtual bool OnReadCompleted(int request_id, int* bytes_read);
[email protected]e3c404b2008-12-23 01:07:3264
[email protected]78994ab02010-12-08 18:06:4465 virtual bool OnResponseCompleted(int request_id,
[email protected]27a112c2011-01-06 04:19:3066 const net::URLRequestStatus& status,
[email protected]78994ab02010-12-08 18:06:4467 const std::string& security_info);
68 virtual void OnRequestClosed();
[email protected]e3c404b2008-12-23 01:07:3269
70 // If the content-length header is not present (or contains something other
71 // than numbers), the incoming content_length is -1 (unknown size).
72 // Set the content length to 0 to indicate unknown size to DownloadManager.
73 void set_content_length(const int64& content_length);
74
75 void set_content_disposition(const std::string& content_disposition);
76
77 void CheckWriteProgress();
78
[email protected]da6e3922010-11-24 21:45:5079 std::string DebugString() const;
80
[email protected]e3c404b2008-12-23 01:07:3281 private:
[email protected]3690ebe02011-05-25 09:08:1982 virtual ~DownloadResourceHandler();
[email protected]84bbb2b52009-11-07 00:23:1483
[email protected]e3c404b2008-12-23 01:07:3284 void StartPauseTimer();
[email protected]8e3ae68c2011-09-16 22:15:4785 void CallStartedCB(net::Error error);
[email protected]e3c404b2008-12-23 01:07:3286
[email protected]eda58402011-09-21 19:32:0287 DownloadId download_id_;
[email protected]92b24c12009-12-10 20:04:3588 GlobalRequestID global_id_;
[email protected]e3c404b2008-12-23 01:07:3289 int render_view_id_;
[email protected]9dea9e1f2009-01-29 00:30:4790 scoped_refptr<net::IOBuffer> read_buffer_;
[email protected]e3c404b2008-12-23 01:07:3291 std::string content_disposition_;
[email protected]e3c404b2008-12-23 01:07:3292 int64 content_length_;
[email protected]aa033af2010-07-27 18:16:3993 DownloadFileManager* download_file_manager_;
[email protected]edfe7fab2010-11-28 13:11:5294 net::URLRequest* request_;
[email protected]e3c404b2008-12-23 01:07:3295 bool save_as_; // Request was initiated via "Save As" by the user.
[email protected]8e3ae68c2011-09-16 22:15:4796 OnStartedCallback started_cb_;
[email protected]8af9d032010-02-10 00:00:3297 DownloadSaveInfo save_info_;
[email protected]8ed01e322011-03-22 23:50:4098 scoped_ptr<DownloadBuffer> buffer_;
[email protected]e3c404b2008-12-23 01:07:3299 ResourceDispatcherHost* rdh_;
100 bool is_paused_;
101 base::OneShotTimer<DownloadResourceHandler> pause_timer_;
[email protected]9fc1e6c12010-12-22 21:07:05102 base::TimeTicks download_start_time_; // used to collect stats.
[email protected]e3c404b2008-12-23 01:07:32103 static const int kReadBufSize = 32768; // bytes
[email protected]3c2fe4e2009-01-08 12:07:23104 static const size_t kLoadsToWrite = 100; // number of data buffers queued
[email protected]e3c404b2008-12-23 01:07:32105 static const int kThrottleTimeMs = 200; // milliseconds
106
107 DISALLOW_COPY_AND_ASSIGN(DownloadResourceHandler);
108};
109
[email protected]a0ce3282011-08-19 20:49:52110#endif // CONTENT_BROWSER_DOWNLOAD_DOWNLOAD_RESOURCE_HANDLER_H_