OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/bluetooth_api.h" | 5 #include "chrome/browser/extensions/api/bluetooth/bluetooth_api.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
10 #include "chrome/browser/extensions/api/bluetooth/bluetooth_api_factory.h" | 10 #include "chrome/browser/extensions/api/bluetooth/bluetooth_api_factory.h" |
11 #include "chrome/browser/extensions/api/bluetooth/bluetooth_api_utils.h" | 11 #include "chrome/browser/extensions/api/bluetooth/bluetooth_api_utils.h" |
12 #include "chrome/browser/extensions/api/bluetooth/bluetooth_event_router.h" | 12 #include "chrome/browser/extensions/api/bluetooth/bluetooth_event_router.h" |
13 #include "chrome/browser/extensions/event_names.h" | 13 #include "chrome/browser/extensions/event_names.h" |
14 #include "chrome/browser/extensions/event_router.h" | 14 #include "chrome/browser/extensions/event_router.h" |
15 #include "chrome/browser/extensions/extension_system.h" | 15 #include "chrome/browser/extensions/extension_system.h" |
16 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
17 #include "chrome/common/extensions/api/bluetooth.h" | 17 #include "chrome/common/extensions/api/bluetooth.h" |
18 #include "chrome/common/extensions/permissions/bluetooth_device_permission.h" | 18 #include "chrome/common/extensions/permissions/bluetooth_device_permission.h" |
19 #include "content/public/browser/browser_thread.h" | 19 #include "content/public/browser/browser_thread.h" |
20 #include "device/bluetooth/bluetooth_adapter.h" | 20 #include "device/bluetooth/bluetooth_adapter.h" |
21 #include "device/bluetooth/bluetooth_device.h" | 21 #include "device/bluetooth/bluetooth_device.h" |
22 #include "device/bluetooth/bluetooth_out_of_band_pairing_data.h" | 22 #include "device/bluetooth/bluetooth_out_of_band_pairing_data.h" |
| 23 #include "device/bluetooth/bluetooth_profile.h" |
23 #include "device/bluetooth/bluetooth_service_record.h" | 24 #include "device/bluetooth/bluetooth_service_record.h" |
24 #include "device/bluetooth/bluetooth_socket.h" | 25 #include "device/bluetooth/bluetooth_socket.h" |
25 #include "device/bluetooth/bluetooth_utils.h" | 26 #include "device/bluetooth/bluetooth_utils.h" |
26 #include "net/base/io_buffer.h" | 27 #include "net/base/io_buffer.h" |
27 | 28 |
28 using device::BluetoothAdapter; | 29 using device::BluetoothAdapter; |
29 using device::BluetoothDevice; | 30 using device::BluetoothDevice; |
30 using device::BluetoothServiceRecord; | 31 using device::BluetoothServiceRecord; |
31 using device::BluetoothSocket; | 32 using device::BluetoothSocket; |
32 | 33 |
(...skipping 10 matching lines...) Expand all Loading... |
43 const char kCouldNotGetLocalOutOfBandPairingData[] = | 44 const char kCouldNotGetLocalOutOfBandPairingData[] = |
44 "Could not get local Out Of Band Pairing Data"; | 45 "Could not get local Out Of Band Pairing Data"; |
45 const char kCouldNotSetOutOfBandPairingData[] = | 46 const char kCouldNotSetOutOfBandPairingData[] = |
46 "Could not set Out Of Band Pairing Data"; | 47 "Could not set Out Of Band Pairing Data"; |
47 const char kDevicePermissionDenied[] = "Permission to access device denied"; | 48 const char kDevicePermissionDenied[] = "Permission to access device denied"; |
48 const char kFailedToConnect[] = "Connection failed"; | 49 const char kFailedToConnect[] = "Connection failed"; |
49 const char kInvalidDevice[] = "Invalid device"; | 50 const char kInvalidDevice[] = "Invalid device"; |
50 const char kInvalidUuid[] = "Invalid UUID"; | 51 const char kInvalidUuid[] = "Invalid UUID"; |
51 const char kPlatformNotSupported[] = | 52 const char kPlatformNotSupported[] = |
52 "This operation is not supported on your platform"; | 53 "This operation is not supported on your platform"; |
| 54 const char kProfileAlreadyRegistered[] = |
| 55 "This profile has already been registered"; |
| 56 const char kProfileNotFound[] = "Profile not found: invalid uuid"; |
| 57 const char kProfileRegistrationFailed[] = "Profile registration failed"; |
53 const char kServiceDiscoveryFailed[] = "Service discovery failed"; | 58 const char kServiceDiscoveryFailed[] = "Service discovery failed"; |
54 const char kSocketNotFoundError[] = "Socket not found: invalid socket id"; | 59 const char kSocketNotFoundError[] = "Socket not found: invalid socket id"; |
55 const char kStartDiscoveryFailed[] = "Starting discovery failed"; | 60 const char kStartDiscoveryFailed[] = "Starting discovery failed"; |
56 const char kStopDiscoveryFailed[] = "Failed to stop discovery"; | 61 const char kStopDiscoveryFailed[] = "Failed to stop discovery"; |
57 | 62 |
58 } // namespace | 63 } // namespace |
59 | 64 |
60 namespace AddProfile = extensions::api::bluetooth::AddProfile; | 65 namespace AddProfile = extensions::api::bluetooth::AddProfile; |
61 namespace Connect = extensions::api::bluetooth::Connect; | 66 namespace Connect = extensions::api::bluetooth::Connect; |
62 namespace Disconnect = extensions::api::bluetooth::Disconnect; | 67 namespace Disconnect = extensions::api::bluetooth::Disconnect; |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
100 bluetooth_event_router()->OnListenerAdded(); | 105 bluetooth_event_router()->OnListenerAdded(); |
101 } | 106 } |
102 | 107 |
103 void BluetoothAPI::OnListenerRemoved(const EventListenerInfo& details) { | 108 void BluetoothAPI::OnListenerRemoved(const EventListenerInfo& details) { |
104 if (bluetooth_event_router()->IsBluetoothSupported()) | 109 if (bluetooth_event_router()->IsBluetoothSupported()) |
105 bluetooth_event_router()->OnListenerRemoved(); | 110 bluetooth_event_router()->OnListenerRemoved(); |
106 } | 111 } |
107 | 112 |
108 namespace api { | 113 namespace api { |
109 | 114 |
110 // TOOD(youngki): Implement. | 115 BluetoothAddProfileFunction::BluetoothAddProfileFunction() { |
111 bool BluetoothAddProfileFunction::RunImpl() { | |
112 return false; | |
113 } | 116 } |
114 | 117 |
115 // TODO(youngki): Implement. | 118 bool BluetoothAddProfileFunction::RunImpl() { |
| 119 scoped_ptr<AddProfile::Params> params(AddProfile::Params::Create(*args_)); |
| 120 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); |
| 121 |
| 122 if (!BluetoothDevice::IsUUIDValid(params->profile.uuid)) { |
| 123 SetError(kInvalidUuid); |
| 124 SendResponse(false); |
| 125 return false; |
| 126 } |
| 127 |
| 128 uuid_ = device::bluetooth_utils::CanonicalUuid(params->profile.uuid); |
| 129 |
| 130 if (GetEventRouter(profile())->HasProfile(uuid_)) { |
| 131 SetError(kProfileAlreadyRegistered); |
| 132 SendResponse(false); |
| 133 return false; |
| 134 } |
| 135 |
| 136 device::BluetoothProfile::Options options; |
| 137 if (params->profile.name.get()) |
| 138 options.name = *params->profile.name.get(); |
| 139 if (params->profile.channel.get()) |
| 140 options.channel = *params->profile.channel.get(); |
| 141 if (params->profile.psm.get()) |
| 142 options.psm = *params->profile.psm.get(); |
| 143 if (params->profile.require_authentication.get()) { |
| 144 options.require_authentication = |
| 145 *params->profile.require_authentication.get(); |
| 146 } |
| 147 if (params->profile.require_authorization.get()) { |
| 148 options.require_authorization = |
| 149 *params->profile.require_authorization.get(); |
| 150 } |
| 151 if (params->profile.auto_connect.get()) |
| 152 options.auto_connect = *params->profile.auto_connect.get(); |
| 153 if (params->profile.version.get()) |
| 154 options.version = *params->profile.version.get(); |
| 155 if (params->profile.features.get()) |
| 156 options.features = *params->profile.features.get(); |
| 157 |
| 158 device::BluetoothProfile::Register( |
| 159 uuid_, |
| 160 options, |
| 161 base::Bind(&BluetoothAddProfileFunction::OnProfileRegistered, this)); |
| 162 return true; |
| 163 } |
| 164 |
| 165 void BluetoothAddProfileFunction::OnProfileRegistered( |
| 166 device::BluetoothProfile* bluetooth_profile) { |
| 167 if (!bluetooth_profile) { |
| 168 SetError(kProfileRegistrationFailed); |
| 169 SendResponse(false); |
| 170 return; |
| 171 } |
| 172 |
| 173 if (GetEventRouter(profile())->HasProfile(uuid_)) { |
| 174 bluetooth_profile->Unregister(); |
| 175 SetError(kProfileAlreadyRegistered); |
| 176 SendResponse(false); |
| 177 return; |
| 178 } |
| 179 |
| 180 bluetooth_profile->SetConnectionCallback( |
| 181 base::Bind(&ExtensionBluetoothEventRouter::DispatchConnectionEvent, |
| 182 base::Unretained(GetEventRouter(profile())), |
| 183 extension_id(), |
| 184 uuid_)); |
| 185 GetEventRouter(profile())->AddProfile(uuid_, bluetooth_profile); |
| 186 SendResponse(true); |
| 187 } |
| 188 |
116 bool BluetoothRemoveProfileFunction::RunImpl() { | 189 bool BluetoothRemoveProfileFunction::RunImpl() { |
117 return false; | 190 scoped_ptr<RemoveProfile::Params> params( |
| 191 RemoveProfile::Params::Create(*args_)); |
| 192 |
| 193 if (!BluetoothDevice::IsUUIDValid(params->profile.uuid)) { |
| 194 SetError(kInvalidUuid); |
| 195 SendResponse(false); |
| 196 return false; |
| 197 } |
| 198 |
| 199 std::string uuid = |
| 200 device::bluetooth_utils::CanonicalUuid(params->profile.uuid); |
| 201 |
| 202 if (!GetEventRouter(profile())->HasProfile(uuid)) { |
| 203 SetError(kProfileNotFound); |
| 204 SendResponse(false); |
| 205 return false; |
| 206 } |
| 207 |
| 208 GetEventRouter(profile())->RemoveProfile(uuid); |
| 209 return true; |
118 } | 210 } |
119 | 211 |
120 bool BluetoothGetProfilesFunction::DoWork( | 212 bool BluetoothGetProfilesFunction::DoWork( |
121 scoped_refptr<device::BluetoothAdapter> adapter) { | 213 scoped_refptr<device::BluetoothAdapter> adapter) { |
122 scoped_ptr<GetProfiles::Params> params(GetProfiles::Params::Create(*args_)); | 214 scoped_ptr<GetProfiles::Params> params(GetProfiles::Params::Create(*args_)); |
123 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); | 215 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); |
124 const bluetooth::GetProfilesOptions& options = params->options; | 216 const bluetooth::GetProfilesOptions& options = params->options; |
125 | 217 |
126 BluetoothDevice* device = adapter->GetDevice(options.device.address); | 218 BluetoothDevice* device = adapter->GetDevice(options.device.address); |
127 if (!device) { | 219 if (!device) { |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
266 | 358 |
267 void BluetoothConnectFunction::ConnectToServiceCallback( | 359 void BluetoothConnectFunction::ConnectToServiceCallback( |
268 const BluetoothDevice* device, | 360 const BluetoothDevice* device, |
269 const std::string& service_uuid, | 361 const std::string& service_uuid, |
270 scoped_refptr<BluetoothSocket> socket) { | 362 scoped_refptr<BluetoothSocket> socket) { |
271 if (socket.get()) { | 363 if (socket.get()) { |
272 int socket_id = GetEventRouter(profile())->RegisterSocket(socket); | 364 int socket_id = GetEventRouter(profile())->RegisterSocket(socket); |
273 | 365 |
274 bluetooth::Socket result_socket; | 366 bluetooth::Socket result_socket; |
275 bluetooth::BluetoothDeviceToApiDevice(*device, &result_socket.device); | 367 bluetooth::BluetoothDeviceToApiDevice(*device, &result_socket.device); |
276 result_socket.service_uuid = service_uuid; | 368 result_socket.profile.uuid = service_uuid; |
277 result_socket.id = socket_id; | 369 result_socket.id = socket_id; |
278 SetResult(result_socket.ToValue().release()); | 370 SetResult(result_socket.ToValue().release()); |
279 SendResponse(true); | 371 SendResponse(true); |
280 } else { | 372 } else { |
281 SetError(kFailedToConnect); | 373 SetError(kFailedToConnect); |
282 SendResponse(false); | 374 SendResponse(false); |
283 } | 375 } |
284 } | 376 } |
285 | 377 |
286 bool BluetoothConnectFunction::DoWork(scoped_refptr<BluetoothAdapter> adapter) { | 378 bool BluetoothConnectFunction::DoWork(scoped_refptr<BluetoothAdapter> adapter) { |
(...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
570 adapter->StopDiscovering( | 662 adapter->StopDiscovering( |
571 base::Bind(&BluetoothStopDiscoveryFunction::OnSuccessCallback, this), | 663 base::Bind(&BluetoothStopDiscoveryFunction::OnSuccessCallback, this), |
572 base::Bind(&BluetoothStopDiscoveryFunction::OnErrorCallback, this)); | 664 base::Bind(&BluetoothStopDiscoveryFunction::OnErrorCallback, this)); |
573 } | 665 } |
574 | 666 |
575 return true; | 667 return true; |
576 } | 668 } |
577 | 669 |
578 } // namespace api | 670 } // namespace api |
579 } // namespace extensions | 671 } // namespace extensions |
OLD | NEW |