OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/extensions/api/file_system/request_file_system_dialog_v
iew.h" |
| 6 |
| 7 #include <stddef.h> |
| 8 |
| 9 #include <cstdlib> |
| 10 |
| 11 #include "base/strings/utf_string_conversions.h" |
| 12 #include "base/threading/thread_task_runner_handle.h" |
| 13 #include "chrome/grit/generated_resources.h" |
| 14 #include "components/constrained_window/constrained_window_views.h" |
| 15 #include "content/public/browser/web_contents.h" |
| 16 #include "ui/base/l10n/l10n_util.h" |
| 17 #include "ui/gfx/font.h" |
| 18 #include "ui/gfx/range/range.h" |
| 19 #include "ui/views/controls/styled_label.h" |
| 20 #include "ui/views/layout/box_layout.h" |
| 21 #include "ui/views/layout/layout_constants.h" |
| 22 |
| 23 namespace { |
| 24 |
| 25 // Maximum width of the dialog in pixels. |
| 26 const int kDialogMaxWidth = 320; |
| 27 |
| 28 } // namespace |
| 29 |
| 30 // static |
| 31 void RequestFileSystemDialogView::ShowDialog( |
| 32 content::WebContents* web_contents, |
| 33 const extensions::Extension& extension, |
| 34 base::WeakPtr<file_manager::Volume> volume, |
| 35 bool writable, |
| 36 const base::Callback<void(ui::DialogButton)>& callback) { |
| 37 constrained_window::ShowWebModalDialogViews( |
| 38 new RequestFileSystemDialogView(extension, volume, writable, callback), |
| 39 web_contents); |
| 40 } |
| 41 |
| 42 RequestFileSystemDialogView::~RequestFileSystemDialogView() { |
| 43 } |
| 44 |
| 45 base::string16 RequestFileSystemDialogView::GetAccessibleWindowTitle() const { |
| 46 return l10n_util::GetStringUTF16( |
| 47 IDS_FILE_SYSTEM_REQUEST_FILE_SYSTEM_DIALOG_TITLE); |
| 48 } |
| 49 |
| 50 int RequestFileSystemDialogView::GetDefaultDialogButton() const { |
| 51 return ui::DIALOG_BUTTON_CANCEL; |
| 52 } |
| 53 |
| 54 base::string16 RequestFileSystemDialogView::GetDialogButtonLabel( |
| 55 ui::DialogButton button) const { |
| 56 switch (button) { |
| 57 case ui::DIALOG_BUTTON_OK: |
| 58 return l10n_util::GetStringUTF16( |
| 59 IDS_FILE_SYSTEM_REQUEST_FILE_SYSTEM_DIALOG_ALLOW_BUTTON); |
| 60 case ui::DIALOG_BUTTON_CANCEL: |
| 61 return l10n_util::GetStringUTF16( |
| 62 IDS_FILE_SYSTEM_REQUEST_FILE_SYSTEM_DIALOG_DENY_BUTTON); |
| 63 default: |
| 64 NOTREACHED(); |
| 65 } |
| 66 return base::string16(); |
| 67 } |
| 68 |
| 69 ui::ModalType RequestFileSystemDialogView::GetModalType() const { |
| 70 return ui::MODAL_TYPE_CHILD; |
| 71 } |
| 72 |
| 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() { |
| 86 callback_.Run(ui::DIALOG_BUTTON_CANCEL); |
| 87 return true; |
| 88 } |
| 89 |
| 90 bool RequestFileSystemDialogView::Accept() { |
| 91 callback_.Run(ui::DIALOG_BUTTON_OK); |
| 92 return true; |
| 93 } |
| 94 |
| 95 RequestFileSystemDialogView::RequestFileSystemDialogView( |
| 96 const extensions::Extension& extension, |
| 97 base::WeakPtr<file_manager::Volume> volume, |
| 98 bool writable, |
| 99 const base::Callback<void(ui::DialogButton)>& callback) |
| 100 : callback_(callback), contents_view_(new views::View) { |
| 101 DCHECK(!callback_.is_null()); |
| 102 |
| 103 // If the volume is gone, then cancel the dialog. |
| 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 |
| 112 // to understand what device is being requested. |
| 113 const base::string16 volume_name = |
| 114 base::UTF8ToUTF16(!volume->volume_label().empty() ? volume->volume_label() |
| 115 : volume->volume_id()); |
| 116 std::vector<size_t> placeholder_offsets; |
| 117 const base::string16 message = l10n_util::GetStringFUTF16( |
| 118 writable ? IDS_FILE_SYSTEM_REQUEST_FILE_SYSTEM_DIALOG_WRITABLE_MESSAGE |
| 119 : IDS_FILE_SYSTEM_REQUEST_FILE_SYSTEM_DIALOG_MESSAGE, |
| 120 app_name, volume_name, &placeholder_offsets); |
| 121 |
| 122 views::StyledLabel* const label = new views::StyledLabel(message, nullptr); |
| 123 views::StyledLabel::RangeStyleInfo bold_style; |
| 124 bold_style.weight = gfx::Font::Weight::BOLD; |
| 125 |
| 126 DCHECK_EQ(2u, placeholder_offsets.size()); |
| 127 label->AddStyleRange(gfx::Range(placeholder_offsets[0], |
| 128 placeholder_offsets[0] + app_name.length()), |
| 129 bold_style); |
| 130 label->AddStyleRange( |
| 131 gfx::Range(placeholder_offsets[1], |
| 132 placeholder_offsets[1] + volume_name.length()), |
| 133 bold_style); |
| 134 |
| 135 views::BoxLayout* const layout = new views::BoxLayout( |
| 136 views::BoxLayout::kHorizontal, views::kButtonHEdgeMarginNew, |
| 137 views::kPanelVertMargin, 0); |
| 138 contents_view_->SetLayoutManager(layout); |
| 139 |
| 140 label->SizeToFit(kDialogMaxWidth - 2 * views::kButtonHEdgeMarginNew); |
| 141 contents_view_->AddChildView(label); |
| 142 } |
OLD | NEW |