Revert of Add button to add new FSP services to Files app. (patchset #8 id:140001...
[chromium-blink-merge.git] / chrome / browser / ui / views / extensions / device_permissions_dialog_view.cc
bloba4acefe0e323ba2cd245a165363cbca88b88caad
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.
5 #include "chrome/browser/ui/views/extensions/device_permissions_dialog_view.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "chrome/browser/extensions/api/chrome_device_permissions_prompt.h"
9 #include "chrome/grit/generated_resources.h"
10 #include "components/constrained_window/constrained_window_views.h"
11 #include "content/public/browser/browser_thread.h"
12 #include "device/usb/usb_device.h"
13 #include "extensions/common/extension.h"
14 #include "ui/base/l10n/l10n_util.h"
15 #include "ui/base/models/table_model.h"
16 #include "ui/base/models/table_model_observer.h"
17 #include "ui/views/controls/label.h"
18 #include "ui/views/controls/table/table_view.h"
19 #include "ui/views/layout/box_layout.h"
20 #include "ui/views/layout/layout_constants.h"
22 using device::UsbDevice;
23 using extensions::DevicePermissionsPrompt;
25 class DevicePermissionsTableModel
26 : public ui::TableModel,
27 public DevicePermissionsPrompt::Prompt::Observer {
28 public:
29 explicit DevicePermissionsTableModel(
30 scoped_refptr<DevicePermissionsPrompt::Prompt> prompt)
31 : prompt_(prompt) {
32 prompt_->SetObserver(this);
35 ~DevicePermissionsTableModel() override { prompt_->SetObserver(nullptr); }
37 // ui::TableModel
38 int RowCount() override;
39 base::string16 GetText(int row, int column) override;
40 void SetObserver(ui::TableModelObserver* observer) override;
42 // extensions::DevicePermissionsPrompt::Prompt::Observer
43 void OnDevicesChanged() override;
45 private:
46 scoped_refptr<DevicePermissionsPrompt::Prompt> prompt_;
47 ui::TableModelObserver* observer_;
50 int DevicePermissionsTableModel::RowCount() {
51 return prompt_->GetDeviceCount();
54 base::string16 DevicePermissionsTableModel::GetText(int row, int col_id) {
55 switch (col_id) {
56 case IDS_DEVICE_PERMISSIONS_DIALOG_DEVICE_NAME_COLUMN:
57 return prompt_->GetDeviceName(row);
58 case IDS_DEVICE_PERMISSIONS_DIALOG_SERIAL_NUMBER_COLUMN:
59 return prompt_->GetDeviceSerialNumber(row);
60 default:
61 NOTREACHED();
62 return base::string16();
66 void DevicePermissionsTableModel::SetObserver(
67 ui::TableModelObserver* observer) {
68 observer_ = observer;
71 void DevicePermissionsTableModel::OnDevicesChanged() {
72 if (observer_) {
73 observer_->OnModelChanged();
77 DevicePermissionsDialogView::DevicePermissionsDialogView(
78 DevicePermissionsPrompt::Delegate* delegate,
79 scoped_refptr<DevicePermissionsPrompt::Prompt> prompt)
80 : delegate_(delegate), prompt_(prompt) {
81 views::BoxLayout* layout =
82 new views::BoxLayout(views::BoxLayout::kVertical,
83 views::kButtonHEdgeMarginNew,
85 views::kRelatedControlVerticalSpacing);
86 SetLayoutManager(layout);
88 views::Label* label = new views::Label(prompt_->GetPromptMessage());
89 label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
90 label->SetMultiLine(true);
91 AddChildView(label);
93 std::vector<ui::TableColumn> table_columns;
94 table_columns.push_back(
95 ui::TableColumn(IDS_DEVICE_PERMISSIONS_DIALOG_DEVICE_NAME_COLUMN,
96 ui::TableColumn::LEFT,
97 -1,
98 0.8f));
99 table_columns.back().title = l10n_util::GetStringUTF16(
100 IDS_DEVICE_PERMISSIONS_DIALOG_DEVICE_NAME_COLUMN);
101 table_columns.push_back(
102 ui::TableColumn(IDS_DEVICE_PERMISSIONS_DIALOG_SERIAL_NUMBER_COLUMN,
103 ui::TableColumn::LEFT,
105 0.2f));
106 table_columns.back().title = l10n_util::GetStringUTF16(
107 IDS_DEVICE_PERMISSIONS_DIALOG_SERIAL_NUMBER_COLUMN);
109 table_model_.reset(new DevicePermissionsTableModel(prompt_));
110 table_view_ = new views::TableView(table_model_.get(),
111 table_columns,
112 views::TEXT_ONLY,
113 !prompt_->multiple());
115 views::View* table_parent = table_view_->CreateParentIfNecessary();
116 AddChildView(table_parent);
117 layout->SetFlexForView(table_parent, 1);
120 DevicePermissionsDialogView::~DevicePermissionsDialogView() {
121 RemoveAllChildViews(true);
124 bool DevicePermissionsDialogView::Cancel() {
125 std::vector<scoped_refptr<UsbDevice>> empty;
126 delegate_->OnUsbDevicesChosen(empty);
127 return true;
130 bool DevicePermissionsDialogView::Accept() {
131 std::vector<scoped_refptr<UsbDevice>> devices;
132 for (int index : table_view_->selection_model().selected_indices()) {
133 prompt_->GrantDevicePermission(index);
134 devices.push_back(prompt_->GetDevice(index));
136 delegate_->OnUsbDevicesChosen(devices);
137 return true;
140 base::string16 DevicePermissionsDialogView::GetDialogButtonLabel(
141 ui::DialogButton button) const {
142 if (button == ui::DIALOG_BUTTON_OK) {
143 return l10n_util::GetStringUTF16(IDS_DEVICE_PERMISSIONS_DIALOG_SELECT);
145 return views::DialogDelegateView::GetDialogButtonLabel(button);
148 ui::ModalType DevicePermissionsDialogView::GetModalType() const {
149 return ui::MODAL_TYPE_CHILD;
152 base::string16 DevicePermissionsDialogView::GetWindowTitle() const {
153 return prompt_->GetHeading();
156 gfx::Size DevicePermissionsDialogView::GetPreferredSize() const {
157 return gfx::Size(500, 250);
160 void ChromeDevicePermissionsPrompt::ShowDialog() {
161 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
162 constrained_window::ShowWebModalDialogViews(
163 new DevicePermissionsDialogView(delegate(), prompt()), web_contents());