Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(7)

Side by Side Diff: chrome/browser/download/download_item_model.cc

Issue 55063002: Prefer opening PDF downloads in the browser. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Destroy PluginService once we are done with our plugin tests. Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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"
11 #include "base/strings/sys_string_conversions.h" 11 #include "base/strings/sys_string_conversions.h"
12 #include "base/strings/utf_string_conversions.h" 12 #include "base/strings/utf_string_conversions.h"
13 #include "base/supports_user_data.h" 13 #include "base/supports_user_data.h"
14 #include "base/time/time.h" 14 #include "base/time/time.h"
15 #include "chrome/browser/download/chrome_download_manager_delegate.h"
15 #include "chrome/browser/download/download_crx_util.h" 16 #include "chrome/browser/download/download_crx_util.h"
17 #include "chrome/browser/download/download_service.h"
18 #include "chrome/browser/download/download_service_factory.h"
19 #include "chrome/browser/download/download_stats.h"
16 #include "chrome/browser/safe_browsing/download_feedback_service.h" 20 #include "chrome/browser/safe_browsing/download_feedback_service.h"
17 #include "content/public/browser/download_danger_type.h" 21 #include "content/public/browser/download_danger_type.h"
18 #include "content/public/browser/download_interrupt_reasons.h" 22 #include "content/public/browser/download_interrupt_reasons.h"
19 #include "content/public/browser/download_item.h" 23 #include "content/public/browser/download_item.h"
20 #include "grit/chromium_strings.h" 24 #include "grit/chromium_strings.h"
21 #include "grit/generated_resources.h" 25 #include "grit/generated_resources.h"
22 #include "ui/base/l10n/l10n_util.h" 26 #include "ui/base/l10n/l10n_util.h"
23 #include "ui/base/l10n/time_format.h" 27 #include "ui/base/l10n/time_format.h"
24 #include "ui/base/text/bytes_formatting.h" 28 #include "ui/base/text/bytes_formatting.h"
25 #include "ui/gfx/text_elider.h" 29 #include "ui/gfx/text_elider.h"
(...skipping 19 matching lines...) Expand all
45 bool should_show_in_shelf() const { return should_show_in_shelf_; } 49 bool should_show_in_shelf() const { return should_show_in_shelf_; }
46 void set_should_show_in_shelf(bool should_show_in_shelf) { 50 void set_should_show_in_shelf(bool should_show_in_shelf) {
47 should_show_in_shelf_ = should_show_in_shelf; 51 should_show_in_shelf_ = should_show_in_shelf;
48 } 52 }
49 53
50 bool should_notify_ui() const { return should_notify_ui_; } 54 bool should_notify_ui() const { return should_notify_ui_; }
51 void set_should_notify_ui(bool should_notify_ui) { 55 void set_should_notify_ui(bool should_notify_ui) {
52 should_notify_ui_ = should_notify_ui; 56 should_notify_ui_ = should_notify_ui;
53 } 57 }
54 58
59 bool should_prefer_opening_in_browser() const {
60 return should_prefer_opening_in_browser_;
61 }
62 void set_should_prefer_opening_in_browser(bool preference) {
63 should_prefer_opening_in_browser_ = preference;
64 }
65
55 private: 66 private:
56 DownloadItemModelData(); 67 DownloadItemModelData();
57 virtual ~DownloadItemModelData() {} 68 virtual ~DownloadItemModelData() {}
58 69
59 static const char kKey[]; 70 static const char kKey[];
60 71
61 // Whether the download should be displayed in the download shelf. True by 72 // Whether the download should be displayed in the download shelf. True by
62 // default. 73 // default.
63 bool should_show_in_shelf_; 74 bool should_show_in_shelf_;
64 75
65 // Whether the UI should be notified when the download is ready to be 76 // Whether the UI should be notified when the download is ready to be
66 // presented. 77 // presented.
67 bool should_notify_ui_; 78 bool should_notify_ui_;
79
80 // Whether the download should be opened in the browser vs. the system handler
81 // for the file type.
82 bool should_prefer_opening_in_browser_;
68 }; 83 };
69 84
70 // static 85 // static
71 const char DownloadItemModelData::kKey[] = "DownloadItemModelData key"; 86 const char DownloadItemModelData::kKey[] = "DownloadItemModelData key";
72 87
73 // static 88 // static
74 const DownloadItemModelData* DownloadItemModelData::Get( 89 const DownloadItemModelData* DownloadItemModelData::Get(
75 const DownloadItem* download) { 90 const DownloadItem* download) {
76 return static_cast<const DownloadItemModelData*>(download->GetUserData(kKey)); 91 return static_cast<const DownloadItemModelData*>(download->GetUserData(kKey));
77 } 92 }
78 93
79 // static 94 // static
80 DownloadItemModelData* DownloadItemModelData::GetOrCreate( 95 DownloadItemModelData* DownloadItemModelData::GetOrCreate(
81 DownloadItem* download) { 96 DownloadItem* download) {
82 DownloadItemModelData* data = 97 DownloadItemModelData* data =
83 static_cast<DownloadItemModelData*>(download->GetUserData(kKey)); 98 static_cast<DownloadItemModelData*>(download->GetUserData(kKey));
84 if (data == NULL) { 99 if (data == NULL) {
85 data = new DownloadItemModelData(); 100 data = new DownloadItemModelData();
86 download->SetUserData(kKey, data); 101 download->SetUserData(kKey, data);
87 } 102 }
88 return data; 103 return data;
89 } 104 }
90 105
91 DownloadItemModelData::DownloadItemModelData() 106 DownloadItemModelData::DownloadItemModelData()
92 : should_show_in_shelf_(true), 107 : should_show_in_shelf_(true),
93 should_notify_ui_(false) { 108 should_notify_ui_(false),
109 should_prefer_opening_in_browser_(false) {
94 } 110 }
95 111
96 string16 InterruptReasonStatusMessage(int reason) { 112 string16 InterruptReasonStatusMessage(int reason) {
97 int string_id = 0; 113 int string_id = 0;
98 114
99 switch (reason) { 115 switch (reason) {
100 case content::DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED: 116 case content::DOWNLOAD_INTERRUPT_REASON_FILE_ACCESS_DENIED:
101 string_id = IDS_DOWNLOAD_INTERRUPTED_STATUS_ACCESS_DENIED; 117 string_id = IDS_DOWNLOAD_INTERRUPTED_STATUS_ACCESS_DENIED;
102 break; 118 break;
103 case content::DOWNLOAD_INTERRUPT_REASON_FILE_NO_SPACE: 119 case content::DOWNLOAD_INTERRUPT_REASON_FILE_NO_SPACE:
(...skipping 421 matching lines...) Expand 10 before | Expand all | Expand 10 after
525 bool DownloadItemModel::ShouldNotifyUI() const { 541 bool DownloadItemModel::ShouldNotifyUI() const {
526 const DownloadItemModelData* data = DownloadItemModelData::Get(download_); 542 const DownloadItemModelData* data = DownloadItemModelData::Get(download_);
527 return data && data->should_notify_ui(); 543 return data && data->should_notify_ui();
528 } 544 }
529 545
530 void DownloadItemModel::SetShouldNotifyUI(bool should_notify) { 546 void DownloadItemModel::SetShouldNotifyUI(bool should_notify) {
531 DownloadItemModelData* data = DownloadItemModelData::GetOrCreate(download_); 547 DownloadItemModelData* data = DownloadItemModelData::GetOrCreate(download_);
532 data->set_should_notify_ui(should_notify); 548 data->set_should_notify_ui(should_notify);
533 } 549 }
534 550
551 bool DownloadItemModel::ShouldPreferOpeningInBrowser() const {
552 const DownloadItemModelData* data = DownloadItemModelData::Get(download_);
553 return data && data->should_prefer_opening_in_browser();
554 }
555
556 void DownloadItemModel::SetShouldPreferOpeningInBrowser(bool preference) {
557 DownloadItemModelData* data = DownloadItemModelData::GetOrCreate(download_);
558 data->set_should_prefer_opening_in_browser(preference);
559 }
560
535 string16 DownloadItemModel::GetProgressSizesString() const { 561 string16 DownloadItemModel::GetProgressSizesString() const {
536 string16 size_ratio; 562 string16 size_ratio;
537 int64 size = GetCompletedBytes(); 563 int64 size = GetCompletedBytes();
538 int64 total = GetTotalBytes(); 564 int64 total = GetTotalBytes();
539 if (total > 0) { 565 if (total > 0) {
540 ui::DataUnits amount_units = ui::GetByteDisplayUnits(total); 566 ui::DataUnits amount_units = ui::GetByteDisplayUnits(total);
541 string16 simple_size = ui::FormatBytesWithUnits(size, amount_units, false); 567 string16 simple_size = ui::FormatBytesWithUnits(size, amount_units, false);
542 568
543 // In RTL locales, we render the text "size/total" in an RTL context. This 569 // In RTL locales, we render the text "size/total" in an RTL context. This
544 // is problematic since a string such as "123/456 MB" is displayed 570 // is problematic since a string such as "123/456 MB" is displayed
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
598 } 624 }
599 625
600 // In progress download with no known time left and non-zero completed bytes: 626 // In progress download with no known time left and non-zero completed bytes:
601 // "100/120 MB" or "100 MB" 627 // "100/120 MB" or "100 MB"
602 if (GetCompletedBytes() > 0) 628 if (GetCompletedBytes() > 0)
603 return size_ratio; 629 return size_ratio;
604 630
605 // Instead of displaying "0 B" we say "Starting..." 631 // Instead of displaying "0 B" we say "Starting..."
606 return l10n_util::GetStringUTF16(IDS_DOWNLOAD_STATUS_STARTING); 632 return l10n_util::GetStringUTF16(IDS_DOWNLOAD_STATUS_STARTING);
607 } 633 }
634
635 void DownloadItemModel::OpenUsingPlatformHandler() {
636 DownloadService* download_service =
637 DownloadServiceFactory::GetForBrowserContext(
638 download_->GetBrowserContext());
639 if (!download_service)
640 return;
641
642 ChromeDownloadManagerDelegate* delegate =
643 download_service->GetDownloadManagerDelegate();
644 if (!delegate)
645 return;
646 delegate->OpenDownloadUsingPlatformHandler(download_);
647 RecordDownloadOpenMethod(DOWNLOAD_OPEN_METHOD_USER_PLATFORM);
648 }
OLDNEW
« no previous file with comments | « chrome/browser/download/download_item_model.h ('k') | chrome/browser/download/download_shelf_context_menu.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698