OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/renderer_host/download_resource_handler.h" | 5 #include "chrome/browser/renderer_host/download_resource_handler.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
11 #include "base/metrics/stats_counters.h" | 11 #include "base/metrics/stats_counters.h" |
12 #include "base/stringprintf.h" | 12 #include "base/stringprintf.h" |
13 #include "chrome/browser/download/download_create_info.h" | 13 #include "chrome/browser/download/download_create_info.h" |
14 #include "chrome/browser/download/download_item.h" | 14 #include "chrome/browser/download/download_item.h" |
15 #include "chrome/browser/download/download_file_manager.h" | 15 #include "chrome/browser/download/download_file_manager.h" |
16 #include "chrome/browser/download/download_manager.h" | |
17 #include "chrome/browser/download/download_prefs.h" | |
16 #include "chrome/browser/download/download_request_handle.h" | 18 #include "chrome/browser/download/download_request_handle.h" |
17 #include "chrome/browser/download/download_util.h" | 19 #include "chrome/browser/download/download_util.h" |
18 #include "content/browser/browser_thread.h" | 20 #include "content/browser/browser_thread.h" |
19 #include "content/browser/renderer_host/global_request_id.h" | 21 #include "content/browser/renderer_host/global_request_id.h" |
20 #include "content/browser/renderer_host/resource_dispatcher_host.h" | 22 #include "content/browser/renderer_host/resource_dispatcher_host.h" |
21 #include "content/browser/renderer_host/resource_dispatcher_host_request_info.h" | 23 #include "content/browser/renderer_host/resource_dispatcher_host_request_info.h" |
22 #include "content/common/resource_response.h" | 24 #include "content/common/resource_response.h" |
23 #include "net/base/io_buffer.h" | 25 #include "net/base/io_buffer.h" |
24 #include "net/http/http_response_headers.h" | 26 #include "net/http/http_response_headers.h" |
25 #include "net/url_request/url_request_context.h" | 27 #include "net/url_request/url_request_context.h" |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
70 download_start_time_ = base::TimeTicks::Now(); | 72 download_start_time_ = base::TimeTicks::Now(); |
71 std::string content_disposition; | 73 std::string content_disposition; |
72 request_->GetResponseHeaderByName("content-disposition", | 74 request_->GetResponseHeaderByName("content-disposition", |
73 &content_disposition); | 75 &content_disposition); |
74 set_content_disposition(content_disposition); | 76 set_content_disposition(content_disposition); |
75 set_content_length(response->response_head.content_length); | 77 set_content_length(response->response_head.content_length); |
76 | 78 |
77 const ResourceDispatcherHostRequestInfo* request_info = | 79 const ResourceDispatcherHostRequestInfo* request_info = |
78 ResourceDispatcherHost::InfoForRequest(request_); | 80 ResourceDispatcherHost::InfoForRequest(request_); |
79 | 81 |
80 download_id_ = download_file_manager_->GetNextId(); | |
81 | 82 |
82 // Deleted in DownloadManager. | 83 // Deleted in DownloadManager. |
83 DownloadCreateInfo* info = new DownloadCreateInfo; | 84 DownloadCreateInfo* info = new DownloadCreateInfo; |
84 info->url_chain = request_->url_chain(); | 85 info->url_chain = request_->url_chain(); |
85 info->referrer_url = GURL(request_->referrer()); | 86 info->referrer_url = GURL(request_->referrer()); |
86 info->start_time = base::Time::Now(); | 87 info->start_time = base::Time::Now(); |
87 info->received_bytes = 0; | 88 info->received_bytes = 0; |
88 info->total_bytes = content_length_; | 89 info->total_bytes = content_length_; |
89 info->state = DownloadItem::IN_PROGRESS; | 90 info->state = DownloadItem::IN_PROGRESS; |
90 info->download_id = download_id_; | |
91 info->has_user_gesture = request_info->has_user_gesture(); | 91 info->has_user_gesture = request_info->has_user_gesture(); |
92 info->request_handle = DownloadRequestHandle(rdh_, | 92 info->request_handle = DownloadRequestHandle(rdh_, |
93 global_id_.child_id, | 93 global_id_.child_id, |
94 render_view_id_, | 94 render_view_id_, |
95 global_id_.request_id); | 95 global_id_.request_id); |
96 info->content_disposition = content_disposition_; | 96 info->content_disposition = content_disposition_; |
97 info->mime_type = response->response_head.mime_type; | 97 info->mime_type = response->response_head.mime_type; |
98 // TODO(ahendrickson) -- Get the last modified time and etag, so we can | 98 // TODO(ahendrickson) -- Get the last modified time and etag, so we can |
99 // resume downloading. | 99 // resume downloading. |
100 | 100 |
101 std::string content_type_header; | 101 std::string content_type_header; |
102 if (!response->response_head.headers || | 102 if (!response->response_head.headers || |
103 !response->response_head.headers->GetMimeType(&content_type_header)) | 103 !response->response_head.headers->GetMimeType(&content_type_header)) |
104 content_type_header = ""; | 104 content_type_header = ""; |
105 info->original_mime_type = content_type_header; | 105 info->original_mime_type = content_type_header; |
106 | 106 |
107 info->prompt_user_for_save_location = | 107 info->prompt_user_for_save_location = |
108 save_as_ && save_info_.file_path.empty(); | 108 save_as_ && save_info_.file_path.empty(); |
109 info->referrer_charset = request_->context()->referrer_charset(); | 109 info->referrer_charset = request_->context()->referrer_charset(); |
110 info->save_info = save_info_; | 110 info->save_info = save_info_; |
111 BrowserThread::PostTask( | 111 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, NewRunnableMethod( |
112 BrowserThread::UI, FROM_HERE, | 112 this, &DownloadResourceHandler::StartDownloadOnUIThread, info)); |
113 NewRunnableMethod( | |
114 download_file_manager_, &DownloadFileManager::StartDownload, info)); | |
115 | 113 |
116 // We can't start saving the data before we create the file on disk. | 114 // We can't start saving the data before we create the file on disk. |
117 // The request will be un-paused in DownloadFileManager::CreateDownloadFile. | 115 // The request will be un-paused in DownloadFileManager::CreateDownloadFile. |
118 rdh_->PauseRequest(global_id_.child_id, global_id_.request_id, true); | 116 rdh_->PauseRequest(global_id_.child_id, global_id_.request_id, true); |
119 | 117 |
120 return true; | 118 return true; |
121 } | 119 } |
122 | 120 |
121 void DownloadResourceHandler::StartDownloadOnUIThread( | |
122 DownloadCreateInfo* info) { | |
123 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
124 download_id_ = info->request_handle.GetDownloadManager()->download_prefs()-> | |
Randy Smith (Not in Mondays)
2011/07/10 23:49:55
I'm concerned about this line--it's setting a valu
benjhayden
2011/07/11 15:57:50
Ah, I see it now.
The primary reason that I moved
Randy Smith (Not in Mondays)
2011/07/12 18:12:52
I think DownloadPrefs is a UI thread object, and I
| |
125 GetNextId(); | |
126 VLOG(20) << __PRETTY_FUNCTION__ << " " << download_id_; | |
127 info->download_id = download_id_; | |
128 download_file_manager_->StartDownload(info); | |
129 } | |
130 | |
123 bool DownloadResourceHandler::OnWillStart(int request_id, | 131 bool DownloadResourceHandler::OnWillStart(int request_id, |
124 const GURL& url, | 132 const GURL& url, |
125 bool* defer) { | 133 bool* defer) { |
126 return true; | 134 return true; |
127 } | 135 } |
128 | 136 |
129 // Create a new buffer, which will be handed to the download thread for file | 137 // Create a new buffer, which will be handed to the download thread for file |
130 // writing and deletion. | 138 // writing and deletion. |
131 bool DownloadResourceHandler::OnWillRead(int request_id, net::IOBuffer** buf, | 139 bool DownloadResourceHandler::OnWillRead(int request_id, net::IOBuffer** buf, |
132 int* buf_size, int min_size) { | 140 int* buf_size, int min_size) { |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
255 " render_view_id_ = " "%d" | 263 " render_view_id_ = " "%d" |
256 " save_info_.file_path = \"%" PRFilePath "\"" | 264 " save_info_.file_path = \"%" PRFilePath "\"" |
257 " }", | 265 " }", |
258 request_->url().spec().c_str(), | 266 request_->url().spec().c_str(), |
259 download_id_, | 267 download_id_, |
260 global_id_.child_id, | 268 global_id_.child_id, |
261 global_id_.request_id, | 269 global_id_.request_id, |
262 render_view_id_, | 270 render_view_id_, |
263 save_info_.file_path.value().c_str()); | 271 save_info_.file_path.value().c_str()); |
264 } | 272 } |
OLD | NEW |