OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 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_model.h" | 5 #include "chrome/browser/download/download_item_model.h" |
6 | 6 |
7 #include "base/i18n/number_formatting.h" | 7 #include "base/i18n/number_formatting.h" |
8 #include "base/i18n/rtl.h" | 8 #include "base/i18n/rtl.h" |
9 #include "base/metrics/field_trial.h" | 9 #include "base/metrics/field_trial.h" |
10 #include "base/strings/string16.h" | 10 #include "base/strings/string16.h" |
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
170 default: | 170 default: |
171 string_id = IDS_DOWNLOAD_INTERRUPTED_STATUS; | 171 string_id = IDS_DOWNLOAD_INTERRUPTED_STATUS; |
172 break; | 172 break; |
173 } | 173 } |
174 | 174 |
175 return l10n_util::GetStringUTF16(string_id); | 175 return l10n_util::GetStringUTF16(string_id); |
176 } | 176 } |
177 | 177 |
178 string16 InterruptReasonMessage(int reason) { | 178 string16 InterruptReasonMessage(int reason) { |
179 int string_id = 0; | 179 int string_id = 0; |
180 string16 status_text; | 180 base::string16 status_text; |
181 | 181 |
182 switch (reason) { | 182 switch (reason) { |
183 case content::DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED: | 183 case content::DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED: |
184 string_id = IDS_DOWNLOAD_INTERRUPTED_DESCRIPTION_ACCESS_DENIED; | 184 string_id = IDS_DOWNLOAD_INTERRUPTED_DESCRIPTION_ACCESS_DENIED; |
185 break; | 185 break; |
186 case content::DOWNLOAD_INTERRUPT_REASON_FILE_NO_SPACE: | 186 case content::DOWNLOAD_INTERRUPT_REASON_FILE_NO_SPACE: |
187 string_id = IDS_DOWNLOAD_INTERRUPTED_DESCRIPTION_DISK_FULL; | 187 string_id = IDS_DOWNLOAD_INTERRUPTED_DESCRIPTION_DISK_FULL; |
188 break; | 188 break; |
189 case content::DOWNLOAD_INTERRUPT_REASON_FILE_NAME_TOO_LONG: | 189 case content::DOWNLOAD_INTERRUPT_REASON_FILE_NAME_TOO_LONG: |
190 string_id = IDS_DOWNLOAD_INTERRUPTED_DESCRIPTION_PATH_TOO_LONG; | 190 string_id = IDS_DOWNLOAD_INTERRUPTED_DESCRIPTION_PATH_TOO_LONG; |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
251 | 251 |
252 DownloadItemModel::DownloadItemModel(DownloadItem* download) | 252 DownloadItemModel::DownloadItemModel(DownloadItem* download) |
253 : download_(download) {} | 253 : download_(download) {} |
254 | 254 |
255 DownloadItemModel::~DownloadItemModel() {} | 255 DownloadItemModel::~DownloadItemModel() {} |
256 | 256 |
257 string16 DownloadItemModel::GetInterruptReasonText() const { | 257 string16 DownloadItemModel::GetInterruptReasonText() const { |
258 if (download_->GetState() != DownloadItem::INTERRUPTED || | 258 if (download_->GetState() != DownloadItem::INTERRUPTED || |
259 download_->GetLastReason() == | 259 download_->GetLastReason() == |
260 content::DOWNLOAD_INTERRUPT_REASON_USER_CANCELED) { | 260 content::DOWNLOAD_INTERRUPT_REASON_USER_CANCELED) { |
261 return string16(); | 261 return base::string16(); |
262 } | 262 } |
263 return InterruptReasonMessage(download_->GetLastReason()); | 263 return InterruptReasonMessage(download_->GetLastReason()); |
264 } | 264 } |
265 | 265 |
266 string16 DownloadItemModel::GetStatusText() const { | 266 string16 DownloadItemModel::GetStatusText() const { |
267 string16 status_text; | 267 base::string16 status_text; |
268 switch (download_->GetState()) { | 268 switch (download_->GetState()) { |
269 case DownloadItem::IN_PROGRESS: | 269 case DownloadItem::IN_PROGRESS: |
270 status_text = GetInProgressStatusString(); | 270 status_text = GetInProgressStatusString(); |
271 break; | 271 break; |
272 case DownloadItem::COMPLETE: | 272 case DownloadItem::COMPLETE: |
273 if (download_->GetFileExternallyRemoved()) { | 273 if (download_->GetFileExternallyRemoved()) { |
274 status_text = l10n_util::GetStringUTF16(IDS_DOWNLOAD_STATUS_REMOVED); | 274 status_text = l10n_util::GetStringUTF16(IDS_DOWNLOAD_STATUS_REMOVED); |
275 } else { | 275 } else { |
276 status_text.clear(); | 276 status_text.clear(); |
277 } | 277 } |
278 break; | 278 break; |
279 case DownloadItem::CANCELLED: | 279 case DownloadItem::CANCELLED: |
280 status_text = l10n_util::GetStringUTF16(IDS_DOWNLOAD_STATUS_CANCELLED); | 280 status_text = l10n_util::GetStringUTF16(IDS_DOWNLOAD_STATUS_CANCELLED); |
281 break; | 281 break; |
282 case DownloadItem::INTERRUPTED: { | 282 case DownloadItem::INTERRUPTED: { |
283 content::DownloadInterruptReason reason = download_->GetLastReason(); | 283 content::DownloadInterruptReason reason = download_->GetLastReason(); |
284 if (reason != content::DOWNLOAD_INTERRUPT_REASON_USER_CANCELED) { | 284 if (reason != content::DOWNLOAD_INTERRUPT_REASON_USER_CANCELED) { |
285 string16 interrupt_reason = InterruptReasonStatusMessage(reason); | 285 base::string16 interrupt_reason = InterruptReasonStatusMessage(reason); |
286 status_text = l10n_util::GetStringFUTF16( | 286 status_text = l10n_util::GetStringFUTF16( |
287 IDS_DOWNLOAD_STATUS_INTERRUPTED, interrupt_reason); | 287 IDS_DOWNLOAD_STATUS_INTERRUPTED, interrupt_reason); |
288 } else { | 288 } else { |
289 // Same as DownloadItem::CANCELLED. | 289 // Same as DownloadItem::CANCELLED. |
290 status_text = l10n_util::GetStringUTF16(IDS_DOWNLOAD_STATUS_CANCELLED); | 290 status_text = l10n_util::GetStringUTF16(IDS_DOWNLOAD_STATUS_CANCELLED); |
291 } | 291 } |
292 break; | 292 break; |
293 } | 293 } |
294 default: | 294 default: |
295 NOTREACHED(); | 295 NOTREACHED(); |
296 } | 296 } |
297 | 297 |
298 return status_text; | 298 return status_text; |
299 } | 299 } |
300 | 300 |
301 string16 DownloadItemModel::GetTabProgressStatusText() const { | 301 string16 DownloadItemModel::GetTabProgressStatusText() const { |
302 int64 total = GetTotalBytes(); | 302 int64 total = GetTotalBytes(); |
303 int64 size = download_->GetReceivedBytes(); | 303 int64 size = download_->GetReceivedBytes(); |
304 string16 received_size = ui::FormatBytes(size); | 304 base::string16 received_size = ui::FormatBytes(size); |
305 string16 amount = received_size; | 305 base::string16 amount = received_size; |
306 | 306 |
307 // Adjust both strings for the locale direction since we don't yet know which | 307 // Adjust both strings for the locale direction since we don't yet know which |
308 // string we'll end up using for constructing the final progress string. | 308 // string we'll end up using for constructing the final progress string. |
309 base::i18n::AdjustStringForLocaleDirection(&amount); | 309 base::i18n::AdjustStringForLocaleDirection(&amount); |
310 | 310 |
311 if (total) { | 311 if (total) { |
312 string16 total_text = ui::FormatBytes(total); | 312 base::string16 total_text = ui::FormatBytes(total); |
313 base::i18n::AdjustStringForLocaleDirection(&total_text); | 313 base::i18n::AdjustStringForLocaleDirection(&total_text); |
314 | 314 |
315 base::i18n::AdjustStringForLocaleDirection(&received_size); | 315 base::i18n::AdjustStringForLocaleDirection(&received_size); |
316 amount = l10n_util::GetStringFUTF16( | 316 amount = l10n_util::GetStringFUTF16( |
317 IDS_DOWNLOAD_TAB_PROGRESS_SIZE, received_size, total_text); | 317 IDS_DOWNLOAD_TAB_PROGRESS_SIZE, received_size, total_text); |
318 } else { | 318 } else { |
319 amount.assign(received_size); | 319 amount.assign(received_size); |
320 } | 320 } |
321 int64 current_speed = download_->CurrentSpeed(); | 321 int64 current_speed = download_->CurrentSpeed(); |
322 string16 speed_text = ui::FormatSpeed(current_speed); | 322 base::string16 speed_text = ui::FormatSpeed(current_speed); |
323 base::i18n::AdjustStringForLocaleDirection(&speed_text); | 323 base::i18n::AdjustStringForLocaleDirection(&speed_text); |
324 | 324 |
325 base::TimeDelta remaining; | 325 base::TimeDelta remaining; |
326 string16 time_remaining; | 326 base::string16 time_remaining; |
327 if (download_->IsPaused()) | 327 if (download_->IsPaused()) |
328 time_remaining = l10n_util::GetStringUTF16(IDS_DOWNLOAD_PROGRESS_PAUSED); | 328 time_remaining = l10n_util::GetStringUTF16(IDS_DOWNLOAD_PROGRESS_PAUSED); |
329 else if (download_->TimeRemaining(&remaining)) | 329 else if (download_->TimeRemaining(&remaining)) |
330 time_remaining = ui::TimeFormat::TimeRemaining(remaining); | 330 time_remaining = ui::TimeFormat::TimeRemaining(remaining); |
331 | 331 |
332 if (time_remaining.empty()) { | 332 if (time_remaining.empty()) { |
333 base::i18n::AdjustStringForLocaleDirection(&amount); | 333 base::i18n::AdjustStringForLocaleDirection(&amount); |
334 return l10n_util::GetStringFUTF16( | 334 return l10n_util::GetStringFUTF16( |
335 IDS_DOWNLOAD_TAB_PROGRESS_STATUS_TIME_UNKNOWN, speed_text, amount); | 335 IDS_DOWNLOAD_TAB_PROGRESS_STATUS_TIME_UNKNOWN, speed_text, amount); |
336 } | 336 } |
337 return l10n_util::GetStringFUTF16( | 337 return l10n_util::GetStringFUTF16( |
338 IDS_DOWNLOAD_TAB_PROGRESS_STATUS, speed_text, amount, time_remaining); | 338 IDS_DOWNLOAD_TAB_PROGRESS_STATUS, speed_text, amount, time_remaining); |
339 } | 339 } |
340 | 340 |
341 string16 DownloadItemModel::GetTooltipText(const gfx::FontList& font_list, | 341 string16 DownloadItemModel::GetTooltipText(const gfx::FontList& font_list, |
342 int max_width) const { | 342 int max_width) const { |
343 string16 tooltip = gfx::ElideFilename( | 343 base::string16 tooltip = gfx::ElideFilename( |
344 download_->GetFileNameToReportUser(), font_list, max_width); | 344 download_->GetFileNameToReportUser(), font_list, max_width); |
345 content::DownloadInterruptReason reason = download_->GetLastReason(); | 345 content::DownloadInterruptReason reason = download_->GetLastReason(); |
346 if (download_->GetState() == DownloadItem::INTERRUPTED && | 346 if (download_->GetState() == DownloadItem::INTERRUPTED && |
347 reason != content::DOWNLOAD_INTERRUPT_REASON_USER_CANCELED) { | 347 reason != content::DOWNLOAD_INTERRUPT_REASON_USER_CANCELED) { |
348 tooltip += ASCIIToUTF16("\n"); | 348 tooltip += ASCIIToUTF16("\n"); |
349 tooltip += gfx::ElideText(InterruptReasonStatusMessage(reason), | 349 tooltip += gfx::ElideText(InterruptReasonStatusMessage(reason), |
350 font_list, max_width, gfx::ELIDE_AT_END); | 350 font_list, max_width, gfx::ELIDE_AT_END); |
351 } | 351 } |
352 return tooltip; | 352 return tooltip; |
353 } | 353 } |
354 | 354 |
355 string16 DownloadItemModel::GetWarningText(const gfx::FontList& font_list, | 355 string16 DownloadItemModel::GetWarningText(const gfx::FontList& font_list, |
356 int base_width) const { | 356 int base_width) const { |
357 // Should only be called if IsDangerous(). | 357 // Should only be called if IsDangerous(). |
358 DCHECK(IsDangerous()); | 358 DCHECK(IsDangerous()); |
359 string16 elided_filename = | 359 base::string16 elided_filename = |
360 gfx::ElideFilename(download_->GetFileNameToReportUser(), font_list, | 360 gfx::ElideFilename(download_->GetFileNameToReportUser(), font_list, |
361 base_width); | 361 base_width); |
362 switch (download_->GetDangerType()) { | 362 switch (download_->GetDangerType()) { |
363 case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL: { | 363 case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_URL: { |
364 return l10n_util::GetStringUTF16(IDS_PROMPT_MALICIOUS_DOWNLOAD_URL); | 364 return l10n_util::GetStringUTF16(IDS_PROMPT_MALICIOUS_DOWNLOAD_URL); |
365 } | 365 } |
366 case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE: { | 366 case content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE: { |
367 if (download_crx_util::IsExtensionDownload(*download_)) { | 367 if (download_crx_util::IsExtensionDownload(*download_)) { |
368 return l10n_util::GetStringUTF16( | 368 return l10n_util::GetStringUTF16( |
369 IDS_PROMPT_DANGEROUS_DOWNLOAD_EXTENSION); | 369 IDS_PROMPT_DANGEROUS_DOWNLOAD_EXTENSION); |
(...skipping 16 matching lines...) Expand all Loading... |
386 IDS_PROMPT_DOWNLOAD_CHANGES_SETTINGS, elided_filename); | 386 IDS_PROMPT_DOWNLOAD_CHANGES_SETTINGS, elided_filename); |
387 } | 387 } |
388 case content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS: | 388 case content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS: |
389 case content::DOWNLOAD_DANGER_TYPE_MAYBE_DANGEROUS_CONTENT: | 389 case content::DOWNLOAD_DANGER_TYPE_MAYBE_DANGEROUS_CONTENT: |
390 case content::DOWNLOAD_DANGER_TYPE_USER_VALIDATED: | 390 case content::DOWNLOAD_DANGER_TYPE_USER_VALIDATED: |
391 case content::DOWNLOAD_DANGER_TYPE_MAX: { | 391 case content::DOWNLOAD_DANGER_TYPE_MAX: { |
392 break; | 392 break; |
393 } | 393 } |
394 } | 394 } |
395 NOTREACHED(); | 395 NOTREACHED(); |
396 return string16(); | 396 return base::string16(); |
397 } | 397 } |
398 | 398 |
399 string16 DownloadItemModel::GetWarningConfirmButtonText() const { | 399 string16 DownloadItemModel::GetWarningConfirmButtonText() const { |
400 // Should only be called if IsDangerous() | 400 // Should only be called if IsDangerous() |
401 DCHECK(IsDangerous()); | 401 DCHECK(IsDangerous()); |
402 if (download_->GetDangerType() == | 402 if (download_->GetDangerType() == |
403 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE && | 403 content::DOWNLOAD_DANGER_TYPE_DANGEROUS_FILE && |
404 download_crx_util::IsExtensionDownload(*download_)) { | 404 download_crx_util::IsExtensionDownload(*download_)) { |
405 return l10n_util::GetStringUTF16(IDS_CONTINUE_EXTENSION_DOWNLOAD); | 405 return l10n_util::GetStringUTF16(IDS_CONTINUE_EXTENSION_DOWNLOAD); |
406 } else { | 406 } else { |
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
556 const DownloadItemModelData* data = DownloadItemModelData::Get(download_); | 556 const DownloadItemModelData* data = DownloadItemModelData::Get(download_); |
557 return data && data->should_prefer_opening_in_browser(); | 557 return data && data->should_prefer_opening_in_browser(); |
558 } | 558 } |
559 | 559 |
560 void DownloadItemModel::SetShouldPreferOpeningInBrowser(bool preference) { | 560 void DownloadItemModel::SetShouldPreferOpeningInBrowser(bool preference) { |
561 DownloadItemModelData* data = DownloadItemModelData::GetOrCreate(download_); | 561 DownloadItemModelData* data = DownloadItemModelData::GetOrCreate(download_); |
562 data->set_should_prefer_opening_in_browser(preference); | 562 data->set_should_prefer_opening_in_browser(preference); |
563 } | 563 } |
564 | 564 |
565 string16 DownloadItemModel::GetProgressSizesString() const { | 565 string16 DownloadItemModel::GetProgressSizesString() const { |
566 string16 size_ratio; | 566 base::string16 size_ratio; |
567 int64 size = GetCompletedBytes(); | 567 int64 size = GetCompletedBytes(); |
568 int64 total = GetTotalBytes(); | 568 int64 total = GetTotalBytes(); |
569 if (total > 0) { | 569 if (total > 0) { |
570 ui::DataUnits amount_units = ui::GetByteDisplayUnits(total); | 570 ui::DataUnits amount_units = ui::GetByteDisplayUnits(total); |
571 string16 simple_size = ui::FormatBytesWithUnits(size, amount_units, false); | 571 base::string16 simple_size = ui::FormatBytesWithUnits(size, amount_units, fa
lse); |
572 | 572 |
573 // In RTL locales, we render the text "size/total" in an RTL context. This | 573 // In RTL locales, we render the text "size/total" in an RTL context. This |
574 // is problematic since a string such as "123/456 MB" is displayed | 574 // is problematic since a string such as "123/456 MB" is displayed |
575 // as "MB 123/456" because it ends with an LTR run. In order to solve this, | 575 // as "MB 123/456" because it ends with an LTR run. In order to solve this, |
576 // we mark the total string as an LTR string if the UI layout is | 576 // we mark the total string as an LTR string if the UI layout is |
577 // right-to-left so that the string "456 MB" is treated as an LTR run. | 577 // right-to-left so that the string "456 MB" is treated as an LTR run. |
578 string16 simple_total = base::i18n::GetDisplayStringInLTRDirectionality( | 578 base::string16 simple_total = base::i18n::GetDisplayStringInLTRDirectionalit
y( |
579 ui::FormatBytesWithUnits(total, amount_units, true)); | 579 ui::FormatBytesWithUnits(total, amount_units, true)); |
580 size_ratio = l10n_util::GetStringFUTF16(IDS_DOWNLOAD_STATUS_SIZES, | 580 size_ratio = l10n_util::GetStringFUTF16(IDS_DOWNLOAD_STATUS_SIZES, |
581 simple_size, simple_total); | 581 simple_size, simple_total); |
582 } else { | 582 } else { |
583 size_ratio = ui::FormatBytes(size); | 583 size_ratio = ui::FormatBytes(size); |
584 } | 584 } |
585 return size_ratio; | 585 return size_ratio; |
586 } | 586 } |
587 | 587 |
588 string16 DownloadItemModel::GetInProgressStatusString() const { | 588 string16 DownloadItemModel::GetInProgressStatusString() const { |
589 DCHECK_EQ(DownloadItem::IN_PROGRESS, download_->GetState()); | 589 DCHECK_EQ(DownloadItem::IN_PROGRESS, download_->GetState()); |
590 | 590 |
591 TimeDelta time_remaining; | 591 TimeDelta time_remaining; |
592 // time_remaining is only known if the download isn't paused. | 592 // time_remaining is only known if the download isn't paused. |
593 bool time_remaining_known = (!download_->IsPaused() && | 593 bool time_remaining_known = (!download_->IsPaused() && |
594 download_->TimeRemaining(&time_remaining)); | 594 download_->TimeRemaining(&time_remaining)); |
595 | 595 |
596 // Indication of progress. (E.g.:"100/200 MB" or "100MB") | 596 // Indication of progress. (E.g.:"100/200 MB" or "100MB") |
597 string16 size_ratio = GetProgressSizesString(); | 597 base::string16 size_ratio = GetProgressSizesString(); |
598 | 598 |
599 // The download is a CRX (app, extension, theme, ...) and it is being unpacked | 599 // The download is a CRX (app, extension, theme, ...) and it is being unpacked |
600 // and validated. | 600 // and validated. |
601 if (download_->AllDataSaved() && | 601 if (download_->AllDataSaved() && |
602 download_crx_util::IsExtensionDownload(*download_)) { | 602 download_crx_util::IsExtensionDownload(*download_)) { |
603 return l10n_util::GetStringUTF16(IDS_DOWNLOAD_STATUS_CRX_INSTALL_RUNNING); | 603 return l10n_util::GetStringUTF16(IDS_DOWNLOAD_STATUS_CRX_INSTALL_RUNNING); |
604 } | 604 } |
605 | 605 |
606 // A paused download: "100/120 MB, Paused" | 606 // A paused download: "100/120 MB, Paused" |
607 if (download_->IsPaused()) { | 607 if (download_->IsPaused()) { |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
643 if (!download_service) | 643 if (!download_service) |
644 return; | 644 return; |
645 | 645 |
646 ChromeDownloadManagerDelegate* delegate = | 646 ChromeDownloadManagerDelegate* delegate = |
647 download_service->GetDownloadManagerDelegate(); | 647 download_service->GetDownloadManagerDelegate(); |
648 if (!delegate) | 648 if (!delegate) |
649 return; | 649 return; |
650 delegate->OpenDownloadUsingPlatformHandler(download_); | 650 delegate->OpenDownloadUsingPlatformHandler(download_); |
651 RecordDownloadOpenMethod(DOWNLOAD_OPEN_METHOD_USER_PLATFORM); | 651 RecordDownloadOpenMethod(DOWNLOAD_OPEN_METHOD_USER_PLATFORM); |
652 } | 652 } |
OLD | NEW |