blob: e252675a5db7b6621346bd31c88f504b10a6e370 [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
5#ifndef CHROME_BROWSER_RENDERER_HOST_DOWNLOAD_RESOURCE_HANDLER_H_
6#define CHROME_BROWSER_RENDERER_HOST_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]92b24c12009-12-10 20:04:3511#include "base/timer.h"
[email protected]8af9d032010-02-10 00:00:3212#include "chrome/browser/download/download_file.h"
[email protected]5de634712011-03-02 00:20:1913#include "content/browser/renderer_host/global_request_id.h"
14#include "content/browser/renderer_host/resource_handler.h"
[email protected]e3c404b2008-12-23 01:07:3215
[email protected]92b24c12009-12-10 20:04:3516class DownloadFileManager;
17class ResourceDispatcherHost;
[email protected]e3c404b2008-12-23 01:07:3218struct DownloadBuffer;
19
[email protected]edfe7fab2010-11-28 13:11:5220namespace net {
21class URLRequest;
22} // namespace net
23
[email protected]e3c404b2008-12-23 01:07:3224// Forwards data to the download thread.
[email protected]287b86b2011-02-26 00:11:3525class DownloadResourceHandler : public ResourceHandler {
[email protected]e3c404b2008-12-23 01:07:3226 public:
27 DownloadResourceHandler(ResourceDispatcherHost* rdh,
28 int render_process_host_id,
29 int render_view_id,
30 int request_id,
[email protected]f6b48532009-02-12 01:56:3231 const GURL& url,
[email protected]aa033af2010-07-27 18:16:3932 DownloadFileManager* download_file_manager,
[email protected]edfe7fab2010-11-28 13:11:5233 net::URLRequest* request,
[email protected]6aa4a1c02010-01-15 18:49:5834 bool save_as,
[email protected]8af9d032010-02-10 00:00:3235 const DownloadSaveInfo& save_info);
[email protected]e3c404b2008-12-23 01:07:3236
[email protected]78994ab02010-12-08 18:06:4437 virtual bool OnUploadProgress(int request_id, uint64 position, uint64 size);
[email protected]afd832c2010-03-02 04:53:3138
[email protected]e3c404b2008-12-23 01:07:3239 // Not needed, as this event handler ought to be the final resource.
[email protected]78994ab02010-12-08 18:06:4440 virtual bool OnRequestRedirected(int request_id, const GURL& url,
41 ResourceResponse* response, bool* defer);
[email protected]e3c404b2008-12-23 01:07:3242
43 // Send the download creation information to the download thread.
[email protected]78994ab02010-12-08 18:06:4444 virtual bool OnResponseStarted(int request_id, ResourceResponse* response);
[email protected]e3c404b2008-12-23 01:07:3245
[email protected]afd832c2010-03-02 04:53:3146 // Pass-through implementation.
[email protected]78994ab02010-12-08 18:06:4447 virtual bool OnWillStart(int request_id, const GURL& url, bool* defer);
[email protected]afd832c2010-03-02 04:53:3148
[email protected]e3c404b2008-12-23 01:07:3249 // Create a new buffer, which will be handed to the download thread for file
50 // writing and deletion.
[email protected]78994ab02010-12-08 18:06:4451 virtual bool OnWillRead(int request_id, net::IOBuffer** buf, int* buf_size,
52 int min_size);
[email protected]e3c404b2008-12-23 01:07:3253
[email protected]78994ab02010-12-08 18:06:4454 virtual bool OnReadCompleted(int request_id, int* bytes_read);
[email protected]e3c404b2008-12-23 01:07:3255
[email protected]78994ab02010-12-08 18:06:4456 virtual bool OnResponseCompleted(int request_id,
[email protected]27a112c2011-01-06 04:19:3057 const net::URLRequestStatus& status,
[email protected]78994ab02010-12-08 18:06:4458 const std::string& security_info);
59 virtual void OnRequestClosed();
[email protected]e3c404b2008-12-23 01:07:3260
61 // If the content-length header is not present (or contains something other
62 // than numbers), the incoming content_length is -1 (unknown size).
63 // Set the content length to 0 to indicate unknown size to DownloadManager.
64 void set_content_length(const int64& content_length);
65
66 void set_content_disposition(const std::string& content_disposition);
67
68 void CheckWriteProgress();
69
[email protected]da6e3922010-11-24 21:45:5070 std::string DebugString() const;
71
[email protected]e3c404b2008-12-23 01:07:3272 private:
[email protected]601858c02010-09-01 17:08:2073 ~DownloadResourceHandler();
[email protected]84bbb2b52009-11-07 00:23:1474
[email protected]e3c404b2008-12-23 01:07:3275 void StartPauseTimer();
76
77 int download_id_;
[email protected]92b24c12009-12-10 20:04:3578 GlobalRequestID global_id_;
[email protected]e3c404b2008-12-23 01:07:3279 int render_view_id_;
[email protected]9dea9e1f2009-01-29 00:30:4780 scoped_refptr<net::IOBuffer> read_buffer_;
[email protected]e3c404b2008-12-23 01:07:3281 std::string content_disposition_;
[email protected]79558c2a2011-01-14 21:46:0782 GURL url_; // final URL from which we're downloading.
83 GURL original_url_; // original URL before any redirection by the server.
[email protected]e3c404b2008-12-23 01:07:3284 int64 content_length_;
[email protected]aa033af2010-07-27 18:16:3985 DownloadFileManager* download_file_manager_;
[email protected]edfe7fab2010-11-28 13:11:5286 net::URLRequest* request_;
[email protected]e3c404b2008-12-23 01:07:3287 bool save_as_; // Request was initiated via "Save As" by the user.
[email protected]8af9d032010-02-10 00:00:3288 DownloadSaveInfo save_info_;
[email protected]e3c404b2008-12-23 01:07:3289 DownloadBuffer* buffer_;
90 ResourceDispatcherHost* rdh_;
91 bool is_paused_;
92 base::OneShotTimer<DownloadResourceHandler> pause_timer_;
[email protected]9fc1e6c12010-12-22 21:07:0593 base::TimeTicks download_start_time_; // used to collect stats.
[email protected]e3c404b2008-12-23 01:07:3294 static const int kReadBufSize = 32768; // bytes
[email protected]3c2fe4e2009-01-08 12:07:2395 static const size_t kLoadsToWrite = 100; // number of data buffers queued
[email protected]e3c404b2008-12-23 01:07:3296 static const int kThrottleTimeMs = 200; // milliseconds
97
98 DISALLOW_COPY_AND_ASSIGN(DownloadResourceHandler);
99};
100
101#endif // CHROME_BROWSER_RENDERER_HOST_DOWNLOAD_RESOURCE_HANDLER_H_