Index: chrome/browser/extensions/api/bluetooth/bluetooth_api.cc |
diff --git a/chrome/browser/extensions/api/bluetooth/bluetooth_api.cc b/chrome/browser/extensions/api/bluetooth/bluetooth_api.cc |
index f36470b30bb7c05585aaacf61f4a6e68530c48cb..331dd4bf0d527f19107a919b587c243d6989e88b 100644 |
--- a/chrome/browser/extensions/api/bluetooth/bluetooth_api.cc |
+++ b/chrome/browser/extensions/api/bluetooth/bluetooth_api.cc |
@@ -10,44 +10,44 @@ |
#include <string> |
+#include "base/memory/ref_counted.h" |
#include "chrome/browser/extensions/api/bluetooth/bluetooth_api_utils.h" |
+#include "chrome/browser/extensions/bluetooth_event_router.h" |
#include "chrome/browser/extensions/event_names.h" |
#include "chrome/browser/extensions/extension_service.h" |
#include "chrome/browser/profiles/profile.h" |
#include "chrome/common/extensions/api/experimental_bluetooth.h" |
#include "content/public/browser/browser_thread.h" |
+#include "device/bluetooth/bluetooth_adapter.h" |
+#include "device/bluetooth/bluetooth_device.h" |
+#include "device/bluetooth/bluetooth_out_of_band_pairing_data.h" |
+#include "device/bluetooth/bluetooth_service_record.h" |
+#include "device/bluetooth/bluetooth_socket.h" |
+#include "device/bluetooth/bluetooth_utils.h" |
#if defined(OS_CHROMEOS) |
-#include "base/memory/ref_counted.h" |
#include "base/safe_strerror_posix.h" |
-#include "chrome/browser/chromeos/bluetooth/bluetooth_adapter.h" |
-#include "chrome/browser/chromeos/bluetooth/bluetooth_device.h" |
-#include "chrome/browser/chromeos/bluetooth/bluetooth_service_record.h" |
-#include "chrome/browser/chromeos/bluetooth/bluetooth_socket.h" |
-#include "chrome/browser/chromeos/bluetooth/bluetooth_utils.h" |
-#include "chrome/browser/chromeos/extensions/bluetooth_event_router.h" |
-#include "chromeos/dbus/bluetooth_out_of_band_client.h" |
-#include "chromeos/dbus/bluetooth_out_of_band_pairing_data.h" |
+#endif |
+ |
+using bluetooth::BluetoothAdapter; |
bryeung
2012/10/10 15:03:53
I would also using bluetooth::BluetoothDevice here
youngki
2012/10/10 18:34:57
Done.
|
namespace { |
-chromeos::ExtensionBluetoothEventRouter* GetEventRouter(Profile* profile) { |
+extensions::ExtensionBluetoothEventRouter* GetEventRouter(Profile* profile) { |
return profile->GetExtensionService()->bluetooth_event_router(); |
} |
-const chromeos::BluetoothAdapter& GetAdapter(Profile* profile) { |
+const bluetooth::BluetoothAdapter* GetAdapter(Profile* profile) { |
bryeung
2012/10/10 15:03:53
you don't need both the using above and bluetooth:
youngki
2012/10/10 18:34:57
Done.
|
return GetEventRouter(profile)->adapter(); |
} |
-chromeos::BluetoothAdapter* GetMutableAdapter(Profile* profile) { |
- chromeos::BluetoothAdapter* adapter = |
+bluetooth::BluetoothAdapter* GetMutableAdapter(Profile* profile) { |
+ bluetooth::BluetoothAdapter* adapter = |
GetEventRouter(profile)->GetMutableAdapter(); |
- CHECK(adapter); |
return adapter; |
} |
} // namespace |
-#endif |
namespace { |
@@ -77,33 +77,43 @@ namespace Write = extensions::api::experimental_bluetooth::Write; |
namespace extensions { |
namespace api { |
-#if defined(OS_CHROMEOS) |
- |
bool BluetoothIsAvailableFunction::RunImpl() { |
- SetResult(Value::CreateBooleanValue(GetAdapter(profile()).IsPresent())); |
- return true; |
+ if (GetAdapter(profile())) { |
bryeung
2012/10/10 15:03:53
I'd prefer if adapter were assigned to a variable
youngki
2012/10/10 18:34:57
Done. That looks much better.
|
+ SetResult(Value::CreateBooleanValue(GetAdapter(profile())->IsPresent())); |
+ return true; |
+ } |
+ return false; |
} |
bool BluetoothIsPoweredFunction::RunImpl() { |
- SetResult(Value::CreateBooleanValue(GetAdapter(profile()).IsPowered())); |
- return true; |
+ if (GetAdapter(profile())) { |
+ SetResult(Value::CreateBooleanValue(GetAdapter(profile())->IsPowered())); |
+ return true; |
+ } |
+ return false; |
} |
bool BluetoothGetAddressFunction::RunImpl() { |
- SetResult(Value::CreateStringValue(GetAdapter(profile()).address())); |
- return true; |
+ if (GetAdapter(profile())) { |
+ SetResult(Value::CreateStringValue(GetAdapter(profile())->address())); |
+ return true; |
+ } |
+ return false; |
} |
bool BluetoothGetNameFunction::RunImpl() { |
- SetResult(Value::CreateStringValue(GetAdapter(profile()).name())); |
- return true; |
+ if (GetAdapter(profile())) { |
+ SetResult(Value::CreateStringValue(GetAdapter(profile())->name())); |
+ return true; |
+ } |
+ return false; |
} |
BluetoothGetDevicesFunction::BluetoothGetDevicesFunction() |
: callbacks_pending_(0) {} |
void BluetoothGetDevicesFunction::DispatchDeviceSearchResult( |
- const chromeos::BluetoothDevice& device) { |
+ const bluetooth::BluetoothDevice& device) { |
experimental_bluetooth::Device extension_device; |
experimental_bluetooth::BluetoothDeviceToApiDevice(device, &extension_device); |
GetEventRouter(profile())->DispatchDeviceEvent( |
@@ -112,7 +122,7 @@ void BluetoothGetDevicesFunction::DispatchDeviceSearchResult( |
} |
void BluetoothGetDevicesFunction::ProvidesServiceCallback( |
- const chromeos::BluetoothDevice* device, |
+ const bluetooth::BluetoothDevice* device, |
bool providesService) { |
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
@@ -126,6 +136,9 @@ void BluetoothGetDevicesFunction::ProvidesServiceCallback( |
} |
bool BluetoothGetDevicesFunction::RunImpl() { |
+ if (!GetAdapter(profile())) { |
bryeung
2012/10/10 15:03:53
no braces
youngki
2012/10/10 18:34:57
Done.
|
+ return false; |
+ } |
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
scoped_ptr<GetDevices::Params> params(GetDevices::Params::Create(*args_)); |
@@ -134,7 +147,7 @@ bool BluetoothGetDevicesFunction::RunImpl() { |
std::string uuid; |
if (options.uuid.get() != NULL) { |
- uuid = chromeos::bluetooth_utils::CanonicalUuid(*options.uuid.get()); |
+ uuid = bluetooth::utils::CanonicalUuid(*options.uuid.get()); |
if (uuid.empty()) { |
SetError(kInvalidUuid); |
return false; |
@@ -143,11 +156,11 @@ bool BluetoothGetDevicesFunction::RunImpl() { |
CHECK_EQ(0, callbacks_pending_); |
- chromeos::BluetoothAdapter::DeviceList devices = |
+ BluetoothAdapter::DeviceList devices = |
GetMutableAdapter(profile())->GetDevices(); |
- for (chromeos::BluetoothAdapter::DeviceList::iterator i = devices.begin(); |
+ for (BluetoothAdapter::DeviceList::iterator i = devices.begin(); |
i != devices.end(); ++i) { |
- chromeos::BluetoothDevice* device = *i; |
+ bluetooth::BluetoothDevice* device = *i; |
CHECK(device); |
if (!uuid.empty() && !(device->ProvidesServiceWithUUID(uuid))) |
@@ -178,10 +191,10 @@ bool BluetoothGetDevicesFunction::RunImpl() { |
void BluetoothGetServicesFunction::GetServiceRecordsCallback( |
base::ListValue* services, |
- const chromeos::BluetoothDevice::ServiceRecordList& records) { |
- for (chromeos::BluetoothDevice::ServiceRecordList::const_iterator i = |
+ const bluetooth::BluetoothDevice::ServiceRecordList& records) { |
+ for (bluetooth::BluetoothDevice::ServiceRecordList::const_iterator i = |
records.begin(); i != records.end(); ++i) { |
- const chromeos::BluetoothServiceRecord& record = **i; |
+ const bluetooth::BluetoothServiceRecord& record = **i; |
experimental_bluetooth::ServiceRecord api_record; |
api_record.name = record.name(); |
if (!record.uuid().empty()) |
@@ -198,11 +211,14 @@ void BluetoothGetServicesFunction::OnErrorCallback() { |
} |
bool BluetoothGetServicesFunction::RunImpl() { |
+ if (!GetAdapter(profile())) { |
+ return false; |
+ } |
scoped_ptr<GetServices::Params> params(GetServices::Params::Create(*args_)); |
EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); |
const experimental_bluetooth::GetServicesOptions& options = params->options; |
- chromeos::BluetoothDevice* device = |
+ bluetooth::BluetoothDevice* device = |
GetMutableAdapter(profile())->GetDevice(options.device_address); |
if (!device) { |
SetError(kInvalidDevice); |
@@ -223,9 +239,9 @@ bool BluetoothGetServicesFunction::RunImpl() { |
} |
void BluetoothConnectFunction::ConnectToServiceCallback( |
- const chromeos::BluetoothDevice* device, |
+ const bluetooth::BluetoothDevice* device, |
const std::string& service_uuid, |
- scoped_refptr<chromeos::BluetoothSocket> socket) { |
+ scoped_refptr<bluetooth::BluetoothSocket> socket) { |
if (socket.get()) { |
int socket_id = GetEventRouter(profile())->RegisterSocket(socket); |
@@ -243,18 +259,21 @@ void BluetoothConnectFunction::ConnectToServiceCallback( |
} |
bool BluetoothConnectFunction::RunImpl() { |
+ if (!GetAdapter(profile())) { |
+ return false; |
+ } |
scoped_ptr<Connect::Params> params(Connect::Params::Create(*args_)); |
EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); |
const experimental_bluetooth::ConnectOptions& options = params->options; |
- std::string uuid = chromeos::bluetooth_utils::CanonicalUuid( |
+ std::string uuid = bluetooth::utils::CanonicalUuid( |
options.service_uuid); |
if (uuid.empty()) { |
SetError(kInvalidUuid); |
return false; |
} |
- chromeos::BluetoothDevice* device = |
+ bluetooth::BluetoothDevice* device = |
GetMutableAdapter(profile())->GetDevice(options.device_address); |
if (!device) { |
SetError(kInvalidDevice); |
@@ -322,8 +341,10 @@ void BluetoothReadFunction::Work() { |
free(all_bytes); |
} |
+#if defined(OS_CHROMEOS) |
bryeung
2012/10/10 15:03:53
Is the rest of this function actually going to wor
youngki
2012/10/10 18:34:57
I made an early return if socket_.get() is null. A
|
if (!success_) |
SetError(safe_strerror(errsv)); |
+#endif |
} |
bool BluetoothReadFunction::Respond() { |
@@ -370,8 +391,10 @@ void BluetoothWriteFunction::Work() { |
success_ = (errsv == EAGAIN || errsv == EWOULDBLOCK); |
} |
+#if defined(OS_CHROMEOS) |
if (!success_) |
SetError(safe_strerror(errsv)); |
+#endif |
} |
bool BluetoothWriteFunction::Respond() { |
@@ -388,6 +411,9 @@ void BluetoothSetOutOfBandPairingDataFunction::OnErrorCallback() { |
} |
bool BluetoothSetOutOfBandPairingDataFunction::RunImpl() { |
+ if (!GetAdapter(profile())) { |
+ return false; |
+ } |
// TODO(bryeung): update to new-style parameter passing when ArrayBuffer |
// support is added |
DictionaryValue* options; |
@@ -395,7 +421,7 @@ bool BluetoothSetOutOfBandPairingDataFunction::RunImpl() { |
std::string address; |
EXTENSION_FUNCTION_VALIDATE(options->GetString("deviceAddress", &address)); |
- chromeos::BluetoothDevice* device = |
+ bluetooth::BluetoothDevice* device = |
GetMutableAdapter(profile())->GetDevice(address); |
if (!device) { |
SetError(kInvalidDevice); |
@@ -406,22 +432,22 @@ bool BluetoothSetOutOfBandPairingDataFunction::RunImpl() { |
DictionaryValue* data_in; |
EXTENSION_FUNCTION_VALIDATE(options->GetDictionary("data", &data_in)); |
- chromeos::BluetoothOutOfBandPairingData data_out; |
+ bluetooth::BluetoothOutOfBandPairingData data_out; |
base::BinaryValue* tmp_data; |
EXTENSION_FUNCTION_VALIDATE(data_in->GetBinary("hash", &tmp_data)); |
EXTENSION_FUNCTION_VALIDATE( |
- tmp_data->GetSize() == chromeos::kBluetoothOutOfBandPairingDataSize); |
+ tmp_data->GetSize() == bluetooth::kBluetoothOutOfBandPairingDataSize); |
memcpy(data_out.hash, |
reinterpret_cast<uint8_t*>(tmp_data->GetBuffer()), |
- chromeos::kBluetoothOutOfBandPairingDataSize); |
+ bluetooth::kBluetoothOutOfBandPairingDataSize); |
EXTENSION_FUNCTION_VALIDATE(data_in->GetBinary("randomizer", &tmp_data)); |
EXTENSION_FUNCTION_VALIDATE( |
- tmp_data->GetSize() == chromeos::kBluetoothOutOfBandPairingDataSize); |
+ tmp_data->GetSize() == bluetooth::kBluetoothOutOfBandPairingDataSize); |
memcpy(data_out.randomizer, |
reinterpret_cast<uint8_t*>(tmp_data->GetBuffer()), |
- chromeos::kBluetoothOutOfBandPairingDataSize); |
+ bluetooth::kBluetoothOutOfBandPairingDataSize); |
device->SetOutOfBandPairingData( |
data_out, |
@@ -441,13 +467,13 @@ bool BluetoothSetOutOfBandPairingDataFunction::RunImpl() { |
} |
void BluetoothGetLocalOutOfBandPairingDataFunction::ReadCallback( |
- const chromeos::BluetoothOutOfBandPairingData& data) { |
+ const bluetooth::BluetoothOutOfBandPairingData& data) { |
base::BinaryValue* hash = base::BinaryValue::CreateWithCopiedBuffer( |
reinterpret_cast<const char*>(data.hash), |
- chromeos::kBluetoothOutOfBandPairingDataSize); |
+ bluetooth::kBluetoothOutOfBandPairingDataSize); |
base::BinaryValue* randomizer = base::BinaryValue::CreateWithCopiedBuffer( |
reinterpret_cast<const char*>(data.randomizer), |
- chromeos::kBluetoothOutOfBandPairingDataSize); |
+ bluetooth::kBluetoothOutOfBandPairingDataSize); |
// TODO(bryeung): convert to experimental_bluetooth::OutOfBandPairingData |
// when ArrayBuffer support within objects is completed. |
@@ -466,6 +492,9 @@ void BluetoothGetLocalOutOfBandPairingDataFunction::ErrorCallback() { |
} |
bool BluetoothGetLocalOutOfBandPairingDataFunction::RunImpl() { |
+ if (!GetAdapter(profile())) { |
+ return false; |
+ } |
GetMutableAdapter(profile())->ReadLocalOutOfBandPairingData( |
base::Bind(&BluetoothGetLocalOutOfBandPairingDataFunction::ReadCallback, |
this), |
@@ -485,6 +514,9 @@ void BluetoothStartDiscoveryFunction::OnErrorCallback() { |
} |
bool BluetoothStartDiscoveryFunction::RunImpl() { |
+ if (!GetAdapter(profile())) { |
+ return false; |
+ } |
GetEventRouter(profile())->SetSendDiscoveryEvents(true); |
// If the adapter is already discovering, there is nothing else to do. |
@@ -509,6 +541,9 @@ void BluetoothStopDiscoveryFunction::OnErrorCallback() { |
} |
bool BluetoothStopDiscoveryFunction::RunImpl() { |
+ if (!GetAdapter(profile())) { |
+ return false; |
+ } |
GetEventRouter(profile())->SetSendDiscoveryEvents(false); |
if (GetEventRouter(profile())->IsResponsibleForDiscovery()) { |
GetMutableAdapter(profile())->SetDiscovering(false, |
@@ -518,97 +553,6 @@ bool BluetoothStopDiscoveryFunction::RunImpl() { |
return true; |
} |
-#else |
- |
-// ----------------------------------------------------------------------------- |
-// NIY stubs |
-// ----------------------------------------------------------------------------- |
-bool BluetoothIsAvailableFunction::RunImpl() { |
- NOTREACHED() << "Not implemented yet"; |
- return false; |
-} |
- |
-bool BluetoothIsPoweredFunction::RunImpl() { |
- NOTREACHED() << "Not implemented yet"; |
- return false; |
-} |
- |
-bool BluetoothGetAddressFunction::RunImpl() { |
- NOTREACHED() << "Not implemented yet"; |
- return false; |
-} |
- |
-bool BluetoothGetNameFunction::RunImpl() { |
- NOTREACHED() << "Not implemented yet"; |
- return false; |
-} |
- |
-bool BluetoothGetDevicesFunction::RunImpl() { |
- NOTREACHED() << "Not implemented yet"; |
- return false; |
-} |
- |
-bool BluetoothGetServicesFunction::RunImpl() { |
- NOTREACHED() << "Not implemented yet"; |
- return false; |
-} |
- |
-bool BluetoothConnectFunction::RunImpl() { |
- NOTREACHED() << "Not implemented yet"; |
- return false; |
-} |
- |
-bool BluetoothDisconnectFunction::RunImpl() { |
- NOTREACHED() << "Not implemented yet"; |
- return false; |
-} |
- |
-bool BluetoothReadFunction::Prepare() { |
- return true; |
-} |
- |
-void BluetoothReadFunction::Work() { |
-} |
- |
-bool BluetoothReadFunction::Respond() { |
- NOTREACHED() << "Not implemented yet"; |
- return false; |
-} |
- |
-bool BluetoothWriteFunction::Prepare() { |
- return true; |
-} |
- |
-void BluetoothWriteFunction::Work() { |
-} |
- |
-bool BluetoothWriteFunction::Respond() { |
- NOTREACHED() << "Not implemented yet"; |
- return false; |
-} |
- |
-bool BluetoothStartDiscoveryFunction::RunImpl() { |
- NOTREACHED() << "Not implemented yet"; |
- return false; |
-} |
- |
-bool BluetoothStopDiscoveryFunction::RunImpl() { |
- NOTREACHED() << "Not implemented yet"; |
- return false; |
-} |
- |
-bool BluetoothSetOutOfBandPairingDataFunction::RunImpl() { |
- NOTREACHED() << "Not implemented yet"; |
- return false; |
-} |
- |
-bool BluetoothGetLocalOutOfBandPairingDataFunction::RunImpl() { |
- NOTREACHED() << "Not implemented yet"; |
- return false; |
-} |
- |
-#endif |
- |
BluetoothReadFunction::BluetoothReadFunction() {} |
bryeung
2012/10/10 15:03:53
These should be moved to go with the rest of their
youngki
2012/10/10 18:34:57
Done.
|
BluetoothReadFunction::~BluetoothReadFunction() {} |