Jun Cai | 149002e | 2019-05-09 23:13:07 | [diff] [blame] | 1 | // Copyright 2019 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 | |
Michael van Ouwerkerk | 19f0cea | 2021-06-30 11:22:16 | [diff] [blame] | 5 | #include "components/permissions/bluetooth_scanning_prompt_controller.h" |
Jun Cai | 149002e | 2019-05-09 23:13:07 | [diff] [blame] | 6 | |
| 7 | #include <algorithm> |
| 8 | |
| 9 | #include "base/strings/utf_string_conversions.h" |
Michael van Ouwerkerk | 12553e2 | 2021-05-17 14:20:58 | [diff] [blame] | 10 | #include "components/strings/grit/components_strings.h" |
Jun Cai | 149002e | 2019-05-09 23:13:07 | [diff] [blame] | 11 | #include "ui/base/l10n/l10n_util.h" |
| 12 | |
Michael van Ouwerkerk | 19f0cea | 2021-06-30 11:22:16 | [diff] [blame] | 13 | namespace permissions { |
| 14 | |
Jun Cai | 149002e | 2019-05-09 23:13:07 | [diff] [blame] | 15 | BluetoothScanningPromptController::BluetoothScanningPromptController( |
| 16 | content::RenderFrameHost* owner, |
Michael van Ouwerkerk | 19f0cea | 2021-06-30 11:22:16 | [diff] [blame] | 17 | const content::BluetoothScanningPrompt::EventHandler& event_handler, |
| 18 | std::u16string title) |
| 19 | : ChooserController(title), event_handler_(event_handler) {} |
Jun Cai | 149002e | 2019-05-09 23:13:07 | [diff] [blame] | 20 | |
| 21 | BluetoothScanningPromptController::~BluetoothScanningPromptController() {} |
| 22 | |
| 23 | bool BluetoothScanningPromptController::ShouldShowHelpButton() const { |
| 24 | return false; |
| 25 | } |
| 26 | |
Jan Wilken Dörrie | 3f97e29 | 2021-03-11 18:07:14 | [diff] [blame] | 27 | std::u16string BluetoothScanningPromptController::GetNoOptionsText() const { |
Jun Cai | 149002e | 2019-05-09 23:13:07 | [diff] [blame] | 28 | return l10n_util::GetStringUTF16( |
| 29 | IDS_BLUETOOTH_SCANNING_PROMPT_NO_DEVICES_FOUND_PROMPT); |
| 30 | } |
| 31 | |
Jan Wilken Dörrie | 3f97e29 | 2021-03-11 18:07:14 | [diff] [blame] | 32 | std::u16string BluetoothScanningPromptController::GetOkButtonLabel() const { |
Jun Cai | 149002e | 2019-05-09 23:13:07 | [diff] [blame] | 33 | return l10n_util::GetStringUTF16( |
| 34 | IDS_BLUETOOTH_SCANNING_PROMPT_ALLOW_BUTTON_TEXT); |
| 35 | } |
| 36 | |
Jan Wilken Dörrie | 3f97e29 | 2021-03-11 18:07:14 | [diff] [blame] | 37 | std::u16string BluetoothScanningPromptController::GetCancelButtonLabel() const { |
Jun Cai | 149002e | 2019-05-09 23:13:07 | [diff] [blame] | 38 | return l10n_util::GetStringUTF16( |
| 39 | IDS_BLUETOOTH_SCANNING_PROMPT_BLOCK_BUTTON_TEXT); |
| 40 | } |
| 41 | |
Jan Wilken Dörrie | 3f97e29 | 2021-03-11 18:07:14 | [diff] [blame] | 42 | std::pair<std::u16string, std::u16string> |
Reilly Grant | 06bb2df5 | 2021-01-13 22:26:37 | [diff] [blame] | 43 | BluetoothScanningPromptController::GetThrobberLabelAndTooltip() const { |
| 44 | return { |
| 45 | l10n_util::GetStringUTF16(IDS_BLUETOOTH_DEVICE_CHOOSER_SCANNING_LABEL), |
| 46 | l10n_util::GetStringUTF16( |
| 47 | IDS_BLUETOOTH_DEVICE_CHOOSER_SCANNING_LABEL_TOOLTIP)}; |
| 48 | } |
| 49 | |
Jun Cai | 149002e | 2019-05-09 23:13:07 | [diff] [blame] | 50 | bool BluetoothScanningPromptController::BothButtonsAlwaysEnabled() const { |
| 51 | return true; |
| 52 | } |
| 53 | |
| 54 | bool BluetoothScanningPromptController::TableViewAlwaysDisabled() const { |
| 55 | return true; |
| 56 | } |
| 57 | |
| 58 | size_t BluetoothScanningPromptController::NumOptions() const { |
| 59 | return device_ids_.size(); |
| 60 | } |
| 61 | |
Jan Wilken Dörrie | 3f97e29 | 2021-03-11 18:07:14 | [diff] [blame] | 62 | std::u16string BluetoothScanningPromptController::GetOption( |
Jun Cai | 149002e | 2019-05-09 23:13:07 | [diff] [blame] | 63 | size_t index) const { |
| 64 | DCHECK_LT(index, device_ids_.size()); |
| 65 | const std::string& device_id = device_ids_[index]; |
| 66 | const auto& device_name_it = device_id_to_name_map_.find(device_id); |
| 67 | DCHECK(device_name_it != device_id_to_name_map_.end()); |
| 68 | const auto& it = device_name_counts_.find(device_name_it->second); |
| 69 | DCHECK(it != device_name_counts_.end()); |
| 70 | return it->second == 1 |
| 71 | ? device_name_it->second |
| 72 | : l10n_util::GetStringFUTF16( |
| 73 | IDS_DEVICE_CHOOSER_DEVICE_NAME_WITH_ID, |
| 74 | device_name_it->second, base::UTF8ToUTF16(device_id)); |
| 75 | } |
| 76 | |
| 77 | void BluetoothScanningPromptController::Select( |
| 78 | const std::vector<size_t>& indices) { |
| 79 | DCHECK(indices.empty()); |
| 80 | |
| 81 | if (event_handler_.is_null()) |
| 82 | return; |
| 83 | |
| 84 | event_handler_.Run(content::BluetoothScanningPrompt::Event::kAllow); |
| 85 | } |
| 86 | |
| 87 | void BluetoothScanningPromptController::Cancel() { |
| 88 | if (event_handler_.is_null()) |
| 89 | return; |
| 90 | |
| 91 | event_handler_.Run(content::BluetoothScanningPrompt::Event::kBlock); |
| 92 | } |
| 93 | |
| 94 | void BluetoothScanningPromptController::Close() { |
| 95 | if (event_handler_.is_null()) |
| 96 | return; |
| 97 | |
| 98 | event_handler_.Run(content::BluetoothScanningPrompt::Event::kCanceled); |
| 99 | } |
| 100 | |
| 101 | void BluetoothScanningPromptController::OpenHelpCenterUrl() const {} |
| 102 | |
| 103 | void BluetoothScanningPromptController::AddOrUpdateDevice( |
| 104 | const std::string& device_id, |
| 105 | bool should_update_name, |
Jan Wilken Dörrie | 3f97e29 | 2021-03-11 18:07:14 | [diff] [blame] | 106 | const std::u16string& device_name) { |
| 107 | std::u16string device_name_for_display = device_name; |
Jun Cai | 149002e | 2019-05-09 23:13:07 | [diff] [blame] | 108 | if (device_name_for_display.empty()) { |
| 109 | device_name_for_display = l10n_util::GetStringFUTF16( |
| 110 | IDS_BLUETOOTH_SCANNING_DEVICE_UNKNOWN, base::UTF8ToUTF16(device_id)); |
| 111 | } |
| 112 | |
| 113 | auto name_it = device_id_to_name_map_.find(device_id); |
| 114 | if (name_it != device_id_to_name_map_.end()) { |
| 115 | if (should_update_name) { |
Jan Wilken Dörrie | 3f97e29 | 2021-03-11 18:07:14 | [diff] [blame] | 116 | std::u16string previous_device_name = name_it->second; |
Jun Cai | 149002e | 2019-05-09 23:13:07 | [diff] [blame] | 117 | name_it->second = device_name_for_display; |
| 118 | |
| 119 | const auto& it = device_name_counts_.find(previous_device_name); |
| 120 | DCHECK(it != device_name_counts_.end()); |
| 121 | DCHECK_GT(it->second, 0); |
| 122 | |
| 123 | if (--(it->second) == 0) |
| 124 | device_name_counts_.erase(it); |
| 125 | |
| 126 | ++device_name_counts_[device_name_for_display]; |
| 127 | } |
| 128 | |
| 129 | auto device_id_it = |
| 130 | std::find(device_ids_.begin(), device_ids_.end(), device_id); |
| 131 | |
| 132 | DCHECK(device_id_it != device_ids_.end()); |
| 133 | if (view()) |
| 134 | view()->OnOptionUpdated(device_id_it - device_ids_.begin()); |
| 135 | return; |
| 136 | } |
| 137 | |
| 138 | device_ids_.push_back(device_id); |
| 139 | device_id_to_name_map_.insert({device_id, device_name_for_display}); |
| 140 | ++device_name_counts_[device_name_for_display]; |
| 141 | if (view()) |
| 142 | view()->OnOptionAdded(device_ids_.size() - 1); |
| 143 | } |
| 144 | |
| 145 | void BluetoothScanningPromptController::ResetEventHandler() { |
| 146 | event_handler_.Reset(); |
| 147 | } |
Mike Wasserman | 9e1196f | 2020-11-20 04:15:50 | [diff] [blame] | 148 | |
| 149 | base::WeakPtr<BluetoothScanningPromptController> |
| 150 | BluetoothScanningPromptController::GetWeakPtr() { |
| 151 | return weak_factory_.GetWeakPtr(); |
| 152 | } |
Michael van Ouwerkerk | 19f0cea | 2021-06-30 11:22:16 | [diff] [blame] | 153 | |
| 154 | } // namespace permissions |