blob: 5a66f66248a29de172bfac2d8cf4dfb1153baa2f [file] [log] [blame]
[email protected]864b1362010-08-19 03:49:381// Copyright (c) 2010 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]92b24c12009-12-10 20:04:3513#include "chrome/browser/renderer_host/global_request_id.h"
[email protected]e3c404b2008-12-23 01:07:3214#include "chrome/browser/renderer_host/resource_handler.h"
[email protected]9fc1e6c12010-12-22 21:07:0515#include "chrome/browser/safe_browsing/safe_browsing_service.h"
[email protected]e3c404b2008-12-23 01:07:3216
[email protected]92b24c12009-12-10 20:04:3517class DownloadFileManager;
18class ResourceDispatcherHost;
[email protected]e3c404b2008-12-23 01:07:3219struct DownloadBuffer;
20
[email protected]edfe7fab2010-11-28 13:11:5221namespace net {
22class URLRequest;
23} // namespace net
24
[email protected]e3c404b2008-12-23 01:07:3225// Forwards data to the download thread.
[email protected]9fc1e6c12010-12-22 21:07:0526class DownloadResourceHandler : public ResourceHandler,
27 public SafeBrowsingService::Client {
[email protected]e3c404b2008-12-23 01:07:3228 public:
29 DownloadResourceHandler(ResourceDispatcherHost* rdh,
30 int render_process_host_id,
31 int render_view_id,
32 int request_id,
[email protected]f6b48532009-02-12 01:56:3233 const GURL& url,
[email protected]aa033af2010-07-27 18:16:3934 DownloadFileManager* download_file_manager,
[email protected]edfe7fab2010-11-28 13:11:5235 net::URLRequest* request,
[email protected]6aa4a1c02010-01-15 18:49:5836 bool save_as,
[email protected]8af9d032010-02-10 00:00:3237 const DownloadSaveInfo& save_info);
[email protected]e3c404b2008-12-23 01:07:3238
[email protected]78994ab02010-12-08 18:06:4439 virtual bool OnUploadProgress(int request_id, uint64 position, uint64 size);
[email protected]afd832c2010-03-02 04:53:3140
[email protected]e3c404b2008-12-23 01:07:3241 // Not needed, as this event handler ought to be the final resource.
[email protected]78994ab02010-12-08 18:06:4442 virtual bool OnRequestRedirected(int request_id, const GURL& url,
43 ResourceResponse* response, bool* defer);
[email protected]e3c404b2008-12-23 01:07:3244
45 // Send the download creation information to the download thread.
[email protected]78994ab02010-12-08 18:06:4446 virtual bool OnResponseStarted(int request_id, ResourceResponse* response);
[email protected]e3c404b2008-12-23 01:07:3247
[email protected]afd832c2010-03-02 04:53:3148 // Pass-through implementation.
[email protected]78994ab02010-12-08 18:06:4449 virtual bool OnWillStart(int request_id, const GURL& url, bool* defer);
[email protected]afd832c2010-03-02 04:53:3150
[email protected]e3c404b2008-12-23 01:07:3251 // Create a new buffer, which will be handed to the download thread for file
52 // writing and deletion.
[email protected]78994ab02010-12-08 18:06:4453 virtual bool OnWillRead(int request_id, net::IOBuffer** buf, int* buf_size,
54 int min_size);
[email protected]e3c404b2008-12-23 01:07:3255
[email protected]78994ab02010-12-08 18:06:4456 virtual bool OnReadCompleted(int request_id, int* bytes_read);
[email protected]e3c404b2008-12-23 01:07:3257
[email protected]78994ab02010-12-08 18:06:4458 virtual bool OnResponseCompleted(int request_id,
59 const URLRequestStatus& status,
60 const std::string& security_info);
61 virtual void OnRequestClosed();
[email protected]e3c404b2008-12-23 01:07:3262
63 // If the content-length header is not present (or contains something other
64 // than numbers), the incoming content_length is -1 (unknown size).
65 // Set the content length to 0 to indicate unknown size to DownloadManager.
66 void set_content_length(const int64& content_length);
67
68 void set_content_disposition(const std::string& content_disposition);
69
70 void CheckWriteProgress();
71
[email protected]da6e3922010-11-24 21:45:5072 std::string DebugString() const;
73
[email protected]e3c404b2008-12-23 01:07:3274 private:
[email protected]9fc1e6c12010-12-22 21:07:0575 // Enumerate for histogramming purposes. DO NOT CHANGE THE
76 // ORDERING OF THESE VALUES.
77 enum SBStatsType {
78 DOWNLOAD_URL_CHECKS_TOTAL,
79 DOWNLOAD_URL_CHECKS_CANCELED,
80 DOWNLOAD_URL_CHECKS_MALWARE,
81
82 // Memory space for histograms is determined by the max. ALWAYS
83 // ADD NEW VALUES BEFORE THIS ONE.
84 DOWNLOAD_URL_CHECKS_MAX
85 };
86
[email protected]601858c02010-09-01 17:08:2087 ~DownloadResourceHandler();
[email protected]84bbb2b52009-11-07 00:23:1488
[email protected]e3c404b2008-12-23 01:07:3289 void StartPauseTimer();
90
[email protected]9fc1e6c12010-12-22 21:07:0591 void StartDownloadUrlCheck();
92
93 // Called when the result of checking a download URL is known.
94 void OnDownloadUrlCheckResult(const GURL& url,
95 SafeBrowsingService::UrlCheckResult result);
96
97 // A helper function that updates UMA for download url checks.
98 static void UpdateDownloadUrlCheckStats(SBStatsType stat_type);
99
[email protected]e3c404b2008-12-23 01:07:32100 int download_id_;
[email protected]92b24c12009-12-10 20:04:35101 GlobalRequestID global_id_;
[email protected]e3c404b2008-12-23 01:07:32102 int render_view_id_;
[email protected]9dea9e1f2009-01-29 00:30:47103 scoped_refptr<net::IOBuffer> read_buffer_;
[email protected]e3c404b2008-12-23 01:07:32104 std::string content_disposition_;
[email protected]f6b48532009-02-12 01:56:32105 GURL url_;
[email protected]e3c404b2008-12-23 01:07:32106 int64 content_length_;
[email protected]aa033af2010-07-27 18:16:39107 DownloadFileManager* download_file_manager_;
[email protected]edfe7fab2010-11-28 13:11:52108 net::URLRequest* request_;
[email protected]e3c404b2008-12-23 01:07:32109 bool save_as_; // Request was initiated via "Save As" by the user.
[email protected]8af9d032010-02-10 00:00:32110 DownloadSaveInfo save_info_;
[email protected]e3c404b2008-12-23 01:07:32111 DownloadBuffer* buffer_;
112 ResourceDispatcherHost* rdh_;
113 bool is_paused_;
114 base::OneShotTimer<DownloadResourceHandler> pause_timer_;
[email protected]9fc1e6c12010-12-22 21:07:05115 bool url_check_pending_;
116 base::TimeTicks download_start_time_; // used to collect stats.
[email protected]e3c404b2008-12-23 01:07:32117
118 static const int kReadBufSize = 32768; // bytes
[email protected]3c2fe4e2009-01-08 12:07:23119 static const size_t kLoadsToWrite = 100; // number of data buffers queued
[email protected]e3c404b2008-12-23 01:07:32120 static const int kThrottleTimeMs = 200; // milliseconds
121
122 DISALLOW_COPY_AND_ASSIGN(DownloadResourceHandler);
123};
124
125#endif // CHROME_BROWSER_RENDERER_HOST_DOWNLOAD_RESOURCE_HANDLER_H_