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

Side by Side Diff: chromeos/components/tether/ble_advertisement_device_queue.cc

Issue 2576283002: [Chrome OS Tether] Create BleAdvertisementDeviceQueue. (Closed)
Patch Set: Created 4 years 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
(Empty)
1 // Copyright 2016 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
5 #include "chromeos/components/tether/ble_advertisement_device_queue.h"
6
7 #include <algorithm>
8 #include <string>
9
10 #include "base/logging.h"
11 #include "components/proximity_auth/logging/logging.h"
12
13 namespace chromeos {
14
15 namespace tether {
16
17 const int BleAdvertisementDeviceQueue::kMaxConcurrentAdvertisements = 2;
18
19 BleAdvertisementDeviceQueue::BleAdvertisementDeviceQueue() {}
20
21 BleAdvertisementDeviceQueue::~BleAdvertisementDeviceQueue() {}
22
23 bool BleAdvertisementDeviceQueue::SetDevices(
24 std::vector<cryptauth::RemoteDevice> devices) {
25 // Determine which devices exist in |devices| but not in |device_queue_|, then
26 // add them to |device_queue_|.
27 std::vector<cryptauth::RemoteDevice> missing_from_queue;
28 for (auto& device : devices) {
29 if (std::find(device_queue_.begin(), device_queue_.end(), device) ==
30 device_queue_.end()) {
31 missing_from_queue.push_back(device);
32 }
33 }
34 if (!missing_from_queue.empty()) {
35 std::string log_message = "Adding device IDs: [";
36 for (auto& device : missing_from_queue) {
37 device_queue_.push_back(device);
38 log_message += device.GetTruncatedDeviceIdForLogs() + ",";
39 }
40 log_message.replace(log_message.length() - 1, log_message.length(), "]");
41 PA_LOG(INFO) << log_message;
42 }
43
44 // Determine which devices do not exist in |devices| but do exist in
45 // |device_queue_|, then remove them from |device_queue_|.
46 std::vector<cryptauth::RemoteDevice> to_remove_from_queue;
47 for (auto& device : device_queue_) {
48 if (std::find(devices.begin(), devices.end(), device) == devices.end()) {
49 to_remove_from_queue.push_back(device);
50 }
51 }
52 if (!to_remove_from_queue.empty()) {
53 std::string log_message = "Removing device IDs: [";
54 for (auto& device : to_remove_from_queue) {
55 device_queue_.erase(
56 std::find(device_queue_.begin(), device_queue_.end(), device));
57 log_message += device.GetTruncatedDeviceIdForLogs() + ",";
58 }
59 log_message.replace(log_message.length() - 1, log_message.length(), "]");
60 PA_LOG(INFO) << log_message;
61 }
62
63 return !missing_from_queue.empty() || !to_remove_from_queue.empty();
64 }
65
66 void BleAdvertisementDeviceQueue::MoveDeviceToEnd(std::string device_id) {
67 if (device_id.empty()) {
68 return;
69 }
70
71 int index = -1;
72 for (size_t i = 0; i < device_queue_.size(); i++) {
73 if (device_id == device_queue_[i].GetDeviceId()) {
74 index = i;
75 break;
76 }
77 }
78
79 if (index >= 0) {
80 cryptauth::RemoteDevice to_move = device_queue_[index];
81 device_queue_.erase(device_queue_.begin() + index);
82 device_queue_.push_back(to_move);
83 }
84 }
85
86 std::vector<cryptauth::RemoteDevice>
87 BleAdvertisementDeviceQueue::GetDevicesToWhichToAdvertise() const {
88 std::vector<cryptauth::RemoteDevice> to_advertise;
89
90 if (device_queue_.empty()) {
91 PA_LOG(INFO) << "No devices to which to advertise.";
92 return to_advertise;
93 }
94
95 std::string log_message = "Should advertise to the following device IDs: [";
96 for (auto& device : device_queue_) {
97 to_advertise.push_back(device);
98 log_message += device.GetTruncatedDeviceIdForLogs() + ",";
99
100 if (to_advertise.size() ==
101 static_cast<size_t>(kMaxConcurrentAdvertisements)) {
102 break;
103 }
104 }
105
106 log_message.replace(log_message.length() - 1, log_message.length(), "]");
107 PA_LOG(INFO) << log_message;
108
109 return to_advertise;
110 }
111
112 size_t BleAdvertisementDeviceQueue::GetSize() const {
113 return device_queue_.size();
114 }
115
116 } // namespace tether
117
118 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698