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

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: owners 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 24 matching lines...) Expand all
57 const char kErrorNotNotifying[] = "Not notifying"; 63 const char kErrorNotNotifying[] = "Not notifying";
58 const char kErrorOffsetInvalid[] = "Offset invalid"; 64 const char kErrorOffsetInvalid[] = "Offset invalid";
59 const char kErrorOperationFailed[] = "Operation failed"; 65 const char kErrorOperationFailed[] = "Operation failed";
60 const char kErrorPermissionDenied[] = "Permission denied"; 66 const char kErrorPermissionDenied[] = "Permission denied";
61 const char kErrorPlatformNotSupported[] = 67 const char kErrorPlatformNotSupported[] =
62 "This operation is not supported on the current platform"; 68 "This operation is not supported on the current platform";
63 const char kErrorRequestNotSupported[] = "Request not supported"; 69 const char kErrorRequestNotSupported[] = "Request not supported";
64 const char kErrorTimeout[] = "Operation timed out"; 70 const char kErrorTimeout[] = "Operation timed out";
65 const char kErrorUnsupportedDevice[] = 71 const char kErrorUnsupportedDevice[] =
66 "This device is not supported on the current platform"; 72 "This device is not supported on the current platform";
73 const char kErrorInvalidServiceId[] = "The service ID doesn't exist.";
74 const char kErrorInvalidCharacteristicId[] =
75 "The characteristic ID doesn't exist.";
67 const char kStatusAdvertisementAlreadyExists[] = 76 const char kStatusAdvertisementAlreadyExists[] =
68 "An advertisement is already advertising"; 77 "An advertisement is already advertising";
69 const char kStatusAdvertisementDoesNotExist[] = 78 const char kStatusAdvertisementDoesNotExist[] =
70 "This advertisement does not exist"; 79 "This advertisement does not exist";
71 80
72 // Returns the correct error string based on error status |status|. This is used 81 // Returns the correct error string based on error status |status|. This is used
73 // to set the value of |chrome.runtime.lastError.message| and should not be 82 // to set the value of |chrome.runtime.lastError.message| and should not be
74 // passed |BluetoothLowEnergyEventRouter::kStatusSuccess|. 83 // passed |BluetoothLowEnergyEventRouter::kStatusSuccess|.
75 std::string StatusToString(BluetoothLowEnergyEventRouter::Status status) { 84 std::string StatusToString(BluetoothLowEnergyEventRouter::Status status) {
76 switch (status) { 85 switch (status) {
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after
218 &DoWorkCallback<bool>, 227 &DoWorkCallback<bool>,
219 base::Bind(&BluetoothLowEnergyExtensionFunctionDeprecated::DoWork, 228 base::Bind(&BluetoothLowEnergyExtensionFunctionDeprecated::DoWork,
220 this)))) { 229 this)))) {
221 SetError(kErrorAdapterNotInitialized); 230 SetError(kErrorAdapterNotInitialized);
222 return false; 231 return false;
223 } 232 }
224 233
225 return true; 234 return true;
226 } 235 }
227 236
228 BluetoothLowEnergyExtensionFunction::BluetoothLowEnergyExtensionFunction() {} 237 BluetoothLowEnergyExtensionFunction::BluetoothLowEnergyExtensionFunction()
238 : event_router_(nullptr) {}
229 239
230 BluetoothLowEnergyExtensionFunction::~BluetoothLowEnergyExtensionFunction() {} 240 BluetoothLowEnergyExtensionFunction::~BluetoothLowEnergyExtensionFunction() {}
231 241
232 ExtensionFunction::ResponseAction BluetoothLowEnergyExtensionFunction::Run() { 242 ExtensionFunction::ResponseAction BluetoothLowEnergyExtensionFunction::Run() {
233 DCHECK_CURRENTLY_ON(BrowserThread::UI); 243 DCHECK_CURRENTLY_ON(BrowserThread::UI);
234 244
235 if (!BluetoothManifestData::CheckLowEnergyPermitted(extension())) 245 if (!BluetoothManifestData::CheckLowEnergyPermitted(extension()))
236 return RespondNow(Error(kErrorPermissionDenied)); 246 return RespondNow(Error(kErrorPermissionDenied));
237 247
238 BluetoothLowEnergyEventRouter* event_router = 248 event_router_ = GetEventRouter(browser_context());
239 GetEventRouter(browser_context()); 249 if (!event_router_->IsBluetoothSupported())
240 if (!event_router->IsBluetoothSupported())
241 return RespondNow(Error(kErrorPlatformNotSupported)); 250 return RespondNow(Error(kErrorPlatformNotSupported));
242 251
243 // It is safe to pass |this| here as ExtensionFunction is refcounted. 252 // It is safe to pass |this| here as ExtensionFunction is refcounted.
244 if (!event_router->InitializeAdapterAndInvokeCallback(base::Bind( 253 if (!event_router_->InitializeAdapterAndInvokeCallback(base::Bind(
245 &DoWorkCallback<void>, 254 &DoWorkCallback<void>,
246 base::Bind(&BluetoothLowEnergyExtensionFunction::DoWork, this)))) { 255 base::Bind(&BluetoothLowEnergyExtensionFunction::PreDoWork, this)))) {
247 // DoWork will respond when the adapter gets initialized. 256 // DoWork will respond when the adapter gets initialized.
248 return RespondNow(Error(kErrorAdapterNotInitialized)); 257 return RespondNow(Error(kErrorAdapterNotInitialized));
249 } 258 }
250 259
251 return RespondLater(); 260 return RespondLater();
252 } 261 }
253 262
263 void BluetoothLowEnergyExtensionFunction::PreDoWork() {
264 // The adapter must be initialized at this point, but return an error instead
265 // of asserting.
266 if (!event_router_->HasAdapter()) {
267 Respond(Error(kErrorAdapterNotInitialized));
268 return;
269 }
270 DoWork();
271 }
272
254 template <typename Params> 273 template <typename Params>
255 BLEPeripheralExtensionFunction<Params>::BLEPeripheralExtensionFunction() {} 274 BLEPeripheralExtensionFunction<Params>::BLEPeripheralExtensionFunction() {}
256 275
257 template <typename Params> 276 template <typename Params>
258 BLEPeripheralExtensionFunction<Params>::~BLEPeripheralExtensionFunction() {} 277 BLEPeripheralExtensionFunction<Params>::~BLEPeripheralExtensionFunction() {}
259 278
260 template <typename Params> 279 template <typename Params>
261 ExtensionFunction::ResponseAction 280 ExtensionFunction::ResponseAction
262 BLEPeripheralExtensionFunction<Params>::Run() { 281 BLEPeripheralExtensionFunction<Params>::Run() {
282 DCHECK_CURRENTLY_ON(BrowserThread::UI);
283
284 // Check permissions in manifest.
285 if (!BluetoothManifestData::CheckPeripheralPermitted(extension()))
286 return RespondNow(Error(kErrorPermissionDenied));
287
263 // Causes link error on Windows. API will never be on Windows, so #ifdefing. 288 // Causes link error on Windows. API will never be on Windows, so #ifdefing.
264 #if !defined(OS_WIN) 289 #if !defined(OS_WIN)
265 params_ = Params::Create(*args_); 290 params_ = Params::Create(*args_);
266 EXTENSION_FUNCTION_VALIDATE(params_.get() != NULL); 291 EXTENSION_FUNCTION_VALIDATE(params_.get() != NULL);
267 #endif 292 #endif
268 293
269 return BluetoothLowEnergyExtensionFunction::Run(); 294 return BluetoothLowEnergyExtensionFunction::Run();
270 } 295 }
271 296
272 bool BluetoothLowEnergyConnectFunction::DoWork() { 297 bool BluetoothLowEnergyConnectFunction::DoWork() {
(...skipping 842 matching lines...) Expand 10 before | Expand all | Expand 10 after
1115 1140
1116 template class BLEPeripheralExtensionFunction<apibtle::CreateService::Params>; 1141 template class BLEPeripheralExtensionFunction<apibtle::CreateService::Params>;
1117 1142
1118 void BluetoothLowEnergyCreateServiceFunction::DoWork() { 1143 void BluetoothLowEnergyCreateServiceFunction::DoWork() {
1119 DCHECK_CURRENTLY_ON(BrowserThread::UI); 1144 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1120 // Causes link error on Windows. API will never be on Windows, so #ifdefing. 1145 // Causes link error on Windows. API will never be on Windows, so #ifdefing.
1121 // TODO: Ideally this should be handled by our feature system, so that this 1146 // TODO: Ideally this should be handled by our feature system, so that this
1122 // code doesn't even compile on OSes it isn't being used on, but currently this 1147 // code doesn't even compile on OSes it isn't being used on, but currently this
1123 // is not possible. 1148 // is not possible.
1124 #if !defined(OS_WIN) 1149 #if !defined(OS_WIN)
1125 Respond(ArgumentList(apibtle::CreateService::Results::Create(std::string()))); 1150 base::WeakPtr<device::BluetoothLocalGattService> service =
1151 device::BluetoothLocalGattService::Create(
1152 event_router_->adapter(),
1153 device::BluetoothUUID(params_->service.uuid),
1154 params_->service.is_primary, nullptr, nullptr);
1155
1156 Respond(ArgumentList(
1157 apibtle::CreateService::Results::Create(service->GetIdentifier())));
1126 #else 1158 #else
1127 Respond(Error(kErrorPlatformNotSupported)); 1159 Respond(Error(kErrorPlatformNotSupported));
1128 #endif 1160 #endif
1129 } 1161 }
1130 1162
1131 template class BLEPeripheralExtensionFunction< 1163 template class BLEPeripheralExtensionFunction<
1132 apibtle::CreateCharacteristic::Params>; 1164 apibtle::CreateCharacteristic::Params>;
1133 1165
1134 void BluetoothLowEnergyCreateCharacteristicFunction::DoWork() { 1166 void BluetoothLowEnergyCreateCharacteristicFunction::DoWork() {
1135 DCHECK_CURRENTLY_ON(BrowserThread::UI); 1167 device::BluetoothLocalGattService* service =
1136 Respond(ArgumentList( 1168 event_router_->adapter()->GetGattService(params_->service_id);
1137 apibtle::CreateCharacteristic::Results::Create(std::string()))); 1169 if (!service) {
1170 Respond(Error(kErrorInvalidServiceId));
1171 return;
1172 }
1173
1174 base::WeakPtr<device::BluetoothLocalGattCharacteristic> characteristic =
1175 device::BluetoothLocalGattCharacteristic::Create(
1176 device::BluetoothUUID(params_->characteristic.uuid),
1177 device::BluetoothGattCharacteristic::Properties(),
1178 device::BluetoothGattCharacteristic::Permissions(), service);
1179
1180 // Keep a track of this characteristic so we can look it up later if a
1181 // descriptor lists it as its parent.
1182 event_router_->AddLocalCharacteristic(characteristic->GetIdentifier(),
1183 service->GetIdentifier());
1184
1185 Respond(ArgumentList(apibtle::CreateCharacteristic::Results::Create(
1186 characteristic->GetIdentifier())));
1138 } 1187 }
1139 1188
1140 template class BLEPeripheralExtensionFunction< 1189 template class BLEPeripheralExtensionFunction<
1141 apibtle::CreateDescriptor::Params>; 1190 apibtle::CreateDescriptor::Params>;
1142 1191
1143 void BluetoothLowEnergyCreateDescriptorFunction::DoWork() { 1192 void BluetoothLowEnergyCreateDescriptorFunction::DoWork() {
1144 DCHECK_CURRENTLY_ON(BrowserThread::UI); 1193 device::BluetoothLocalGattCharacteristic* characteristic =
1145 Respond( 1194 event_router_->GetLocalCharacteristic(params_->characteristic_id);
1146 ArgumentList(apibtle::CreateDescriptor::Results::Create(std::string()))); 1195 if (!characteristic) {
1196 Respond(Error(kErrorInvalidCharacteristicId));
1197 return;
1198 }
1199
1200 base::WeakPtr<device::BluetoothLocalGattDescriptor> descriptor =
1201 device::BluetoothLocalGattDescriptor::Create(
1202 device::BluetoothUUID(params_->descriptor.uuid),
1203 device::BluetoothGattCharacteristic::Permissions(), characteristic);
1204
1205 Respond(ArgumentList(
1206 apibtle::CreateDescriptor::Results::Create(descriptor->GetIdentifier())));
1147 } 1207 }
1148 1208
1149 template class BLEPeripheralExtensionFunction<apibtle::RegisterService::Params>; 1209 template class BLEPeripheralExtensionFunction<apibtle::RegisterService::Params>;
1150 1210
1151 void BluetoothLowEnergyRegisterServiceFunction::DoWork() { 1211 void BluetoothLowEnergyRegisterServiceFunction::DoWork() {
1152 DCHECK_CURRENTLY_ON(BrowserThread::UI); 1212 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1153 Respond(ArgumentList(apibtle::RegisterService::Results::Create( 1213 Respond(ArgumentList(apibtle::RegisterService::Results::Create(
1154 apibtle::SERVICE_RESULT_SUCCESS))); 1214 apibtle::SERVICE_RESULT_SUCCESS)));
1155 } 1215 }
1156 1216
1157 template class BLEPeripheralExtensionFunction< 1217 template class BLEPeripheralExtensionFunction<
1158 apibtle::UnregisterService::Params>; 1218 apibtle::UnregisterService::Params>;
1159 1219
1160 void BluetoothLowEnergyUnregisterServiceFunction::DoWork() { 1220 void BluetoothLowEnergyUnregisterServiceFunction::DoWork() {
1161 DCHECK_CURRENTLY_ON(BrowserThread::UI); 1221 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1162 Respond(ArgumentList(apibtle::UnregisterService::Results::Create( 1222 Respond(ArgumentList(apibtle::UnregisterService::Results::Create(
1163 apibtle::SERVICE_RESULT_SUCCESS))); 1223 apibtle::SERVICE_RESULT_SUCCESS)));
1164 } 1224 }
1165 1225
1166 template class BLEPeripheralExtensionFunction< 1226 template class BLEPeripheralExtensionFunction<
1167 apibtle::SendRequestResponse::Params>; 1227 apibtle::SendRequestResponse::Params>;
1168 1228
1169 void BluetoothLowEnergySendRequestResponseFunction::DoWork() { 1229 void BluetoothLowEnergySendRequestResponseFunction::DoWork() {
1170 DCHECK_CURRENTLY_ON(BrowserThread::UI); 1230 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1171 Respond(NoArguments()); 1231 Respond(NoArguments());
1172 } 1232 }
1173 1233
1174 } // namespace api 1234 } // namespace api
1175 } // namespace extensions 1235 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698