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