OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/android/offline_pages/downloads/offline_page_infobar_de
legate.h" | 5 #include "chrome/browser/android/offline_pages/downloads/offline_page_infobar_de
legate.h" |
6 | 6 |
7 #include "base/memory/ptr_util.h" | 7 #include "base/memory/ptr_util.h" |
8 #include "base/strings/utf_string_conversions.h" | 8 #include "base/strings/utf_string_conversions.h" |
9 #include "chrome/browser/infobars/infobar_service.h" | 9 #include "chrome/browser/infobars/infobar_service.h" |
10 #include "chrome/browser/ui/android/infobars/download_overwrite_infobar.h" | 10 #include "chrome/browser/ui/android/infobars/duplicate_download_infobar.h" |
11 #include "components/url_formatter/url_formatter.h" | 11 #include "components/url_formatter/url_formatter.h" |
12 #include "ui/gfx/text_elider.h" | 12 #include "ui/gfx/text_elider.h" |
13 | 13 |
14 namespace offline_pages { | 14 namespace offline_pages { |
15 | 15 |
16 // static | 16 // static |
17 void OfflinePageInfoBarDelegate::Create( | 17 void OfflinePageInfoBarDelegate::Create( |
18 const base::Callback<void(Action)>& confirm_continuation, | 18 const base::Closure& confirm_continuation, |
19 const std::string& downloads_label, | |
20 const GURL& page_to_download, | 19 const GURL& page_to_download, |
21 content::WebContents* web_contents) { | 20 content::WebContents* web_contents) { |
22 // The URL could be very long, especially since we are including query | 21 // The URL could be very long, especially since we are including query |
23 // parameters, path, etc. Elide the URL to a shorter length because the | 22 // parameters, path, etc. Elide the URL to a shorter length because the |
24 // infobar cannot handle scrolling and completely obscures Chrome if the text | 23 // infobar cannot handle scrolling and completely obscures Chrome if the text |
25 // is too long. | 24 // is too long. |
26 // | 25 // |
27 // 150 was chosen as it does not cause the infobar to overrun the screen on a | 26 // 150 was chosen as it does not cause the infobar to overrun the screen on a |
28 // test Android One device with 480 x 854 resolution. At this resolution the | 27 // test Android One device with 480 x 854 resolution. At this resolution the |
29 // infobar covers approximately 2/3 of the screen, and all controls are still | 28 // infobar covers approximately 2/3 of the screen, and all controls are still |
30 // visible. | 29 // visible. |
31 // | 30 // |
32 // TODO(dewittj): Display something better than an elided URL string in the | 31 // TODO(dewittj): Display something better than an elided URL string in the |
33 // infobar. | 32 // infobar. |
34 const size_t kMaxLengthOfDisplayedPageUrl = 150; | 33 const size_t kMaxLengthOfDisplayedPageUrl = 150; |
35 | 34 |
36 base::string16 formatted_url = url_formatter::FormatUrl(page_to_download); | 35 base::string16 formatted_url = url_formatter::FormatUrl(page_to_download); |
37 base::string16 elided_url; | 36 base::string16 elided_url; |
38 gfx::ElideString(formatted_url, kMaxLengthOfDisplayedPageUrl, &elided_url); | 37 gfx::ElideString(formatted_url, kMaxLengthOfDisplayedPageUrl, &elided_url); |
39 | 38 |
40 InfoBarService::FromWebContents(web_contents) | 39 InfoBarService::FromWebContents(web_contents) |
41 ->AddInfoBar(DownloadOverwriteInfoBar::CreateInfoBar(base::WrapUnique( | 40 ->AddInfoBar(DuplicateDownloadInfoBar::CreateInfoBar(base::WrapUnique( |
42 new OfflinePageInfoBarDelegate(confirm_continuation, downloads_label, | 41 new OfflinePageInfoBarDelegate(confirm_continuation, |
43 base::UTF16ToUTF8(elided_url))))); | 42 base::UTF16ToUTF8(elided_url), |
| 43 page_to_download)))); |
44 } | 44 } |
45 | 45 |
46 OfflinePageInfoBarDelegate::~OfflinePageInfoBarDelegate() {} | 46 OfflinePageInfoBarDelegate::~OfflinePageInfoBarDelegate() {} |
47 | 47 |
48 OfflinePageInfoBarDelegate::OfflinePageInfoBarDelegate( | 48 OfflinePageInfoBarDelegate::OfflinePageInfoBarDelegate( |
49 const base::Callback<void(Action)>& confirm_continuation, | 49 const base::Closure& confirm_continuation, |
50 const std::string& downloads_label, | 50 const std::string& page_name, |
51 const std::string& page_name) | 51 const GURL& page_to_download) |
52 : confirm_continuation_(confirm_continuation), | 52 : confirm_continuation_(confirm_continuation), |
53 downloads_label_(downloads_label), | 53 page_name_(page_name), |
54 page_name_(page_name) {} | 54 page_to_download_(page_to_download) {} |
55 | 55 |
56 infobars::InfoBarDelegate::InfoBarIdentifier | 56 infobars::InfoBarDelegate::InfoBarIdentifier |
57 OfflinePageInfoBarDelegate::GetIdentifier() const { | 57 OfflinePageInfoBarDelegate::GetIdentifier() const { |
58 return OFFLINE_PAGE_INFOBAR_DELEGATE; | 58 return OFFLINE_PAGE_INFOBAR_DELEGATE; |
59 } | 59 } |
60 | 60 |
61 bool OfflinePageInfoBarDelegate::EqualsDelegate( | 61 bool OfflinePageInfoBarDelegate::EqualsDelegate( |
62 InfoBarDelegate* delegate) const { | 62 InfoBarDelegate* delegate) const { |
63 OfflinePageInfoBarDelegate* confirm_delegate = | 63 OfflinePageInfoBarDelegate* confirm_delegate = |
64 delegate->AsOfflinePageInfoBarDelegate(); | 64 delegate->AsOfflinePageInfoBarDelegate(); |
65 return confirm_delegate && GetFileName() == confirm_delegate->GetFileName(); | 65 return confirm_delegate && GetFilePath() == confirm_delegate->GetFilePath(); |
66 } | 66 } |
67 | 67 |
68 bool OfflinePageInfoBarDelegate::OverwriteExistingFile() { | 68 bool OfflinePageInfoBarDelegate::Cancel() { |
69 // TODO(dewittj): Downloads UI intends to remove this functionality. | |
70 confirm_continuation_.Run(Action::OVERWRITE); | |
71 return true; | 69 return true; |
72 } | 70 } |
73 | 71 |
74 bool OfflinePageInfoBarDelegate::CreateNewFile() { | 72 bool OfflinePageInfoBarDelegate::Download() { |
75 confirm_continuation_.Run(Action::CREATE_NEW); | 73 confirm_continuation_.Run(); |
76 return true; | 74 return true; |
77 } | 75 } |
78 | 76 |
79 std::string OfflinePageInfoBarDelegate::GetFileName() const { | 77 std::string OfflinePageInfoBarDelegate::GetFilePath() const { |
80 return page_name_; | 78 return page_name_; |
81 } | 79 } |
82 | 80 |
83 std::string OfflinePageInfoBarDelegate::GetDirName() const { | 81 bool OfflinePageInfoBarDelegate::IsOfflinePage() const { |
84 return downloads_label_; | 82 return true; |
85 } | 83 } |
86 | 84 |
87 std::string OfflinePageInfoBarDelegate::GetDirFullPath() const { | 85 std::string OfflinePageInfoBarDelegate::GetPageURL() const { |
88 return std::string(); | 86 return page_to_download_.spec(); |
89 } | 87 } |
90 | 88 |
91 bool OfflinePageInfoBarDelegate::ShouldExpire( | 89 bool OfflinePageInfoBarDelegate::ShouldExpire( |
92 const NavigationDetails& details) const { | 90 const NavigationDetails& details) const { |
93 return InfoBarDelegate::ShouldExpire(details); | 91 return InfoBarDelegate::ShouldExpire(details); |
94 } | 92 } |
95 | 93 |
96 OfflinePageInfoBarDelegate* | 94 OfflinePageInfoBarDelegate* |
97 OfflinePageInfoBarDelegate::AsOfflinePageInfoBarDelegate() { | 95 OfflinePageInfoBarDelegate::AsOfflinePageInfoBarDelegate() { |
98 return this; | 96 return this; |
99 } | 97 } |
100 | 98 |
101 } // namespace offline_pages | 99 } // namespace offline_pages |
OLD | NEW |