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

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: 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 void DoWorkReturnBoolCallback(const base::Callback<bool()>& callback) {
131 DCHECK(!callback.is_null()); 133 DCHECK(!callback.is_null());
132 callback.Run(); 134 callback.Run();
133 } 135 }
136
137 void DoWorkCallback(const base::Callback<void()>& callback) {
138 DCHECK(!callback.is_null());
139 callback.Run();
140 }
134 141
135 std::unique_ptr<device::BluetoothAdvertisement::ManufacturerData> 142 std::unique_ptr<device::BluetoothAdvertisement::ManufacturerData>
136 CreateManufacturerData( 143 CreateManufacturerData(
137 std::vector<apibtle::ManufacturerData>* manufacturer_data) { 144 std::vector<apibtle::ManufacturerData>* manufacturer_data) {
138 std::unique_ptr<device::BluetoothAdvertisement::ManufacturerData> 145 std::unique_ptr<device::BluetoothAdvertisement::ManufacturerData>
139 created_data(new device::BluetoothAdvertisement::ManufacturerData()); 146 created_data(new device::BluetoothAdvertisement::ManufacturerData());
140 for (const auto& it : *manufacturer_data) { 147 for (const auto& it : *manufacturer_data) {
141 std::vector<uint8_t> data(it.data.size()); 148 std::vector<uint8_t> data(it.data.size());
142 std::copy(it.data.begin(), it.data.end(), data.begin()); 149 std::copy(it.data.begin(), it.data.end(), data.begin());
143 (*created_data)[it.id] = data; 150 (*created_data)[it.id] = data;
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
182 189
183 BluetoothLowEnergyAPI::~BluetoothLowEnergyAPI() { 190 BluetoothLowEnergyAPI::~BluetoothLowEnergyAPI() {
184 } 191 }
185 192
186 void BluetoothLowEnergyAPI::Shutdown() { 193 void BluetoothLowEnergyAPI::Shutdown() {
187 DCHECK_CURRENTLY_ON(BrowserThread::UI); 194 DCHECK_CURRENTLY_ON(BrowserThread::UI);
188 } 195 }
189 196
190 namespace api { 197 namespace api {
191 198
192 BluetoothLowEnergyExtensionFunction::BluetoothLowEnergyExtensionFunction() { 199 BluetoothLowEnergyExtensionFunctionDeprecated::
193 } 200 BluetoothLowEnergyExtensionFunctionDeprecated() {}
194 201
195 BluetoothLowEnergyExtensionFunction::~BluetoothLowEnergyExtensionFunction() { 202 BluetoothLowEnergyExtensionFunctionDeprecated::
196 } 203 ~BluetoothLowEnergyExtensionFunctionDeprecated() {}
197 204
198 bool BluetoothLowEnergyExtensionFunction::RunAsync() { 205 bool BluetoothLowEnergyExtensionFunctionDeprecated::RunAsync() {
199 DCHECK_CURRENTLY_ON(BrowserThread::UI); 206 DCHECK_CURRENTLY_ON(BrowserThread::UI);
200 207
201 if (!BluetoothManifestData::CheckLowEnergyPermitted(extension())) { 208 if (!BluetoothManifestData::CheckLowEnergyPermitted(extension())) {
202 error_ = kErrorPermissionDenied; 209 error_ = kErrorPermissionDenied;
203 return false; 210 return false;
204 } 211 }
205 212
206 BluetoothLowEnergyEventRouter* event_router = 213 BluetoothLowEnergyEventRouter* event_router =
207 GetEventRouter(browser_context()); 214 GetEventRouter(browser_context());
208 if (!event_router->IsBluetoothSupported()) { 215 if (!event_router->IsBluetoothSupported()) {
209 SetError(kErrorPlatformNotSupported); 216 SetError(kErrorPlatformNotSupported);
210 return false; 217 return false;
211 } 218 }
212 219
213 // It is safe to pass |this| here as ExtensionFunction is refcounted. 220 // It is safe to pass |this| here as ExtensionFunction is refcounted.
214 if (!event_router->InitializeAdapterAndInvokeCallback(base::Bind( 221 if (!event_router->InitializeAdapterAndInvokeCallback(base::Bind(
215 &DoWorkCallback, 222 &DoWorkReturnBoolCallback,
216 base::Bind(&BluetoothLowEnergyExtensionFunction::DoWork, this)))) { 223 base::Bind(&BluetoothLowEnergyExtensionFunctionDeprecated::DoWork,
224 this)))) {
217 SetError(kErrorAdapterNotInitialized); 225 SetError(kErrorAdapterNotInitialized);
218 return false; 226 return false;
219 } 227 }
220 228
221 return true; 229 return true;
222 } 230 }
223 231
232 BluetoothLowEnergyExtensionFunction::BluetoothLowEnergyExtensionFunction() {}
233
234 BluetoothLowEnergyExtensionFunction::~BluetoothLowEnergyExtensionFunction() {}
235
236 ExtensionFunction::ResponseAction BluetoothLowEnergyExtensionFunction::Run() {
237 DCHECK_CURRENTLY_ON(BrowserThread::UI);
238
239 if (!BluetoothManifestData::CheckLowEnergyPermitted(extension())) {
240 return RespondNow(Error(kErrorPermissionDenied));
241 }
242
243 BluetoothLowEnergyEventRouter* event_router =
244 GetEventRouter(browser_context());
245 if (!event_router->IsBluetoothSupported()) {
246 return RespondNow(Error(kErrorPlatformNotSupported));
247 }
248
249 // It is safe to pass |this| here as ExtensionFunction is refcounted.
250 if (!event_router->InitializeAdapterAndInvokeCallback(base::Bind(
251 &DoWorkCallback,
252 base::Bind(&BluetoothLowEnergyExtensionFunction::DoWork, this)))) {
253 return RespondNow(Error(kErrorAdapterNotInitialized));
254 }
255
256 return RespondLater();
257 }
258
224 bool BluetoothLowEnergyConnectFunction::DoWork() { 259 bool BluetoothLowEnergyConnectFunction::DoWork() {
225 DCHECK_CURRENTLY_ON(BrowserThread::UI); 260 DCHECK_CURRENTLY_ON(BrowserThread::UI);
226 261
227 BluetoothLowEnergyEventRouter* event_router = 262 BluetoothLowEnergyEventRouter* event_router =
228 GetEventRouter(browser_context()); 263 GetEventRouter(browser_context());
229 264
230 // The adapter must be initialized at this point, but return an error instead 265 // The adapter must be initialized at this point, but return an error instead
231 // of asserting. 266 // of asserting.
232 if (!event_router->HasAdapter()) { 267 if (!event_router->HasAdapter()) {
233 SetError(kErrorAdapterNotInitialized); 268 SetError(kErrorAdapterNotInitialized);
(...skipping 633 matching lines...) Expand 10 before | Expand all | Expand 10 after
867 } 902 }
868 903
869 void BluetoothLowEnergyAdvertisementFunction::RemoveAdvertisement( 904 void BluetoothLowEnergyAdvertisementFunction::RemoveAdvertisement(
870 int advertisement_id) { 905 int advertisement_id) {
871 DCHECK(advertisements_manager_); 906 DCHECK(advertisements_manager_);
872 advertisements_manager_->Remove(extension_id(), advertisement_id); 907 advertisements_manager_->Remove(extension_id(), advertisement_id);
873 } 908 }
874 909
875 bool BluetoothLowEnergyAdvertisementFunction::RunAsync() { 910 bool BluetoothLowEnergyAdvertisementFunction::RunAsync() {
876 Initialize(); 911 Initialize();
877 return BluetoothLowEnergyExtensionFunction::RunAsync(); 912 return BluetoothLowEnergyExtensionFunctionDeprecated::RunAsync();
878 } 913 }
879 914
880 void BluetoothLowEnergyAdvertisementFunction::Initialize() { 915 void BluetoothLowEnergyAdvertisementFunction::Initialize() {
881 advertisements_manager_ = 916 advertisements_manager_ =
882 ApiResourceManager<BluetoothApiAdvertisement>::Get(browser_context()); 917 ApiResourceManager<BluetoothApiAdvertisement>::Get(browser_context());
883 } 918 }
884 919
885 static bool IsAutoLaunchedKioskApp(const ExtensionId& id) { 920 static bool IsAutoLaunchedKioskApp(const ExtensionId& id) {
886 #if defined(OS_CHROMEOS) 921 #if defined(OS_CHROMEOS)
887 chromeos::KioskAppManager::App app_info; 922 chromeos::KioskAppManager::App app_info;
888 return chromeos::KioskAppManager::Get()->GetApp(id, &app_info) && 923 return chromeos::KioskAppManager::Get()->GetApp(id, &app_info) &&
889 app_info.was_auto_launched_with_zero_delay; 924 app_info.was_auto_launched_with_zero_delay;
890 #else 925 #else
891 return false; 926 return false;
892 #endif 927 #endif
893 } 928 }
894 929
895 static bool IsPeripheralFlagEnabled() { 930 static bool IsPeripheralFlagEnabled() {
896 return base::CommandLine::ForCurrentProcess()->HasSwitch( 931 return base::CommandLine::ForCurrentProcess()->HasSwitch(
897 switches::kEnableBLEAdvertising); 932 switches::kEnableBLEAdvertising);
898 } 933 }
899 934
935 bool BluetoothLowEnergyRegisterServiceFunction::DoWork() {
936 DCHECK_CURRENTLY_ON(BrowserThread::UI);
937 results_ = apibtle::RegisterService::Results::Create(
938 apibtle::SERVICE_RESULT_SUCCESS);
939 SendResponse(true);
940 return true;
941 }
942
943 bool BluetoothLowEnergyUnregisterServiceFunction::DoWork() {
944 DCHECK_CURRENTLY_ON(BrowserThread::UI);
945 results_ = apibtle::UnregisterService::Results::Create(
946 apibtle::SERVICE_RESULT_SUCCESS);
947 SendResponse(true);
948 return true;
949 }
950
900 // RegisterAdvertisement: 951 // RegisterAdvertisement:
901 952
902 bool BluetoothLowEnergyRegisterAdvertisementFunction::DoWork() { 953 bool BluetoothLowEnergyRegisterAdvertisementFunction::DoWork() {
903 DCHECK_CURRENTLY_ON(BrowserThread::UI); 954 DCHECK_CURRENTLY_ON(BrowserThread::UI);
904 955
905 // Check permissions in manifest. 956 // Check permissions in manifest.
906 if (!BluetoothManifestData::CheckPeripheralPermitted(extension())) { 957 if (!BluetoothManifestData::CheckPeripheralPermitted(extension())) {
907 error_ = kErrorPermissionDenied; 958 error_ = kErrorPermissionDenied;
908 SendResponse(false); 959 SendResponse(false);
909 return false; 960 return false;
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after
1058 case device::BluetoothAdvertisement::ErrorCode:: 1109 case device::BluetoothAdvertisement::ErrorCode::
1059 ERROR_ADVERTISEMENT_DOES_NOT_EXIST: 1110 ERROR_ADVERTISEMENT_DOES_NOT_EXIST:
1060 SetError(kStatusAdvertisementDoesNotExist); 1111 SetError(kStatusAdvertisementDoesNotExist);
1061 break; 1112 break;
1062 default: 1113 default:
1063 SetError(kErrorOperationFailed); 1114 SetError(kErrorOperationFailed);
1064 } 1115 }
1065 SendResponse(false); 1116 SendResponse(false);
1066 } 1117 }
1067 1118
1119 void BluetoothLowEnergyCreateServiceFunction::DoWork() {
1120 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1121 Respond(ArgumentList(apibtle::CreateService::Results::Create(std::string())));
1122 }
1123
1124 void BluetoothLowEnergyCreateCharacteristicFunction::DoWork() {
1125 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1126 Respond(ArgumentList(
1127 apibtle::CreateCharacteristic::Results::Create(std::string())));
1128 }
1129
1130 void BluetoothLowEnergyCreateDescriptorFunction::DoWork() {
1131 DCHECK_CURRENTLY_ON(BrowserThread::UI);
1132 Respond(
1133 ArgumentList(apibtle::CreateDescriptor::Results::Create(std::string())));
1134 }
1135
1068 } // namespace api 1136 } // namespace api
1069 } // namespace extensions 1137 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698