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

Side by Side Diff: chrome/browser/extensions/api/bluetooth_low_energy/bluetooth_low_energy_api.cc

Issue 1920353002: Implement create attribute API functions for BTLE. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@api_changes
Patch Set: Created 4 years, 7 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
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_low_energy/bluetooth_low_energ y_api.h" 5 #include "chrome/browser/extensions/api/bluetooth_low_energy/bluetooth_low_energ y_api.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 #include <algorithm> 8 #include <algorithm>
9 #include <iterator> 9 #include <iterator>
10 #include <vector> 10 #include <vector>
11 11
12 #include "base/bind.h" 12 #include "base/bind.h"
13 #include "base/callback.h" 13 #include "base/callback.h"
14 #include "base/callback_forward.h" 14 #include "base/callback_forward.h"
15 #include "base/command_line.h" 15 #include "base/command_line.h"
16 #include "base/lazy_instance.h" 16 #include "base/lazy_instance.h"
17 #include "base/logging.h" 17 #include "base/logging.h"
18 #include "base/memory/weak_ptr.h"
18 #include "base/values.h" 19 #include "base/values.h"
19 #include "chrome/browser/extensions/api/bluetooth_low_energy/utils.h" 20 #include "chrome/browser/extensions/api/bluetooth_low_energy/utils.h"
20 #include "chrome/common/extensions/api/bluetooth_low_energy.h" 21 #include "chrome/common/extensions/api/bluetooth_low_energy.h"
21 #include "content/public/browser/browser_thread.h" 22 #include "content/public/browser/browser_thread.h"
22 #include "device/bluetooth/bluetooth_adapter.h" 23 #include "device/bluetooth/bluetooth_adapter.h"
24 #include "device/bluetooth/bluetooth_gatt_characteristic.h"
25 #include "device/bluetooth/bluetooth_local_gatt_characteristic.h"
26 #include "device/bluetooth/bluetooth_local_gatt_descriptor.h"
27 #include "device/bluetooth/bluetooth_local_gatt_service.h"
28 #include "device/bluetooth/bluetooth_uuid.h"
23 #include "extensions/common/api/bluetooth/bluetooth_manifest_data.h" 29 #include "extensions/common/api/bluetooth/bluetooth_manifest_data.h"
24 #include "extensions/common/extension.h" 30 #include "extensions/common/extension.h"
25 #include "extensions/common/switches.h" 31 #include "extensions/common/switches.h"
26 32
27 #if defined(OS_CHROMEOS) 33 #if defined(OS_CHROMEOS)
28 #include "chrome/browser/chromeos/app_mode/kiosk_app_manager.h" 34 #include "chrome/browser/chromeos/app_mode/kiosk_app_manager.h"
29 #endif 35 #endif
30 36
31 using content::BrowserContext; 37 using content::BrowserContext;
32 using content::BrowserThread; 38 using content::BrowserThread;
(...skipping 22 matching lines...) Expand all
55 const char kErrorNotNotifying[] = "Not notifying"; 61 const char kErrorNotNotifying[] = "Not notifying";
56 const char kErrorOffsetInvalid[] = "Offset invalid"; 62 const char kErrorOffsetInvalid[] = "Offset invalid";
57 const char kErrorOperationFailed[] = "Operation failed"; 63 const char kErrorOperationFailed[] = "Operation failed";
58 const char kErrorPermissionDenied[] = "Permission denied"; 64 const char kErrorPermissionDenied[] = "Permission denied";
59 const char kErrorPlatformNotSupported[] = 65 const char kErrorPlatformNotSupported[] =
60 "This operation is not supported on the current platform"; 66 "This operation is not supported on the current platform";
61 const char kErrorRequestNotSupported[] = "Request not supported"; 67 const char kErrorRequestNotSupported[] = "Request not supported";
62 const char kErrorTimeout[] = "Operation timed out"; 68 const char kErrorTimeout[] = "Operation timed out";
63 const char kErrorUnsupportedDevice[] = 69 const char kErrorUnsupportedDevice[] =
64 "This device is not supported on the current platform"; 70 "This device is not supported on the current platform";
71 const char kErrorInvalidServiceId[] = "The service ID doesn't exist.";
72 const char kErrorInvalidCharacteristicId[] =
73 "The characteristic ID doesn't exist.";
65 const char kStatusAdvertisementAlreadyExists[] = 74 const char kStatusAdvertisementAlreadyExists[] =
66 "An advertisement is already advertising"; 75 "An advertisement is already advertising";
67 const char kStatusAdvertisementDoesNotExist[] = 76 const char kStatusAdvertisementDoesNotExist[] =
68 "This advertisement does not exist"; 77 "This advertisement does not exist";
69 78
70 // Returns the correct error string based on error status |status|. This is used 79 // Returns the correct error string based on error status |status|. This is used
71 // to set the value of |chrome.runtime.lastError.message| and should not be 80 // to set the value of |chrome.runtime.lastError.message| and should not be
72 // passed |BluetoothLowEnergyEventRouter::kStatusSuccess|. 81 // passed |BluetoothLowEnergyEventRouter::kStatusSuccess|.
73 std::string StatusToString(BluetoothLowEnergyEventRouter::Status status) { 82 std::string StatusToString(BluetoothLowEnergyEventRouter::Status status) {
74 switch (status) { 83 switch (status) {
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after
216 &DoWorkCallback<bool>, 225 &DoWorkCallback<bool>,
217 base::Bind(&BluetoothLowEnergyExtensionFunctionDeprecated::DoWork, 226 base::Bind(&BluetoothLowEnergyExtensionFunctionDeprecated::DoWork,
218 this)))) { 227 this)))) {
219 SetError(kErrorAdapterNotInitialized); 228 SetError(kErrorAdapterNotInitialized);
220 return false; 229 return false;
221 } 230 }
222 231
223 return true; 232 return true;
224 } 233 }
225 234
226 BluetoothLowEnergyExtensionFunction::BluetoothLowEnergyExtensionFunction() {} 235 BluetoothLowEnergyExtensionFunction::BluetoothLowEnergyExtensionFunction()
236 : event_router_(nullptr) {}
227 237
228 BluetoothLowEnergyExtensionFunction::~BluetoothLowEnergyExtensionFunction() {} 238 BluetoothLowEnergyExtensionFunction::~BluetoothLowEnergyExtensionFunction() {}
229 239
230 ExtensionFunction::ResponseAction BluetoothLowEnergyExtensionFunction::Run() { 240 ExtensionFunction::ResponseAction BluetoothLowEnergyExtensionFunction::Run() {
231 DCHECK_CURRENTLY_ON(BrowserThread::UI); 241 DCHECK_CURRENTLY_ON(BrowserThread::UI);
232 242
233 if (!BluetoothManifestData::CheckLowEnergyPermitted(extension())) 243 if (!BluetoothManifestData::CheckLowEnergyPermitted(extension()))
234 return RespondNow(Error(kErrorPermissionDenied)); 244 return RespondNow(Error(kErrorPermissionDenied));
235 245
236 BluetoothLowEnergyEventRouter* event_router = 246 event_router_ = GetEventRouter(browser_context());
237 GetEventRouter(browser_context()); 247 if (!event_router_->IsBluetoothSupported())
238 if (!event_router->IsBluetoothSupported())
239 return RespondNow(Error(kErrorPlatformNotSupported)); 248 return RespondNow(Error(kErrorPlatformNotSupported));
240 249
241 // It is safe to pass |this| here as ExtensionFunction is refcounted. 250 // It is safe to pass |this| here as ExtensionFunction is refcounted.
242 if (!event_router->InitializeAdapterAndInvokeCallback(base::Bind( 251 if (!event_router_->InitializeAdapterAndInvokeCallback(base::Bind(
243 &DoWorkCallback<void>, 252 &DoWorkCallback<void>,
244 base::Bind(&BluetoothLowEnergyExtensionFunction::DoWork, this)))) { 253 base::Bind(&BluetoothLowEnergyExtensionFunction::PreDoWork, this)))) {
245 // DoWork will respond when the adapter gets initialized. 254 // DoWork will respond when the adapter gets initialized.
246 return RespondNow(Error(kErrorAdapterNotInitialized)); 255 return RespondNow(Error(kErrorAdapterNotInitialized));
247 } 256 }
248 257
249 return RespondLater(); 258 return RespondLater();
250 } 259 }
251 260
261 void BluetoothLowEnergyExtensionFunction::PreDoWork() {
262 // The adapter must be initialized at this point, but return an error instead
263 // of asserting.
264 if (!event_router_->HasAdapter()) {
265 Respond(Error(kErrorAdapterNotInitialized));
266 return;
267 }
268 DoWork();
269 }
270
252 template <typename Params> 271 template <typename Params>
253 BLEPeripheralExtensionFunction<Params>::BLEPeripheralExtensionFunction() {} 272 BLEPeripheralExtensionFunction<Params>::BLEPeripheralExtensionFunction() {}
254 273
255 template <typename Params> 274 template <typename Params>
256 BLEPeripheralExtensionFunction<Params>::~BLEPeripheralExtensionFunction() {} 275 BLEPeripheralExtensionFunction<Params>::~BLEPeripheralExtensionFunction() {}
257 276
258 template <typename Params> 277 template <typename Params>
259 ExtensionFunction::ResponseAction 278 ExtensionFunction::ResponseAction
260 BLEPeripheralExtensionFunction<Params>::Run() { 279 BLEPeripheralExtensionFunction<Params>::Run() {
280 DCHECK_CURRENTLY_ON(BrowserThread::UI);
281
261 // Check permissions in manifest. 282 // Check permissions in manifest.
262 if (!BluetoothManifestData::CheckPeripheralPermitted(extension())) 283 if (!BluetoothManifestData::CheckPeripheralPermitted(extension()))
263 return RespondNow(Error(kErrorPermissionDenied)); 284 return RespondNow(Error(kErrorPermissionDenied));
264 285
265 params_ = Params::Create(*args_); 286 params_ = Params::Create(*args_);
266 EXTENSION_FUNCTION_VALIDATE(params_.get() != NULL); 287 EXTENSION_FUNCTION_VALIDATE(params_.get() != NULL);
267 288
268 return BluetoothLowEnergyExtensionFunction::Run(); 289 return BluetoothLowEnergyExtensionFunction::Run();
269 } 290 }
270 291
(...skipping 843 matching lines...) Expand 10 before | Expand all | Expand 10 after
1114 1135
1115 template class BLEPeripheralExtensionFunction<apibtle::CreateService::Params>; 1136 template class BLEPeripheralExtensionFunction<apibtle::CreateService::Params>;
1116 1137
1117 void BluetoothLowEnergyCreateServiceFunction::DoWork() { 1138 void BluetoothLowEnergyCreateServiceFunction::DoWork() {
1118 DCHECK_CURRENTLY_ON(BrowserThread::UI); 1139 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1119 // Using apibtle::CreateService::Results::Create causes a link error on 1140 // Using apibtle::CreateService::Results::Create causes a link error on
1120 // Windows for some reason, despite it being almost identical to its 1141 // Windows for some reason, despite it being almost identical to its
1121 // characteristic and descriptor counterparts. Since there is no plan to 1142 // characteristic and descriptor counterparts. Since there is no plan to
1122 // provide this API on Windows, ifdef'ing this out is fine. 1143 // provide this API on Windows, ifdef'ing this out is fine.
1123 #if !defined(OS_WIN) 1144 #if !defined(OS_WIN)
1124 Respond(ArgumentList(apibtle::CreateService::Results::Create(std::string()))); 1145 base::WeakPtr<device::BluetoothLocalGattService> service =
1146 device::BluetoothLocalGattService::Create(
1147 event_router_->adapter(),
1148 device::BluetoothUUID(params_->service.uuid),
1149 params_->service.is_primary, nullptr, nullptr);
1150
1151 Respond(ArgumentList(
1152 apibtle::CreateService::Results::Create(service->GetIdentifier())));
1125 #else 1153 #else
1126 Respond(Error(kErrorPlatformNotSupported)); 1154 Respond(Error(kErrorPlatformNotSupported));
1127 #endif 1155 #endif
1128 } 1156 }
1129 1157
1130 template class BLEPeripheralExtensionFunction< 1158 template class BLEPeripheralExtensionFunction<
1131 apibtle::CreateCharacteristic::Params>; 1159 apibtle::CreateCharacteristic::Params>;
1132 1160
1133 void BluetoothLowEnergyCreateCharacteristicFunction::DoWork() { 1161 void BluetoothLowEnergyCreateCharacteristicFunction::DoWork() {
1134 DCHECK_CURRENTLY_ON(BrowserThread::UI); 1162 device::BluetoothLocalGattService* service =
1135 Respond(ArgumentList( 1163 event_router_->adapter()->GetGattService(params_->service_id);
1136 apibtle::CreateCharacteristic::Results::Create(std::string()))); 1164 if (!service) {
1165 Respond(Error(kErrorInvalidServiceId));
1166 return;
1167 }
1168
1169 base::WeakPtr<device::BluetoothLocalGattCharacteristic> characteristic =
1170 device::BluetoothLocalGattCharacteristic::Create(
1171 device::BluetoothUUID(params_->characteristic.uuid),
1172 device::BluetoothGattCharacteristic::Properties(),
1173 device::BluetoothGattCharacteristic::Permissions(), service);
1174
1175 // Keep a track of this characteristic so we can look it up later if a
1176 // descriptor lists it as its parent.
1177 event_router_->AddLocalCharacteristic(characteristic->GetIdentifier(),
1178 service->GetIdentifier());
1179
1180 Respond(ArgumentList(apibtle::CreateCharacteristic::Results::Create(
1181 characteristic->GetIdentifier())));
1137 } 1182 }
1138 1183
1139 template class BLEPeripheralExtensionFunction< 1184 template class BLEPeripheralExtensionFunction<
1140 apibtle::CreateDescriptor::Params>; 1185 apibtle::CreateDescriptor::Params>;
1141 1186
1142 void BluetoothLowEnergyCreateDescriptorFunction::DoWork() { 1187 void BluetoothLowEnergyCreateDescriptorFunction::DoWork() {
1143 DCHECK_CURRENTLY_ON(BrowserThread::UI); 1188 device::BluetoothLocalGattCharacteristic* characteristic =
1144 Respond( 1189 event_router_->GetLocalCharacteristic(params_->characteristic_id);
1145 ArgumentList(apibtle::CreateDescriptor::Results::Create(std::string()))); 1190 if (!characteristic) {
1191 Respond(Error(kErrorInvalidCharacteristicId));
1192 return;
1193 }
1194
1195 base::WeakPtr<device::BluetoothLocalGattDescriptor> descriptor =
1196 device::BluetoothLocalGattDescriptor::Create(
1197 device::BluetoothUUID(params_->descriptor.uuid),
1198 device::BluetoothGattCharacteristic::Permissions(), characteristic);
1199
1200 Respond(ArgumentList(
1201 apibtle::CreateDescriptor::Results::Create(descriptor->GetIdentifier())));
1146 } 1202 }
1147 1203
1148 template class BLEPeripheralExtensionFunction<apibtle::RegisterService::Params>; 1204 template class BLEPeripheralExtensionFunction<apibtle::RegisterService::Params>;
1149 1205
1150 void BluetoothLowEnergyRegisterServiceFunction::DoWork() { 1206 void BluetoothLowEnergyRegisterServiceFunction::DoWork() {
1151 DCHECK_CURRENTLY_ON(BrowserThread::UI); 1207 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1152 Respond(ArgumentList(apibtle::RegisterService::Results::Create( 1208 Respond(ArgumentList(apibtle::RegisterService::Results::Create(
1153 apibtle::SERVICE_RESULT_SUCCESS))); 1209 apibtle::SERVICE_RESULT_SUCCESS)));
1154 } 1210 }
1155 1211
1156 template class BLEPeripheralExtensionFunction< 1212 template class BLEPeripheralExtensionFunction<
1157 apibtle::UnregisterService::Params>; 1213 apibtle::UnregisterService::Params>;
1158 1214
1159 void BluetoothLowEnergyUnregisterServiceFunction::DoWork() { 1215 void BluetoothLowEnergyUnregisterServiceFunction::DoWork() {
1160 DCHECK_CURRENTLY_ON(BrowserThread::UI); 1216 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1161 Respond(ArgumentList(apibtle::UnregisterService::Results::Create( 1217 Respond(ArgumentList(apibtle::UnregisterService::Results::Create(
1162 apibtle::SERVICE_RESULT_SUCCESS))); 1218 apibtle::SERVICE_RESULT_SUCCESS)));
1163 } 1219 }
1164 1220
1165 template class BLEPeripheralExtensionFunction< 1221 template class BLEPeripheralExtensionFunction<
1166 apibtle::SendRequestResponse::Params>; 1222 apibtle::SendRequestResponse::Params>;
1167 1223
1168 void BluetoothLowEnergySendRequestResponseFunction::DoWork() { 1224 void BluetoothLowEnergySendRequestResponseFunction::DoWork() {
1169 DCHECK_CURRENTLY_ON(BrowserThread::UI); 1225 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1170 Respond(NoArguments()); 1226 Respond(NoArguments());
1171 } 1227 }
1172 1228
1173 } // namespace api 1229 } // namespace api
1174 } // namespace extensions 1230 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698