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