OLD | NEW |
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 "base/bind.h" |
| 8 #include "base/lazy_instance.h" |
| 9 #include "base/strings/stringprintf.h" |
| 10 #include "chrome/browser/extensions/api/bluetooth_low_energy/bluetooth_low_energ
y_event_router.h" |
| 11 #include "chrome/common/extensions/api/bluetooth_low_energy.h" |
| 12 #include "content/public/browser/browser_thread.h" |
| 13 #include "extensions/browser/event_router.h" |
| 14 |
| 15 using content::BrowserContext; |
| 16 using content::BrowserThread; |
| 17 |
| 18 namespace apibtle = extensions::api::bluetooth_low_energy; |
| 19 |
| 20 namespace { |
| 21 |
| 22 const char kErrorAdapterNotInitialized[] = |
| 23 "Could not initialize Bluetooth adapter."; |
| 24 const char kErrorDeviceNotFoundFormat[] = |
| 25 "Device with address \"%s\" not found."; |
| 26 const char kErrorServiceNotFoundFormat[] = "Service with ID \"%s\" not found."; |
| 27 const char kErrorPlatformNotSupported[] = |
| 28 "This operation is not supported on the current platform"; |
| 29 |
| 30 extensions::BluetoothLowEnergyEventRouter* GetEventRouter( |
| 31 BrowserContext* context) { |
| 32 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 33 return extensions::BluetoothLowEnergyAPI::Get(context)->event_router(); |
| 34 } |
| 35 |
| 36 void DoWorkCallback(const base::Callback<bool()>& callback) { |
| 37 DCHECK(!callback.is_null()); |
| 38 callback.Run(); |
| 39 } |
| 40 |
| 41 } // namespace |
| 42 |
7 namespace extensions { | 43 namespace extensions { |
| 44 |
| 45 static base::LazyInstance<BrowserContextKeyedAPIFactory<BluetoothLowEnergyAPI> > |
| 46 g_factory = LAZY_INSTANCE_INITIALIZER; |
| 47 |
| 48 // static |
| 49 BrowserContextKeyedAPIFactory<BluetoothLowEnergyAPI>* |
| 50 BluetoothLowEnergyAPI::GetFactoryInstance() { |
| 51 return g_factory.Pointer(); |
| 52 } |
| 53 |
| 54 // static |
| 55 BluetoothLowEnergyAPI* BluetoothLowEnergyAPI::Get(BrowserContext* context) { |
| 56 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 57 return GetFactoryInstance()->Get(context); |
| 58 } |
| 59 |
| 60 BluetoothLowEnergyAPI::BluetoothLowEnergyAPI(BrowserContext* context) |
| 61 : event_router_(new BluetoothLowEnergyEventRouter(context)), |
| 62 browser_context_(context) { |
| 63 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 64 } |
| 65 |
| 66 BluetoothLowEnergyAPI::~BluetoothLowEnergyAPI() { |
| 67 } |
| 68 |
| 69 void BluetoothLowEnergyAPI::Shutdown() { |
| 70 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 71 } |
| 72 |
8 namespace api { | 73 namespace api { |
9 | 74 |
10 bool BluetoothLowEnergyGetServiceFunction::RunImpl() { | 75 BluetoothLowEnergyExtensionFunction::BluetoothLowEnergyExtensionFunction() { |
11 // TODO(armansito): Implement. | 76 } |
12 SetError("Call not supported."); | 77 |
13 return false; | 78 BluetoothLowEnergyExtensionFunction::~BluetoothLowEnergyExtensionFunction() { |
14 } | 79 } |
15 | 80 |
16 bool BluetoothLowEnergyGetServicesFunction::RunImpl() { | 81 bool BluetoothLowEnergyExtensionFunction::RunImpl() { |
17 // TODO(armansito): Implement. | 82 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
18 SetError("Call not supported."); | 83 |
19 return false; | 84 BluetoothLowEnergyEventRouter* event_router = |
20 } | 85 GetEventRouter(browser_context()); |
21 | 86 if (!event_router->IsBluetoothSupported()) { |
22 bool BluetoothLowEnergyGetCharacteristicFunction::RunImpl() { | 87 SetError(kErrorPlatformNotSupported); |
23 // TODO(armansito): Implement. | 88 return false; |
24 SetError("Call not supported."); | 89 } |
25 return false; | 90 |
26 } | 91 // It is safe to pass |this| here as ExtensionFunction is refcounted. |
27 | 92 if (!event_router->InitializeAdapterAndInvokeCallback(base::Bind( |
28 bool BluetoothLowEnergyGetCharacteristicsFunction::RunImpl() { | 93 &DoWorkCallback, |
29 // TODO(armansito): Implement. | 94 base::Bind(&BluetoothLowEnergyExtensionFunction::DoWork, this)))) { |
30 SetError("Call not supported."); | 95 SetError(kErrorAdapterNotInitialized); |
31 return false; | 96 return false; |
32 } | 97 } |
33 | 98 |
34 bool BluetoothLowEnergyGetIncludedServicesFunction::RunImpl() { | 99 return true; |
35 // TODO(armansito): Implement. | 100 } |
36 SetError("Call not supported."); | 101 |
37 return false; | 102 bool BluetoothLowEnergyGetServiceFunction::DoWork() { |
38 } | 103 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
39 | 104 |
40 bool BluetoothLowEnergyGetDescriptorFunction::RunImpl() { | 105 BluetoothLowEnergyEventRouter* event_router = |
41 // TODO(armansito): Implement. | 106 GetEventRouter(browser_context()); |
42 SetError("Call not supported."); | 107 |
43 return false; | 108 // The adapter must be initialized at this point, but return an error instead |
44 } | 109 // of asserting. |
45 | 110 if (!event_router->HasAdapter()) { |
46 bool BluetoothLowEnergyGetDescriptorsFunction::RunImpl() { | 111 SetError(kErrorAdapterNotInitialized); |
47 // TODO(armansito): Implement. | 112 SendResponse(false); |
48 SetError("Call not supported."); | 113 return false; |
49 return false; | 114 } |
50 } | 115 |
51 | 116 scoped_ptr<apibtle::GetService::Params> params( |
52 bool BluetoothLowEnergyReadCharacteristicValueFunction::RunImpl() { | 117 apibtle::GetService::Params::Create(*args_)); |
53 // TODO(armansito): Implement. | 118 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); |
54 SetError("Call not supported."); | 119 |
55 return false; | 120 std::string service_id = params->service_id; |
56 } | 121 |
57 | 122 apibtle::Service service; |
58 bool BluetoothLowEnergyWriteCharacteristicValueFunction::RunImpl() { | 123 if (!event_router->GetService(service_id, &service)) { |
59 // TODO(armansito): Implement. | 124 SetError( |
60 SetError("Call not supported."); | 125 base::StringPrintf(kErrorServiceNotFoundFormat, service_id.c_str())); |
61 return false; | 126 SendResponse(false); |
62 } | 127 return false; |
63 | 128 } |
64 bool BluetoothLowEnergyReadDescriptorValueFunction::RunImpl() { | 129 |
65 // TODO(armansito): Implement. | 130 results_ = apibtle::GetService::Results::Create(service); |
66 SetError("Call not supported."); | 131 SendResponse(true); |
67 return false; | 132 |
68 } | 133 return true; |
69 | 134 } |
70 bool BluetoothLowEnergyWriteDescriptorValueFunction::RunImpl() { | 135 |
71 // TODO(armansito): Implement. | 136 bool BluetoothLowEnergyGetServicesFunction::DoWork() { |
72 SetError("Call not supported."); | 137 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
73 return false; | 138 |
74 } | 139 BluetoothLowEnergyEventRouter* event_router = |
75 | 140 GetEventRouter(browser_context()); |
| 141 |
| 142 // The adapter must be initialized at this point, but return an error instead |
| 143 // of asserting. |
| 144 if (!event_router->HasAdapter()) { |
| 145 SetError(kErrorAdapterNotInitialized); |
| 146 SendResponse(false); |
| 147 return false; |
| 148 } |
| 149 |
| 150 scoped_ptr<apibtle::GetServices::Params> params( |
| 151 apibtle::GetServices::Params::Create(*args_)); |
| 152 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); |
| 153 |
| 154 std::string device_address = params->device_address; |
| 155 |
| 156 BluetoothLowEnergyEventRouter::ServiceList service_list; |
| 157 if (!event_router->GetServices(device_address, &service_list)) { |
| 158 SetError( |
| 159 base::StringPrintf(kErrorDeviceNotFoundFormat, device_address.c_str())); |
| 160 SendResponse(false); |
| 161 return false; |
| 162 } |
| 163 |
| 164 results_ = apibtle::GetServices::Results::Create(service_list).Pass(); |
| 165 SendResponse(true); |
| 166 |
| 167 return true; |
| 168 } |
| 169 |
| 170 bool BluetoothLowEnergyGetCharacteristicFunction::DoWork() { |
| 171 // TODO(armansito): Implement. |
| 172 SetError("Call not supported."); |
| 173 SendResponse(false); |
| 174 return false; |
| 175 } |
| 176 |
| 177 bool BluetoothLowEnergyGetCharacteristicsFunction::DoWork() { |
| 178 // TODO(armansito): Implement. |
| 179 SetError("Call not supported."); |
| 180 SendResponse(false); |
| 181 return false; |
| 182 } |
| 183 |
| 184 bool BluetoothLowEnergyGetIncludedServicesFunction::DoWork() { |
| 185 // TODO(armansito): Implement. |
| 186 SetError("Call not supported."); |
| 187 SendResponse(false); |
| 188 return false; |
| 189 } |
| 190 |
| 191 bool BluetoothLowEnergyGetDescriptorFunction::DoWork() { |
| 192 // TODO(armansito): Implement. |
| 193 SetError("Call not supported."); |
| 194 SendResponse(false); |
| 195 return false; |
| 196 } |
| 197 |
| 198 bool BluetoothLowEnergyGetDescriptorsFunction::DoWork() { |
| 199 // TODO(armansito): Implement. |
| 200 SetError("Call not supported."); |
| 201 SendResponse(false); |
| 202 return false; |
| 203 } |
| 204 |
| 205 bool BluetoothLowEnergyReadCharacteristicValueFunction::DoWork() { |
| 206 // TODO(armansito): Implement. |
| 207 SetError("Call not supported."); |
| 208 SendResponse(false); |
| 209 return false; |
| 210 } |
| 211 |
| 212 bool BluetoothLowEnergyWriteCharacteristicValueFunction::DoWork() { |
| 213 // TODO(armansito): Implement. |
| 214 SetError("Call not supported."); |
| 215 SendResponse(false); |
| 216 return false; |
| 217 } |
| 218 |
| 219 bool BluetoothLowEnergyReadDescriptorValueFunction::DoWork() { |
| 220 // TODO(armansito): Implement. |
| 221 SetError("Call not supported."); |
| 222 SendResponse(false); |
| 223 return false; |
| 224 } |
| 225 |
| 226 bool BluetoothLowEnergyWriteDescriptorValueFunction::DoWork() { |
| 227 // TODO(armansito): Implement. |
| 228 SetError("Call not supported."); |
| 229 SendResponse(false); |
| 230 return false; |
| 231 } |
| 232 |
76 } // namespace api | 233 } // namespace api |
77 } // namespace extensions | 234 } // namespace extensions |
OLD | NEW |