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_item.h" | 5 #include "chrome/browser/download/download_item.h" |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
9 #include "base/format_macros.h" | 9 #include "base/format_macros.h" |
10 #include "base/i18n/case_conversion.h" | 10 #include "base/i18n/case_conversion.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/metrics/histogram.h" | 12 #include "base/metrics/histogram.h" |
13 #include "base/stringprintf.h" | 13 #include "base/stringprintf.h" |
14 #include "base/timer.h" | 14 #include "base/timer.h" |
15 #include "base/utf_string_conversions.h" | 15 #include "base/utf_string_conversions.h" |
16 #include "net/base/net_util.h" | 16 #include "net/base/net_util.h" |
17 #include "chrome/browser/download/download_create_info.h" | |
17 #include "chrome/browser/download/download_extensions.h" | 18 #include "chrome/browser/download/download_extensions.h" |
18 #include "chrome/browser/download/download_file_manager.h" | 19 #include "chrome/browser/download/download_file_manager.h" |
19 #include "chrome/browser/download/download_history.h" | 20 #include "chrome/browser/download/download_history.h" |
20 #include "chrome/browser/download/download_manager.h" | 21 #include "chrome/browser/download/download_manager.h" |
21 #include "chrome/browser/download/download_prefs.h" | 22 #include "chrome/browser/download/download_prefs.h" |
22 #include "chrome/browser/download/download_util.h" | 23 #include "chrome/browser/download/download_util.h" |
23 #include "chrome/browser/history/download_create_info.h" | |
24 #include "chrome/browser/platform_util.h" | 24 #include "chrome/browser/platform_util.h" |
25 #include "chrome/browser/prefs/pref_service.h" | 25 #include "chrome/browser/prefs/pref_service.h" |
26 #include "chrome/browser/profiles/profile.h" | 26 #include "chrome/browser/profiles/profile.h" |
27 #include "chrome/common/extensions/extension.h" | 27 #include "chrome/common/extensions/extension.h" |
28 #include "chrome/common/pref_names.h" | 28 #include "chrome/common/pref_names.h" |
29 #include "content/browser/browser_thread.h" | 29 #include "content/browser/browser_thread.h" |
30 #include "ui/base/l10n/l10n_util.h" | 30 #include "ui/base/l10n/l10n_util.h" |
31 | 31 |
32 // A DownloadItem normally goes through the following states: | 32 // A DownloadItem normally goes through the following states: |
33 // * Created (when download starts) | 33 // * Created (when download starts) |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
105 if (dangerous_url) { | 105 if (dangerous_url) { |
106 // dangerous URL overweights dangerous file. We check dangerous URL first. | 106 // dangerous URL overweights dangerous file. We check dangerous URL first. |
107 return DownloadItem::DANGEROUS_URL; | 107 return DownloadItem::DANGEROUS_URL; |
108 } | 108 } |
109 return dangerous_file ? | 109 return dangerous_file ? |
110 DownloadItem::DANGEROUS_FILE : DownloadItem::NOT_DANGEROUS; | 110 DownloadItem::DANGEROUS_FILE : DownloadItem::NOT_DANGEROUS; |
111 } | 111 } |
112 | 112 |
113 } // namespace | 113 } // namespace |
114 | 114 |
115 DownloadItem::DownloadStateInfo::DownloadStateInfo() | |
116 : path_uniquifier(0), | |
117 has_user_gesture(false), | |
118 prompt_user_for_save_location(false), | |
119 is_dangerous_file(false), | |
120 is_dangerous_url(false), | |
121 is_extension_install(false) { | |
122 } | |
123 | |
124 DownloadItem::DownloadStateInfo::DownloadStateInfo(const DownloadItem& item) | |
125 : target_name(item.manager_state_.target_name), | |
126 suggested_path(item.manager_state_.suggested_path), | |
127 path_uniquifier(item.manager_state_.path_uniquifier), | |
128 has_user_gesture(item.manager_state_.has_user_gesture), | |
129 prompt_user_for_save_location( | |
130 item.manager_state_.prompt_user_for_save_location), | |
131 is_dangerous_file(item.manager_state_.is_dangerous_file), | |
132 is_dangerous_url(item.manager_state_.is_dangerous_url), | |
133 is_extension_install(item.manager_state_.is_extension_install), | |
134 force_file_name(item.manager_state_.force_file_name) { | |
135 } | |
136 | |
137 DownloadItem::DownloadStateInfo::DownloadStateInfo( | |
138 bool has_user_gesture, | |
139 bool prompt_user_for_save_location) | |
140 : path_uniquifier(0), | |
141 has_user_gesture(has_user_gesture), | |
142 prompt_user_for_save_location(prompt_user_for_save_location), | |
143 is_dangerous_file(false), | |
144 is_dangerous_url(false), | |
145 is_extension_install(false) { | |
146 } | |
147 | |
148 DownloadItem::DownloadStateInfo::DownloadStateInfo( | |
149 const FilePath& target, | |
150 const FilePath& forced_name, | |
151 bool has_user_gesture, | |
152 bool prompt_user_for_save_location, | |
153 int uniquifier, | |
154 bool dangerous_file, | |
155 bool dangerous_url, | |
156 bool extension_install) | |
157 : target_name(target), | |
158 path_uniquifier(uniquifier), | |
159 has_user_gesture(has_user_gesture), | |
160 prompt_user_for_save_location(prompt_user_for_save_location), | |
161 is_dangerous_file(dangerous_file), | |
162 is_dangerous_url(dangerous_url), | |
163 is_extension_install(extension_install), | |
164 force_file_name(forced_name) { | |
165 } | |
166 | |
167 bool DownloadItem::DownloadStateInfo::IsDangerous() const { | |
168 return is_dangerous_url || is_dangerous_file; | |
169 } | |
170 | |
115 // Constructor for reading from the history service. | 171 // Constructor for reading from the history service. |
116 DownloadItem::DownloadItem(DownloadManager* download_manager, | 172 DownloadItem::DownloadItem(DownloadManager* download_manager, |
117 const DownloadCreateInfo& info) | 173 const DownloadHistoryInfo& info) |
118 : id_(-1), | 174 : history_info_(info.path, info.url_chain, info.referrer_url, |
119 full_path_(info.path), | 175 info.start_time, info.received_bytes, info.total_bytes, |
120 path_uniquifier_(0), | 176 info.state, info.db_handle, info.download_id), |
121 url_chain_(info.url_chain), | |
122 referrer_url_(info.referrer_url), | |
123 mime_type_(info.mime_type), | |
124 original_mime_type_(info.original_mime_type), | |
125 total_bytes_(info.total_bytes), | |
126 received_bytes_(info.received_bytes), | |
127 start_tick_(base::TimeTicks()), | 177 start_tick_(base::TimeTicks()), |
128 state_(static_cast<DownloadState>(info.state)), | |
129 start_time_(info.start_time), | |
130 db_handle_(info.db_handle), | |
131 download_manager_(download_manager), | 178 download_manager_(download_manager), |
132 is_paused_(false), | 179 is_paused_(false), |
133 open_when_complete_(false), | 180 open_when_complete_(false), |
134 safety_state_(SAFE), | 181 safety_state_(SAFE), |
135 danger_type_(NOT_DANGEROUS), | 182 danger_type_(NOT_DANGEROUS), |
136 auto_opened_(false), | 183 auto_opened_(false), |
137 target_name_(info.original_name), | |
138 save_as_(false), | |
139 is_otr_(false), | 184 is_otr_(false), |
140 is_extension_install_(info.is_extension_install), | |
141 is_temporary_(false), | 185 is_temporary_(false), |
142 all_data_saved_(false), | 186 all_data_saved_(false), |
143 opened_(false) { | 187 opened_(false) { |
144 if (IsInProgress()) | 188 if (IsInProgress()) |
145 state_ = CANCELLED; | 189 history_info_.state = CANCELLED; |
146 if (IsComplete()) | 190 if (IsComplete()) |
147 all_data_saved_ = true; | 191 all_data_saved_ = true; |
148 Init(false /* don't start progress timer */); | 192 Init(false /* don't start progress timer */); |
149 } | 193 } |
150 | 194 |
151 // Constructing for a regular download: | 195 // Constructing for a regular download: |
152 DownloadItem::DownloadItem(DownloadManager* download_manager, | 196 DownloadItem::DownloadItem(DownloadManager* download_manager, |
153 const DownloadCreateInfo& info, | 197 const DownloadCreateInfo& info, |
154 bool is_otr) | 198 bool is_otr) |
155 : id_(info.download_id), | 199 : history_info_(info.path, info.url_chain, info.referrer_url, |
156 full_path_(info.path), | 200 info.start_time, info.received_bytes, info.total_bytes, |
157 path_uniquifier_(info.path_uniquifier), | 201 IN_PROGRESS, DownloadHistory::kUninitializedHandle, |
158 url_chain_(info.url_chain), | 202 info.download_id), |
159 referrer_url_(info.referrer_url), | 203 manager_state_(info.original_name, info.save_info.file_path, |
204 info.has_user_gesture, info.prompt_user_for_save_location, | |
205 info.path_uniquifier, info.is_dangerous_file, | |
206 info.is_dangerous_url, info.is_extension_install), | |
207 process_handle_(info.process_handle), | |
208 content_disposition_(info.content_disposition), | |
160 mime_type_(info.mime_type), | 209 mime_type_(info.mime_type), |
161 original_mime_type_(info.original_mime_type), | 210 original_mime_type_(info.original_mime_type), |
162 total_bytes_(info.total_bytes), | 211 referrer_charset_(info.referrer_charset), |
163 received_bytes_(0), | |
164 last_os_error_(0), | 212 last_os_error_(0), |
165 start_tick_(base::TimeTicks::Now()), | 213 start_tick_(base::TimeTicks::Now()), |
166 state_(IN_PROGRESS), | |
167 start_time_(info.start_time), | |
168 db_handle_(DownloadHistory::kUninitializedHandle), | |
169 download_manager_(download_manager), | 214 download_manager_(download_manager), |
170 is_paused_(false), | 215 is_paused_(false), |
171 open_when_complete_(false), | 216 open_when_complete_(false), |
172 safety_state_(GetSafetyState(info.is_dangerous_file, | 217 safety_state_(GetSafetyState(info.is_dangerous_file, |
173 info.is_dangerous_url)), | 218 info.is_dangerous_url)), |
174 danger_type_(GetDangerType(info.is_dangerous_file, | 219 danger_type_(GetDangerType(info.is_dangerous_file, |
175 info.is_dangerous_url)), | 220 info.is_dangerous_url)), |
176 auto_opened_(false), | 221 auto_opened_(false), |
177 target_name_(info.original_name), | |
178 process_handle_(info.process_handle), | |
179 save_as_(info.prompt_user_for_save_location), | |
180 is_otr_(is_otr), | 222 is_otr_(is_otr), |
181 is_extension_install_(info.is_extension_install), | |
182 is_temporary_(!info.save_info.file_path.empty()), | 223 is_temporary_(!info.save_info.file_path.empty()), |
183 all_data_saved_(false), | 224 all_data_saved_(false), |
184 opened_(false) { | 225 opened_(false) { |
185 Init(true /* start progress timer */); | 226 Init(true /* start progress timer */); |
186 } | 227 } |
187 | 228 |
188 // Constructing for the "Save Page As..." feature: | 229 // Constructing for the "Save Page As..." feature: |
189 DownloadItem::DownloadItem(DownloadManager* download_manager, | 230 DownloadItem::DownloadItem(DownloadManager* download_manager, |
190 const FilePath& path, | 231 const FilePath& path, |
191 const GURL& url, | 232 const GURL& url, |
192 bool is_otr) | 233 bool is_otr) |
193 : id_(1), | 234 : history_info_(path, url, base::Time::Now(), 0, 0, IN_PROGRESS), |
194 full_path_(path), | |
195 path_uniquifier_(0), | |
196 url_chain_(1, url), | |
197 referrer_url_(GURL()), | |
198 mime_type_(std::string()), | |
199 original_mime_type_(std::string()), | |
200 total_bytes_(0), | |
201 received_bytes_(0), | |
202 last_os_error_(0), | 235 last_os_error_(0), |
203 start_tick_(base::TimeTicks::Now()), | 236 start_tick_(base::TimeTicks::Now()), |
204 state_(IN_PROGRESS), | |
205 start_time_(base::Time::Now()), | |
206 db_handle_(DownloadHistory::kUninitializedHandle), | |
207 download_manager_(download_manager), | 237 download_manager_(download_manager), |
208 is_paused_(false), | 238 is_paused_(false), |
209 open_when_complete_(false), | 239 open_when_complete_(false), |
210 safety_state_(SAFE), | 240 safety_state_(SAFE), |
211 danger_type_(NOT_DANGEROUS), | 241 danger_type_(NOT_DANGEROUS), |
212 auto_opened_(false), | 242 auto_opened_(false), |
213 save_as_(false), | |
214 is_otr_(is_otr), | 243 is_otr_(is_otr), |
215 is_extension_install_(false), | |
216 is_temporary_(false), | 244 is_temporary_(false), |
217 all_data_saved_(false), | 245 all_data_saved_(false), |
218 opened_(false) { | 246 opened_(false) { |
219 Init(true /* start progress timer */); | 247 Init(true /* start progress timer */); |
220 } | 248 } |
221 | 249 |
222 DownloadItem::~DownloadItem() { | 250 DownloadItem::~DownloadItem() { |
223 state_ = REMOVING; | 251 history_info_.state = REMOVING; |
224 UpdateObservers(); | 252 UpdateObservers(); |
225 } | 253 } |
226 | 254 |
227 void DownloadItem::AddObserver(Observer* observer) { | 255 void DownloadItem::AddObserver(Observer* observer) { |
228 observers_.AddObserver(observer); | 256 observers_.AddObserver(observer); |
229 } | 257 } |
230 | 258 |
231 void DownloadItem::RemoveObserver(Observer* observer) { | 259 void DownloadItem::RemoveObserver(Observer* observer) { |
232 observers_.RemoveObserver(observer); | 260 observers_.RemoveObserver(observer); |
233 } | 261 } |
234 | 262 |
235 void DownloadItem::UpdateObservers() { | 263 void DownloadItem::UpdateObservers() { |
236 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadUpdated(this)); | 264 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadUpdated(this)); |
237 } | 265 } |
238 | 266 |
239 bool DownloadItem::CanOpenDownload() { | 267 bool DownloadItem::CanOpenDownload() { |
240 return !Extension::IsExtension(target_name_); | 268 return !Extension::IsExtension(manager_state_.target_name); |
241 } | 269 } |
242 | 270 |
243 bool DownloadItem::ShouldOpenFileBasedOnExtension() { | 271 bool DownloadItem::ShouldOpenFileBasedOnExtension() { |
244 return download_manager_->ShouldOpenFileBasedOnExtension( | 272 return download_manager_->ShouldOpenFileBasedOnExtension( |
245 GetUserVerifiedFilePath()); | 273 GetUserVerifiedFilePath()); |
246 } | 274 } |
247 | 275 |
248 void DownloadItem::OpenFilesBasedOnExtension(bool open) { | 276 void DownloadItem::OpenFilesBasedOnExtension(bool open) { |
249 DownloadPrefs* prefs = download_manager_->download_prefs(); | 277 DownloadPrefs* prefs = download_manager_->download_prefs(); |
250 if (open) | 278 if (open) |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
289 } | 317 } |
290 | 318 |
291 void DownloadItem::DangerousDownloadValidated() { | 319 void DownloadItem::DangerousDownloadValidated() { |
292 UMA_HISTOGRAM_ENUMERATION("Download.DangerousDownloadValidated", | 320 UMA_HISTOGRAM_ENUMERATION("Download.DangerousDownloadValidated", |
293 danger_type_, | 321 danger_type_, |
294 DANGEROUS_TYPE_MAX); | 322 DANGEROUS_TYPE_MAX); |
295 download_manager_->DangerousDownloadValidated(this); | 323 download_manager_->DangerousDownloadValidated(this); |
296 } | 324 } |
297 | 325 |
298 void DownloadItem::UpdateSize(int64 bytes_so_far) { | 326 void DownloadItem::UpdateSize(int64 bytes_so_far) { |
299 received_bytes_ = bytes_so_far; | 327 history_info_.received_bytes = bytes_so_far; |
300 | 328 |
301 // If we've received more data than we were expecting (bad server info?), | 329 // If we've received more data than we were expecting (bad server info?), |
302 // revert to 'unknown size mode'. | 330 // revert to 'unknown size mode'. |
303 if (received_bytes_ > total_bytes_) | 331 if (history_info_.received_bytes > history_info_.total_bytes) |
304 total_bytes_ = 0; | 332 history_info_.total_bytes = 0; |
305 } | 333 } |
306 | 334 |
307 void DownloadItem::StartProgressTimer() { | 335 void DownloadItem::StartProgressTimer() { |
308 update_timer_.Start(base::TimeDelta::FromMilliseconds(kUpdateTimeMs), this, | 336 update_timer_.Start(base::TimeDelta::FromMilliseconds(kUpdateTimeMs), this, |
309 &DownloadItem::UpdateObservers); | 337 &DownloadItem::UpdateObservers); |
310 } | 338 } |
311 | 339 |
312 void DownloadItem::StopProgressTimer() { | 340 void DownloadItem::StopProgressTimer() { |
313 update_timer_.Stop(); | 341 update_timer_.Stop(); |
314 } | 342 } |
(...skipping 14 matching lines...) Expand all Loading... | |
329 void DownloadItem::Cancel(bool update_history) { | 357 void DownloadItem::Cancel(bool update_history) { |
330 VLOG(20) << __FUNCTION__ << "() download = " << DebugString(true); | 358 VLOG(20) << __FUNCTION__ << "() download = " << DebugString(true); |
331 if (!IsPartialDownload()) { | 359 if (!IsPartialDownload()) { |
332 // Small downloads might be complete before this method has | 360 // Small downloads might be complete before this method has |
333 // a chance to run. | 361 // a chance to run. |
334 return; | 362 return; |
335 } | 363 } |
336 | 364 |
337 download_util::RecordDownloadCount(download_util::CANCELLED_COUNT); | 365 download_util::RecordDownloadCount(download_util::CANCELLED_COUNT); |
338 | 366 |
339 state_ = CANCELLED; | 367 history_info_.state = CANCELLED; |
340 UpdateObservers(); | 368 UpdateObservers(); |
341 StopProgressTimer(); | 369 StopProgressTimer(); |
342 if (update_history) | 370 if (update_history) |
343 download_manager_->DownloadCancelled(id_); | 371 download_manager_->DownloadCancelled(history_info_.download_id); |
344 } | 372 } |
345 | 373 |
346 void DownloadItem::MarkAsComplete() { | 374 void DownloadItem::MarkAsComplete() { |
347 DCHECK(all_data_saved_); | 375 DCHECK(all_data_saved_); |
348 state_ = COMPLETE; | 376 history_info_.state = COMPLETE; |
349 UpdateObservers(); | 377 UpdateObservers(); |
350 } | 378 } |
351 | 379 |
352 void DownloadItem::OnAllDataSaved(int64 size) { | 380 void DownloadItem::OnAllDataSaved(int64 size) { |
353 DCHECK(!all_data_saved_); | 381 DCHECK(!all_data_saved_); |
354 all_data_saved_ = true; | 382 all_data_saved_ = true; |
355 UpdateSize(size); | 383 UpdateSize(size); |
356 StopProgressTimer(); | 384 StopProgressTimer(); |
357 } | 385 } |
358 | 386 |
(...skipping 14 matching lines...) Expand all Loading... | |
373 is_temporary()) { | 401 is_temporary()) { |
374 // If the download is temporary, like in drag-and-drop, do not open it but | 402 // If the download is temporary, like in drag-and-drop, do not open it but |
375 // we still need to set it auto-opened so that it can be removed from the | 403 // we still need to set it auto-opened so that it can be removed from the |
376 // download shelf. | 404 // download shelf. |
377 if (!is_temporary()) | 405 if (!is_temporary()) |
378 OpenDownload(); | 406 OpenDownload(); |
379 auto_opened_ = true; | 407 auto_opened_ = true; |
380 } | 408 } |
381 | 409 |
382 DCHECK(all_data_saved_); | 410 DCHECK(all_data_saved_); |
383 state_ = COMPLETE; | 411 history_info_.state = COMPLETE; |
384 UpdateObservers(); | 412 UpdateObservers(); |
385 download_manager_->DownloadCompleted(id()); | 413 download_manager_->DownloadCompleted(id()); |
386 } | 414 } |
387 | 415 |
388 void DownloadItem::Interrupted(int64 size, int os_error) { | 416 void DownloadItem::Interrupted(int64 size, int os_error) { |
389 if (!IsInProgress()) | 417 if (!IsInProgress()) |
390 return; | 418 return; |
391 state_ = INTERRUPTED; | 419 history_info_.state = INTERRUPTED; |
392 last_os_error_ = os_error; | 420 last_os_error_ = os_error; |
393 UpdateSize(size); | 421 UpdateSize(size); |
394 StopProgressTimer(); | 422 StopProgressTimer(); |
395 UpdateObservers(); | 423 UpdateObservers(); |
396 } | 424 } |
397 | 425 |
398 void DownloadItem::Delete(DeleteReason reason) { | 426 void DownloadItem::Delete(DeleteReason reason) { |
399 switch (reason) { | 427 switch (reason) { |
400 case DELETE_DUE_TO_USER_DISCARD: | 428 case DELETE_DUE_TO_USER_DISCARD: |
401 UMA_HISTOGRAM_ENUMERATION("Download.UserDiscard", danger_type_, | 429 UMA_HISTOGRAM_ENUMERATION("Download.UserDiscard", danger_type_, |
402 DANGEROUS_TYPE_MAX); | 430 DANGEROUS_TYPE_MAX); |
403 break; | 431 break; |
404 case DELETE_DUE_TO_BROWSER_SHUTDOWN: | 432 case DELETE_DUE_TO_BROWSER_SHUTDOWN: |
405 UMA_HISTOGRAM_ENUMERATION("Download.Discard", danger_type_, | 433 UMA_HISTOGRAM_ENUMERATION("Download.Discard", danger_type_, |
406 DANGEROUS_TYPE_MAX); | 434 DANGEROUS_TYPE_MAX); |
407 break; | 435 break; |
408 default: | 436 default: |
409 NOTREACHED(); | 437 NOTREACHED(); |
410 } | 438 } |
411 | 439 |
412 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 440 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
413 NewRunnableFunction(&DeleteDownloadedFile, full_path_)); | 441 NewRunnableFunction(&DeleteDownloadedFile, history_info_.path)); |
414 Remove(); | 442 Remove(); |
415 // We have now been deleted. | 443 // We have now been deleted. |
416 } | 444 } |
417 | 445 |
418 void DownloadItem::Remove() { | 446 void DownloadItem::Remove() { |
419 Cancel(true); | 447 Cancel(true); |
420 state_ = REMOVING; | 448 history_info_.state = REMOVING; |
421 download_manager_->RemoveDownload(db_handle_); | 449 download_manager_->RemoveDownload(history_info_.db_handle); |
422 // We have now been deleted. | 450 // We have now been deleted. |
423 } | 451 } |
424 | 452 |
425 bool DownloadItem::TimeRemaining(base::TimeDelta* remaining) const { | 453 bool DownloadItem::TimeRemaining(base::TimeDelta* remaining) const { |
426 if (total_bytes_ <= 0) | 454 if (history_info_.total_bytes <= 0) |
427 return false; // We never received the content_length for this download. | 455 return false; // We never received the content_length for this download. |
428 | 456 |
429 int64 speed = CurrentSpeed(); | 457 int64 speed = CurrentSpeed(); |
430 if (speed == 0) | 458 if (speed == 0) |
431 return false; | 459 return false; |
432 | 460 |
433 *remaining = | 461 *remaining = base::TimeDelta::FromSeconds( |
434 base::TimeDelta::FromSeconds((total_bytes_ - received_bytes_) / speed); | 462 (history_info_.total_bytes - history_info_.received_bytes) / speed); |
435 return true; | 463 return true; |
436 } | 464 } |
437 | 465 |
438 int64 DownloadItem::CurrentSpeed() const { | 466 int64 DownloadItem::CurrentSpeed() const { |
439 if (is_paused_) | 467 if (is_paused_) |
440 return 0; | 468 return 0; |
441 base::TimeDelta diff = base::TimeTicks::Now() - start_tick_; | 469 base::TimeDelta diff = base::TimeTicks::Now() - start_tick_; |
442 int64 diff_ms = diff.InMilliseconds(); | 470 int64 diff_ms = diff.InMilliseconds(); |
443 return diff_ms == 0 ? 0 : received_bytes_ * 1000 / diff_ms; | 471 return diff_ms == 0 ? 0 : history_info_.received_bytes * 1000 / diff_ms; |
444 } | 472 } |
445 | 473 |
446 int DownloadItem::PercentComplete() const { | 474 int DownloadItem::PercentComplete() const { |
447 return (total_bytes_ > 0) ? | 475 return (history_info_.total_bytes > 0) ? |
448 static_cast<int>(received_bytes_ * 100.0 / total_bytes_) : -1; | 476 static_cast<int>(history_info_.received_bytes * 100.0 / |
477 history_info_.total_bytes) : | |
478 -1; | |
449 } | 479 } |
450 | 480 |
451 void DownloadItem::Rename(const FilePath& full_path) { | 481 void DownloadItem::Rename(const FilePath& full_path) { |
452 VLOG(20) << __FUNCTION__ << "()" | 482 VLOG(20) << __FUNCTION__ << "()" |
453 << " full_path = \"" << full_path.value() << "\"" | 483 << " full_path = \"" << full_path.value() << "\"" |
454 << " " << DebugString(true); | 484 << " " << DebugString(true); |
455 DCHECK(!full_path.empty()); | 485 DCHECK(!full_path.empty()); |
456 full_path_ = full_path; | 486 history_info_.path = full_path; |
457 } | 487 } |
458 | 488 |
459 void DownloadItem::TogglePause() { | 489 void DownloadItem::TogglePause() { |
460 DCHECK(IsInProgress()); | 490 DCHECK(IsInProgress()); |
461 download_manager_->PauseDownload(id_, !is_paused_); | 491 download_manager_->PauseDownload(history_info_.download_id, !is_paused_); |
462 is_paused_ = !is_paused_; | 492 is_paused_ = !is_paused_; |
463 UpdateObservers(); | 493 UpdateObservers(); |
464 } | 494 } |
465 | 495 |
466 void DownloadItem::OnDownloadCompleting(DownloadFileManager* file_manager) { | 496 void DownloadItem::OnDownloadCompleting(DownloadFileManager* file_manager) { |
467 VLOG(20) << __FUNCTION__ << "()" | 497 VLOG(20) << __FUNCTION__ << "()" |
468 << " needs rename = " << NeedsRename() | 498 << " needs rename = " << NeedsRename() |
469 << " " << DebugString(true); | 499 << " " << DebugString(true); |
470 DCHECK_NE(DANGEROUS, safety_state()); | 500 DCHECK_NE(DANGEROUS, safety_state()); |
471 DCHECK(file_manager); | 501 DCHECK(file_manager); |
472 | 502 |
473 if (NeedsRename()) { | 503 if (NeedsRename()) { |
474 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 504 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
475 NewRunnableMethod(file_manager, | 505 NewRunnableMethod(file_manager, |
476 &DownloadFileManager::RenameCompletingDownloadFile, id(), | 506 &DownloadFileManager::RenameCompletingDownloadFile, id(), |
477 GetTargetFilePath(), safety_state() == SAFE)); | 507 GetTargetFilePath(), safety_state() == SAFE)); |
478 return; | 508 return; |
479 } | 509 } |
480 | 510 |
481 Completed(); | 511 Completed(); |
482 | 512 |
483 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 513 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
484 NewRunnableMethod(file_manager, &DownloadFileManager::CompleteDownload, | 514 NewRunnableMethod(file_manager, &DownloadFileManager::CompleteDownload, |
485 id())); | 515 id())); |
486 } | 516 } |
487 | 517 |
488 void DownloadItem::OnDownloadRenamedToFinalName(const FilePath& full_path) { | 518 void DownloadItem::OnDownloadRenamedToFinalName(const FilePath& full_path) { |
489 VLOG(20) << __FUNCTION__ << "()" | 519 VLOG(20) << __FUNCTION__ << "()" |
490 << " full_path = " << full_path.value() | 520 << " full_path = \"" << full_path.value() << "\"" |
491 << " needed rename = " << NeedsRename() | 521 << " needed rename = " << NeedsRename() |
492 << " " << DebugString(false); | 522 << " " << DebugString(false); |
493 DCHECK(NeedsRename()); | 523 DCHECK(NeedsRename()); |
494 | 524 |
495 Rename(full_path); | 525 Rename(full_path); |
496 | 526 |
497 Completed(); | 527 Completed(); |
498 } | 528 } |
499 | 529 |
500 bool DownloadItem::MatchesQuery(const string16& query) const { | 530 bool DownloadItem::MatchesQuery(const string16& query) const { |
(...skipping 18 matching lines...) Expand all Loading... | |
519 return true; | 549 return true; |
520 | 550 |
521 string16 path(base::i18n::ToLower(full_path().LossyDisplayName())); | 551 string16 path(base::i18n::ToLower(full_path().LossyDisplayName())); |
522 // This shouldn't just do a substring match; it is wrong for Unicode | 552 // This shouldn't just do a substring match; it is wrong for Unicode |
523 // due to normalization and we have a fancier search-query system | 553 // due to normalization and we have a fancier search-query system |
524 // used elsewhere. | 554 // used elsewhere. |
525 // http://code.google.com/p/chromium/issues/detail?id=71982 | 555 // http://code.google.com/p/chromium/issues/detail?id=71982 |
526 return (path.find(query) != string16::npos); | 556 return (path.find(query) != string16::npos); |
527 } | 557 } |
528 | 558 |
529 void DownloadItem::SetFileCheckResults(const FilePath& path, | 559 void DownloadItem::SetFileCheckResults(const DownloadStateInfo& state) { |
530 bool is_dangerous_file, | 560 VLOG(20) << " " << __FUNCTION__ << "()" << " this = " << DebugString(true); |
531 bool is_dangerous_url, | 561 manager_state_ = state; |
532 int path_uniquifier, | 562 VLOG(20) << " " << __FUNCTION__ << "()" << " this = " << DebugString(true); |
533 bool prompt, | |
534 bool is_extension_install, | |
535 const FilePath& original_name) { | |
536 VLOG(20) << " " << __FUNCTION__ << "()" | |
537 << " path = \"" << path.value() << "\"" | |
538 << " is_dangerous_file = " << is_dangerous_file | |
539 << " is_dangerous_url = " << is_dangerous_url | |
540 << " path_uniquifier = " << path_uniquifier | |
541 << " prompt = " << prompt | |
542 << " is_extension_install = " << is_extension_install | |
543 << " path = \"" << path.value() << "\"" | |
544 << " original_name = \"" << original_name.value() << "\"" | |
545 << " " << DebugString(true); | |
546 // Make sure the initial file name is set only once. | |
547 DCHECK(full_path_.empty()); | |
548 DCHECK(!path.empty()); | |
549 | 563 |
550 full_path_ = path; | 564 safety_state_ = GetSafetyState(manager_state_.is_dangerous_file, |
551 safety_state_ = GetSafetyState(is_dangerous_file, is_dangerous_url); | 565 manager_state_.is_dangerous_url); |
552 danger_type_ = GetDangerType(is_dangerous_file, is_dangerous_url); | 566 danger_type_ = GetDangerType(manager_state_.is_dangerous_file, |
553 path_uniquifier_ = path_uniquifier; | 567 manager_state_.is_dangerous_url); |
554 save_as_ = prompt; | 568 } |
555 is_extension_install_ = is_extension_install; | |
556 target_name_ = original_name; | |
557 | 569 |
558 if (target_name_.value().empty()) | 570 void DownloadItem::UpdateTarget() { |
559 target_name_ = full_path_.BaseName(); | 571 if (manager_state_.target_name.value().empty()) |
572 manager_state_.target_name = history_info_.path.BaseName(); | |
560 } | 573 } |
561 | 574 |
562 FilePath DownloadItem::GetTargetFilePath() const { | 575 FilePath DownloadItem::GetTargetFilePath() const { |
563 return full_path_.DirName().Append(target_name_); | 576 return history_info_.path.DirName().Append(manager_state_.target_name); |
564 } | 577 } |
565 | 578 |
566 FilePath DownloadItem::GetFileNameToReportUser() const { | 579 FilePath DownloadItem::GetFileNameToReportUser() const { |
567 if (path_uniquifier_ > 0) { | 580 if (manager_state_.path_uniquifier > 0) { |
568 FilePath name(target_name_); | 581 FilePath name(manager_state_.target_name); |
569 download_util::AppendNumberToPath(&name, path_uniquifier_); | 582 download_util::AppendNumberToPath(&name, manager_state_.path_uniquifier); |
570 return name; | 583 return name; |
571 } | 584 } |
572 return target_name_; | 585 return manager_state_.target_name; |
573 } | 586 } |
574 | 587 |
575 FilePath DownloadItem::GetUserVerifiedFilePath() const { | 588 FilePath DownloadItem::GetUserVerifiedFilePath() const { |
576 return (safety_state_ == DownloadItem::SAFE) ? | 589 return (safety_state_ == DownloadItem::SAFE) ? |
577 GetTargetFilePath() : full_path_; | 590 GetTargetFilePath() : history_info_.path; |
578 } | 591 } |
579 | 592 |
580 void DownloadItem::Init(bool start_timer) { | 593 void DownloadItem::Init(bool start_timer) { |
581 if (target_name_.value().empty()) | 594 UpdateTarget(); |
582 target_name_ = full_path_.BaseName(); | |
583 if (start_timer) | 595 if (start_timer) |
584 StartProgressTimer(); | 596 StartProgressTimer(); |
585 VLOG(20) << __FUNCTION__ << "() " << DebugString(true); | 597 VLOG(20) << __FUNCTION__ << "() " << DebugString(true); |
586 } | 598 } |
587 | 599 |
588 // TODO(ahendrickson) -- Move |INTERRUPTED| from |IsCancelled()| to | 600 // TODO(ahendrickson) -- Move |INTERRUPTED| from |IsCancelled()| to |
589 // |IsPartialDownload()|, when resuming interrupted downloads is implemented. | 601 // |IsPartialDownload()|, when resuming interrupted downloads is implemented. |
590 bool DownloadItem::IsPartialDownload() const { | 602 bool DownloadItem::IsPartialDownload() const { |
591 return (state_ == IN_PROGRESS); | 603 return (history_info_.state == IN_PROGRESS); |
592 } | 604 } |
593 | 605 |
594 bool DownloadItem::IsInProgress() const { | 606 bool DownloadItem::IsInProgress() const { |
595 return (state_ == IN_PROGRESS); | 607 return (history_info_.state == IN_PROGRESS); |
596 } | 608 } |
597 | 609 |
598 bool DownloadItem::IsCancelled() const { | 610 bool DownloadItem::IsCancelled() const { |
599 return (state_ == CANCELLED) || (state_ == INTERRUPTED); | 611 return (history_info_.state == CANCELLED) || |
612 (history_info_.state == INTERRUPTED); | |
600 } | 613 } |
601 | 614 |
602 bool DownloadItem::IsInterrupted() const { | 615 bool DownloadItem::IsInterrupted() const { |
603 return (state_ == INTERRUPTED); | 616 return (history_info_.state == INTERRUPTED); |
604 } | 617 } |
605 | 618 |
606 bool DownloadItem::IsComplete() const { | 619 bool DownloadItem::IsComplete() const { |
607 return (state_ == COMPLETE); | 620 return (history_info_.state == COMPLETE); |
621 } | |
622 | |
623 // This function converts history_info_.state (which is an int32) to a | |
624 // DownloadItem::DownloadState enum, and returns it. | |
625 // A DCHECK() will be fire if it has an illegal value. | |
626 DownloadItem::DownloadState DownloadItem::state() const { | |
Paweł Hajdan Jr.
2011/05/19 16:18:25
nit: This isn't a simple inlined accessor, could y
ahendrickson
2011/05/19 20:16:49
Would you be willing to let me do this in a separa
Paweł Hajdan Jr.
2011/05/20 09:04:42
Yes, that makes sense. Please add a TODO in this C
| |
627 DCHECK_LE(IN_PROGRESS, history_info_.state); | |
628 DCHECK_GE(INTERRUPTED, history_info_.state); | |
Randy Smith (Not in Mondays)
2011/05/19 17:05:27
I'd rather have these DCHECKs (and the comparisons
ahendrickson
2011/05/19 20:16:49
Done.
| |
629 | |
630 if ((history_info_.state < IN_PROGRESS) || | |
631 (history_info_.state > INTERRUPTED)) { | |
632 return IN_PROGRESS; | |
Paweł Hajdan Jr.
2011/05/19 16:18:25
I think it'd be simpler to write NOTREACHED() << h
ahendrickson
2011/05/19 20:16:49
Done.
| |
633 } | |
634 return static_cast<DownloadState>(history_info_.state); | |
635 } | |
636 | |
637 const GURL& DownloadItem::url() const { | |
Paweł Hajdan Jr.
2011/05/19 16:18:25
nit: This isn't a simple inlined accessor, could y
ahendrickson
2011/05/19 20:16:49
Done.
| |
638 return history_info_.url_chain.empty() ? | |
639 GURL::EmptyGURL() : history_info_.url_chain.back(); | |
608 } | 640 } |
609 | 641 |
610 std::string DownloadItem::DebugString(bool verbose) const { | 642 std::string DownloadItem::DebugString(bool verbose) const { |
611 std::string description = base::StringPrintf( | 643 std::string description = |
612 "{ id_ = %d state = %s", id_, DebugDownloadStateString(state())); | 644 base::StringPrintf("{ id = %d" |
645 " state = %s", | |
646 history_info_.download_id, | |
647 DebugDownloadStateString(state())); | |
613 | 648 |
614 // Construct a string of the URL chain. | 649 // Construct a string of the URL chain. |
615 std::string url_list("<none>"); | 650 std::string url_list("<none>"); |
616 if (!url_chain_.empty()) { | 651 if (!history_info_.url_chain.empty()) { |
617 std::vector<GURL>::const_iterator iter = url_chain_.begin(); | 652 std::vector<GURL>::const_iterator iter = history_info_.url_chain.begin(); |
618 std::vector<GURL>::const_iterator last = url_chain_.end(); | 653 std::vector<GURL>::const_iterator last = history_info_.url_chain.end(); |
619 url_list = (*iter).spec(); | 654 url_list = (*iter).spec(); |
620 ++iter; | 655 ++iter; |
621 for ( ; verbose && (iter != last); ++iter) { | 656 for ( ; verbose && (iter != last); ++iter) { |
622 url_list += " -> "; | 657 url_list += " ->\n\t"; |
623 const GURL& next_url = *iter; | 658 const GURL& next_url = *iter; |
624 url_list += next_url.spec(); | 659 url_list += next_url.spec(); |
625 } | 660 } |
626 } | 661 } |
627 | 662 |
628 if (verbose) { | 663 if (verbose) { |
629 description += base::StringPrintf( | 664 description += base::StringPrintf( |
630 " db_handle = %" PRId64 | 665 " db_handle = %" PRId64 |
631 " total_bytes = %" PRId64 | 666 " total_bytes = %" PRId64 |
632 " is_paused = " "%c" | 667 " is_paused = %c" |
633 " is_extension_install = " "%c" | 668 " is_extension_install = %c" |
634 " is_otr = " "%c" | 669 " is_otr = %c" |
635 " safety_state = " "%s" | 670 " safety_state = %s" |
636 " url_chain = " "\"%s\"" | 671 " url_chain = \n\t\"%s\"\n\t" |
637 " target_name_ = \"%" PRFilePath "\"" | 672 " target_name = \"%" PRFilePath "\"" |
638 " full_path = \"%" PRFilePath "\"", | 673 " full_path = \"%" PRFilePath "\"", |
639 db_handle(), | 674 db_handle(), |
640 total_bytes(), | 675 total_bytes(), |
641 is_paused() ? 'T' : 'F', | 676 is_paused() ? 'T' : 'F', |
642 is_extension_install() ? 'T' : 'F', | 677 is_extension_install() ? 'T' : 'F', |
643 is_otr() ? 'T' : 'F', | 678 is_otr() ? 'T' : 'F', |
644 DebugSafetyStateString(safety_state()), | 679 DebugSafetyStateString(safety_state()), |
645 url_list.c_str(), | 680 url_list.c_str(), |
646 target_name_.value().c_str(), | 681 manager_state_.target_name.value().c_str(), |
647 full_path().value().c_str()); | 682 full_path().value().c_str()); |
648 } else { | 683 } else { |
649 description += base::StringPrintf(" url = \"%s\"", url_list.c_str()); | 684 description += base::StringPrintf(" url = \"%s\"", url_list.c_str()); |
650 } | 685 } |
686 | |
687 description += " }"; | |
688 | |
651 return description; | 689 return description; |
652 } | 690 } |
OLD | NEW |