Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(557)

Side by Side Diff: chrome/browser/ui/bluetooth/bluetooth_chooser_controller.cc

Issue 2210873003: bluetooth: Allow updates on chooser items. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@bluetooth-fix-advertised-services
Patch Set: Fix content_shell Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/ui/bluetooth/bluetooth_chooser_controller.h" 5 #include "chrome/browser/ui/bluetooth/bluetooth_chooser_controller.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "base/strings/utf_string_conversions.h" 8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/browser/net/referrer.h" 9 #include "chrome/browser/net/referrer.h"
10 #include "chrome/browser/profiles/profile_manager.h" 10 #include "chrome/browser/profiles/profile_manager.h"
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
42 base::string16 BluetoothChooserController::GetNoOptionsText() const { 42 base::string16 BluetoothChooserController::GetNoOptionsText() const {
43 return no_devices_text_; 43 return no_devices_text_;
44 } 44 }
45 45
46 base::string16 BluetoothChooserController::GetOkButtonLabel() const { 46 base::string16 BluetoothChooserController::GetOkButtonLabel() const {
47 return l10n_util::GetStringUTF16( 47 return l10n_util::GetStringUTF16(
48 IDS_BLUETOOTH_DEVICE_CHOOSER_PAIR_BUTTON_TEXT); 48 IDS_BLUETOOTH_DEVICE_CHOOSER_PAIR_BUTTON_TEXT);
49 } 49 }
50 50
51 size_t BluetoothChooserController::NumOptions() const { 51 size_t BluetoothChooserController::NumOptions() const {
52 return device_names_and_ids_.size(); 52 return device_ids_.size();
53 } 53 }
54 54
55 base::string16 BluetoothChooserController::GetOption(size_t index) const { 55 base::string16 BluetoothChooserController::GetOption(size_t index) const {
56 DCHECK_LT(index, device_names_and_ids_.size()); 56 DCHECK_LT(index, device_ids_.size());
57 const base::string16& device_name = device_names_and_ids_[index].first; 57 const std::string& device_id = device_ids_[index];
58 const auto& it = device_name_map_.find(device_name); 58 const auto& device_name_it = device_id_to_name_map_.find(device_id);
juncai 2016/08/04 21:52:23 nit: DCHECK(device_name_it != device_id_to_name_ma
ortuno 2016/08/05 02:16:09 Done.
59 const auto& it = device_name_map_.find(device_name_it->second);
59 DCHECK(it != device_name_map_.end()); 60 DCHECK(it != device_name_map_.end());
60 return it->second == 1 61 return it->second == 1
61 ? device_name 62 ? device_name_it->second
62 : l10n_util::GetStringFUTF16( 63 : l10n_util::GetStringFUTF16(
63 IDS_DEVICE_CHOOSER_DEVICE_NAME_WITH_ID, device_name, 64 IDS_DEVICE_CHOOSER_DEVICE_NAME_WITH_ID,
64 base::UTF8ToUTF16(device_names_and_ids_[index].second)); 65 device_name_it->second, base::UTF8ToUTF16(device_id));
65 } 66 }
66 67
67 void BluetoothChooserController::RefreshOptions() { 68 void BluetoothChooserController::RefreshOptions() {
68 ClearAllDevices(); 69 ClearAllDevices();
69 event_handler_.Run(content::BluetoothChooser::Event::RESCAN, std::string()); 70 event_handler_.Run(content::BluetoothChooser::Event::RESCAN, std::string());
70 } 71 }
71 72
72 base::string16 BluetoothChooserController::GetStatus() const { 73 base::string16 BluetoothChooserController::GetStatus() const {
73 return status_text_; 74 return status_text_;
74 } 75 }
75 76
76 void BluetoothChooserController::Select(size_t index) { 77 void BluetoothChooserController::Select(size_t index) {
77 DCHECK_LT(index, device_names_and_ids_.size()); 78 DCHECK_LT(index, device_ids_.size());
78 event_handler_.Run(content::BluetoothChooser::Event::SELECTED, 79 event_handler_.Run(content::BluetoothChooser::Event::SELECTED,
79 device_names_and_ids_[index].second); 80 device_ids_[index]);
80 } 81 }
81 82
82 void BluetoothChooserController::Cancel() { 83 void BluetoothChooserController::Cancel() {
83 event_handler_.Run(content::BluetoothChooser::Event::CANCELLED, 84 event_handler_.Run(content::BluetoothChooser::Event::CANCELLED,
84 std::string()); 85 std::string());
85 } 86 }
86 87
87 void BluetoothChooserController::Close() { 88 void BluetoothChooserController::Close() {
88 event_handler_.Run(content::BluetoothChooser::Event::CANCELLED, 89 event_handler_.Run(content::BluetoothChooser::Event::CANCELLED,
89 std::string()); 90 std::string());
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
141 status_text_ = 142 status_text_ =
142 l10n_util::GetStringUTF16(IDS_BLUETOOTH_DEVICE_CHOOSER_RE_SCAN); 143 l10n_util::GetStringUTF16(IDS_BLUETOOTH_DEVICE_CHOOSER_RE_SCAN);
143 if (view()) { 144 if (view()) {
144 view()->OnRefreshStateChanged( 145 view()->OnRefreshStateChanged(
145 false /* Refreshing options is complete */); 146 false /* Refreshing options is complete */);
146 } 147 }
147 break; 148 break;
148 } 149 }
149 } 150 }
150 151
151 void BluetoothChooserController::AddDevice(const std::string& device_id, 152 void BluetoothChooserController::AddOrUpdateDevice(
152 const base::string16& device_name) { 153 const std::string& device_id,
153 device_names_and_ids_.push_back(std::make_pair(device_name, device_id)); 154 const base::string16& device_name) {
155 auto result = device_id_to_name_map_.insert({device_id, device_name});
156 if (!result.second) {
157 // TODO(ortuno): Update device's name if necessary.
158 // https://crbug.com/634366
159 return;
160 }
161
162 device_ids_.push_back(device_id);
154 ++device_name_map_[device_name]; 163 ++device_name_map_[device_name];
155 if (view()) 164 if (view())
156 view()->OnOptionAdded(device_names_and_ids_.size() - 1); 165 view()->OnOptionAdded(device_ids_.size() - 1);
157 } 166 }
158 167
159 void BluetoothChooserController::RemoveDevice(const std::string& device_id) { 168 void BluetoothChooserController::RemoveDevice(const std::string& device_id) {
160 for (auto it = device_names_and_ids_.begin(); 169 const auto& name_it = device_id_to_name_map_.find(device_id);
161 it != device_names_and_ids_.end(); ++it) { 170 if (name_it == device_id_to_name_map_.end()) {
juncai 2016/08/04 21:52:23 nit: no {} for single line if statement.
ortuno 2016/08/05 02:16:08 Done.
162 if (it->second == device_id) { 171 return;
163 size_t index = it - device_names_and_ids_.begin(); 172 }
164 DCHECK_GT(device_name_map_[it->first], 0); 173
165 if (--device_name_map_[it->first] == 0) 174 int index = 0;
juncai 2016/08/04 21:52:23 use size_t instead of int.
ortuno 2016/08/05 02:16:09 Done.
166 device_name_map_.erase(it->first); 175 for (const auto& saved_device_id : device_ids_) {
167 device_names_and_ids_.erase(it); 176 if (saved_device_id != device_id) {
168 if (view()) 177 index++;
juncai 2016/08/04 21:52:23 nit: ++index;
ortuno 2016/08/05 02:16:09 Done.
169 view()->OnOptionRemoved(index); 178 continue;
170 return;
171 } 179 }
180
181 device_ids_.erase(device_ids_.begin() + index);
182
183 const auto& it = device_name_map_.find(name_it->second);
184 DCHECK(it != device_name_map_.end());
185 DCHECK_GT(it->second, 0);
186 --(it->second);
187
188 if (it->second == 0) {
juncai 2016/08/04 21:52:25 nit: use if (--it->second == 0) ? no {} for singl
ortuno 2016/08/05 02:16:09 Done.
189 device_name_map_.erase(it);
190 }
191
192 device_id_to_name_map_.erase(name_it);
193
194 if (view())
195 view()->OnOptionRemoved(index);
196 return;
172 } 197 }
173 } 198 }
174 199
175 void BluetoothChooserController::ClearAllDevices() { 200 void BluetoothChooserController::ClearAllDevices() {
176 device_names_and_ids_.clear(); 201 device_ids_.clear();
202 device_id_to_name_map_.clear();
177 device_name_map_.clear(); 203 device_name_map_.clear();
178 } 204 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698