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/download/download_file_manager.h" | 5 #include "chrome/browser/download/download_file_manager.h" |
6 | 6 |
7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/stl_util-inl.h" | 9 #include "base/stl_util-inl.h" |
10 #include "base/task.h" | 10 #include "base/task.h" |
11 #include "base/utf_string_conversions.h" | 11 #include "base/utf_string_conversions.h" |
12 #include "build/build_config.h" | 12 #include "build/build_config.h" |
13 #include "chrome/browser/download/download_manager.h" | 13 #include "chrome/browser/download/download_manager.h" |
| 14 #include "chrome/browser/download/download_process_handle.h" |
14 #include "chrome/browser/download/download_util.h" | 15 #include "chrome/browser/download/download_util.h" |
15 #include "chrome/browser/history/download_create_info.h" | 16 #include "chrome/browser/history/download_create_info.h" |
16 #include "chrome/browser/net/chrome_url_request_context.h" | 17 #include "chrome/browser/net/chrome_url_request_context.h" |
17 #include "chrome/browser/platform_util.h" | 18 #include "chrome/browser/platform_util.h" |
18 #include "chrome/browser/profiles/profile.h" | 19 #include "chrome/browser/profiles/profile.h" |
19 #include "chrome/browser/safe_browsing/safe_browsing_service.h" | 20 #include "chrome/browser/safe_browsing/safe_browsing_service.h" |
20 #include "chrome/browser/tab_contents/tab_util.h" | 21 #include "chrome/browser/tab_contents/tab_util.h" |
21 #include "content/browser/browser_thread.h" | 22 #include "content/browser/browser_thread.h" |
22 #include "content/browser/renderer_host/resource_dispatcher_host.h" | 23 #include "content/browser/renderer_host/resource_dispatcher_host.h" |
23 #include "content/browser/tab_contents/tab_contents.h" | 24 #include "content/browser/tab_contents/tab_contents.h" |
24 #include "googleurl/src/gurl.h" | 25 #include "googleurl/src/gurl.h" |
25 #include "net/base/io_buffer.h" | 26 #include "net/base/io_buffer.h" |
26 | 27 |
27 namespace { | 28 namespace { |
28 | 29 |
29 // Throttle updates to the UI thread so that a fast moving download doesn't | 30 // Throttle updates to the UI thread so that a fast moving download doesn't |
30 // cause it to become unresponsive (in milliseconds). | 31 // cause it to become unresponsive (in milliseconds). |
31 const int kUpdatePeriodMs = 500; | 32 const int kUpdatePeriodMs = 500; |
32 | 33 |
33 DownloadManager* DownloadManagerForRenderViewHost(int render_process_id, | |
34 int render_view_id) { | |
35 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
36 | |
37 TabContents* contents = tab_util::GetTabContentsByID(render_process_id, | |
38 render_view_id); | |
39 if (contents) { | |
40 Profile* profile = contents->profile(); | |
41 if (profile) | |
42 return profile->GetDownloadManager(); | |
43 } | |
44 | |
45 return NULL; | |
46 } | |
47 | |
48 } // namespace | 34 } // namespace |
49 | 35 |
50 DownloadFileManager::DownloadFileManager(ResourceDispatcherHost* rdh) | 36 DownloadFileManager::DownloadFileManager(ResourceDispatcherHost* rdh) |
51 : next_id_(0), | 37 : next_id_(0), |
52 resource_dispatcher_host_(rdh) { | 38 resource_dispatcher_host_(rdh) { |
53 } | 39 } |
54 | 40 |
55 DownloadFileManager::~DownloadFileManager() { | 41 DownloadFileManager::~DownloadFileManager() { |
56 DCHECK(downloads_.empty()); | 42 DCHECK(downloads_.empty()); |
57 } | 43 } |
(...skipping 13 matching lines...) Expand all Loading... |
71 | 57 |
72 void DownloadFileManager::CreateDownloadFile(DownloadCreateInfo* info, | 58 void DownloadFileManager::CreateDownloadFile(DownloadCreateInfo* info, |
73 DownloadManager* download_manager, | 59 DownloadManager* download_manager, |
74 bool get_hash) { | 60 bool get_hash) { |
75 VLOG(20) << __FUNCTION__ << "()" << " info = " << info->DebugString(); | 61 VLOG(20) << __FUNCTION__ << "()" << " info = " << info->DebugString(); |
76 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 62 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
77 | 63 |
78 scoped_ptr<DownloadFile> | 64 scoped_ptr<DownloadFile> |
79 download_file(new DownloadFile(info, download_manager)); | 65 download_file(new DownloadFile(info, download_manager)); |
80 if (!download_file->Initialize(get_hash)) { | 66 if (!download_file->Initialize(get_hash)) { |
81 BrowserThread::PostTask( | 67 download_util::CancelDownloadRequest(resource_dispatcher_host_, |
82 BrowserThread::IO, FROM_HERE, | 68 info->process_handle); |
83 NewRunnableFunction(&download_util::CancelDownloadRequest, | |
84 resource_dispatcher_host_, | |
85 info->child_id, | |
86 info->request_id)); | |
87 delete info; | 69 delete info; |
88 return; | 70 return; |
89 } | 71 } |
90 | 72 |
91 DCHECK(GetDownloadFile(info->download_id) == NULL); | 73 DCHECK(GetDownloadFile(info->download_id) == NULL); |
92 downloads_[info->download_id] = download_file.release(); | 74 downloads_[info->download_id] = download_file.release(); |
93 // TODO(phajdan.jr): fix the duplication of path info below. | 75 // TODO(phajdan.jr): fix the duplication of path info below. |
94 info->path = info->save_info.file_path; | 76 info->path = info->save_info.file_path; |
95 | 77 |
96 // The file is now ready, we can un-pause the request and start saving data. | 78 // The file is now ready, we can un-pause the request and start saving data. |
97 BrowserThread::PostTask( | 79 BrowserThread::PostTask( |
98 BrowserThread::IO, FROM_HERE, | 80 BrowserThread::IO, FROM_HERE, |
99 NewRunnableMethod(this, &DownloadFileManager::ResumeDownloadRequest, | 81 NewRunnableMethod(this, &DownloadFileManager::ResumeDownloadRequest, |
100 info->child_id, info->request_id)); | 82 info->process_handle)); |
101 | 83 |
102 StartUpdateTimer(); | 84 StartUpdateTimer(); |
103 | 85 |
104 BrowserThread::PostTask( | 86 BrowserThread::PostTask( |
105 BrowserThread::UI, FROM_HERE, | 87 BrowserThread::UI, FROM_HERE, |
106 NewRunnableMethod(download_manager, | 88 NewRunnableMethod(download_manager, |
107 &DownloadManager::StartDownload, info)); | 89 &DownloadManager::StartDownload, info)); |
108 } | 90 } |
109 | 91 |
110 void DownloadFileManager::ResumeDownloadRequest(int child_id, int request_id) { | 92 void DownloadFileManager::ResumeDownloadRequest( |
| 93 DownloadProcessHandle process) { |
111 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 94 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
112 | 95 |
113 // This balances the pause in DownloadResourceHandler::OnResponseStarted. | 96 // This balances the pause in DownloadResourceHandler::OnResponseStarted. |
114 resource_dispatcher_host_->PauseRequest(child_id, request_id, false); | 97 resource_dispatcher_host_->PauseRequest(process.child_id(), |
| 98 process.request_id(), |
| 99 false); |
115 } | 100 } |
116 | 101 |
117 DownloadFile* DownloadFileManager::GetDownloadFile(int id) { | 102 DownloadFile* DownloadFileManager::GetDownloadFile(int id) { |
118 DownloadFileMap::iterator it = downloads_.find(id); | 103 DownloadFileMap::iterator it = downloads_.find(id); |
119 return it == downloads_.end() ? NULL : it->second; | 104 return it == downloads_.end() ? NULL : it->second; |
120 } | 105 } |
121 | 106 |
122 void DownloadFileManager::StartUpdateTimer() { | 107 void DownloadFileManager::StartUpdateTimer() { |
123 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 108 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
124 if (!update_timer_.IsRunning()) { | 109 if (!update_timer_.IsRunning()) { |
(...skipping 26 matching lines...) Expand all Loading... |
151 // request is a download. | 136 // request is a download. |
152 int DownloadFileManager::GetNextId() { | 137 int DownloadFileManager::GetNextId() { |
153 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 138 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
154 return next_id_++; | 139 return next_id_++; |
155 } | 140 } |
156 | 141 |
157 void DownloadFileManager::StartDownload(DownloadCreateInfo* info) { | 142 void DownloadFileManager::StartDownload(DownloadCreateInfo* info) { |
158 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 143 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
159 DCHECK(info); | 144 DCHECK(info); |
160 | 145 |
161 DownloadManager* manager = DownloadManagerForRenderViewHost( | 146 DownloadManager* manager = info->process_handle.GetDownloadManager(); |
162 info->child_id, info->render_view_id); | |
163 if (!manager) { | 147 if (!manager) { |
164 BrowserThread::PostTask( | 148 download_util::CancelDownloadRequest(resource_dispatcher_host_, |
165 BrowserThread::IO, FROM_HERE, | 149 info->process_handle); |
166 NewRunnableFunction(&download_util::CancelDownloadRequest, | |
167 resource_dispatcher_host_, | |
168 info->child_id, | |
169 info->request_id)); | |
170 delete info; | 150 delete info; |
171 return; | 151 return; |
172 } | 152 } |
173 | 153 |
174 manager->CreateDownloadItem(info); | 154 manager->CreateDownloadItem(info); |
175 | 155 |
176 bool hash_needed = resource_dispatcher_host_->safe_browsing_service()-> | 156 bool hash_needed = resource_dispatcher_host_->safe_browsing_service()-> |
177 DownloadBinHashNeeded(); | 157 DownloadBinHashNeeded(); |
178 | 158 |
179 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 159 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
180 NewRunnableMethod(this, &DownloadFileManager::CreateDownloadFile, | 160 NewRunnableMethod(this, &DownloadFileManager::CreateDownloadFile, |
181 info, make_scoped_refptr(manager), hash_needed)); | 161 info, |
| 162 make_scoped_refptr(manager), hash_needed)); |
182 } | 163 } |
183 | 164 |
184 // We don't forward an update to the UI thread here, since we want to throttle | 165 // We don't forward an update to the UI thread here, since we want to throttle |
185 // the UI update rate via a periodic timer. If the user has cancelled the | 166 // the UI update rate via a periodic timer. If the user has cancelled the |
186 // download (in the UI thread), we may receive a few more updates before the IO | 167 // download (in the UI thread), we may receive a few more updates before the IO |
187 // thread gets the cancel message: we just delete the data since the | 168 // thread gets the cancel message: we just delete the data since the |
188 // DownloadFile has been deleted. | 169 // DownloadFile has been deleted. |
189 void DownloadFileManager::UpdateDownload(int id, DownloadBuffer* buffer) { | 170 void DownloadFileManager::UpdateDownload(int id, DownloadBuffer* buffer) { |
190 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 171 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
191 std::vector<DownloadBuffer::Contents> contents; | 172 std::vector<DownloadBuffer::Contents> contents; |
(...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
419 << " id = " << id | 400 << " id = " << id |
420 << " download_file = " << download_file->DebugString(); | 401 << " download_file = " << download_file->DebugString(); |
421 | 402 |
422 downloads_.erase(id); | 403 downloads_.erase(id); |
423 | 404 |
424 delete download_file; | 405 delete download_file; |
425 | 406 |
426 if (downloads_.empty()) | 407 if (downloads_.empty()) |
427 StopUpdateTimer(); | 408 StopUpdateTimer(); |
428 } | 409 } |
OLD | NEW |