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" | 7 #include "base/bind.h" |
8 #include "base/lazy_instance.h" | 8 #include "base/lazy_instance.h" |
9 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
10 #include "chrome/browser/extensions/api/bluetooth_low_energy/utils.h" | 10 #include "chrome/browser/extensions/api/bluetooth_low_energy/utils.h" |
11 #include "chrome/common/extensions/api/bluetooth/bluetooth_manifest_data.h" | 11 #include "chrome/common/extensions/api/bluetooth/bluetooth_manifest_data.h" |
12 #include "chrome/common/extensions/api/bluetooth_low_energy.h" | 12 #include "chrome/common/extensions/api/bluetooth_low_energy.h" |
13 #include "content/public/browser/browser_thread.h" | 13 #include "content/public/browser/browser_thread.h" |
14 #include "extensions/browser/event_router.h" | 14 #include "extensions/browser/event_router.h" |
15 #include "extensions/common/permissions/permissions_data.h" | 15 #include "extensions/common/permissions/permissions_data.h" |
16 | 16 |
17 using content::BrowserContext; | 17 using content::BrowserContext; |
18 using content::BrowserThread; | 18 using content::BrowserThread; |
19 | 19 |
20 namespace apibtle = extensions::api::bluetooth_low_energy; | 20 namespace apibtle = extensions::api::bluetooth_low_energy; |
21 | 21 |
22 namespace extensions { | 22 namespace extensions { |
23 | 23 |
24 namespace { | 24 namespace { |
25 | 25 |
26 const char kErrorAdapterNotInitialized[] = | 26 const char kErrorAdapterNotInitialized[] = |
27 "Could not initialize Bluetooth adapter"; | 27 "Could not initialize Bluetooth adapter"; |
| 28 const char kErrorAlreadyConnected[] = "Already connected"; |
| 29 const char kErrorNotConnected[] = "Not connected"; |
28 const char kErrorNotFound[] = "Instance not found"; | 30 const char kErrorNotFound[] = "Instance not found"; |
29 const char kErrorOperationFailed[] = "Operation failed"; | 31 const char kErrorOperationFailed[] = "Operation failed"; |
30 const char kErrorPermissionDenied[] = "Permission denied"; | 32 const char kErrorPermissionDenied[] = "Permission denied"; |
31 const char kErrorPlatformNotSupported[] = | 33 const char kErrorPlatformNotSupported[] = |
32 "This operation is not supported on the current platform"; | 34 "This operation is not supported on the current platform"; |
33 | 35 |
34 // Returns the correct error string based on error status |status|. This is used | 36 // Returns the correct error string based on error status |status|. This is used |
35 // to set the value of |chrome.runtime.lastError.message| and should not be | 37 // to set the value of |chrome.runtime.lastError.message| and should not be |
36 // passed |BluetoothLowEnergyEventRouter::kStatusSuccess|. | 38 // passed |BluetoothLowEnergyEventRouter::kStatusSuccess|. |
37 std::string StatusToString(BluetoothLowEnergyEventRouter::Status status) { | 39 std::string StatusToString(BluetoothLowEnergyEventRouter::Status status) { |
38 switch (status) { | 40 switch (status) { |
39 case BluetoothLowEnergyEventRouter::kStatusErrorPermissionDenied: | 41 case BluetoothLowEnergyEventRouter::kStatusErrorPermissionDenied: |
40 return kErrorPermissionDenied; | 42 return kErrorPermissionDenied; |
41 case BluetoothLowEnergyEventRouter::kStatusErrorNotFound: | 43 case BluetoothLowEnergyEventRouter::kStatusErrorNotFound: |
42 return kErrorNotFound; | 44 return kErrorNotFound; |
| 45 case BluetoothLowEnergyEventRouter::kStatusErrorAlreadyConnected: |
| 46 return kErrorAlreadyConnected; |
| 47 case BluetoothLowEnergyEventRouter::kStatusErrorNotConnected: |
| 48 return kErrorNotConnected; |
43 case BluetoothLowEnergyEventRouter::kStatusSuccess: | 49 case BluetoothLowEnergyEventRouter::kStatusSuccess: |
44 NOTREACHED(); | 50 NOTREACHED(); |
45 break; | 51 break; |
46 default: | 52 default: |
47 return kErrorOperationFailed; | 53 return kErrorOperationFailed; |
48 } | 54 } |
49 return ""; | 55 return ""; |
50 } | 56 } |
51 | 57 |
52 extensions::BluetoothLowEnergyEventRouter* GetEventRouter( | 58 extensions::BluetoothLowEnergyEventRouter* GetEventRouter( |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 if (!event_router->InitializeAdapterAndInvokeCallback(base::Bind( | 124 if (!event_router->InitializeAdapterAndInvokeCallback(base::Bind( |
119 &DoWorkCallback, | 125 &DoWorkCallback, |
120 base::Bind(&BluetoothLowEnergyExtensionFunction::DoWork, this)))) { | 126 base::Bind(&BluetoothLowEnergyExtensionFunction::DoWork, this)))) { |
121 SetError(kErrorAdapterNotInitialized); | 127 SetError(kErrorAdapterNotInitialized); |
122 return false; | 128 return false; |
123 } | 129 } |
124 | 130 |
125 return true; | 131 return true; |
126 } | 132 } |
127 | 133 |
| 134 bool BluetoothLowEnergyConnectFunction::DoWork() { |
| 135 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 136 |
| 137 BluetoothLowEnergyEventRouter* event_router = |
| 138 GetEventRouter(browser_context()); |
| 139 |
| 140 // The adapter must be initialized at this point, but return an error instead |
| 141 // of asserting. |
| 142 if (!event_router->HasAdapter()) { |
| 143 SetError(kErrorAdapterNotInitialized); |
| 144 SendResponse(false); |
| 145 return false; |
| 146 } |
| 147 |
| 148 scoped_ptr<apibtle::Connect::Params> params( |
| 149 apibtle::Connect::Params::Create(*args_)); |
| 150 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); |
| 151 |
| 152 BluetoothLowEnergyEventRouter::Status status = |
| 153 event_router->Connect( |
| 154 GetExtension(), |
| 155 params->device_address, |
| 156 base::Bind(&BluetoothLowEnergyConnectFunction::SuccessCallback, this), |
| 157 base::Bind(&BluetoothLowEnergyConnectFunction::ErrorCallback, this)); |
| 158 |
| 159 if (status != BluetoothLowEnergyEventRouter::kStatusSuccess) { |
| 160 SetError(StatusToString(status)); |
| 161 SendResponse(false); |
| 162 return false; |
| 163 } |
| 164 |
| 165 return true; |
| 166 } |
| 167 |
| 168 void BluetoothLowEnergyConnectFunction::SuccessCallback() { |
| 169 SendResponse(true); |
| 170 } |
| 171 |
| 172 void BluetoothLowEnergyConnectFunction::ErrorCallback( |
| 173 BluetoothLowEnergyEventRouter::Status status) { |
| 174 SetError(StatusToString(status)); |
| 175 SendResponse(false); |
| 176 } |
| 177 |
| 178 bool BluetoothLowEnergyDisconnectFunction::DoWork() { |
| 179 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 180 |
| 181 BluetoothLowEnergyEventRouter* event_router = |
| 182 GetEventRouter(browser_context()); |
| 183 |
| 184 // The adapter must be initialized at this point, but return an error instead |
| 185 // of asserting. |
| 186 if (!event_router->HasAdapter()) { |
| 187 SetError(kErrorAdapterNotInitialized); |
| 188 SendResponse(false); |
| 189 return false; |
| 190 } |
| 191 |
| 192 scoped_ptr<apibtle::Disconnect::Params> params( |
| 193 apibtle::Disconnect::Params::Create(*args_)); |
| 194 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); |
| 195 |
| 196 BluetoothLowEnergyEventRouter::Status status = |
| 197 event_router->Disconnect( |
| 198 GetExtension(), |
| 199 params->device_address, |
| 200 base::Bind(&BluetoothLowEnergyDisconnectFunction::SuccessCallback, |
| 201 this)); |
| 202 |
| 203 if (status != BluetoothLowEnergyEventRouter::kStatusSuccess) { |
| 204 SetError(StatusToString(status)); |
| 205 SendResponse(false); |
| 206 return false; |
| 207 } |
| 208 |
| 209 return true; |
| 210 } |
| 211 |
| 212 void BluetoothLowEnergyDisconnectFunction::SuccessCallback() { |
| 213 SendResponse(true); |
| 214 } |
| 215 |
128 bool BluetoothLowEnergyGetServiceFunction::DoWork() { | 216 bool BluetoothLowEnergyGetServiceFunction::DoWork() { |
129 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 217 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
130 | 218 |
131 BluetoothLowEnergyEventRouter* event_router = | 219 BluetoothLowEnergyEventRouter* event_router = |
132 GetEventRouter(browser_context()); | 220 GetEventRouter(browser_context()); |
133 | 221 |
134 // The adapter must be initialized at this point, but return an error instead | 222 // The adapter must be initialized at this point, but return an error instead |
135 // of asserting. | 223 // of asserting. |
136 if (!event_router->HasAdapter()) { | 224 if (!event_router->HasAdapter()) { |
137 SetError(kErrorAdapterNotInitialized); | 225 SetError(kErrorAdapterNotInitialized); |
(...skipping 469 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
607 } | 695 } |
608 | 696 |
609 void BluetoothLowEnergyWriteDescriptorValueFunction::ErrorCallback( | 697 void BluetoothLowEnergyWriteDescriptorValueFunction::ErrorCallback( |
610 BluetoothLowEnergyEventRouter::Status status) { | 698 BluetoothLowEnergyEventRouter::Status status) { |
611 SetError(StatusToString(status)); | 699 SetError(StatusToString(status)); |
612 SendResponse(false); | 700 SendResponse(false); |
613 } | 701 } |
614 | 702 |
615 } // namespace api | 703 } // namespace api |
616 } // namespace extensions | 704 } // namespace extensions |
OLD | NEW |