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

Side by Side 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 unified diff | 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 »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 // ID Not In Map Note: 5 // ID Not In Map Note:
6 // A service, characteristic, or descriptor ID not in the corresponding 6 // A service, characteristic, or descriptor ID not in the corresponding
7 // BluetoothDispatcherHost map [service_to_device_, characteristic_to_service_, 7 // BluetoothDispatcherHost map [service_to_device_, characteristic_to_service_,
8 // descriptor_to_characteristic_] implies a hostile renderer because a renderer 8 // descriptor_to_characteristic_] implies a hostile renderer because a renderer
9 // obtains the corresponding ID from this class and it will be added to the map 9 // obtains the corresponding ID from this class and it will be added to the map
10 // at that time. 10 // at that time.
(...skipping 28 matching lines...) Expand all
39 namespace content { 39 namespace content {
40 40
41 namespace { 41 namespace {
42 42
43 // TODO(ortuno): Once we have a chooser for scanning, a way to control that 43 // TODO(ortuno): Once we have a chooser for scanning, a way to control that
44 // chooser from tests, and the right callback for discovered services we should 44 // chooser from tests, and the right callback for discovered services we should
45 // delete these constants. 45 // delete these constants.
46 // https://crbug.com/436280 and https://crbug.com/484504 46 // https://crbug.com/436280 and https://crbug.com/484504
47 const int kDelayTime = 5; // 5 seconds for scanning and discovering 47 const int kDelayTime = 5; // 5 seconds for scanning and discovering
48 const int kTestingDelayTime = 0; // No need to wait during tests 48 const int kTestingDelayTime = 0; // No need to wait during tests
49 const size_t kMaxLengthForDeviceName =
50 29; // max length of device name in filter.
51
52 bool IsEmptyOrInvalidFilter(const content::BluetoothScanFilter& filter) {
53 return filter.name.empty() && filter.namePrefix.empty() &&
54 filter.services.empty() &&
55 filter.name.length() > kMaxLengthForDeviceName &&
56 filter.namePrefix.length() > kMaxLengthForDeviceName;
57 }
58
59 bool HasEmptyOrInvalidFilter(
60 const std::vector<content::BluetoothScanFilter>& filters) {
61 return filters.empty()
62 ? true
63 : filters.end() != std::find_if(filters.begin(), filters.end(),
64 IsEmptyOrInvalidFilter);
65 }
49 66
50 // Defined at 67 // Defined at
51 // https://webbluetoothchrome.github.io/web-bluetooth/#dfn-matches-a-filter 68 // https://webbluetoothchrome.github.io/web-bluetooth/#dfn-matches-a-filter
52 bool MatchesFilter(const std::set<BluetoothUUID>& device_uuids, 69 bool MatchesFilter(const device::BluetoothDevice& device,
53 const content::BluetoothScanFilter& filter) { 70 const content::BluetoothScanFilter& filter) {
54 if (filter.services.empty()) 71 DCHECK(!IsEmptyOrInvalidFilter(filter));
72
73 const std::string device_name = base::UTF16ToUTF8(device.GetName());
74
75 if (!filter.name.empty() && (device_name != filter.name)) {
76 return false;
77 }
78
79 if (!filter.namePrefix.empty() &&
80 (!base::StartsWith(device_name, filter.namePrefix,
81 base::CompareCase::SENSITIVE))) {
55 return false; 82 return false;
56 for (const BluetoothUUID& service : filter.services) { 83 }
57 if (!ContainsKey(device_uuids, service)) { 84
58 return false; 85 if (!filter.services.empty()) {
86 const auto& device_uuid_list = device.GetUUIDs();
87 const std::set<BluetoothUUID> device_uuids(device_uuid_list.begin(),
88 device_uuid_list.end());
89 for (const auto& service : filter.services) {
90 if (!ContainsKey(device_uuids, service)) {
91 return false;
92 }
59 } 93 }
60 } 94 }
95
61 return true; 96 return true;
62 } 97 }
63 98
64 bool MatchesFilters(const device::BluetoothDevice& device, 99 bool MatchesFilters(const device::BluetoothDevice& device,
65 const std::vector<content::BluetoothScanFilter>& filters) { 100 const std::vector<content::BluetoothScanFilter>& filters) {
66 const std::vector<BluetoothUUID>& device_uuid_list = device.GetUUIDs(); 101 DCHECK(!HasEmptyOrInvalidFilter(filters));
67 const std::set<BluetoothUUID> device_uuids(device_uuid_list.begin(),
68 device_uuid_list.end());
69 for (const content::BluetoothScanFilter& filter : filters) { 102 for (const content::BluetoothScanFilter& filter : filters) {
70 if (MatchesFilter(device_uuids, filter)) { 103 if (MatchesFilter(device, filter)) {
71 return true; 104 return true;
72 } 105 }
73 } 106 }
74 return false; 107 return false;
75 } 108 }
76 109
77 WebBluetoothError TranslateConnectError( 110 WebBluetoothError TranslateConnectError(
78 device::BluetoothDevice::ConnectErrorCode error_code) { 111 device::BluetoothDevice::ConnectErrorCode error_code) {
79 switch (error_code) { 112 switch (error_code) {
80 case device::BluetoothDevice::ERROR_UNKNOWN: 113 case device::BluetoothDevice::ERROR_UNKNOWN:
(...skipping 355 matching lines...) Expand 10 before | Expand all | Expand 10 after
436 int request_id, 469 int request_id,
437 int frame_routing_id, 470 int frame_routing_id,
438 const std::vector<BluetoothScanFilter>& filters, 471 const std::vector<BluetoothScanFilter>& filters,
439 const std::vector<BluetoothUUID>& optional_services) { 472 const std::vector<BluetoothUUID>& optional_services) {
440 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 473 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
441 RecordWebBluetoothFunctionCall(UMAWebBluetoothFunction::REQUEST_DEVICE); 474 RecordWebBluetoothFunctionCall(UMAWebBluetoothFunction::REQUEST_DEVICE);
442 RecordRequestDeviceArguments(filters, optional_services); 475 RecordRequestDeviceArguments(filters, optional_services);
443 476
444 VLOG(1) << "requestDevice called with the following filters: "; 477 VLOG(1) << "requestDevice called with the following filters: ";
445 for (const BluetoothScanFilter& filter : filters) { 478 for (const BluetoothScanFilter& filter : filters) {
479 VLOG(1) << "Name: " << filter.name;
480 VLOG(1) << "Name Prefix: " << filter.namePrefix;
481 VLOG(1) << "Services:";
446 VLOG(1) << "\t["; 482 VLOG(1) << "\t[";
447 for (const BluetoothUUID& service : filter.services) 483 for (const BluetoothUUID& service : filter.services)
448 VLOG(1) << "\t\t" << service.value(); 484 VLOG(1) << "\t\t" << service.value();
449 VLOG(1) << "\t]"; 485 VLOG(1) << "\t]";
450 } 486 }
451 487
452 VLOG(1) << "requestDevice called with the following optional services: "; 488 VLOG(1) << "requestDevice called with the following optional services: ";
453 for (const BluetoothUUID& service : optional_services) 489 for (const BluetoothUUID& service : optional_services)
454 VLOG(1) << "\t" << service.value(); 490 VLOG(1) << "\t" << service.value();
455 491
(...skipping 20 matching lines...) Expand all
476 512
477 if (!adapter_->IsPresent()) { 513 if (!adapter_->IsPresent()) {
478 VLOG(1) << "Bluetooth Adapter not present. Can't serve requestDevice."; 514 VLOG(1) << "Bluetooth Adapter not present. Can't serve requestDevice.";
479 RecordRequestDeviceOutcome( 515 RecordRequestDeviceOutcome(
480 UMARequestDeviceOutcome::BLUETOOTH_ADAPTER_NOT_PRESENT); 516 UMARequestDeviceOutcome::BLUETOOTH_ADAPTER_NOT_PRESENT);
481 Send(new BluetoothMsg_RequestDeviceError( 517 Send(new BluetoothMsg_RequestDeviceError(
482 thread_id, request_id, WebBluetoothError::NoBluetoothAdapter)); 518 thread_id, request_id, WebBluetoothError::NoBluetoothAdapter));
483 return; 519 return;
484 } 520 }
485 521
522 // The renderer should never send empty filters.
523 if (HasEmptyOrInvalidFilter(filters)) {
524 bad_message::ReceivedBadMessage(this,
525 bad_message::BDH_EMPTY_OR_INVALID_FILTERS);
526 return;
527 }
528
486 // Create storage for the information that backs the chooser, and show the 529 // Create storage for the information that backs the chooser, and show the
487 // chooser. 530 // chooser.
488 RequestDeviceSession* const session = new RequestDeviceSession( 531 RequestDeviceSession* const session = new RequestDeviceSession(
489 thread_id, request_id, filters, optional_services); 532 thread_id, request_id, filters, optional_services);
490 int chooser_id = request_device_sessions_.Add(session); 533 int chooser_id = request_device_sessions_.Add(session);
491 534
492 BluetoothChooser::EventHandler chooser_event_handler = 535 BluetoothChooser::EventHandler chooser_event_handler =
493 base::Bind(&BluetoothDispatcherHost::OnBluetoothChooserEvent, 536 base::Bind(&BluetoothDispatcherHost::OnBluetoothChooserEvent,
494 weak_ptr_on_ui_thread_, chooser_id); 537 weak_ptr_on_ui_thread_, chooser_id);
495 if (WebContents* web_contents = 538 if (WebContents* web_contents =
(...skipping 642 matching lines...) Expand 10 before | Expand all | Expand 10 after
1138 DCHECK_CURRENTLY_ON(BrowserThread::UI); 1181 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1139 NOTIMPLEMENTED(); 1182 NOTIMPLEMENTED();
1140 } 1183 }
1141 1184
1142 void BluetoothDispatcherHost::ShowBluetoothAdapterOffLink() { 1185 void BluetoothDispatcherHost::ShowBluetoothAdapterOffLink() {
1143 DCHECK_CURRENTLY_ON(BrowserThread::UI); 1186 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1144 NOTIMPLEMENTED(); 1187 NOTIMPLEMENTED();
1145 } 1188 }
1146 1189
1147 } // namespace content 1190 } // namespace content
OLDNEW
« 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