download_resource_handler.h revision 3345a6884c488ff3a535c2c9acdd33d74b37e311
1// Copyright (c) 2010 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.
4
5#ifndef CHROME_BROWSER_RENDERER_HOST_DOWNLOAD_RESOURCE_HANDLER_H_
6#define CHROME_BROWSER_RENDERER_HOST_DOWNLOAD_RESOURCE_HANDLER_H_
7#pragma once
8
9#include <string>
10
11#include "base/timer.h"
12#include "chrome/browser/download/download_file.h"
13#include "chrome/browser/renderer_host/global_request_id.h"
14#include "chrome/browser/renderer_host/resource_handler.h"
15
16class DownloadFileManager;
17class ResourceDispatcherHost;
18class URLRequest;
19struct DownloadBuffer;
20
21// Forwards data to the download thread.
22class DownloadResourceHandler : public ResourceHandler {
23 public:
24  DownloadResourceHandler(ResourceDispatcherHost* rdh,
25                          int render_process_host_id,
26                          int render_view_id,
27                          int request_id,
28                          const GURL& url,
29                          DownloadFileManager* download_file_manager,
30                          URLRequest* request,
31                          bool save_as,
32                          const DownloadSaveInfo& save_info);
33
34  bool OnUploadProgress(int request_id, uint64 position, uint64 size);
35
36  // Not needed, as this event handler ought to be the final resource.
37  bool OnRequestRedirected(int request_id, const GURL& url,
38                           ResourceResponse* response, bool* defer);
39
40  // Send the download creation information to the download thread.
41  bool OnResponseStarted(int request_id, ResourceResponse* response);
42
43  // Pass-through implementation.
44  bool OnWillStart(int request_id, const GURL& url, bool* defer);
45
46  // Create a new buffer, which will be handed to the download thread for file
47  // writing and deletion.
48  bool OnWillRead(int request_id, net::IOBuffer** buf, int* buf_size,
49                  int min_size);
50
51  bool OnReadCompleted(int request_id, int* bytes_read);
52
53  bool OnResponseCompleted(int request_id,
54                           const URLRequestStatus& status,
55                           const std::string& security_info);
56  void OnRequestClosed();
57
58  // If the content-length header is not present (or contains something other
59  // than numbers), the incoming content_length is -1 (unknown size).
60  // Set the content length to 0 to indicate unknown size to DownloadManager.
61  void set_content_length(const int64& content_length);
62
63  void set_content_disposition(const std::string& content_disposition);
64
65  void CheckWriteProgress();
66
67 private:
68  ~DownloadResourceHandler();
69
70  void StartPauseTimer();
71
72  int download_id_;
73  GlobalRequestID global_id_;
74  int render_view_id_;
75  scoped_refptr<net::IOBuffer> read_buffer_;
76  std::string content_disposition_;
77  GURL url_;
78  int64 content_length_;
79  DownloadFileManager* download_file_manager_;
80  URLRequest* request_;
81  bool save_as_;  // Request was initiated via "Save As" by the user.
82  DownloadSaveInfo save_info_;
83  DownloadBuffer* buffer_;
84  ResourceDispatcherHost* rdh_;
85  bool is_paused_;
86  base::OneShotTimer<DownloadResourceHandler> pause_timer_;
87
88  static const int kReadBufSize = 32768;  // bytes
89  static const size_t kLoadsToWrite = 100;  // number of data buffers queued
90  static const int kThrottleTimeMs = 200;  // milliseconds
91
92  DISALLOW_COPY_AND_ASSIGN(DownloadResourceHandler);
93};
94
95#endif  // CHROME_BROWSER_RENDERER_HOST_DOWNLOAD_RESOURCE_HANDLER_H_
96