1 // Copyright 2019 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "components/permissions/bluetooth_scanning_prompt_controller.h"
7 #include "base/ranges/algorithm.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "components/strings/grit/components_strings.h"
10 #include "ui/base/l10n/l10n_util.h"
12 namespace permissions {
14 BluetoothScanningPromptController::BluetoothScanningPromptController(
15 content::RenderFrameHost* owner,
16 const content::BluetoothScanningPrompt::EventHandler& event_handler,
18 : ChooserController(title), event_handler_(event_handler) {}
20 BluetoothScanningPromptController::~BluetoothScanningPromptController() {}
22 bool BluetoothScanningPromptController::ShouldShowHelpButton() const {
26 std::u16string BluetoothScanningPromptController::GetNoOptionsText() const {
27 return l10n_util::GetStringUTF16(
28 IDS_BLUETOOTH_SCANNING_PROMPT_NO_DEVICES_FOUND_PROMPT);
31 std::u16string BluetoothScanningPromptController::GetOkButtonLabel() const {
32 return l10n_util::GetStringUTF16(
33 IDS_BLUETOOTH_SCANNING_PROMPT_ALLOW_BUTTON_TEXT);
36 std::u16string BluetoothScanningPromptController::GetCancelButtonLabel() const {
37 return l10n_util::GetStringUTF16(
38 IDS_BLUETOOTH_SCANNING_PROMPT_BLOCK_BUTTON_TEXT);
41 std::pair<std::u16string, std::u16string>
42 BluetoothScanningPromptController::GetThrobberLabelAndTooltip() const {
44 l10n_util::GetStringUTF16(IDS_BLUETOOTH_DEVICE_CHOOSER_SCANNING_LABEL),
45 l10n_util::GetStringUTF16(
46 IDS_BLUETOOTH_DEVICE_CHOOSER_SCANNING_LABEL_TOOLTIP)};
49 bool BluetoothScanningPromptController::BothButtonsAlwaysEnabled() const {
53 bool BluetoothScanningPromptController::TableViewAlwaysDisabled() const {
57 size_t BluetoothScanningPromptController::NumOptions() const {
58 return device_ids_.size();
61 std::u16string BluetoothScanningPromptController::GetOption(
63 DCHECK_LT(index, device_ids_.size());
64 const std::string& device_id = device_ids_[index];
65 const auto& device_name_it = device_id_to_name_map_.find(device_id);
66 DCHECK(device_name_it != device_id_to_name_map_.end());
67 const auto& it = device_name_counts_.find(device_name_it->second);
68 DCHECK(it != device_name_counts_.end());
69 return it->second == 1
70 ? device_name_it->second
71 : l10n_util::GetStringFUTF16(
72 IDS_DEVICE_CHOOSER_DEVICE_NAME_WITH_ID,
73 device_name_it->second, base::UTF8ToUTF16(device_id));
76 void BluetoothScanningPromptController::Select(
77 const std::vector<size_t>& indices) {
78 DCHECK(indices.empty());
80 if (event_handler_.is_null())
83 event_handler_.Run(content::BluetoothScanningPrompt::Event::kAllow);
86 void BluetoothScanningPromptController::Cancel() {
87 if (event_handler_.is_null())
90 event_handler_.Run(content::BluetoothScanningPrompt::Event::kBlock);
93 void BluetoothScanningPromptController::Close() {
94 if (event_handler_.is_null())
97 event_handler_.Run(content::BluetoothScanningPrompt::Event::kCanceled);
100 void BluetoothScanningPromptController::OpenHelpCenterUrl() const {}
102 void BluetoothScanningPromptController::AddOrUpdateDevice(
103 const std::string& device_id,
104 bool should_update_name,
105 const std::u16string& device_name) {
106 std::u16string device_name_for_display = device_name;
107 if (device_name_for_display.empty()) {
108 device_name_for_display = l10n_util::GetStringFUTF16(
109 IDS_BLUETOOTH_SCANNING_DEVICE_UNKNOWN, base::UTF8ToUTF16(device_id));
112 auto name_it = device_id_to_name_map_.find(device_id);
113 if (name_it != device_id_to_name_map_.end()) {
114 if (should_update_name) {
115 std::u16string previous_device_name = name_it->second;
116 name_it->second = device_name_for_display;
118 const auto& it = device_name_counts_.find(previous_device_name);
119 DCHECK(it != device_name_counts_.end());
120 DCHECK_GT(it->second, 0);
122 if (--(it->second) == 0)
123 device_name_counts_.erase(it);
125 ++device_name_counts_[device_name_for_display];
128 auto device_id_it = base::ranges::find(device_ids_, device_id);
130 DCHECK(device_id_it != device_ids_.end());
132 view()->OnOptionUpdated(device_id_it - device_ids_.begin());
136 device_ids_.push_back(device_id);
137 device_id_to_name_map_.insert({device_id, device_name_for_display});
138 ++device_name_counts_[device_name_for_display];
140 view()->OnOptionAdded(device_ids_.size() - 1);
143 void BluetoothScanningPromptController::ResetEventHandler() {
144 event_handler_.Reset();
147 base::WeakPtr<BluetoothScanningPromptController>
148 BluetoothScanningPromptController::GetWeakPtr() {
149 return weak_factory_.GetWeakPtr();
152 } // namespace permissions