Fix emulator build error
[platform/framework/web/chromium-efl.git] / components / permissions / bluetooth_scanning_prompt_controller.cc
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.
4
5 #include "components/permissions/bluetooth_scanning_prompt_controller.h"
6
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"
11
12 namespace permissions {
13
14 BluetoothScanningPromptController::BluetoothScanningPromptController(
15     content::RenderFrameHost* owner,
16     const content::BluetoothScanningPrompt::EventHandler& event_handler,
17     std::u16string title)
18     : ChooserController(title), event_handler_(event_handler) {}
19
20 BluetoothScanningPromptController::~BluetoothScanningPromptController() {}
21
22 bool BluetoothScanningPromptController::ShouldShowHelpButton() const {
23   return false;
24 }
25
26 std::u16string BluetoothScanningPromptController::GetNoOptionsText() const {
27   return l10n_util::GetStringUTF16(
28       IDS_BLUETOOTH_SCANNING_PROMPT_NO_DEVICES_FOUND_PROMPT);
29 }
30
31 std::u16string BluetoothScanningPromptController::GetOkButtonLabel() const {
32   return l10n_util::GetStringUTF16(
33       IDS_BLUETOOTH_SCANNING_PROMPT_ALLOW_BUTTON_TEXT);
34 }
35
36 std::u16string BluetoothScanningPromptController::GetCancelButtonLabel() const {
37   return l10n_util::GetStringUTF16(
38       IDS_BLUETOOTH_SCANNING_PROMPT_BLOCK_BUTTON_TEXT);
39 }
40
41 std::pair<std::u16string, std::u16string>
42 BluetoothScanningPromptController::GetThrobberLabelAndTooltip() const {
43   return {
44       l10n_util::GetStringUTF16(IDS_BLUETOOTH_DEVICE_CHOOSER_SCANNING_LABEL),
45       l10n_util::GetStringUTF16(
46           IDS_BLUETOOTH_DEVICE_CHOOSER_SCANNING_LABEL_TOOLTIP)};
47 }
48
49 bool BluetoothScanningPromptController::BothButtonsAlwaysEnabled() const {
50   return true;
51 }
52
53 bool BluetoothScanningPromptController::TableViewAlwaysDisabled() const {
54   return true;
55 }
56
57 size_t BluetoothScanningPromptController::NumOptions() const {
58   return device_ids_.size();
59 }
60
61 std::u16string BluetoothScanningPromptController::GetOption(
62     size_t index) const {
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));
74 }
75
76 void BluetoothScanningPromptController::Select(
77     const std::vector<size_t>& indices) {
78   DCHECK(indices.empty());
79
80   if (event_handler_.is_null())
81     return;
82
83   event_handler_.Run(content::BluetoothScanningPrompt::Event::kAllow);
84 }
85
86 void BluetoothScanningPromptController::Cancel() {
87   if (event_handler_.is_null())
88     return;
89
90   event_handler_.Run(content::BluetoothScanningPrompt::Event::kBlock);
91 }
92
93 void BluetoothScanningPromptController::Close() {
94   if (event_handler_.is_null())
95     return;
96
97   event_handler_.Run(content::BluetoothScanningPrompt::Event::kCanceled);
98 }
99
100 void BluetoothScanningPromptController::OpenHelpCenterUrl() const {}
101
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));
110   }
111
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;
117
118       const auto& it = device_name_counts_.find(previous_device_name);
119       DCHECK(it != device_name_counts_.end());
120       DCHECK_GT(it->second, 0);
121
122       if (--(it->second) == 0)
123         device_name_counts_.erase(it);
124
125       ++device_name_counts_[device_name_for_display];
126     }
127
128     auto device_id_it = base::ranges::find(device_ids_, device_id);
129
130     DCHECK(device_id_it != device_ids_.end());
131     if (view())
132       view()->OnOptionUpdated(device_id_it - device_ids_.begin());
133     return;
134   }
135
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];
139   if (view())
140     view()->OnOptionAdded(device_ids_.size() - 1);
141 }
142
143 void BluetoothScanningPromptController::ResetEventHandler() {
144   event_handler_.Reset();
145 }
146
147 base::WeakPtr<BluetoothScanningPromptController>
148 BluetoothScanningPromptController::GetWeakPtr() {
149   return weak_factory_.GetWeakPtr();
150 }
151
152 }  // namespace permissions