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 "content/browser/download/download_item_impl.h" | 5 #include "content/browser/download/download_item_impl.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/file_util.h" | 11 #include "base/file_util.h" |
12 #include "base/format_macros.h" | 12 #include "base/format_macros.h" |
13 #include "base/i18n/case_conversion.h" | 13 #include "base/i18n/case_conversion.h" |
14 #include "base/i18n/string_search.h" | 14 #include "base/i18n/string_search.h" |
15 #include "base/logging.h" | 15 #include "base/logging.h" |
16 #include "base/metrics/histogram.h" | 16 #include "base/metrics/histogram.h" |
17 #include "base/stringprintf.h" | 17 #include "base/stringprintf.h" |
18 #include "base/utf_string_conversions.h" | 18 #include "base/utf_string_conversions.h" |
19 #include "content/browser/download/download_create_info.h" | 19 #include "content/browser/download/download_create_info.h" |
20 #include "content/browser/download/download_file.h" | 20 #include "content/browser/download/download_file.h" |
21 #include "content/browser/download/download_file_manager.h" | 21 #include "content/browser/download/download_file_manager.h" |
22 #include "content/browser/download/download_id.h" | 22 #include "content/browser/download/download_id.h" |
23 #include "content/browser/download/download_manager.h" | |
24 #include "content/browser/download/download_persistent_store_info.h" | 23 #include "content/browser/download/download_persistent_store_info.h" |
25 #include "content/browser/download/download_request_handle.h" | 24 #include "content/browser/download/download_request_handle.h" |
26 #include "content/browser/download/download_stats.h" | 25 #include "content/browser/download/download_stats.h" |
27 #include "content/browser/download/interrupt_reasons.h" | 26 #include "content/browser/download/interrupt_reasons.h" |
28 #include "content/browser/tab_contents/tab_contents.h" | 27 #include "content/browser/tab_contents/tab_contents.h" |
29 #include "content/public/browser/browser_thread.h" | 28 #include "content/public/browser/browser_thread.h" |
30 #include "content/public/browser/content_browser_client.h" | 29 #include "content/public/browser/content_browser_client.h" |
31 #include "content/public/browser/download_manager_delegate.h" | |
32 #include "net/base/net_util.h" | 30 #include "net/base/net_util.h" |
33 | 31 |
34 using content::BrowserThread; | 32 using content::BrowserThread; |
35 | 33 |
36 // A DownloadItem normally goes through the following states: | 34 // A DownloadItem normally goes through the following states: |
37 // * Created (when download starts) | 35 // * Created (when download starts) |
38 // * Made visible to consumers (e.g. Javascript) after the | 36 // * Made visible to consumers (e.g. Javascript) after the |
39 // destination file has been determined. | 37 // destination file has been determined. |
40 // * Entered into the history database. | 38 // * Entered into the history database. |
41 // * Made visible in the download shelf. | 39 // * Made visible in the download shelf. |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
117 }; | 115 }; |
118 | 116 |
119 } // namespace | 117 } // namespace |
120 | 118 |
121 // Our download table ID starts at 1, so we use 0 to represent a download that | 119 // Our download table ID starts at 1, so we use 0 to represent a download that |
122 // has started, but has not yet had its data persisted in the table. We use fake | 120 // has started, but has not yet had its data persisted in the table. We use fake |
123 // database handles in incognito mode starting at -1 and progressively getting | 121 // database handles in incognito mode starting at -1 and progressively getting |
124 // more negative. | 122 // more negative. |
125 | 123 |
126 // Constructor for reading from the history service. | 124 // Constructor for reading from the history service. |
127 DownloadItemImpl::DownloadItemImpl(DownloadManager* download_manager, | 125 DownloadItemImpl::DownloadItemImpl(Delegate* delegate, |
126 DownloadId download_id, | |
128 const DownloadPersistentStoreInfo& info) | 127 const DownloadPersistentStoreInfo& info) |
129 : download_id_(download_manager->GetNextId()), | 128 : download_id_(download_id), |
130 full_path_(info.path), | 129 full_path_(info.path), |
131 url_chain_(1, info.url), | 130 url_chain_(1, info.url), |
132 referrer_url_(info.referrer_url), | 131 referrer_url_(info.referrer_url), |
133 total_bytes_(info.total_bytes), | 132 total_bytes_(info.total_bytes), |
134 received_bytes_(info.received_bytes), | 133 received_bytes_(info.received_bytes), |
135 start_tick_(base::TimeTicks()), | 134 start_tick_(base::TimeTicks()), |
136 state_(static_cast<DownloadState>(info.state)), | 135 state_(static_cast<DownloadState>(info.state)), |
137 start_time_(info.start_time), | 136 start_time_(info.start_time), |
138 end_time_(info.end_time), | 137 end_time_(info.end_time), |
139 db_handle_(info.db_handle), | 138 db_handle_(info.db_handle), |
140 download_manager_(download_manager), | 139 delegate_(delegate), |
141 is_paused_(false), | 140 is_paused_(false), |
142 open_when_complete_(false), | 141 open_when_complete_(false), |
143 file_externally_removed_(false), | 142 file_externally_removed_(false), |
144 safety_state_(SAFE), | 143 safety_state_(SAFE), |
145 auto_opened_(false), | 144 auto_opened_(false), |
146 is_otr_(false), | 145 is_otr_(false), |
147 is_temporary_(false), | 146 is_temporary_(false), |
148 all_data_saved_(false), | 147 all_data_saved_(false), |
149 opened_(info.opened), | 148 opened_(info.opened), |
150 open_enabled_(true), | 149 open_enabled_(true), |
151 delegate_delayed_complete_(false) { | 150 delegate_delayed_complete_(false) { |
152 if (IsInProgress()) | 151 if (IsInProgress()) |
153 state_ = CANCELLED; | 152 state_ = CANCELLED; |
154 if (IsComplete()) | 153 if (IsComplete()) |
155 all_data_saved_ = true; | 154 all_data_saved_ = true; |
156 Init(false /* not actively downloading */); | 155 Init(false /* not actively downloading */); |
157 } | 156 } |
158 | 157 |
159 // Constructing for a regular download: | 158 // Constructing for a regular download: |
160 DownloadItemImpl::DownloadItemImpl( | 159 DownloadItemImpl::DownloadItemImpl( |
161 DownloadManager* download_manager, | 160 Delegate* delegate, |
162 const DownloadCreateInfo& info, | 161 const DownloadCreateInfo& info, |
163 DownloadRequestHandleInterface* request_handle, | 162 DownloadRequestHandleInterface* request_handle, |
164 bool is_otr) | 163 bool is_otr) |
165 : state_info_(info.original_name, info.save_info.file_path, | 164 : state_info_(info.original_name, info.save_info.file_path, |
166 info.has_user_gesture, info.transition_type, | 165 info.has_user_gesture, info.transition_type, |
167 info.prompt_user_for_save_location, info.path_uniquifier, | 166 info.prompt_user_for_save_location, info.path_uniquifier, |
168 DownloadStateInfo::NOT_DANGEROUS), | 167 DownloadStateInfo::NOT_DANGEROUS), |
169 request_handle_(request_handle), | 168 request_handle_(request_handle), |
170 download_id_(info.download_id), | 169 download_id_(info.download_id), |
171 full_path_(info.path), | 170 full_path_(info.path), |
172 url_chain_(info.url_chain), | 171 url_chain_(info.url_chain), |
173 referrer_url_(info.referrer_url), | 172 referrer_url_(info.referrer_url), |
174 suggested_filename_(UTF16ToUTF8(info.save_info.suggested_name)), | 173 suggested_filename_(UTF16ToUTF8(info.save_info.suggested_name)), |
175 content_disposition_(info.content_disposition), | 174 content_disposition_(info.content_disposition), |
176 mime_type_(info.mime_type), | 175 mime_type_(info.mime_type), |
177 original_mime_type_(info.original_mime_type), | 176 original_mime_type_(info.original_mime_type), |
178 referrer_charset_(info.referrer_charset), | 177 referrer_charset_(info.referrer_charset), |
179 total_bytes_(info.total_bytes), | 178 total_bytes_(info.total_bytes), |
180 received_bytes_(0), | 179 received_bytes_(0), |
181 last_reason_(DOWNLOAD_INTERRUPT_REASON_NONE), | 180 last_reason_(DOWNLOAD_INTERRUPT_REASON_NONE), |
182 start_tick_(base::TimeTicks::Now()), | 181 start_tick_(base::TimeTicks::Now()), |
183 state_(IN_PROGRESS), | 182 state_(IN_PROGRESS), |
184 start_time_(info.start_time), | 183 start_time_(info.start_time), |
185 db_handle_(DownloadItem::kUninitializedHandle), | 184 db_handle_(DownloadItem::kUninitializedHandle), |
186 download_manager_(download_manager), | 185 delegate_(delegate), |
187 is_paused_(false), | 186 is_paused_(false), |
188 open_when_complete_(false), | 187 open_when_complete_(false), |
189 file_externally_removed_(false), | 188 file_externally_removed_(false), |
190 safety_state_(SAFE), | 189 safety_state_(SAFE), |
191 auto_opened_(false), | 190 auto_opened_(false), |
192 is_otr_(is_otr), | 191 is_otr_(is_otr), |
193 is_temporary_(!info.save_info.file_path.empty()), | 192 is_temporary_(!info.save_info.file_path.empty()), |
194 all_data_saved_(false), | 193 all_data_saved_(false), |
195 opened_(false), | 194 opened_(false), |
196 open_enabled_(true), | 195 open_enabled_(true), |
197 delegate_delayed_complete_(false) { | 196 delegate_delayed_complete_(false) { |
198 Init(true /* actively downloading */); | 197 Init(true /* actively downloading */); |
199 } | 198 } |
200 | 199 |
201 // Constructing for the "Save Page As..." feature: | 200 // Constructing for the "Save Page As..." feature: |
202 DownloadItemImpl::DownloadItemImpl(DownloadManager* download_manager, | 201 DownloadItemImpl::DownloadItemImpl(Delegate* delegate, |
203 const FilePath& path, | 202 const FilePath& path, |
204 const GURL& url, | 203 const GURL& url, |
205 bool is_otr, | 204 bool is_otr, |
206 DownloadId download_id) | 205 DownloadId download_id) |
207 : request_handle_(new NullDownloadRequestHandle()), | 206 : request_handle_(new NullDownloadRequestHandle()), |
208 download_id_(download_id), | 207 download_id_(download_id), |
209 full_path_(path), | 208 full_path_(path), |
210 url_chain_(1, url), | 209 url_chain_(1, url), |
211 referrer_url_(GURL()), | 210 referrer_url_(GURL()), |
212 total_bytes_(0), | 211 total_bytes_(0), |
213 received_bytes_(0), | 212 received_bytes_(0), |
214 last_reason_(DOWNLOAD_INTERRUPT_REASON_NONE), | 213 last_reason_(DOWNLOAD_INTERRUPT_REASON_NONE), |
215 start_tick_(base::TimeTicks::Now()), | 214 start_tick_(base::TimeTicks::Now()), |
216 state_(IN_PROGRESS), | 215 state_(IN_PROGRESS), |
217 start_time_(base::Time::Now()), | 216 start_time_(base::Time::Now()), |
218 db_handle_(DownloadItem::kUninitializedHandle), | 217 db_handle_(DownloadItem::kUninitializedHandle), |
219 download_manager_(download_manager), | 218 delegate_(delegate), |
220 is_paused_(false), | 219 is_paused_(false), |
221 open_when_complete_(false), | 220 open_when_complete_(false), |
222 file_externally_removed_(false), | 221 file_externally_removed_(false), |
223 safety_state_(SAFE), | 222 safety_state_(SAFE), |
224 auto_opened_(false), | 223 auto_opened_(false), |
225 is_otr_(is_otr), | 224 is_otr_(is_otr), |
226 is_temporary_(false), | 225 is_temporary_(false), |
227 all_data_saved_(false), | 226 all_data_saved_(false), |
228 opened_(false), | 227 opened_(false), |
229 open_enabled_(true), | 228 open_enabled_(true), |
230 delegate_delayed_complete_(false) { | 229 delegate_delayed_complete_(false) { |
231 Init(true /* actively downloading */); | 230 Init(true /* actively downloading */); |
232 } | 231 } |
233 | 232 |
234 DownloadItemImpl::~DownloadItemImpl() { | 233 DownloadItemImpl::~DownloadItemImpl() { |
235 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 234 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
236 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 235 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
237 | 236 |
238 TransitionTo(REMOVING); | 237 TransitionTo(REMOVING); |
239 download_manager_->AssertQueueStateConsistent(this); | 238 delegate_->AssertStateConsistent(this); |
240 } | 239 } |
241 | 240 |
242 void DownloadItemImpl::AddObserver(Observer* observer) { | 241 void DownloadItemImpl::AddObserver(Observer* observer) { |
243 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 242 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
244 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 243 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
245 | 244 |
246 observers_.AddObserver(observer); | 245 observers_.AddObserver(observer); |
247 } | 246 } |
248 | 247 |
249 void DownloadItemImpl::RemoveObserver(Observer* observer) { | 248 void DownloadItemImpl::RemoveObserver(Observer* observer) { |
(...skipping 12 matching lines...) Expand all Loading... | |
262 | 261 |
263 bool DownloadItemImpl::CanShowInFolder() { | 262 bool DownloadItemImpl::CanShowInFolder() { |
264 return !IsCancelled() && !file_externally_removed_; | 263 return !IsCancelled() && !file_externally_removed_; |
265 } | 264 } |
266 | 265 |
267 bool DownloadItemImpl::CanOpenDownload() { | 266 bool DownloadItemImpl::CanOpenDownload() { |
268 return !file_externally_removed_; | 267 return !file_externally_removed_; |
269 } | 268 } |
270 | 269 |
271 bool DownloadItemImpl::ShouldOpenFileBasedOnExtension() { | 270 bool DownloadItemImpl::ShouldOpenFileBasedOnExtension() { |
272 return download_manager_->delegate()->ShouldOpenFileBasedOnExtension( | 271 return delegate_->ShouldOpenFileBasedOnExtension( |
273 GetUserVerifiedFilePath()); | 272 GetUserVerifiedFilePath()); |
cbentzel
2011/11/29 21:30:44
Nit: this could probably fit on previous line.
Randy Smith (Not in Mondays)
2011/11/30 22:44:05
Done.
| |
274 } | 273 } |
275 | 274 |
276 void DownloadItemImpl::OpenDownload() { | 275 void DownloadItemImpl::OpenDownload() { |
277 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 276 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
278 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 277 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
279 | 278 |
280 if (IsPartialDownload()) { | 279 if (IsPartialDownload()) { |
281 open_when_complete_ = !open_when_complete_; | 280 open_when_complete_ = !open_when_complete_; |
282 return; | 281 return; |
283 } | 282 } |
284 | 283 |
285 if (!IsComplete() || file_externally_removed_) | 284 if (!IsComplete() || file_externally_removed_) |
286 return; | 285 return; |
287 | 286 |
288 // Ideally, we want to detect errors in opening and report them, but we | 287 // Ideally, we want to detect errors in opening and report them, but we |
289 // don't generally have the proper interface for that to the external | 288 // don't generally have the proper interface for that to the external |
290 // program that opens the file. So instead we spawn a check to update | 289 // program that opens the file. So instead we spawn a check to update |
291 // the UI if the file has been deleted in parallel with the open. | 290 // the UI if the file has been deleted in parallel with the open. |
292 download_manager_->CheckForFileRemoval(this); | 291 delegate_->CheckForFileRemoval(this); |
293 download_stats::RecordOpen(GetEndTime(), !GetOpened()); | 292 download_stats::RecordOpen(GetEndTime(), !GetOpened()); |
294 opened_ = true; | 293 opened_ = true; |
295 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadOpened(this)); | 294 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadOpened(this)); |
296 download_manager_->MarkDownloadOpened(this); | 295 delegate_->DownloadOpened(this); |
297 | 296 |
298 // For testing: If download opening is disabled on this item, | 297 // For testing: If download opening is disabled on this item, |
299 // make the rest of the routine a no-op. | 298 // make the rest of the routine a no-op. |
300 if (!open_enabled_) | 299 if (!open_enabled_) |
301 return; | 300 return; |
302 | 301 |
303 content::GetContentClient()->browser()->OpenItem(GetFullPath()); | 302 content::GetContentClient()->browser()->OpenItem(GetFullPath()); |
304 } | 303 } |
305 | 304 |
306 void DownloadItemImpl::ShowDownloadInShell() { | 305 void DownloadItemImpl::ShowDownloadInShell() { |
307 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 306 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
308 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 307 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
309 | 308 |
310 content::GetContentClient()->browser()->ShowItemInFolder(GetFullPath()); | 309 content::GetContentClient()->browser()->ShowItemInFolder(GetFullPath()); |
311 } | 310 } |
312 | 311 |
313 void DownloadItemImpl::DangerousDownloadValidated() { | 312 void DownloadItemImpl::DangerousDownloadValidated() { |
314 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 313 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
315 DCHECK_EQ(DANGEROUS, GetSafetyState()); | 314 DCHECK_EQ(DANGEROUS, GetSafetyState()); |
316 | 315 |
317 UMA_HISTOGRAM_ENUMERATION("Download.DangerousDownloadValidated", | 316 UMA_HISTOGRAM_ENUMERATION("Download.DangerousDownloadValidated", |
318 GetDangerType(), | 317 GetDangerType(), |
319 DownloadStateInfo::DANGEROUS_TYPE_MAX); | 318 DownloadStateInfo::DANGEROUS_TYPE_MAX); |
320 | 319 |
321 safety_state_ = DANGEROUS_BUT_VALIDATED; | 320 safety_state_ = DANGEROUS_BUT_VALIDATED; |
322 UpdateObservers(); | 321 UpdateObservers(); |
323 | 322 |
324 download_manager_->MaybeCompleteDownload(this); | 323 delegate_->MaybeCompleteDownload(this); |
325 } | 324 } |
326 | 325 |
327 void DownloadItemImpl::UpdateSize(int64 bytes_so_far) { | 326 void DownloadItemImpl::UpdateSize(int64 bytes_so_far) { |
328 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 327 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
329 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 328 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
330 | 329 |
331 received_bytes_ = bytes_so_far; | 330 received_bytes_ = bytes_so_far; |
332 | 331 |
333 // If we've received more data than we were expecting (bad server info?), | 332 // If we've received more data than we were expecting (bad server info?), |
334 // revert to 'unknown size mode'. | 333 // revert to 'unknown size mode'. |
(...skipping 29 matching lines...) Expand all Loading... | |
364 if (!IsPartialDownload()) { | 363 if (!IsPartialDownload()) { |
365 // Small downloads might be complete before this method has | 364 // Small downloads might be complete before this method has |
366 // a chance to run. | 365 // a chance to run. |
367 return; | 366 return; |
368 } | 367 } |
369 | 368 |
370 download_stats::RecordDownloadCount(download_stats::CANCELLED_COUNT); | 369 download_stats::RecordDownloadCount(download_stats::CANCELLED_COUNT); |
371 | 370 |
372 TransitionTo(CANCELLED); | 371 TransitionTo(CANCELLED); |
373 if (user_cancel) | 372 if (user_cancel) |
374 download_manager_->DownloadCancelledInternal(this); | 373 delegate_->DownloadCancelled(this); |
375 } | 374 } |
376 | 375 |
377 void DownloadItemImpl::MarkAsComplete() { | 376 void DownloadItemImpl::MarkAsComplete() { |
378 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 377 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
379 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 378 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
380 | 379 |
381 DCHECK(all_data_saved_); | 380 DCHECK(all_data_saved_); |
382 end_time_ = base::Time::Now(); | 381 end_time_ = base::Time::Now(); |
383 TransitionTo(COMPLETE); | 382 TransitionTo(COMPLETE); |
384 } | 383 } |
(...skipping 21 matching lines...) Expand all Loading... | |
406 | 405 |
407 void DownloadItemImpl::Completed() { | 406 void DownloadItemImpl::Completed() { |
408 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 407 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
409 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 408 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
410 | 409 |
411 VLOG(20) << __FUNCTION__ << "() " << DebugString(false); | 410 VLOG(20) << __FUNCTION__ << "() " << DebugString(false); |
412 | 411 |
413 DCHECK(all_data_saved_); | 412 DCHECK(all_data_saved_); |
414 end_time_ = base::Time::Now(); | 413 end_time_ = base::Time::Now(); |
415 TransitionTo(COMPLETE); | 414 TransitionTo(COMPLETE); |
416 download_manager_->DownloadCompleted(GetId()); | 415 delegate_->DownloadCompleted(this); |
417 download_stats::RecordDownloadCompleted(start_tick_, received_bytes_); | 416 download_stats::RecordDownloadCompleted(start_tick_, received_bytes_); |
418 | 417 |
419 if (auto_opened_) { | 418 if (auto_opened_) { |
420 // If it was already handled by the delegate, do nothing. | 419 // If it was already handled by the delegate, do nothing. |
421 } else if (GetOpenWhenComplete() || | 420 } else if (GetOpenWhenComplete() || |
422 ShouldOpenFileBasedOnExtension() || | 421 ShouldOpenFileBasedOnExtension() || |
423 IsTemporary()) { | 422 IsTemporary()) { |
424 // If the download is temporary, like in drag-and-drop, do not open it but | 423 // If the download is temporary, like in drag-and-drop, do not open it but |
425 // we still need to set it auto-opened so that it can be removed from the | 424 // we still need to set it auto-opened so that it can be removed from the |
426 // download shelf. | 425 // download shelf. |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
492 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 491 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
493 base::Bind(&DeleteDownloadedFile, full_path_)); | 492 base::Bind(&DeleteDownloadedFile, full_path_)); |
494 Remove(); | 493 Remove(); |
495 // We have now been deleted. | 494 // We have now been deleted. |
496 } | 495 } |
497 | 496 |
498 void DownloadItemImpl::Remove() { | 497 void DownloadItemImpl::Remove() { |
499 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 498 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
500 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 499 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
501 | 500 |
502 download_manager_->AssertQueueStateConsistent(this); | 501 delegate_->AssertStateConsistent(this); |
cbentzel
2011/11/29 21:30:44
It seems strange to have the DownloadItem ask the
Randy Smith (Not in Mondays)
2011/11/30 22:44:05
I tried, but it turned out that we're still gettin
| |
503 Cancel(true); | 502 Cancel(true); |
504 download_manager_->AssertQueueStateConsistent(this); | 503 delegate_->AssertStateConsistent(this); |
505 | 504 |
506 TransitionTo(REMOVING); | 505 TransitionTo(REMOVING); |
507 download_manager_->RemoveDownload(db_handle_); | 506 delegate_->DownloadRemoved(this); |
508 // We have now been deleted. | 507 // We have now been deleted. |
509 } | 508 } |
510 | 509 |
511 bool DownloadItemImpl::TimeRemaining(base::TimeDelta* remaining) const { | 510 bool DownloadItemImpl::TimeRemaining(base::TimeDelta* remaining) const { |
512 if (total_bytes_ <= 0) | 511 if (total_bytes_ <= 0) |
513 return false; // We never received the content_length for this download. | 512 return false; // We never received the content_length for this download. |
514 | 513 |
515 int64 speed = CurrentSpeed(); | 514 int64 speed = CurrentSpeed(); |
516 if (speed == 0) | 515 if (speed == 0) |
517 return false; | 516 return false; |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
573 | 572 |
574 VLOG(20) << __FUNCTION__ << "()" | 573 VLOG(20) << __FUNCTION__ << "()" |
575 << " needs rename = " << NeedsRename() | 574 << " needs rename = " << NeedsRename() |
576 << " " << DebugString(true); | 575 << " " << DebugString(true); |
577 DCHECK_NE(DANGEROUS, GetSafetyState()); | 576 DCHECK_NE(DANGEROUS, GetSafetyState()); |
578 DCHECK(file_manager); | 577 DCHECK(file_manager); |
579 | 578 |
580 if (NeedsRename()) { | 579 if (NeedsRename()) { |
581 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 580 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
582 base::Bind(&DownloadFileManager::RenameCompletingDownloadFile, | 581 base::Bind(&DownloadFileManager::RenameCompletingDownloadFile, |
583 file_manager, GetGlobalId(), | 582 file_manager, download_id_, |
584 GetTargetFilePath(), GetSafetyState() == SAFE)); | 583 GetTargetFilePath(), GetSafetyState() == SAFE)); |
585 return; | 584 return; |
586 } | 585 } |
587 | 586 |
588 Completed(); | 587 Completed(); |
589 | 588 |
590 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 589 BrowserThread::PostTask( |
591 base::Bind(&DownloadFileManager::CompleteDownload, | 590 BrowserThread::FILE, FROM_HERE, |
592 file_manager, GetGlobalId())); | 591 base::Bind(&DownloadFileManager::CompleteDownload, |
592 file_manager, download_id_)); | |
593 } | 593 } |
594 | 594 |
595 void DownloadItemImpl::OnDownloadRenamedToFinalName(const FilePath& full_path) { | 595 void DownloadItemImpl::OnDownloadRenamedToFinalName(const FilePath& full_path) { |
596 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 596 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
597 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 597 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
598 | 598 |
599 VLOG(20) << __FUNCTION__ << "()" | 599 VLOG(20) << __FUNCTION__ << "()" |
600 << " full_path = \"" << full_path.value() << "\"" | 600 << " full_path = \"" << full_path.value() << "\"" |
601 << " needed rename = " << NeedsRename() | 601 << " needed rename = " << NeedsRename() |
602 << " " << DebugString(false); | 602 << " " << DebugString(false); |
603 DCHECK(NeedsRename()); | 603 DCHECK(NeedsRename()); |
604 | 604 |
605 Rename(full_path); | 605 Rename(full_path); |
606 | 606 |
607 if (download_manager_->delegate()->ShouldOpenDownload(this)) { | 607 if (delegate_->ShouldOpenDownload(this)) { |
608 Completed(); | 608 Completed(); |
609 } else { | 609 } else { |
610 delegate_delayed_complete_ = true; | 610 delegate_delayed_complete_ = true; |
611 } | 611 } |
612 } | 612 } |
613 | 613 |
614 bool DownloadItemImpl::MatchesQuery(const string16& query) const { | 614 bool DownloadItemImpl::MatchesQuery(const string16& query) const { |
615 if (query.empty()) | 615 if (query.empty()) |
616 return true; | 616 return true; |
617 | 617 |
618 DCHECK_EQ(query, base::i18n::ToLower(query)); | 618 DCHECK_EQ(query, base::i18n::ToLower(query)); |
619 | 619 |
620 string16 url_raw(UTF8ToUTF16(GetURL().spec())); | 620 string16 url_raw(UTF8ToUTF16(GetURL().spec())); |
621 if (base::i18n::StringSearchIgnoringCaseAndAccents(query, url_raw)) | 621 if (base::i18n::StringSearchIgnoringCaseAndAccents(query, url_raw)) |
622 return true; | 622 return true; |
623 | 623 |
624 // TODO(phajdan.jr): write a test case for the following code. | 624 // TODO(phajdan.jr): write a test case for the following code. |
625 // A good test case would be: | 625 // A good test case would be: |
626 // "/\xe4\xbd\xa0\xe5\xa5\xbd\xe4\xbd\xa0\xe5\xa5\xbd", | 626 // "/\xe4\xbd\xa0\xe5\xa5\xbd\xe4\xbd\xa0\xe5\xa5\xbd", |
627 // L"/\x4f60\x597d\x4f60\x597d", | 627 // L"/\x4f60\x597d\x4f60\x597d", |
628 // "/%E4%BD%A0%E5%A5%BD%E4%BD%A0%E5%A5%BD" | 628 // "/%E4%BD%A0%E5%A5%BD%E4%BD%A0%E5%A5%BD" |
629 std::string languages; | 629 std::string languages; |
630 TabContents* tab = GetTabContents(); | 630 languages = content::GetContentClient()->browser()->GetAcceptLangs( |
631 if (tab) { | 631 delegate_->BrowserContext()); |
cbentzel
2011/11/29 21:30:44
Should DownloadItem::BrowserContext be used here i
Randy Smith (Not in Mondays)
2011/11/30 22:44:05
In an ideal world, DownloadItem wouldn't have a Br
| |
632 languages = content::GetContentClient()->browser()->GetAcceptLangs( | |
633 tab->browser_context()); | |
634 } | |
635 string16 url_formatted(net::FormatUrl(GetURL(), languages)); | 632 string16 url_formatted(net::FormatUrl(GetURL(), languages)); |
636 if (base::i18n::StringSearchIgnoringCaseAndAccents(query, url_formatted)) | 633 if (base::i18n::StringSearchIgnoringCaseAndAccents(query, url_formatted)) |
637 return true; | 634 return true; |
638 | 635 |
639 string16 path(GetFullPath().LossyDisplayName()); | 636 string16 path(GetFullPath().LossyDisplayName()); |
640 return base::i18n::StringSearchIgnoringCaseAndAccents(query, path); | 637 return base::i18n::StringSearchIgnoringCaseAndAccents(query, path); |
641 } | 638 } |
642 | 639 |
643 void DownloadItemImpl::SetFileCheckResults(const DownloadStateInfo& state) { | 640 void DownloadItemImpl::SetFileCheckResults(const DownloadStateInfo& state) { |
644 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 641 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
696 TabContents* DownloadItemImpl::GetTabContents() const { | 693 TabContents* DownloadItemImpl::GetTabContents() const { |
697 // TODO(rdsmith): Remove null check after removing GetTabContents() from | 694 // TODO(rdsmith): Remove null check after removing GetTabContents() from |
698 // paths that might be used by DownloadItems created from history import. | 695 // paths that might be used by DownloadItems created from history import. |
699 // Currently such items have null request_handle_s, where other items | 696 // Currently such items have null request_handle_s, where other items |
700 // (regular and SavePackage downloads) have actual objects off the pointer. | 697 // (regular and SavePackage downloads) have actual objects off the pointer. |
701 if (request_handle_.get()) | 698 if (request_handle_.get()) |
702 return request_handle_->GetTabContents(); | 699 return request_handle_->GetTabContents(); |
703 return NULL; | 700 return NULL; |
704 } | 701 } |
705 | 702 |
703 content::BrowserContext* DownloadItemImpl::BrowserContext() const { | |
704 return delegate_->BrowserContext(); | |
705 } | |
706 | |
706 FilePath DownloadItemImpl::GetTargetFilePath() const { | 707 FilePath DownloadItemImpl::GetTargetFilePath() const { |
707 return full_path_.DirName().Append(state_info_.target_name); | 708 return full_path_.DirName().Append(state_info_.target_name); |
708 } | 709 } |
709 | 710 |
710 FilePath DownloadItemImpl::GetFileNameToReportUser() const { | 711 FilePath DownloadItemImpl::GetFileNameToReportUser() const { |
711 if (state_info_.path_uniquifier > 0) { | 712 if (state_info_.path_uniquifier > 0) { |
712 FilePath name(state_info_.target_name); | 713 FilePath name(state_info_.target_name); |
713 DownloadFile::AppendNumberToPath(&name, state_info_.path_uniquifier); | 714 DownloadFile::AppendNumberToPath(&name, state_info_.path_uniquifier); |
714 return name; | 715 return name; |
715 } | 716 } |
716 return state_info_.target_name; | 717 return state_info_.target_name; |
717 } | 718 } |
718 | 719 |
719 FilePath DownloadItemImpl::GetUserVerifiedFilePath() const { | 720 FilePath DownloadItemImpl::GetUserVerifiedFilePath() const { |
720 return (safety_state_ == DownloadItem::SAFE) ? | 721 return (safety_state_ == DownloadItem::SAFE) ? |
721 GetTargetFilePath() : full_path_; | 722 GetTargetFilePath() : full_path_; |
722 } | 723 } |
723 | 724 |
724 void DownloadItemImpl::OffThreadCancel(DownloadFileManager* file_manager) { | 725 void DownloadItemImpl::OffThreadCancel(DownloadFileManager* file_manager) { |
725 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 726 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
726 request_handle_->CancelRequest(); | 727 request_handle_->CancelRequest(); |
727 | 728 |
728 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 729 BrowserThread::PostTask( |
729 base::Bind(&DownloadFileManager::CancelDownload, | 730 BrowserThread::FILE, FROM_HERE, |
730 file_manager, GetGlobalId())); | 731 base::Bind(&DownloadFileManager::CancelDownload, |
732 file_manager, download_id_)); | |
731 } | 733 } |
732 | 734 |
733 void DownloadItemImpl::Init(bool active) { | 735 void DownloadItemImpl::Init(bool active) { |
734 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 736 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
735 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 737 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
736 | 738 |
737 UpdateTarget(); | 739 UpdateTarget(); |
738 if (active) { | 740 if (active) { |
739 download_stats::RecordDownloadCount(download_stats::START_COUNT); | 741 download_stats::RecordDownloadCount(download_stats::START_COUNT); |
740 } | 742 } |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
852 total_bytes_ = total_bytes; | 854 total_bytes_ = total_bytes; |
853 } | 855 } |
854 const std::string& DownloadItemImpl::GetHash() const { return hash_; } | 856 const std::string& DownloadItemImpl::GetHash() const { return hash_; } |
855 int64 DownloadItemImpl::GetReceivedBytes() const { return received_bytes_; } | 857 int64 DownloadItemImpl::GetReceivedBytes() const { return received_bytes_; } |
856 int32 DownloadItemImpl::GetId() const { return download_id_.local(); } | 858 int32 DownloadItemImpl::GetId() const { return download_id_.local(); } |
857 DownloadId DownloadItemImpl::GetGlobalId() const { return download_id_; } | 859 DownloadId DownloadItemImpl::GetGlobalId() const { return download_id_; } |
858 base::Time DownloadItemImpl::GetStartTime() const { return start_time_; } | 860 base::Time DownloadItemImpl::GetStartTime() const { return start_time_; } |
859 base::Time DownloadItemImpl::GetEndTime() const { return end_time_; } | 861 base::Time DownloadItemImpl::GetEndTime() const { return end_time_; } |
860 void DownloadItemImpl::SetDbHandle(int64 handle) { db_handle_ = handle; } | 862 void DownloadItemImpl::SetDbHandle(int64 handle) { db_handle_ = handle; } |
861 int64 DownloadItemImpl::GetDbHandle() const { return db_handle_; } | 863 int64 DownloadItemImpl::GetDbHandle() const { return db_handle_; } |
862 DownloadManager* DownloadItemImpl::GetDownloadManager() { | |
863 return download_manager_; | |
864 } | |
865 bool DownloadItemImpl::IsPaused() const { return is_paused_; } | 864 bool DownloadItemImpl::IsPaused() const { return is_paused_; } |
866 bool DownloadItemImpl::GetOpenWhenComplete() const { | 865 bool DownloadItemImpl::GetOpenWhenComplete() const { |
867 return open_when_complete_; | 866 return open_when_complete_; |
868 } | 867 } |
869 void DownloadItemImpl::SetOpenWhenComplete(bool open) { | 868 void DownloadItemImpl::SetOpenWhenComplete(bool open) { |
870 open_when_complete_ = open; | 869 open_when_complete_ = open; |
871 } | 870 } |
872 bool DownloadItemImpl::GetFileExternallyRemoved() const { | 871 bool DownloadItemImpl::GetFileExternallyRemoved() const { |
873 return file_externally_removed_; | 872 return file_externally_removed_; |
874 } | 873 } |
(...skipping 15 matching lines...) Expand all Loading... | |
890 void DownloadItemImpl::SetOpened(bool opened) { opened_ = opened; } | 889 void DownloadItemImpl::SetOpened(bool opened) { opened_ = opened; } |
891 bool DownloadItemImpl::GetOpened() const { return opened_; } | 890 bool DownloadItemImpl::GetOpened() const { return opened_; } |
892 InterruptReason DownloadItemImpl::GetLastReason() const { | 891 InterruptReason DownloadItemImpl::GetLastReason() const { |
893 return last_reason_; | 892 return last_reason_; |
894 } | 893 } |
895 DownloadStateInfo DownloadItemImpl::GetStateInfo() const { return state_info_; } | 894 DownloadStateInfo DownloadItemImpl::GetStateInfo() const { return state_info_; } |
896 bool DownloadItemImpl::NeedsRename() const { | 895 bool DownloadItemImpl::NeedsRename() const { |
897 return state_info_.target_name != full_path_.BaseName(); | 896 return state_info_.target_name != full_path_.BaseName(); |
898 } | 897 } |
899 void DownloadItemImpl::MockDownloadOpenForTesting() { open_enabled_ = false; } | 898 void DownloadItemImpl::MockDownloadOpenForTesting() { open_enabled_ = false; } |
OLD | NEW |