Chromium Code Reviews| 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 } // namespace | |
| 37 | |
| 7 namespace extensions { | 38 namespace extensions { |
| 39 | |
| 40 static base::LazyInstance<BrowserContextKeyedAPIFactory<BluetoothLowEnergyAPI> > | |
| 41 g_factory = LAZY_INSTANCE_INITIALIZER; | |
| 42 | |
| 43 // static | |
| 44 BrowserContextKeyedAPIFactory<BluetoothLowEnergyAPI>* | |
| 45 BluetoothLowEnergyAPI::GetFactoryInstance() { | |
| 46 return g_factory.Pointer(); | |
| 47 } | |
| 48 | |
| 49 // static | |
| 50 BluetoothLowEnergyAPI* BluetoothLowEnergyAPI::Get(BrowserContext* context) { | |
| 51 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 52 return GetFactoryInstance()->Get(context); | |
| 53 } | |
| 54 | |
| 55 BluetoothLowEnergyAPI::BluetoothLowEnergyAPI(BrowserContext* context) | |
| 56 : event_router_(new BluetoothLowEnergyEventRouter(context)), | |
| 57 browser_context_(context) { | |
| 58 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 59 } | |
| 60 | |
| 61 BluetoothLowEnergyAPI::~BluetoothLowEnergyAPI() { | |
| 62 } | |
| 63 | |
| 64 void BluetoothLowEnergyAPI::Shutdown() { | |
| 65 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 66 } | |
| 67 | |
| 8 namespace api { | 68 namespace api { |
| 9 | 69 |
| 10 bool BluetoothLowEnergyGetServiceFunction::RunImpl() { | 70 BluetoothLowEnergyExtensionFunction::BluetoothLowEnergyExtensionFunction() |
| 11 // TODO(armansito): Implement. | 71 : weak_ptr_factory_(this) { |
| 12 SetError("Call not supported."); | 72 } |
| 13 return false; | 73 |
| 14 } | 74 BluetoothLowEnergyExtensionFunction::~BluetoothLowEnergyExtensionFunction() { |
| 15 | 75 } |
| 16 bool BluetoothLowEnergyGetServicesFunction::RunImpl() { | 76 |
| 17 // TODO(armansito): Implement. | 77 bool BluetoothLowEnergyExtensionFunction::RunImpl() { |
| 18 SetError("Call not supported."); | 78 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 19 return false; | 79 |
| 20 } | 80 BluetoothLowEnergyEventRouter* event_router = |
| 21 | 81 GetEventRouter(browser_context()); |
| 22 bool BluetoothLowEnergyGetCharacteristicFunction::RunImpl() { | 82 if (!event_router->IsBluetoothSupported()) { |
| 23 // TODO(armansito): Implement. | 83 SetError(kErrorPlatformNotSupported); |
| 24 SetError("Call not supported."); | 84 return false; |
| 25 return false; | 85 } |
| 26 } | 86 |
| 27 | 87 if (!event_router->InitializeAdapterAndInvokeCallback( |
| 28 bool BluetoothLowEnergyGetCharacteristicsFunction::RunImpl() { | 88 base::Bind(&BluetoothLowEnergyExtensionFunction::DoWork, |
| 29 // TODO(armansito): Implement. | 89 weak_ptr_factory_.GetWeakPtr()))) { |
|
rpaquay
2014/04/24 20:38:45
Does this work? I would think you should pass "thi
armansito
2014/04/24 22:02:00
I'm confused, if you actually pass "this" here and
rpaquay
2014/04/24 22:12:46
AFAIK:
1) If you pass "this", Bind calls "this->A
armansito
2014/04/24 22:38:33
Ah got it, I didn't realize that ExtensionFunction
| |
| 30 SetError("Call not supported."); | 90 SetError(kErrorAdapterNotInitialized); |
| 31 return false; | 91 return false; |
| 32 } | 92 } |
| 33 | 93 |
| 34 bool BluetoothLowEnergyGetIncludedServicesFunction::RunImpl() { | 94 return true; |
| 35 // TODO(armansito): Implement. | 95 } |
| 36 SetError("Call not supported."); | 96 |
| 37 return false; | 97 void BluetoothLowEnergyGetServiceFunction::DoWork() { |
| 38 } | 98 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 39 | 99 |
| 40 bool BluetoothLowEnergyGetDescriptorFunction::RunImpl() { | 100 BluetoothLowEnergyEventRouter* event_router = |
| 41 // TODO(armansito): Implement. | 101 GetEventRouter(browser_context()); |
| 42 SetError("Call not supported."); | 102 |
| 43 return false; | 103 // The adapter must be initialized at this point, but return an error instead |
| 44 } | 104 // of asserting. |
| 45 | 105 if (!event_router->HasAdapter()) { |
| 46 bool BluetoothLowEnergyGetDescriptorsFunction::RunImpl() { | 106 SetError(kErrorAdapterNotInitialized); |
| 47 // TODO(armansito): Implement. | 107 SendResponse(false); |
| 48 SetError("Call not supported."); | 108 return; |
| 49 return false; | 109 } |
| 50 } | 110 |
| 51 | 111 scoped_ptr<apibtle::GetService::Params> params( |
| 52 bool BluetoothLowEnergyReadCharacteristicValueFunction::RunImpl() { | 112 apibtle::GetService::Params::Create(*args_)); |
| 53 // TODO(armansito): Implement. | 113 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); |
| 54 SetError("Call not supported."); | 114 |
| 55 return false; | 115 std::string service_id = params->service_id; |
| 56 } | 116 |
| 57 | 117 apibtle::Service service; |
| 58 bool BluetoothLowEnergyWriteCharacteristicValueFunction::RunImpl() { | 118 if (!event_router->GetService(service_id, &service)) { |
| 59 // TODO(armansito): Implement. | 119 SetError( |
| 60 SetError("Call not supported."); | 120 base::StringPrintf(kErrorServiceNotFoundFormat, service_id.c_str())); |
| 61 return false; | 121 SendResponse(false); |
| 62 } | 122 return; |
| 63 | 123 } |
| 64 bool BluetoothLowEnergyReadDescriptorValueFunction::RunImpl() { | 124 |
| 65 // TODO(armansito): Implement. | 125 SetResult(service.ToValue().release()); |
| 66 SetError("Call not supported."); | 126 SendResponse(true); |
| 67 return false; | 127 } |
| 68 } | 128 |
| 69 | 129 void BluetoothLowEnergyGetServicesFunction::DoWork() { |
| 70 bool BluetoothLowEnergyWriteDescriptorValueFunction::RunImpl() { | 130 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 71 // TODO(armansito): Implement. | 131 |
| 72 SetError("Call not supported."); | 132 BluetoothLowEnergyEventRouter* event_router = |
| 73 return false; | 133 GetEventRouter(browser_context()); |
| 134 | |
| 135 // The adapter must be initialized at this point, but return an error instead | |
| 136 // of asserting. | |
| 137 if (!event_router->HasAdapter()) { | |
| 138 SetError(kErrorAdapterNotInitialized); | |
| 139 SendResponse(false); | |
| 140 return; | |
| 141 } | |
| 142 | |
| 143 scoped_ptr<apibtle::GetServices::Params> params( | |
| 144 apibtle::GetServices::Params::Create(*args_)); | |
| 145 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); | |
| 146 | |
| 147 std::string device_address = params->device_address; | |
| 148 | |
| 149 base::ListValue* service_list = event_router->GetServices(device_address); | |
| 150 if (!service_list) { | |
| 151 SetError( | |
| 152 base::StringPrintf(kErrorDeviceNotFoundFormat, device_address.c_str())); | |
| 153 SendResponse(false); | |
| 154 return; | |
| 155 } | |
| 156 | |
| 157 SetResult(service_list); | |
| 158 SendResponse(true); | |
| 159 } | |
| 160 | |
| 161 void BluetoothLowEnergyGetCharacteristicFunction::DoWork() { | |
| 162 // TODO(armansito): Implement. | |
| 163 SetError("Call not supported."); | |
| 164 SendResponse(false); | |
| 165 } | |
| 166 | |
| 167 void BluetoothLowEnergyGetCharacteristicsFunction::DoWork() { | |
| 168 // TODO(armansito): Implement. | |
| 169 SetError("Call not supported."); | |
| 170 SendResponse(false); | |
| 171 } | |
| 172 | |
| 173 void BluetoothLowEnergyGetIncludedServicesFunction::DoWork() { | |
| 174 // TODO(armansito): Implement. | |
| 175 SetError("Call not supported."); | |
| 176 SendResponse(false); | |
| 177 } | |
| 178 | |
| 179 void BluetoothLowEnergyGetDescriptorFunction::DoWork() { | |
| 180 // TODO(armansito): Implement. | |
| 181 SetError("Call not supported."); | |
| 182 SendResponse(false); | |
| 183 } | |
| 184 | |
| 185 void BluetoothLowEnergyGetDescriptorsFunction::DoWork() { | |
| 186 // TODO(armansito): Implement. | |
| 187 SetError("Call not supported."); | |
| 188 SendResponse(false); | |
| 189 } | |
| 190 | |
| 191 void BluetoothLowEnergyReadCharacteristicValueFunction::DoWork() { | |
| 192 // TODO(armansito): Implement. | |
| 193 SetError("Call not supported."); | |
| 194 SendResponse(false); | |
| 195 } | |
| 196 | |
| 197 void BluetoothLowEnergyWriteCharacteristicValueFunction::DoWork() { | |
| 198 // TODO(armansito): Implement. | |
| 199 SetError("Call not supported."); | |
| 200 SendResponse(false); | |
| 201 } | |
| 202 | |
| 203 void BluetoothLowEnergyReadDescriptorValueFunction::DoWork() { | |
| 204 // TODO(armansito): Implement. | |
| 205 SetError("Call not supported."); | |
| 206 SendResponse(false); | |
| 207 } | |
| 208 | |
| 209 void BluetoothLowEnergyWriteDescriptorValueFunction::DoWork() { | |
| 210 // TODO(armansito): Implement. | |
| 211 SetError("Call not supported."); | |
| 212 SendResponse(false); | |
| 74 } | 213 } |
| 75 | 214 |
| 76 } // namespace api | 215 } // namespace api |
| 77 } // namespace extensions | 216 } // namespace extensions |
| OLD | NEW |