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

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

Issue 1915243003: API Bindings for GATT server functionality. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@adapter_and_tests
Patch Set: win fix 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 <utility> 9 #include <iterator>
10 #include <vector>
10 11
11 #include "base/bind.h" 12 #include "base/bind.h"
13 #include "base/callback.h"
14 #include "base/callback_forward.h"
12 #include "base/command_line.h" 15 #include "base/command_line.h"
13 #include "base/lazy_instance.h" 16 #include "base/lazy_instance.h"
14 #include "base/strings/stringprintf.h" 17 #include "base/logging.h"
15 #include "build/build_config.h" 18 #include "base/values.h"
16 #include "chrome/browser/extensions/api/bluetooth_low_energy/bluetooth_api_adver tisement.h"
17 #include "chrome/browser/extensions/api/bluetooth_low_energy/utils.h" 19 #include "chrome/browser/extensions/api/bluetooth_low_energy/utils.h"
18 #include "chrome/common/extensions/api/bluetooth_low_energy.h" 20 #include "chrome/common/extensions/api/bluetooth_low_energy.h"
19 #include "content/public/browser/browser_thread.h" 21 #include "content/public/browser/browser_thread.h"
20 #include "extensions/browser/event_router.h" 22 #include "device/bluetooth/bluetooth_adapter.h"
21 #include "extensions/common/api/bluetooth/bluetooth_manifest_data.h" 23 #include "extensions/common/api/bluetooth/bluetooth_manifest_data.h"
22 #include "extensions/common/permissions/permissions_data.h" 24 #include "extensions/common/extension.h"
23 #include "extensions/common/switches.h" 25 #include "extensions/common/switches.h"
24 26
25 #if defined(OS_CHROMEOS) 27 #if defined(OS_CHROMEOS)
26 #include "chrome/browser/chromeos/app_mode/kiosk_app_manager.h" 28 #include "chrome/browser/chromeos/app_mode/kiosk_app_manager.h"
27 #endif 29 #endif
28 30
29 using content::BrowserContext; 31 using content::BrowserContext;
30 using content::BrowserThread; 32 using content::BrowserThread;
31 33
32 namespace apibtle = extensions::api::bluetooth_low_energy; 34 namespace apibtle = extensions::api::bluetooth_low_energy;
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
120 } 122 }
121 return ""; 123 return "";
122 } 124 }
123 125
124 extensions::BluetoothLowEnergyEventRouter* GetEventRouter( 126 extensions::BluetoothLowEnergyEventRouter* GetEventRouter(
125 BrowserContext* context) { 127 BrowserContext* context) {
126 DCHECK_CURRENTLY_ON(BrowserThread::UI); 128 DCHECK_CURRENTLY_ON(BrowserThread::UI);
127 return extensions::BluetoothLowEnergyAPI::Get(context)->event_router(); 129 return extensions::BluetoothLowEnergyAPI::Get(context)->event_router();
128 } 130 }
129 131
130 void DoWorkCallback(const base::Callback<bool()>& callback) { 132 template <typename T>
133 void DoWorkCallback(const base::Callback<T()>& callback) {
131 DCHECK(!callback.is_null()); 134 DCHECK(!callback.is_null());
132 callback.Run(); 135 callback.Run();
133 } 136 }
134 137
135 std::unique_ptr<device::BluetoothAdvertisement::ManufacturerData> 138 std::unique_ptr<device::BluetoothAdvertisement::ManufacturerData>
136 CreateManufacturerData( 139 CreateManufacturerData(
137 std::vector<apibtle::ManufacturerData>* manufacturer_data) { 140 std::vector<apibtle::ManufacturerData>* manufacturer_data) {
138 std::unique_ptr<device::BluetoothAdvertisement::ManufacturerData> 141 std::unique_ptr<device::BluetoothAdvertisement::ManufacturerData>
139 created_data(new device::BluetoothAdvertisement::ManufacturerData()); 142 created_data(new device::BluetoothAdvertisement::ManufacturerData());
140 for (const auto& it : *manufacturer_data) { 143 for (const auto& it : *manufacturer_data) {
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
182 185
183 BluetoothLowEnergyAPI::~BluetoothLowEnergyAPI() { 186 BluetoothLowEnergyAPI::~BluetoothLowEnergyAPI() {
184 } 187 }
185 188
186 void BluetoothLowEnergyAPI::Shutdown() { 189 void BluetoothLowEnergyAPI::Shutdown() {
187 DCHECK_CURRENTLY_ON(BrowserThread::UI); 190 DCHECK_CURRENTLY_ON(BrowserThread::UI);
188 } 191 }
189 192
190 namespace api { 193 namespace api {
191 194
192 BluetoothLowEnergyExtensionFunction::BluetoothLowEnergyExtensionFunction() { 195 BluetoothLowEnergyExtensionFunctionDeprecated::
193 } 196 BluetoothLowEnergyExtensionFunctionDeprecated() {}
194 197
195 BluetoothLowEnergyExtensionFunction::~BluetoothLowEnergyExtensionFunction() { 198 BluetoothLowEnergyExtensionFunctionDeprecated::
196 } 199 ~BluetoothLowEnergyExtensionFunctionDeprecated() {}
197 200
198 bool BluetoothLowEnergyExtensionFunction::RunAsync() { 201 bool BluetoothLowEnergyExtensionFunctionDeprecated::RunAsync() {
199 DCHECK_CURRENTLY_ON(BrowserThread::UI); 202 DCHECK_CURRENTLY_ON(BrowserThread::UI);
200 203
201 if (!BluetoothManifestData::CheckLowEnergyPermitted(extension())) { 204 if (!BluetoothManifestData::CheckLowEnergyPermitted(extension())) {
202 error_ = kErrorPermissionDenied; 205 error_ = kErrorPermissionDenied;
203 return false; 206 return false;
204 } 207 }
205 208
206 BluetoothLowEnergyEventRouter* event_router = 209 BluetoothLowEnergyEventRouter* event_router =
207 GetEventRouter(browser_context()); 210 GetEventRouter(browser_context());
208 if (!event_router->IsBluetoothSupported()) { 211 if (!event_router->IsBluetoothSupported()) {
209 SetError(kErrorPlatformNotSupported); 212 SetError(kErrorPlatformNotSupported);
210 return false; 213 return false;
211 } 214 }
212 215
213 // It is safe to pass |this| here as ExtensionFunction is refcounted. 216 // It is safe to pass |this| here as ExtensionFunction is refcounted.
214 if (!event_router->InitializeAdapterAndInvokeCallback(base::Bind( 217 if (!event_router->InitializeAdapterAndInvokeCallback(base::Bind(
215 &DoWorkCallback, 218 &DoWorkCallback<bool>,
216 base::Bind(&BluetoothLowEnergyExtensionFunction::DoWork, this)))) { 219 base::Bind(&BluetoothLowEnergyExtensionFunctionDeprecated::DoWork,
220 this)))) {
217 SetError(kErrorAdapterNotInitialized); 221 SetError(kErrorAdapterNotInitialized);
218 return false; 222 return false;
219 } 223 }
220 224
221 return true; 225 return true;
222 } 226 }
223 227
228 BluetoothLowEnergyExtensionFunction::BluetoothLowEnergyExtensionFunction() {}
229
230 BluetoothLowEnergyExtensionFunction::~BluetoothLowEnergyExtensionFunction() {}
231
232 ExtensionFunction::ResponseAction BluetoothLowEnergyExtensionFunction::Run() {
233 DCHECK_CURRENTLY_ON(BrowserThread::UI);
234
235 if (!BluetoothManifestData::CheckLowEnergyPermitted(extension())) {
236 return RespondNow(Error(kErrorPermissionDenied));
237 }
238
239 BluetoothLowEnergyEventRouter* event_router =
240 GetEventRouter(browser_context());
241 if (!event_router->IsBluetoothSupported()) {
242 return RespondNow(Error(kErrorPlatformNotSupported));
243 }
244
245 // It is safe to pass |this| here as ExtensionFunction is refcounted.
246 if (!event_router->InitializeAdapterAndInvokeCallback(base::Bind(
247 &DoWorkCallback<void>,
248 base::Bind(&BluetoothLowEnergyExtensionFunction::DoWork, this)))) {
249 return RespondNow(Error(kErrorAdapterNotInitialized));
250 }
251
252 return RespondLater();
253 }
254
224 bool BluetoothLowEnergyConnectFunction::DoWork() { 255 bool BluetoothLowEnergyConnectFunction::DoWork() {
225 DCHECK_CURRENTLY_ON(BrowserThread::UI); 256 DCHECK_CURRENTLY_ON(BrowserThread::UI);
226 257
227 BluetoothLowEnergyEventRouter* event_router = 258 BluetoothLowEnergyEventRouter* event_router =
228 GetEventRouter(browser_context()); 259 GetEventRouter(browser_context());
229 260
230 // The adapter must be initialized at this point, but return an error instead 261 // The adapter must be initialized at this point, but return an error instead
231 // of asserting. 262 // of asserting.
232 if (!event_router->HasAdapter()) { 263 if (!event_router->HasAdapter()) {
233 SetError(kErrorAdapterNotInitialized); 264 SetError(kErrorAdapterNotInitialized);
(...skipping 633 matching lines...) Expand 10 before | Expand all | Expand 10 after
867 } 898 }
868 899
869 void BluetoothLowEnergyAdvertisementFunction::RemoveAdvertisement( 900 void BluetoothLowEnergyAdvertisementFunction::RemoveAdvertisement(
870 int advertisement_id) { 901 int advertisement_id) {
871 DCHECK(advertisements_manager_); 902 DCHECK(advertisements_manager_);
872 advertisements_manager_->Remove(extension_id(), advertisement_id); 903 advertisements_manager_->Remove(extension_id(), advertisement_id);
873 } 904 }
874 905
875 bool BluetoothLowEnergyAdvertisementFunction::RunAsync() { 906 bool BluetoothLowEnergyAdvertisementFunction::RunAsync() {
876 Initialize(); 907 Initialize();
877 return BluetoothLowEnergyExtensionFunction::RunAsync(); 908 return BluetoothLowEnergyExtensionFunctionDeprecated::RunAsync();
878 } 909 }
879 910
880 void BluetoothLowEnergyAdvertisementFunction::Initialize() { 911 void BluetoothLowEnergyAdvertisementFunction::Initialize() {
881 advertisements_manager_ = 912 advertisements_manager_ =
882 ApiResourceManager<BluetoothApiAdvertisement>::Get(browser_context()); 913 ApiResourceManager<BluetoothApiAdvertisement>::Get(browser_context());
883 } 914 }
884 915
885 static bool IsAutoLaunchedKioskApp(const ExtensionId& id) { 916 static bool IsAutoLaunchedKioskApp(const ExtensionId& id) {
886 #if defined(OS_CHROMEOS) 917 #if defined(OS_CHROMEOS)
887 chromeos::KioskAppManager::App app_info; 918 chromeos::KioskAppManager::App app_info;
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after
1058 case device::BluetoothAdvertisement::ErrorCode:: 1089 case device::BluetoothAdvertisement::ErrorCode::
1059 ERROR_ADVERTISEMENT_DOES_NOT_EXIST: 1090 ERROR_ADVERTISEMENT_DOES_NOT_EXIST:
1060 SetError(kStatusAdvertisementDoesNotExist); 1091 SetError(kStatusAdvertisementDoesNotExist);
1061 break; 1092 break;
1062 default: 1093 default:
1063 SetError(kErrorOperationFailed); 1094 SetError(kErrorOperationFailed);
1064 } 1095 }
1065 SendResponse(false); 1096 SendResponse(false);
1066 } 1097 }
1067 1098
1099 void BluetoothLowEnergyCreateServiceFunction::DoWork() {
1100 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1101 // Using apibtle::CreateService::Results::Create causes a link error on
1102 // Windows for some reason, despite it being almost identical to its
1103 // characteristic and descriptor counterparts. Since there is no plan to
1104 // provide this API on Windows, ifdef'ing this out is fine.
1105 #if !defined(OS_WIN)
1106 Respond(ArgumentList(apibtle::CreateService::Results::Create(std::string())));
Devlin 2016/04/29 17:13:13 What's the error? This isn't quite fine, because
rkc 2016/04/29 20:15:58 Added to the permissions, this function will only
1107 #endif
1108 }
1109
1110 void BluetoothLowEnergyCreateCharacteristicFunction::DoWork() {
1111 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1112 Respond(ArgumentList(
1113 apibtle::CreateCharacteristic::Results::Create(std::string())));
1114 }
1115
1116 void BluetoothLowEnergyCreateDescriptorFunction::DoWork() {
1117 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1118 Respond(
1119 ArgumentList(apibtle::CreateDescriptor::Results::Create(std::string())));
1120 }
1121
1122 void BluetoothLowEnergyRegisterServiceFunction::DoWork() {
1123 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1124 Respond(ArgumentList(apibtle::RegisterService::Results::Create(
1125 apibtle::SERVICE_RESULT_SUCCESS)));
1126 }
1127
1128 void BluetoothLowEnergyUnregisterServiceFunction::DoWork() {
1129 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1130 Respond(ArgumentList(apibtle::UnregisterService::Results::Create(
1131 apibtle::SERVICE_RESULT_SUCCESS)));
1132 }
1133
1134 void BluetoothLowEnergySendRequestResponseFunction::DoWork() {
1135 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1136 Respond(NoArguments());
1137 }
1138
1068 } // namespace api 1139 } // namespace api
1069 } // namespace extensions 1140 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698