| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/bluetooth_api.h" | 5 #include "chrome/browser/extensions/api/bluetooth/bluetooth_api.h" |
| 6 | 6 |
| 7 #include "base/memory/weak_ptr.h" |
| 7 #include "base/utf_string_conversions.h" | 8 #include "base/utf_string_conversions.h" |
| 8 #include "chrome/browser/extensions/extension_service.h" | 9 #include "chrome/browser/extensions/extension_service.h" |
| 9 #include "chrome/browser/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
| 10 #include "chrome/common/extensions/api/experimental.bluetooth.h" | 11 #include "chrome/common/extensions/api/experimental.bluetooth.h" |
| 11 | 12 |
| 12 #if defined(OS_CHROMEOS) | 13 #if defined(OS_CHROMEOS) |
| 13 #include "chrome/browser/chromeos/bluetooth/bluetooth_adapter.h" | 14 #include "chrome/browser/chromeos/bluetooth/bluetooth_adapter.h" |
| 14 #include "chrome/browser/chromeos/bluetooth/bluetooth_device.h" | 15 #include "chrome/browser/chromeos/bluetooth/bluetooth_device.h" |
| 16 #include "chrome/browser/chromeos/bluetooth/bluetooth_socket.h" |
| 15 #include "chrome/browser/chromeos/extensions/bluetooth_event_router.h" | 17 #include "chrome/browser/chromeos/extensions/bluetooth_event_router.h" |
| 16 | 18 |
| 17 using chromeos::BluetoothAdapter; | 19 using chromeos::BluetoothAdapter; |
| 18 using chromeos::BluetoothDevice; | 20 using chromeos::BluetoothDevice; |
| 19 #endif | 21 #endif |
| 20 | 22 |
| 21 namespace GetDevicesWithService = | 23 namespace GetDevicesWithService = |
| 22 extensions::api::experimental_bluetooth::GetDevicesWithService; | 24 extensions::api::experimental_bluetooth::GetDevicesWithService; |
| 25 namespace Connect = extensions::api::experimental_bluetooth::Connect; |
| 26 namespace Disconnect = extensions::api::experimental_bluetooth::Disconnect; |
| 23 | 27 |
| 24 namespace extensions { | 28 namespace extensions { |
| 25 namespace api { | 29 namespace api { |
| 26 | 30 |
| 27 #if defined(OS_CHROMEOS) | 31 #if defined(OS_CHROMEOS) |
| 28 BluetoothExtensionFunction::BluetoothExtensionFunction() : adapter_( | 32 BluetoothExtensionFunction::BluetoothExtensionFunction() { |
| 29 profile()->GetExtensionService()->bluetooth_event_router()->adapter()) { | 33 chromeos::ExtensionBluetoothEventRouter* event_router = |
| 34 profile()->GetExtensionService()->bluetooth_event_router(); |
| 35 const_adapter_ = event_router->adapter(); |
| 36 adapter_ = event_router->GetMutableAdapter(); |
| 30 } | 37 } |
| 31 | 38 |
| 32 bool BluetoothIsAvailableFunction::RunImpl() { | 39 bool BluetoothIsAvailableFunction::RunImpl() { |
| 33 const BluetoothAdapter *adapter = | 40 result_.reset(Value::CreateBooleanValue(const_adapter_->IsPresent())); |
| 34 profile()->GetExtensionService()->bluetooth_event_router()->adapter(); | |
| 35 result_.reset(Value::CreateBooleanValue(adapter->IsPresent())); | |
| 36 return true; | 41 return true; |
| 37 } | 42 } |
| 38 | 43 |
| 39 bool BluetoothIsPoweredFunction::RunImpl() { | 44 bool BluetoothIsPoweredFunction::RunImpl() { |
| 40 const BluetoothAdapter *adapter = | 45 result_.reset(Value::CreateBooleanValue(const_adapter_->IsPowered())); |
| 41 profile()->GetExtensionService()->bluetooth_event_router()->adapter(); | |
| 42 result_.reset(Value::CreateBooleanValue(adapter->IsPowered())); | |
| 43 return true; | 46 return true; |
| 44 } | 47 } |
| 45 | 48 |
| 46 bool BluetoothGetAddressFunction::RunImpl() { | 49 bool BluetoothGetAddressFunction::RunImpl() { |
| 47 const chromeos::BluetoothAdapter *adapter = | 50 result_.reset(Value::CreateStringValue(const_adapter_->address())); |
| 48 profile()->GetExtensionService()->bluetooth_event_router()->adapter(); | |
| 49 result_.reset(Value::CreateStringValue(adapter->address())); | |
| 50 return false; | 51 return false; |
| 51 } | 52 } |
| 52 | 53 |
| 53 bool BluetoothGetDevicesWithServiceFunction::RunImpl() { | 54 bool BluetoothGetDevicesWithServiceFunction::RunImpl() { |
| 54 scoped_ptr<GetDevicesWithService::Params> params( | 55 scoped_ptr<GetDevicesWithService::Params> params( |
| 55 GetDevicesWithService::Params::Create(*args_)); | 56 GetDevicesWithService::Params::Create(*args_)); |
| 56 | 57 |
| 57 BluetoothAdapter::ConstDeviceList devices = adapter_->GetDevices(); | 58 BluetoothAdapter::ConstDeviceList devices = const_adapter_->GetDevices(); |
| 58 | 59 |
| 59 ListValue* matches = new ListValue(); | 60 ListValue* matches = new ListValue(); |
| 60 for (BluetoothAdapter::ConstDeviceList::const_iterator i = | 61 for (BluetoothAdapter::ConstDeviceList::const_iterator i = |
| 61 devices.begin(); i != devices.end(); ++i) { | 62 devices.begin(); i != devices.end(); ++i) { |
| 62 const BluetoothDevice::ServiceList& services = (*i)->GetServices(); | 63 const BluetoothDevice::ServiceList& services = (*i)->GetServices(); |
| 63 for (BluetoothDevice::ServiceList::const_iterator j = services.begin(); | 64 for (BluetoothDevice::ServiceList::const_iterator j = services.begin(); |
| 64 j != services.end(); ++j) { | 65 j != services.end(); ++j) { |
| 65 if (*j == params->service) { | 66 if (*j == params->service) { |
| 66 experimental_bluetooth::Device device; | 67 experimental_bluetooth::Device device; |
| 67 device.name = UTF16ToUTF8((*i)->GetName()); | 68 device.name = UTF16ToUTF8((*i)->GetName()); |
| 68 device.address = (*i)->address(); | 69 device.address = (*i)->address(); |
| 69 matches->Append(device.ToValue().get()); | 70 matches->Append(device.ToValue().get()); |
| 70 } | 71 } |
| 71 } | 72 } |
| 72 } | 73 } |
| 73 | 74 |
| 74 result_.reset(matches); | 75 result_.reset(matches); |
| 75 return true; | 76 return true; |
| 76 } | 77 } |
| 77 | 78 |
| 79 bool BluetoothConnectFunction::RunImpl() { |
| 80 scoped_ptr<Connect::Params> params(Connect::Params::Create(*args_)); |
| 81 |
| 82 chromeos::BluetoothDevice* device = |
| 83 adapter_->GetDevice(params->device.address); |
| 84 if (!device) |
| 85 return false; |
| 86 |
| 87 base::WeakPtr<chromeos::BluetoothSocket> socket = |
| 88 device->OpenSocket(params->service); |
| 89 if (!socket) |
| 90 return false; |
| 91 |
| 92 experimental_bluetooth::Socket result_socket; |
| 93 result_socket.device.address = device->address(); |
| 94 result_socket.device.name = UTF16ToUTF8(device->GetName()); |
| 95 result_socket.service_uuid = params->service; |
| 96 result_socket.id = socket->id(); |
| 97 result_.reset(result_socket.ToValue().get()); |
| 98 return true; |
| 99 } |
| 100 |
| 101 bool BluetoothDisconnectFunction::RunImpl() { |
| 102 scoped_ptr<Disconnect::Params> params(Disconnect::Params::Create(*args_)); |
| 103 |
| 104 chromeos::BluetoothDevice* device = |
| 105 adapter_->GetDevice(params->socket.device.address); |
| 106 if (!device) |
| 107 return false; |
| 108 |
| 109 device->CloseSocket(params->socket.id); |
| 110 |
| 111 return true; |
| 112 } |
| 113 |
| 78 #else | 114 #else |
| 79 | 115 |
| 80 BluetoothExtensionFunction::BluetoothExtensionFunction() {} | 116 BluetoothExtensionFunction::BluetoothExtensionFunction() {} |
| 81 | 117 |
| 82 // ----------------------------------------------------------------------------- | 118 // ----------------------------------------------------------------------------- |
| 83 // NIY stubs | 119 // NIY stubs |
| 84 // ----------------------------------------------------------------------------- | 120 // ----------------------------------------------------------------------------- |
| 85 bool BluetoothIsAvailableFunction::RunImpl() { | 121 bool BluetoothIsAvailableFunction::RunImpl() { |
| 86 NOTREACHED() << "Not implemented yet"; | 122 NOTREACHED() << "Not implemented yet"; |
| 87 return false; | 123 return false; |
| 88 } | 124 } |
| 89 | 125 |
| 90 bool BluetoothIsPoweredFunction::RunImpl() { | 126 bool BluetoothIsPoweredFunction::RunImpl() { |
| 91 NOTREACHED() << "Not implemented yet"; | 127 NOTREACHED() << "Not implemented yet"; |
| 92 return false; | 128 return false; |
| 93 } | 129 } |
| 94 | 130 |
| 95 bool BluetoothGetAddressFunction::RunImpl() { | 131 bool BluetoothGetAddressFunction::RunImpl() { |
| 96 NOTREACHED() << "Not implemented yet"; | 132 NOTREACHED() << "Not implemented yet"; |
| 97 return false; | 133 return false; |
| 98 } | 134 } |
| 99 | 135 |
| 100 bool BluetoothGetDevicesWithServiceFunction::RunImpl() { | 136 bool BluetoothGetDevicesWithServiceFunction::RunImpl() { |
| 101 NOTREACHED() << "Not implemented yet"; | 137 NOTREACHED() << "Not implemented yet"; |
| 102 return false; | 138 return false; |
| 103 } | 139 } |
| 104 | 140 |
| 105 #endif | 141 bool BluetoothConnectFunction::RunImpl() { |
| 142 NOTREACHED() << "Not implemented yet"; |
| 143 return false; |
| 144 } |
| 106 | 145 |
| 107 bool BluetoothDisconnectFunction::RunImpl() { | 146 bool BluetoothDisconnectFunction::RunImpl() { |
| 108 NOTREACHED() << "Not implemented yet"; | 147 NOTREACHED() << "Not implemented yet"; |
| 109 return false; | 148 return false; |
| 110 } | 149 } |
| 111 | 150 |
| 151 #endif |
| 152 |
| 112 bool BluetoothReadFunction::RunImpl() { | 153 bool BluetoothReadFunction::RunImpl() { |
| 113 NOTREACHED() << "Not implemented yet"; | 154 NOTREACHED() << "Not implemented yet"; |
| 114 return false; | 155 return false; |
| 115 } | 156 } |
| 116 | 157 |
| 117 bool BluetoothSetOutOfBandPairingDataFunction::RunImpl() { | 158 bool BluetoothSetOutOfBandPairingDataFunction::RunImpl() { |
| 118 NOTREACHED() << "Not implemented yet"; | 159 NOTREACHED() << "Not implemented yet"; |
| 119 return false; | 160 return false; |
| 120 } | 161 } |
| 121 | 162 |
| 122 bool BluetoothGetOutOfBandPairingDataFunction::RunImpl() { | 163 bool BluetoothGetOutOfBandPairingDataFunction::RunImpl() { |
| 123 NOTREACHED() << "Not implemented yet"; | 164 NOTREACHED() << "Not implemented yet"; |
| 124 return false; | 165 return false; |
| 125 } | 166 } |
| 126 | 167 |
| 127 bool BluetoothWriteFunction::RunImpl() { | 168 bool BluetoothWriteFunction::RunImpl() { |
| 128 NOTREACHED() << "Not implemented yet"; | 169 NOTREACHED() << "Not implemented yet"; |
| 129 return false; | 170 return false; |
| 130 } | 171 } |
| 131 | 172 |
| 132 bool BluetoothConnectFunction::RunImpl() { | |
| 133 NOTREACHED() << "Not implemented yet"; | |
| 134 return false; | |
| 135 } | |
| 136 | |
| 137 } // namespace api | 173 } // namespace api |
| 138 } // namespace extensions | 174 } // namespace extensions |
| OLD | NEW |