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

Unified Diff: content/browser/bluetooth/bluetooth_dispatcher_host.cc

Issue 1415533006: bluetooth: Implement requestDevice by name or name prefix (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@bluetooth-characteristic-properties
Patch Set: Merge with ToT and fix histograms conflict Created 5 years, 1 month 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « content/browser/bad_message.h ('k') | content/common/bluetooth/bluetooth_messages.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: content/browser/bluetooth/bluetooth_dispatcher_host.cc
diff --git a/content/browser/bluetooth/bluetooth_dispatcher_host.cc b/content/browser/bluetooth/bluetooth_dispatcher_host.cc
index d0762c7beb0de40a45acdbe4e4205262cfb13afb..8c6c7d87af68de281381e61467d896a64a45e3a0 100644
--- a/content/browser/bluetooth/bluetooth_dispatcher_host.cc
+++ b/content/browser/bluetooth/bluetooth_dispatcher_host.cc
@@ -46,28 +46,61 @@ namespace {
// https://crbug.com/436280 and https://crbug.com/484504
const int kDelayTime = 5; // 5 seconds for scanning and discovering
const int kTestingDelayTime = 0; // No need to wait during tests
+const size_t kMaxLengthForDeviceName =
+ 29; // max length of device name in filter.
+
+bool IsEmptyOrInvalidFilter(const content::BluetoothScanFilter& filter) {
+ return filter.name.empty() && filter.namePrefix.empty() &&
+ filter.services.empty() &&
+ filter.name.length() > kMaxLengthForDeviceName &&
+ filter.namePrefix.length() > kMaxLengthForDeviceName;
+}
+
+bool HasEmptyOrInvalidFilter(
+ const std::vector<content::BluetoothScanFilter>& filters) {
+ return filters.empty()
+ ? true
+ : filters.end() != std::find_if(filters.begin(), filters.end(),
+ IsEmptyOrInvalidFilter);
+}
// Defined at
// https://webbluetoothchrome.github.io/web-bluetooth/#dfn-matches-a-filter
-bool MatchesFilter(const std::set<BluetoothUUID>& device_uuids,
+bool MatchesFilter(const device::BluetoothDevice& device,
const content::BluetoothScanFilter& filter) {
- if (filter.services.empty())
- return false;
- for (const BluetoothUUID& service : filter.services) {
- if (!ContainsKey(device_uuids, service)) {
+ DCHECK(!IsEmptyOrInvalidFilter(filter));
+
+ const std::string device_name = base::UTF16ToUTF8(device.GetName());
+
+ if (!filter.name.empty() && (device_name != filter.name)) {
return false;
+ }
+
+ if (!filter.namePrefix.empty() &&
+ (!base::StartsWith(device_name, filter.namePrefix,
+ base::CompareCase::SENSITIVE))) {
+ return false;
+ }
+
+ if (!filter.services.empty()) {
+ const auto& device_uuid_list = device.GetUUIDs();
+ const std::set<BluetoothUUID> device_uuids(device_uuid_list.begin(),
+ device_uuid_list.end());
+ for (const auto& service : filter.services) {
+ if (!ContainsKey(device_uuids, service)) {
+ return false;
+ }
}
}
+
return true;
}
bool MatchesFilters(const device::BluetoothDevice& device,
const std::vector<content::BluetoothScanFilter>& filters) {
- const std::vector<BluetoothUUID>& device_uuid_list = device.GetUUIDs();
- const std::set<BluetoothUUID> device_uuids(device_uuid_list.begin(),
- device_uuid_list.end());
+ DCHECK(!HasEmptyOrInvalidFilter(filters));
for (const content::BluetoothScanFilter& filter : filters) {
- if (MatchesFilter(device_uuids, filter)) {
+ if (MatchesFilter(device, filter)) {
return true;
}
}
@@ -443,6 +476,9 @@ void BluetoothDispatcherHost::OnRequestDevice(
VLOG(1) << "requestDevice called with the following filters: ";
for (const BluetoothScanFilter& filter : filters) {
+ VLOG(1) << "Name: " << filter.name;
+ VLOG(1) << "Name Prefix: " << filter.namePrefix;
+ VLOG(1) << "Services:";
VLOG(1) << "\t[";
for (const BluetoothUUID& service : filter.services)
VLOG(1) << "\t\t" << service.value();
@@ -483,6 +519,13 @@ void BluetoothDispatcherHost::OnRequestDevice(
return;
}
+ // The renderer should never send empty filters.
+ if (HasEmptyOrInvalidFilter(filters)) {
+ bad_message::ReceivedBadMessage(this,
+ bad_message::BDH_EMPTY_OR_INVALID_FILTERS);
+ return;
+ }
+
// Create storage for the information that backs the chooser, and show the
// chooser.
RequestDeviceSession* const session = new RequestDeviceSession(
« no previous file with comments | « content/browser/bad_message.h ('k') | content/common/bluetooth/bluetooth_messages.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698