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