OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/extensions/api/file_system/request_file_system_dialog_v iew.h" | 5 #include "chrome/browser/ui/views/extensions/request_file_system_dialog_view.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include <cstdlib> | 9 #include <cstdlib> |
10 | 10 |
11 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
12 #include "base/threading/thread_task_runner_handle.h" | 12 #include "base/threading/thread_task_runner_handle.h" |
13 #include "chrome/browser/ui/views/harmony/chrome_layout_provider.h" | |
13 #include "chrome/grit/generated_resources.h" | 14 #include "chrome/grit/generated_resources.h" |
14 #include "components/constrained_window/constrained_window_views.h" | 15 #include "components/constrained_window/constrained_window_views.h" |
15 #include "content/public/browser/web_contents.h" | 16 #include "content/public/browser/web_contents.h" |
16 #include "ui/base/l10n/l10n_util.h" | 17 #include "ui/base/l10n/l10n_util.h" |
17 #include "ui/gfx/font.h" | 18 #include "ui/gfx/font.h" |
18 #include "ui/gfx/range/range.h" | 19 #include "ui/gfx/range/range.h" |
19 #include "ui/views/controls/styled_label.h" | 20 #include "ui/views/controls/styled_label.h" |
20 #include "ui/views/layout/box_layout.h" | 21 #include "ui/views/layout/fill_layout.h" |
21 #include "ui/views/layout/layout_constants.h" | |
22 | 22 |
23 namespace { | 23 namespace { |
24 | 24 |
25 // Maximum width of the dialog in pixels. | 25 // Maximum width of the dialog in pixels. |
26 const int kDialogMaxWidth = 320; | 26 const int kDialogMaxWidth = 320; |
27 | 27 |
28 } // namespace | 28 } // namespace |
29 | 29 |
30 // static | 30 // static |
31 void RequestFileSystemDialogView::ShowDialog( | 31 void RequestFileSystemDialogView::ShowDialog( |
32 content::WebContents* web_contents, | 32 content::WebContents* web_contents, |
33 const extensions::Extension& extension, | 33 const std::string& extension_id, |
34 base::WeakPtr<file_manager::Volume> volume, | 34 const std::string volume_label, |
35 const std::string& volume_id, | |
35 bool writable, | 36 bool writable, |
36 const base::Callback<void(ui::DialogButton)>& callback) { | 37 const base::Callback<void(ui::DialogButton)>& callback) { |
37 constrained_window::ShowWebModalDialogViews( | 38 constrained_window::ShowWebModalDialogViews( |
38 new RequestFileSystemDialogView(extension, volume, writable, callback), | 39 new RequestFileSystemDialogView(extension_id, volume_label, volume_id, |
40 writable, callback), | |
39 web_contents); | 41 web_contents); |
40 } | 42 } |
41 | 43 |
42 RequestFileSystemDialogView::~RequestFileSystemDialogView() { | 44 RequestFileSystemDialogView::~RequestFileSystemDialogView() {} |
43 } | |
44 | 45 |
45 base::string16 RequestFileSystemDialogView::GetAccessibleWindowTitle() const { | 46 base::string16 RequestFileSystemDialogView::GetAccessibleWindowTitle() const { |
46 return l10n_util::GetStringUTF16( | 47 return l10n_util::GetStringUTF16( |
47 IDS_FILE_SYSTEM_REQUEST_FILE_SYSTEM_DIALOG_TITLE); | 48 IDS_FILE_SYSTEM_REQUEST_FILE_SYSTEM_DIALOG_TITLE); |
48 } | 49 } |
49 | 50 |
50 int RequestFileSystemDialogView::GetDefaultDialogButton() const { | 51 int RequestFileSystemDialogView::GetDefaultDialogButton() const { |
51 return ui::DIALOG_BUTTON_CANCEL; | 52 return ui::DIALOG_BUTTON_CANCEL; |
52 } | 53 } |
53 | 54 |
54 base::string16 RequestFileSystemDialogView::GetDialogButtonLabel( | 55 base::string16 RequestFileSystemDialogView::GetDialogButtonLabel( |
55 ui::DialogButton button) const { | 56 ui::DialogButton button) const { |
56 switch (button) { | 57 switch (button) { |
57 case ui::DIALOG_BUTTON_OK: | 58 case ui::DIALOG_BUTTON_OK: |
58 return l10n_util::GetStringUTF16( | 59 return l10n_util::GetStringUTF16( |
59 IDS_FILE_SYSTEM_REQUEST_FILE_SYSTEM_DIALOG_ALLOW_BUTTON); | 60 IDS_FILE_SYSTEM_REQUEST_FILE_SYSTEM_DIALOG_ALLOW_BUTTON); |
60 case ui::DIALOG_BUTTON_CANCEL: | 61 case ui::DIALOG_BUTTON_CANCEL: |
61 return l10n_util::GetStringUTF16( | 62 return l10n_util::GetStringUTF16( |
62 IDS_FILE_SYSTEM_REQUEST_FILE_SYSTEM_DIALOG_DENY_BUTTON); | 63 IDS_FILE_SYSTEM_REQUEST_FILE_SYSTEM_DIALOG_DENY_BUTTON); |
63 default: | 64 default: |
64 NOTREACHED(); | 65 NOTREACHED(); |
65 } | 66 } |
66 return base::string16(); | 67 return base::string16(); |
67 } | 68 } |
68 | 69 |
69 ui::ModalType RequestFileSystemDialogView::GetModalType() const { | 70 ui::ModalType RequestFileSystemDialogView::GetModalType() const { |
70 return ui::MODAL_TYPE_CHILD; | 71 return ui::MODAL_TYPE_CHILD; |
71 } | 72 } |
72 | 73 |
73 views::View* RequestFileSystemDialogView::GetContentsView() { | |
74 return contents_view_; | |
75 } | |
76 | |
77 views::Widget* RequestFileSystemDialogView::GetWidget() { | |
78 return contents_view_->GetWidget(); | |
79 } | |
80 | |
81 const views::Widget* RequestFileSystemDialogView::GetWidget() const { | |
82 return contents_view_->GetWidget(); | |
83 } | |
84 | |
85 bool RequestFileSystemDialogView::Cancel() { | 74 bool RequestFileSystemDialogView::Cancel() { |
86 callback_.Run(ui::DIALOG_BUTTON_CANCEL); | 75 callback_.Run(ui::DIALOG_BUTTON_CANCEL); |
87 return true; | 76 return true; |
88 } | 77 } |
89 | 78 |
90 bool RequestFileSystemDialogView::Accept() { | 79 bool RequestFileSystemDialogView::Accept() { |
91 callback_.Run(ui::DIALOG_BUTTON_OK); | 80 callback_.Run(ui::DIALOG_BUTTON_OK); |
92 return true; | 81 return true; |
93 } | 82 } |
94 | 83 |
84 gfx::Size RequestFileSystemDialogView::GetPreferredSize() const { | |
85 return gfx::Size(kDialogMaxWidth, | |
86 child_at(0)->GetHeightForWidth(kDialogMaxWidth)); | |
87 } | |
88 | |
89 gfx::Insets RequestFileSystemDialogView::GetInsets() const { | |
90 return ChromeLayoutProvider::Get()->GetInsetsMetric(views::INSETS_PANEL); | |
91 } | |
92 | |
95 RequestFileSystemDialogView::RequestFileSystemDialogView( | 93 RequestFileSystemDialogView::RequestFileSystemDialogView( |
96 const extensions::Extension& extension, | 94 const std::string& extension_name, |
97 base::WeakPtr<file_manager::Volume> volume, | 95 const std::string& volume_label, |
96 const std::string& volume_id, | |
98 bool writable, | 97 bool writable, |
99 const base::Callback<void(ui::DialogButton)>& callback) | 98 const base::Callback<void(ui::DialogButton)>& callback) |
100 : callback_(callback), contents_view_(new views::View) { | 99 : callback_(callback), contents_view_(new views::View) { |
101 DCHECK(!callback_.is_null()); | 100 DCHECK(!callback_.is_null()); |
102 | 101 |
103 // If the volume is gone, then cancel the dialog. | 102 const base::string16 app_name = base::UTF8ToUTF16(extension_name); |
104 if (!volume.get()) { | |
105 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
106 FROM_HERE, base::Bind(callback, ui::DIALOG_BUTTON_CANCEL)); | |
107 return; | |
108 } | |
109 | |
110 const base::string16 app_name = base::UTF8ToUTF16(extension.name()); | |
111 // TODO(mtomasz): Improve the dialog contents, so it's easier for the user | 103 // TODO(mtomasz): Improve the dialog contents, so it's easier for the user |
112 // to understand what device is being requested. | 104 // to understand what device is being requested. |
113 const base::string16 volume_name = | 105 const base::string16 volume_name = |
114 base::UTF8ToUTF16(!volume->volume_label().empty() ? volume->volume_label() | 106 base::UTF8ToUTF16(!volume_label.empty() ? volume_label : volume_id); |
Peter Kasting
2017/04/27 01:08:05
Nit: It seems like it'd be better to just pass in
ananta
2017/04/27 01:29:53
Done.
| |
115 : volume->volume_id()); | 107 |
116 std::vector<size_t> placeholder_offsets; | 108 std::vector<size_t> placeholder_offsets; |
117 const base::string16 message = l10n_util::GetStringFUTF16( | 109 const base::string16 message = l10n_util::GetStringFUTF16( |
118 writable ? IDS_FILE_SYSTEM_REQUEST_FILE_SYSTEM_DIALOG_WRITABLE_MESSAGE | 110 writable ? IDS_FILE_SYSTEM_REQUEST_FILE_SYSTEM_DIALOG_WRITABLE_MESSAGE |
119 : IDS_FILE_SYSTEM_REQUEST_FILE_SYSTEM_DIALOG_MESSAGE, | 111 : IDS_FILE_SYSTEM_REQUEST_FILE_SYSTEM_DIALOG_MESSAGE, |
120 app_name, volume_name, &placeholder_offsets); | 112 app_name, volume_name, &placeholder_offsets); |
121 | 113 |
122 views::StyledLabel* const label = new views::StyledLabel(message, nullptr); | 114 views::StyledLabel* const label = new views::StyledLabel(message, nullptr); |
123 views::StyledLabel::RangeStyleInfo bold_style; | 115 views::StyledLabel::RangeStyleInfo bold_style; |
124 bold_style.weight = gfx::Font::Weight::BOLD; | 116 bold_style.weight = gfx::Font::Weight::BOLD; |
125 | 117 |
126 DCHECK_EQ(2u, placeholder_offsets.size()); | 118 DCHECK_EQ(2u, placeholder_offsets.size()); |
127 label->AddStyleRange(gfx::Range(placeholder_offsets[0], | 119 label->AddStyleRange(gfx::Range(placeholder_offsets[0], |
128 placeholder_offsets[0] + app_name.length()), | 120 placeholder_offsets[0] + app_name.length()), |
129 bold_style); | 121 bold_style); |
130 label->AddStyleRange( | 122 label->AddStyleRange( |
131 gfx::Range(placeholder_offsets[1], | 123 gfx::Range(placeholder_offsets[1], |
132 placeholder_offsets[1] + volume_name.length()), | 124 placeholder_offsets[1] + volume_name.length()), |
133 bold_style); | 125 bold_style); |
134 | 126 |
135 views::BoxLayout* const layout = new views::BoxLayout( | 127 SetLayoutManager(new views::FillLayout()); |
136 views::BoxLayout::kHorizontal, views::kButtonHEdgeMarginNew, | |
137 views::kPanelVertMargin, 0); | |
138 contents_view_->SetLayoutManager(layout); | |
139 | 128 |
140 label->SizeToFit(kDialogMaxWidth - 2 * views::kButtonHEdgeMarginNew); | 129 AddChildView(label); |
141 contents_view_->AddChildView(label); | |
142 } | 130 } |
OLD | NEW |