OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 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/ui/views/extensions/device_permissions_dialog_view.h" |
| 6 |
| 7 #include "base/strings/utf_string_conversions.h" |
| 8 #include "chrome/browser/ui/views/constrained_window_views.h" |
| 9 #include "chrome/grit/generated_resources.h" |
| 10 #include "content/public/browser/browser_thread.h" |
| 11 #include "device/usb/usb_device.h" |
| 12 #include "extensions/common/extension.h" |
| 13 #include "ui/base/l10n/l10n_util.h" |
| 14 #include "ui/base/models/table_model.h" |
| 15 #include "ui/base/models/table_model_observer.h" |
| 16 #include "ui/views/controls/label.h" |
| 17 #include "ui/views/controls/table/table_view.h" |
| 18 #include "ui/views/layout/box_layout.h" |
| 19 #include "ui/views/layout/layout_constants.h" |
| 20 |
| 21 using device::UsbDevice; |
| 22 using extensions::DevicePermissionsPrompt; |
| 23 |
| 24 namespace { |
| 25 |
| 26 void ShowDevicePermissionsDialogImpl( |
| 27 content::WebContents* web_contents, |
| 28 DevicePermissionsPrompt::Delegate* delegate, |
| 29 scoped_refptr<DevicePermissionsPrompt::Prompt> prompt) { |
| 30 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 31 ShowWebModalDialogViews(new DevicePermissionsDialogView(delegate, prompt), |
| 32 web_contents); |
| 33 } |
| 34 |
| 35 } // namespace |
| 36 |
| 37 class DevicePermissionsTableModel |
| 38 : public ui::TableModel, |
| 39 public DevicePermissionsPrompt::Prompt::Observer { |
| 40 public: |
| 41 explicit DevicePermissionsTableModel( |
| 42 scoped_refptr<DevicePermissionsPrompt::Prompt> prompt) |
| 43 : prompt_(prompt) { |
| 44 prompt_->SetObserver(this); |
| 45 } |
| 46 |
| 47 virtual ~DevicePermissionsTableModel() { prompt_->SetObserver(nullptr); } |
| 48 |
| 49 // ui::TableModel |
| 50 virtual int RowCount() override; |
| 51 virtual base::string16 GetText(int row, int column) override; |
| 52 virtual base::string16 GetTooltip(int row) override; |
| 53 virtual void SetObserver(ui::TableModelObserver* observer) override; |
| 54 |
| 55 // extensions::DevicePermissionsPrompt::Prompt::Observer |
| 56 virtual void OnDevicesChanged() override; |
| 57 |
| 58 private: |
| 59 scoped_refptr<DevicePermissionsPrompt::Prompt> prompt_; |
| 60 ui::TableModelObserver* observer_; |
| 61 }; |
| 62 |
| 63 int DevicePermissionsTableModel::RowCount() { |
| 64 return prompt_->GetDeviceCount(); |
| 65 } |
| 66 |
| 67 base::string16 DevicePermissionsTableModel::GetText(int row, int col_id) { |
| 68 switch (col_id) { |
| 69 case IDS_DEVICE_PERMISSIONS_DIALOG_DEVICE_NAME_COLUMN: |
| 70 return prompt_->GetDeviceName(row); |
| 71 case IDS_DEVICE_PERMISSIONS_DIALOG_SERIAL_NUMBER_COLUMN: |
| 72 return prompt_->GetDeviceSerialNumber(row); |
| 73 default: |
| 74 NOTREACHED(); |
| 75 return base::string16(); |
| 76 } |
| 77 } |
| 78 |
| 79 base::string16 DevicePermissionsTableModel::GetTooltip(int row) { |
| 80 return prompt_->GetDeviceTooltip(row); |
| 81 } |
| 82 |
| 83 void DevicePermissionsTableModel::SetObserver( |
| 84 ui::TableModelObserver* observer) { |
| 85 observer_ = observer; |
| 86 } |
| 87 |
| 88 void DevicePermissionsTableModel::OnDevicesChanged() { |
| 89 if (observer_) { |
| 90 observer_->OnModelChanged(); |
| 91 } |
| 92 } |
| 93 |
| 94 DevicePermissionsDialogView::DevicePermissionsDialogView( |
| 95 DevicePermissionsPrompt::Delegate* delegate, |
| 96 scoped_refptr<DevicePermissionsPrompt::Prompt> prompt) |
| 97 : delegate_(delegate), prompt_(prompt) { |
| 98 views::BoxLayout* layout = |
| 99 new views::BoxLayout(views::BoxLayout::kVertical, |
| 100 views::kButtonHEdgeMarginNew, |
| 101 0, |
| 102 views::kRelatedControlVerticalSpacing); |
| 103 SetLayoutManager(layout); |
| 104 |
| 105 views::Label* label = new views::Label(prompt_->GetPromptMessage()); |
| 106 label->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
| 107 AddChildView(label); |
| 108 |
| 109 std::vector<ui::TableColumn> table_columns; |
| 110 table_columns.push_back( |
| 111 ui::TableColumn(IDS_DEVICE_PERMISSIONS_DIALOG_DEVICE_NAME_COLUMN, |
| 112 ui::TableColumn::LEFT, |
| 113 -1, |
| 114 0.8f)); |
| 115 table_columns.back().title = l10n_util::GetStringUTF16( |
| 116 IDS_DEVICE_PERMISSIONS_DIALOG_DEVICE_NAME_COLUMN); |
| 117 table_columns.push_back( |
| 118 ui::TableColumn(IDS_DEVICE_PERMISSIONS_DIALOG_SERIAL_NUMBER_COLUMN, |
| 119 ui::TableColumn::LEFT, |
| 120 -1, |
| 121 0.2f)); |
| 122 table_columns.back().title = l10n_util::GetStringUTF16( |
| 123 IDS_DEVICE_PERMISSIONS_DIALOG_SERIAL_NUMBER_COLUMN); |
| 124 |
| 125 table_model_.reset(new DevicePermissionsTableModel(prompt_)); |
| 126 table_view_ = new views::TableView(table_model_.get(), |
| 127 table_columns, |
| 128 views::TEXT_ONLY, |
| 129 !prompt_->multiple()); |
| 130 |
| 131 views::View* table_parent = table_view_->CreateParentIfNecessary(); |
| 132 AddChildView(table_parent); |
| 133 layout->SetFlexForView(table_parent, 1); |
| 134 } |
| 135 |
| 136 DevicePermissionsDialogView::~DevicePermissionsDialogView() { |
| 137 RemoveAllChildViews(true); |
| 138 } |
| 139 |
| 140 bool DevicePermissionsDialogView::Cancel() { |
| 141 std::vector<scoped_refptr<UsbDevice>> empty; |
| 142 delegate_->OnUsbDevicesChosen(empty); |
| 143 return true; |
| 144 } |
| 145 |
| 146 bool DevicePermissionsDialogView::Accept() { |
| 147 std::vector<scoped_refptr<UsbDevice>> devices; |
| 148 for (int index : table_view_->selection_model().selected_indices()) { |
| 149 prompt_->GrantDevicePermission(index); |
| 150 devices.push_back(prompt_->GetDevice(index)); |
| 151 } |
| 152 delegate_->OnUsbDevicesChosen(devices); |
| 153 return true; |
| 154 } |
| 155 |
| 156 base::string16 DevicePermissionsDialogView::GetDialogButtonLabel( |
| 157 ui::DialogButton button) const { |
| 158 if (button == ui::DIALOG_BUTTON_OK) { |
| 159 return l10n_util::GetStringUTF16(IDS_DEVICE_PERMISSIONS_DIALOG_SELECT); |
| 160 } |
| 161 return views::DialogDelegateView::GetDialogButtonLabel(button); |
| 162 } |
| 163 |
| 164 ui::ModalType DevicePermissionsDialogView::GetModalType() const { |
| 165 return ui::MODAL_TYPE_CHILD; |
| 166 } |
| 167 |
| 168 base::string16 DevicePermissionsDialogView::GetWindowTitle() const { |
| 169 return prompt_->GetHeading(); |
| 170 } |
| 171 |
| 172 gfx::Size DevicePermissionsDialogView::GetPreferredSize() const { |
| 173 return gfx::Size(500, 250); |
| 174 } |
| 175 |
| 176 // static |
| 177 DevicePermissionsPrompt::ShowDialogCallback |
| 178 DevicePermissionsPrompt::GetDefaultShowDialogCallback() { |
| 179 return base::Bind(&ShowDevicePermissionsDialogImpl); |
| 180 } |
OLD | NEW |