OLD | NEW |
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_bubble_delegate.h" | 5 #include "chrome/browser/ui/bluetooth/bluetooth_chooser_bubble_delegate.h" |
6 | 6 |
7 #include "base/stl_util.h" | 7 #include "base/stl_util.h" |
8 #include "chrome/browser/ui/bluetooth/bluetooth_chooser_desktop.h" | 8 #include "chrome/browser/ui/bluetooth/bluetooth_chooser_desktop.h" |
9 #include "components/bubble/bubble_controller.h" | 9 #include "components/bubble/bubble_controller.h" |
10 | 10 |
11 BluetoothChooserBubbleDelegate::BluetoothChooserBubbleDelegate(Browser* browser) | 11 BluetoothChooserBubbleDelegate::BluetoothChooserBubbleDelegate(Browser* browser) |
12 : ChooserBubbleDelegate(browser), bluetooth_chooser_(nullptr) {} | 12 : ChooserBubbleDelegate(browser), bluetooth_chooser_(nullptr) {} |
13 | 13 |
14 BluetoothChooserBubbleDelegate::~BluetoothChooserBubbleDelegate() { | 14 BluetoothChooserBubbleDelegate::~BluetoothChooserBubbleDelegate() { |
15 if (bluetooth_chooser_) | 15 if (bluetooth_chooser_) |
16 bluetooth_chooser_->set_bluetooth_chooser_bubble_delegate(nullptr); | 16 bluetooth_chooser_->set_bluetooth_chooser_bubble_delegate(nullptr); |
17 } | 17 } |
18 | 18 |
19 const std::vector<base::string16>& BluetoothChooserBubbleDelegate::GetOptions() | 19 size_t BluetoothChooserBubbleDelegate::NumOptions() const { |
20 const { | 20 return device_names_and_ids_.size(); |
21 return device_names_; | |
22 } | 21 } |
23 | 22 |
24 // TODO(juncai): Change the index type to be size_t in base class to avoid | 23 const base::string16& BluetoothChooserBubbleDelegate::GetOption( |
25 // extra type casting. | 24 size_t index) const { |
26 void BluetoothChooserBubbleDelegate::Select(int index) { | 25 DCHECK_LT(index, device_names_and_ids_.size()); |
27 size_t idx = static_cast<size_t>(index); | 26 return device_names_and_ids_[index].first; |
28 size_t num_options = device_ids_.size(); | 27 } |
29 DCHECK_LT(idx, num_options); | 28 |
| 29 void BluetoothChooserBubbleDelegate::Select(size_t index) { |
| 30 DCHECK_LT(index, device_names_and_ids_.size()); |
30 if (bluetooth_chooser_) { | 31 if (bluetooth_chooser_) { |
31 bluetooth_chooser_->CallEventHandler( | 32 bluetooth_chooser_->CallEventHandler( |
32 content::BluetoothChooser::Event::SELECTED, device_ids_[idx]); | 33 content::BluetoothChooser::Event::SELECTED, |
| 34 device_names_and_ids_[index].second); |
33 } | 35 } |
34 | 36 |
35 if (bubble_controller_) | 37 if (bubble_controller_) |
36 bubble_controller_->CloseBubble(BUBBLE_CLOSE_ACCEPTED); | 38 bubble_controller_->CloseBubble(BUBBLE_CLOSE_ACCEPTED); |
37 } | 39 } |
38 | 40 |
39 void BluetoothChooserBubbleDelegate::Cancel() { | 41 void BluetoothChooserBubbleDelegate::Cancel() { |
40 if (bluetooth_chooser_) { | 42 if (bluetooth_chooser_) { |
41 bluetooth_chooser_->CallEventHandler( | 43 bluetooth_chooser_->CallEventHandler( |
42 content::BluetoothChooser::Event::CANCELLED, std::string()); | 44 content::BluetoothChooser::Event::CANCELLED, std::string()); |
43 } | 45 } |
44 | 46 |
45 if (bubble_controller_) | 47 if (bubble_controller_) |
46 bubble_controller_->CloseBubble(BUBBLE_CLOSE_CANCELED); | 48 bubble_controller_->CloseBubble(BUBBLE_CLOSE_CANCELED); |
47 } | 49 } |
48 | 50 |
49 void BluetoothChooserBubbleDelegate::Close() { | 51 void BluetoothChooserBubbleDelegate::Close() { |
50 if (bluetooth_chooser_) { | 52 if (bluetooth_chooser_) { |
51 bluetooth_chooser_->CallEventHandler( | 53 bluetooth_chooser_->CallEventHandler( |
52 content::BluetoothChooser::Event::CANCELLED, std::string()); | 54 content::BluetoothChooser::Event::CANCELLED, std::string()); |
53 } | 55 } |
54 } | 56 } |
55 | 57 |
56 void BluetoothChooserBubbleDelegate::AddDevice( | 58 void BluetoothChooserBubbleDelegate::AddDevice( |
57 const std::string& device_id, | 59 const std::string& device_id, |
58 const base::string16& device_name) { | 60 const base::string16& device_name) { |
59 DCHECK(!ContainsValue(device_ids_, device_id)); | 61 device_names_and_ids_.push_back(std::make_pair(device_name, device_id)); |
60 device_names_.push_back(device_name); | |
61 device_ids_.push_back(device_id); | |
62 // TODO(juncai): Change OnOptionAdded's index type to be size_t to avoid | |
63 // extra type casting here. | |
64 if (observer()) | 62 if (observer()) |
65 observer()->OnOptionAdded(static_cast<int>(device_names_.size()) - 1); | 63 observer()->OnOptionAdded(device_names_and_ids_.size() - 1); |
66 } | 64 } |
67 | 65 |
68 void BluetoothChooserBubbleDelegate::RemoveDevice( | 66 void BluetoothChooserBubbleDelegate::RemoveDevice( |
69 const std::string& device_id) { | 67 const std::string& device_id) { |
70 auto iter = std::find(device_ids_.begin(), device_ids_.end(), device_id); | 68 for (auto it = device_names_and_ids_.begin(); |
71 if (iter != device_ids_.end()) { | 69 it != device_names_and_ids_.end(); ++it) { |
72 size_t index = iter - device_ids_.begin(); | 70 if (it->second == device_id) { |
73 device_ids_.erase(iter); | 71 size_t index = it - device_names_and_ids_.begin(); |
74 device_names_.erase(device_names_.begin() + index); | 72 device_names_and_ids_.erase(it); |
75 // TODO(juncai): Change OnOptionRemoved's index type to be size_t to avoid | 73 if (observer()) |
76 // extra type casting here. | 74 observer()->OnOptionRemoved(index); |
77 if (observer()) | 75 return; |
78 observer()->OnOptionRemoved(index); | 76 } |
79 } | 77 } |
80 } | 78 } |
OLD | NEW |