File: device_permissions_dialog_view.cc

package info (click to toggle)
chromium-browser 41.0.2272.118-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 2,189,132 kB
  • sloc: cpp: 9,691,462; ansic: 3,341,451; python: 712,689; asm: 518,779; xml: 208,926; java: 169,820; sh: 119,353; perl: 68,907; makefile: 28,311; yacc: 13,305; objc: 11,385; tcl: 3,186; cs: 2,225; sql: 2,217; lex: 2,215; lisp: 1,349; pascal: 1,256; awk: 407; ruby: 155; sed: 53; php: 14; exp: 11
file content (164 lines) | stat: -rw-r--r-- 5,571 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/views/extensions/device_permissions_dialog_view.h"

#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/extensions/api/chrome_device_permissions_prompt.h"
#include "chrome/grit/generated_resources.h"
#include "components/constrained_window/constrained_window_views.h"
#include "content/public/browser/browser_thread.h"
#include "device/usb/usb_device.h"
#include "extensions/common/extension.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/models/table_model.h"
#include "ui/base/models/table_model_observer.h"
#include "ui/views/controls/label.h"
#include "ui/views/controls/table/table_view.h"
#include "ui/views/layout/box_layout.h"
#include "ui/views/layout/layout_constants.h"

using device::UsbDevice;
using extensions::DevicePermissionsPrompt;

class DevicePermissionsTableModel
    : public ui::TableModel,
      public DevicePermissionsPrompt::Prompt::Observer {
 public:
  explicit DevicePermissionsTableModel(
      scoped_refptr<DevicePermissionsPrompt::Prompt> prompt)
      : prompt_(prompt) {
    prompt_->SetObserver(this);
  }

  ~DevicePermissionsTableModel() override { prompt_->SetObserver(nullptr); }

  // ui::TableModel
  int RowCount() override;
  base::string16 GetText(int row, int column) override;
  void SetObserver(ui::TableModelObserver* observer) override;

  // extensions::DevicePermissionsPrompt::Prompt::Observer
  void OnDevicesChanged() override;

 private:
  scoped_refptr<DevicePermissionsPrompt::Prompt> prompt_;
  ui::TableModelObserver* observer_;
};

int DevicePermissionsTableModel::RowCount() {
  return prompt_->GetDeviceCount();
}

base::string16 DevicePermissionsTableModel::GetText(int row, int col_id) {
  switch (col_id) {
    case IDS_DEVICE_PERMISSIONS_DIALOG_DEVICE_NAME_COLUMN:
      return prompt_->GetDeviceName(row);
    case IDS_DEVICE_PERMISSIONS_DIALOG_SERIAL_NUMBER_COLUMN:
      return prompt_->GetDeviceSerialNumber(row);
    default:
      NOTREACHED();
      return base::string16();
  }
}

void DevicePermissionsTableModel::SetObserver(
    ui::TableModelObserver* observer) {
  observer_ = observer;
}

void DevicePermissionsTableModel::OnDevicesChanged() {
  if (observer_) {
    observer_->OnModelChanged();
  }
}

DevicePermissionsDialogView::DevicePermissionsDialogView(
    DevicePermissionsPrompt::Delegate* delegate,
    scoped_refptr<DevicePermissionsPrompt::Prompt> prompt)
    : delegate_(delegate), prompt_(prompt) {
  views::BoxLayout* layout =
      new views::BoxLayout(views::BoxLayout::kVertical,
                           views::kButtonHEdgeMarginNew,
                           0,
                           views::kRelatedControlVerticalSpacing);
  SetLayoutManager(layout);

  views::Label* label = new views::Label(prompt_->GetPromptMessage());
  label->SetHorizontalAlignment(gfx::ALIGN_LEFT);
  label->SetMultiLine(true);
  AddChildView(label);

  std::vector<ui::TableColumn> table_columns;
  table_columns.push_back(
      ui::TableColumn(IDS_DEVICE_PERMISSIONS_DIALOG_DEVICE_NAME_COLUMN,
                      ui::TableColumn::LEFT,
                      -1,
                      0.8f));
  table_columns.back().title = l10n_util::GetStringUTF16(
      IDS_DEVICE_PERMISSIONS_DIALOG_DEVICE_NAME_COLUMN);
  table_columns.push_back(
      ui::TableColumn(IDS_DEVICE_PERMISSIONS_DIALOG_SERIAL_NUMBER_COLUMN,
                      ui::TableColumn::LEFT,
                      -1,
                      0.2f));
  table_columns.back().title = l10n_util::GetStringUTF16(
      IDS_DEVICE_PERMISSIONS_DIALOG_SERIAL_NUMBER_COLUMN);

  table_model_.reset(new DevicePermissionsTableModel(prompt_));
  table_view_ = new views::TableView(table_model_.get(),
                                     table_columns,
                                     views::TEXT_ONLY,
                                     !prompt_->multiple());

  views::View* table_parent = table_view_->CreateParentIfNecessary();
  AddChildView(table_parent);
  layout->SetFlexForView(table_parent, 1);
}

DevicePermissionsDialogView::~DevicePermissionsDialogView() {
  RemoveAllChildViews(true);
}

bool DevicePermissionsDialogView::Cancel() {
  std::vector<scoped_refptr<UsbDevice>> empty;
  delegate_->OnUsbDevicesChosen(empty);
  return true;
}

bool DevicePermissionsDialogView::Accept() {
  std::vector<scoped_refptr<UsbDevice>> devices;
  for (int index : table_view_->selection_model().selected_indices()) {
    prompt_->GrantDevicePermission(index);
    devices.push_back(prompt_->GetDevice(index));
  }
  delegate_->OnUsbDevicesChosen(devices);
  return true;
}

base::string16 DevicePermissionsDialogView::GetDialogButtonLabel(
    ui::DialogButton button) const {
  if (button == ui::DIALOG_BUTTON_OK) {
    return l10n_util::GetStringUTF16(IDS_DEVICE_PERMISSIONS_DIALOG_SELECT);
  }
  return views::DialogDelegateView::GetDialogButtonLabel(button);
}

ui::ModalType DevicePermissionsDialogView::GetModalType() const {
  return ui::MODAL_TYPE_CHILD;
}

base::string16 DevicePermissionsDialogView::GetWindowTitle() const {
  return prompt_->GetHeading();
}

gfx::Size DevicePermissionsDialogView::GetPreferredSize() const {
  return gfx::Size(500, 250);
}

void ChromeDevicePermissionsPrompt::ShowDialog() {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  constrained_window::ShowWebModalDialogViews(
      new DevicePermissionsDialogView(delegate(), prompt()), web_contents());
}