• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2011 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 #include "chrome/browser/renderer_host/save_file_resource_handler.h"
6 
7 #include "base/logging.h"
8 #include "base/message_loop.h"
9 #include "base/string_number_conversions.h"
10 #include "chrome/browser/download/save_file_manager.h"
11 #include "content/browser/browser_thread.h"
12 #include "net/base/io_buffer.h"
13 #include "net/url_request/url_request_status.h"
14 
SaveFileResourceHandler(int render_process_host_id,int render_view_id,const GURL & url,SaveFileManager * manager)15 SaveFileResourceHandler::SaveFileResourceHandler(int render_process_host_id,
16                                                  int render_view_id,
17                                                  const GURL& url,
18                                                  SaveFileManager* manager)
19     : save_id_(-1),
20       render_process_id_(render_process_host_id),
21       render_view_id_(render_view_id),
22       url_(url),
23       content_length_(0),
24       save_manager_(manager) {
25 }
26 
OnUploadProgress(int request_id,uint64 position,uint64 size)27 bool SaveFileResourceHandler::OnUploadProgress(int request_id,
28                                                uint64 position,
29                                                uint64 size) {
30   return true;
31 }
32 
OnRequestRedirected(int request_id,const GURL & url,ResourceResponse * response,bool * defer)33 bool SaveFileResourceHandler::OnRequestRedirected(int request_id,
34                                                   const GURL& url,
35                                                   ResourceResponse* response,
36                                                   bool* defer) {
37   final_url_ = url;
38   return true;
39 }
40 
OnResponseStarted(int request_id,ResourceResponse * response)41 bool SaveFileResourceHandler::OnResponseStarted(int request_id,
42                                                 ResourceResponse* response) {
43   save_id_ = save_manager_->GetNextId();
44   // |save_manager_| consumes (deletes):
45   SaveFileCreateInfo* info = new SaveFileCreateInfo;
46   info->url = url_;
47   info->final_url = final_url_;
48   info->total_bytes = content_length_;
49   info->save_id = save_id_;
50   info->render_process_id = render_process_id_;
51   info->render_view_id = render_view_id_;
52   info->request_id = request_id;
53   info->content_disposition = content_disposition_;
54   info->save_source = SaveFileCreateInfo::SAVE_FILE_FROM_NET;
55   BrowserThread::PostTask(
56       BrowserThread::FILE, FROM_HERE,
57       NewRunnableMethod(save_manager_,
58                         &SaveFileManager::StartSave,
59                         info));
60   return true;
61 }
62 
OnWillStart(int request_id,const GURL & url,bool * defer)63 bool SaveFileResourceHandler::OnWillStart(int request_id,
64                                           const GURL& url,
65                                           bool* defer) {
66   return true;
67 }
68 
OnWillRead(int request_id,net::IOBuffer ** buf,int * buf_size,int min_size)69 bool SaveFileResourceHandler::OnWillRead(int request_id, net::IOBuffer** buf,
70                                          int* buf_size, int min_size) {
71   DCHECK(buf && buf_size);
72   if (!read_buffer_) {
73     *buf_size = min_size < 0 ? kReadBufSize : min_size;
74     read_buffer_ = new net::IOBuffer(*buf_size);
75   }
76   *buf = read_buffer_.get();
77   return true;
78 }
79 
OnReadCompleted(int request_id,int * bytes_read)80 bool SaveFileResourceHandler::OnReadCompleted(int request_id, int* bytes_read) {
81   DCHECK(read_buffer_);
82   // We are passing ownership of this buffer to the save file manager.
83   scoped_refptr<net::IOBuffer> buffer;
84   read_buffer_.swap(buffer);
85   BrowserThread::PostTask(
86       BrowserThread::FILE, FROM_HERE,
87       NewRunnableMethod(save_manager_,
88                         &SaveFileManager::UpdateSaveProgress,
89                         save_id_,
90                         buffer,
91                         *bytes_read));
92   return true;
93 }
94 
OnResponseCompleted(int request_id,const net::URLRequestStatus & status,const std::string & security_info)95 bool SaveFileResourceHandler::OnResponseCompleted(
96     int request_id,
97     const net::URLRequestStatus& status,
98     const std::string& security_info) {
99   BrowserThread::PostTask(
100       BrowserThread::FILE, FROM_HERE,
101       NewRunnableMethod(save_manager_,
102                         &SaveFileManager::SaveFinished,
103                         save_id_,
104                         url_,
105                         render_process_id_,
106                         status.is_success() && !status.is_io_pending()));
107   read_buffer_ = NULL;
108   return true;
109 }
110 
OnRequestClosed()111 void SaveFileResourceHandler::OnRequestClosed() {
112 }
113 
set_content_length(const std::string & content_length)114 void SaveFileResourceHandler::set_content_length(
115     const std::string& content_length) {
116   base::StringToInt64(content_length, &content_length_);
117 }
118 
~SaveFileResourceHandler()119 SaveFileResourceHandler::~SaveFileResourceHandler() {}
120