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

Side by Side Diff: chrome/browser/extensions/api/bluetooth_socket/bluetooth_socket_api.cc

Issue 391453002: [Bluetooth] Enforce that PSMs must be odd. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Validate PSMs more correctly Created 6 years, 5 months 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | chrome/test/data/extensions/api_test/bluetooth_socket/listen/runtest.js » ('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 #include "chrome/browser/extensions/api/bluetooth_socket/bluetooth_socket_api.h" 5 #include "chrome/browser/extensions/api/bluetooth_socket/bluetooth_socket_api.h"
6 6
7 #include <stdint.h>
8
7 #include "chrome/browser/extensions/api/bluetooth_socket/bluetooth_api_socket.h" 9 #include "chrome/browser/extensions/api/bluetooth_socket/bluetooth_api_socket.h"
8 #include "chrome/browser/extensions/api/bluetooth_socket/bluetooth_socket_event_ dispatcher.h" 10 #include "chrome/browser/extensions/api/bluetooth_socket/bluetooth_socket_event_ dispatcher.h"
9 #include "chrome/common/extensions/api/bluetooth/bluetooth_manifest_data.h" 11 #include "chrome/common/extensions/api/bluetooth/bluetooth_manifest_data.h"
10 #include "content/public/browser/browser_context.h" 12 #include "content/public/browser/browser_context.h"
11 #include "content/public/browser/browser_thread.h" 13 #include "content/public/browser/browser_thread.h"
12 #include "device/bluetooth/bluetooth_adapter.h" 14 #include "device/bluetooth/bluetooth_adapter.h"
13 #include "device/bluetooth/bluetooth_adapter_factory.h" 15 #include "device/bluetooth/bluetooth_adapter_factory.h"
14 #include "device/bluetooth/bluetooth_device.h" 16 #include "device/bluetooth/bluetooth_device.h"
15 #include "device/bluetooth/bluetooth_socket.h" 17 #include "device/bluetooth/bluetooth_socket.h"
16 #include "extensions/common/permissions/permissions_data.h" 18 #include "extensions/common/permissions/permissions_data.h"
17 #include "net/base/io_buffer.h" 19 #include "net/base/io_buffer.h"
18 20
19 using content::BrowserThread; 21 using content::BrowserThread;
20 using extensions::BluetoothApiSocket; 22 using extensions::BluetoothApiSocket;
21 using extensions::api::bluetooth_socket::ListenOptions; 23 using extensions::api::bluetooth_socket::ListenOptions;
22 using extensions::api::bluetooth_socket::SocketInfo; 24 using extensions::api::bluetooth_socket::SocketInfo;
23 using extensions::api::bluetooth_socket::SocketProperties; 25 using extensions::api::bluetooth_socket::SocketProperties;
24 26
25 namespace { 27 namespace {
26 28
27 const char kDeviceNotFoundError[] = "Device not found"; 29 const char kDeviceNotFoundError[] = "Device not found";
30 const char kInvalidPsmError[] = "Invalid PSM";
28 const char kInvalidUuidError[] = "Invalid UUID"; 31 const char kInvalidUuidError[] = "Invalid UUID";
29 const char kPermissionDeniedError[] = "Permission denied"; 32 const char kPermissionDeniedError[] = "Permission denied";
30 const char kSocketNotFoundError[] = "Socket not found"; 33 const char kSocketNotFoundError[] = "Socket not found";
31 34
32 linked_ptr<SocketInfo> CreateSocketInfo(int socket_id, 35 linked_ptr<SocketInfo> CreateSocketInfo(int socket_id,
33 BluetoothApiSocket* socket) { 36 BluetoothApiSocket* socket) {
34 DCHECK(BrowserThread::CurrentlyOn(BluetoothApiSocket::kThreadId)); 37 DCHECK(BrowserThread::CurrentlyOn(BluetoothApiSocket::kThreadId));
35 linked_ptr<SocketInfo> socket_info(new SocketInfo()); 38 linked_ptr<SocketInfo> socket_info(new SocketInfo());
36 // This represents what we know about the socket, and does not call through 39 // This represents what we know about the socket, and does not call through
37 // to the system. 40 // to the system.
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
73 extensions::api::BluetoothSocketEventDispatcher* socket_event_dispatcher = 76 extensions::api::BluetoothSocketEventDispatcher* socket_event_dispatcher =
74 extensions::api::BluetoothSocketEventDispatcher::Get(browser_context); 77 extensions::api::BluetoothSocketEventDispatcher::Get(browser_context);
75 DCHECK(socket_event_dispatcher) 78 DCHECK(socket_event_dispatcher)
76 << "There is no socket event dispatcher. " 79 << "There is no socket event dispatcher. "
77 "If this assertion is failing during a test, then it is likely that " 80 "If this assertion is failing during a test, then it is likely that "
78 "TestExtensionSystem is failing to provide an instance of " 81 "TestExtensionSystem is failing to provide an instance of "
79 "BluetoothSocketEventDispatcher."; 82 "BluetoothSocketEventDispatcher.";
80 return socket_event_dispatcher; 83 return socket_event_dispatcher;
81 } 84 }
82 85
86 // Returns |true| if |psm| is a valid PSM.
87 // Per the Bluetooth specification, the PSM field must be at least two octets in
88 // length, with least significant bit of the least significant octet equal to
89 // '1' and the least significant bit of the most significant octet equal to '0'.
90 bool IsValidPsm(int psm) {
91 if (psm <= 0)
92 return false;
93
94 std::vector<int16_t> octets;
95 while (psm > 0) {
96 octets.push_back(psm & 0xFF);
97 psm = psm >> 8;
98 }
99
100 if (octets.size() < 2U)
101 return false;
102
103 // The least significant bit of the least significant octet must be '1'.
104 if ((octets.front() & 0x01) != 1)
105 return false;
106
107 // The least significant bit of the most significant octet must be '0'.
108 if ((octets.back() & 0x01) != 0)
109 return false;
110
111 return true;
112 }
113
83 } // namespace 114 } // namespace
84 115
85 namespace extensions { 116 namespace extensions {
86 namespace api { 117 namespace api {
87 118
88 BluetoothSocketAsyncApiFunction::BluetoothSocketAsyncApiFunction() {} 119 BluetoothSocketAsyncApiFunction::BluetoothSocketAsyncApiFunction() {}
89 120
90 BluetoothSocketAsyncApiFunction::~BluetoothSocketAsyncApiFunction() {} 121 BluetoothSocketAsyncApiFunction::~BluetoothSocketAsyncApiFunction() {}
91 122
92 bool BluetoothSocketAsyncApiFunction::RunAsync() { 123 bool BluetoothSocketAsyncApiFunction::RunAsync() {
(...skipping 287 matching lines...) Expand 10 before | Expand all | Expand 10 after
380 const device::BluetoothUUID& uuid, 411 const device::BluetoothUUID& uuid,
381 scoped_ptr<std::string> name, 412 scoped_ptr<std::string> name,
382 const device::BluetoothAdapter::CreateServiceCallback& callback, 413 const device::BluetoothAdapter::CreateServiceCallback& callback,
383 const device::BluetoothAdapter::CreateServiceErrorCallback& 414 const device::BluetoothAdapter::CreateServiceErrorCallback&
384 error_callback) { 415 error_callback) {
385 device::BluetoothAdapter::ServiceOptions service_options; 416 device::BluetoothAdapter::ServiceOptions service_options;
386 service_options.name = name.Pass(); 417 service_options.name = name.Pass();
387 418
388 ListenOptions* options = params_->options.get(); 419 ListenOptions* options = params_->options.get();
389 if (options) { 420 if (options) {
390 if (options->psm.get()) 421 if (options->psm) {
391 service_options.psm.reset(new int(*(options->psm))); 422 int psm = *options->psm;
423 if (!IsValidPsm(psm)) {
424 error_callback.Run(kInvalidPsmError);
425 return;
426 }
427
428 service_options.psm.reset(new int(psm));
429 }
392 } 430 }
393 431
394 adapter->CreateL2capService(uuid, service_options, callback, error_callback); 432 adapter->CreateL2capService(uuid, service_options, callback, error_callback);
395 } 433 }
396 434
397 void BluetoothSocketListenUsingL2capFunction::CreateResults() { 435 void BluetoothSocketListenUsingL2capFunction::CreateResults() {
398 results_ = bluetooth_socket::ListenUsingL2cap::Results::Create(); 436 results_ = bluetooth_socket::ListenUsingL2cap::Results::Create();
399 } 437 }
400 438
401 BluetoothSocketConnectFunction::BluetoothSocketConnectFunction() {} 439 BluetoothSocketConnectFunction::BluetoothSocketConnectFunction() {}
(...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after
619 if (socket) { 657 if (socket) {
620 socket_infos.push_back(CreateSocketInfo(socket_id, socket)); 658 socket_infos.push_back(CreateSocketInfo(socket_id, socket));
621 } 659 }
622 } 660 }
623 } 661 }
624 results_ = bluetooth_socket::GetSockets::Results::Create(socket_infos); 662 results_ = bluetooth_socket::GetSockets::Results::Create(socket_infos);
625 } 663 }
626 664
627 } // namespace api 665 } // namespace api
628 } // namespace extensions 666 } // namespace extensions
OLDNEW
« no previous file with comments | « no previous file | chrome/test/data/extensions/api_test/bluetooth_socket/listen/runtest.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698