| 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 #if defined(OS_CHROMEOS) | 7 #if defined(OS_CHROMEOS) |
| 8 #include <errno.h> | 8 #include <errno.h> |
| 9 #endif | 9 #endif |
| 10 | 10 |
| 11 #include <string> | 11 #include <string> |
| 12 | 12 |
| 13 #include "chrome/browser/extensions/api/bluetooth/bluetooth_api_utils.h" | 13 #include "chrome/browser/extensions/api/bluetooth/bluetooth_api_utils.h" |
| 14 #include "chrome/browser/extensions/event_names.h" | |
| 15 #include "chrome/browser/extensions/extension_service.h" | 14 #include "chrome/browser/extensions/extension_service.h" |
| 16 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
| 17 #include "chrome/common/extensions/api/experimental_bluetooth.h" | 16 #include "chrome/common/extensions/api/experimental_bluetooth.h" |
| 18 #include "content/public/browser/browser_thread.h" | 17 #include "content/public/browser/browser_thread.h" |
| 19 | 18 |
| 20 #if defined(OS_CHROMEOS) | 19 #if defined(OS_CHROMEOS) |
| 21 #include "base/memory/ref_counted.h" | 20 #include "base/memory/ref_counted.h" |
| 22 #include "base/safe_strerror_posix.h" | 21 #include "base/safe_strerror_posix.h" |
| 23 #include "chrome/browser/chromeos/bluetooth/bluetooth_adapter.h" | 22 #include "chrome/browser/chromeos/bluetooth/bluetooth_adapter.h" |
| 24 #include "chrome/browser/chromeos/bluetooth/bluetooth_device.h" | 23 #include "chrome/browser/chromeos/bluetooth/bluetooth_device.h" |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 93 } | 92 } |
| 94 | 93 |
| 95 bool BluetoothGetNameFunction::RunImpl() { | 94 bool BluetoothGetNameFunction::RunImpl() { |
| 96 SetResult(Value::CreateStringValue(GetAdapter(profile()).name())); | 95 SetResult(Value::CreateStringValue(GetAdapter(profile()).name())); |
| 97 return true; | 96 return true; |
| 98 } | 97 } |
| 99 | 98 |
| 100 BluetoothGetDevicesFunction::BluetoothGetDevicesFunction() | 99 BluetoothGetDevicesFunction::BluetoothGetDevicesFunction() |
| 101 : callbacks_pending_(0) {} | 100 : callbacks_pending_(0) {} |
| 102 | 101 |
| 103 void BluetoothGetDevicesFunction::DispatchDeviceSearchResult( | 102 void BluetoothGetDevicesFunction::AddDeviceIfTrueCallback( |
| 104 const chromeos::BluetoothDevice& device) { | 103 ListValue* list, |
| 105 experimental_bluetooth::Device extension_device; | |
| 106 experimental_bluetooth::BluetoothDeviceToApiDevice(device, &extension_device); | |
| 107 GetEventRouter(profile())->DispatchDeviceEvent( | |
| 108 extensions::event_names::kBluetoothOnDeviceSearchResult, | |
| 109 extension_device); | |
| 110 } | |
| 111 | |
| 112 void BluetoothGetDevicesFunction::ProvidesServiceCallback( | |
| 113 const chromeos::BluetoothDevice* device, | 104 const chromeos::BluetoothDevice* device, |
| 114 bool providesService) { | 105 bool shouldAdd) { |
| 115 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 106 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 116 | 107 |
| 117 CHECK(device); | 108 if (shouldAdd) |
| 118 if (providesService) | 109 list->Append(experimental_bluetooth::BluetoothDeviceToValue(*device)); |
| 119 DispatchDeviceSearchResult(*device); | |
| 120 | 110 |
| 121 callbacks_pending_--; | 111 callbacks_pending_--; |
| 122 if (callbacks_pending_ == -1) | 112 if (callbacks_pending_ == -1) |
| 123 SendResponse(true); | 113 SendResponse(true); |
| 124 } | 114 } |
| 125 | 115 |
| 126 bool BluetoothGetDevicesFunction::RunImpl() { | 116 bool BluetoothGetDevicesFunction::RunImpl() { |
| 127 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 117 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 128 | 118 |
| 129 scoped_ptr<GetDevices::Params> params(GetDevices::Params::Create(*args_)); | 119 scoped_ptr<GetDevices::Params> params(GetDevices::Params::Create(*args_)); |
| 130 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); | 120 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); |
| 131 const experimental_bluetooth::GetDevicesOptions& options = params->options; | 121 const experimental_bluetooth::GetDevicesOptions& options = params->options; |
| 132 | 122 |
| 133 std::string uuid; | 123 std::string uuid; |
| 134 if (options.uuid.get() != NULL) { | 124 if (options.uuid.get() != NULL) { |
| 135 uuid = chromeos::bluetooth_utils::CanonicalUuid(*options.uuid.get()); | 125 uuid = chromeos::bluetooth_utils::CanonicalUuid(*options.uuid.get()); |
| 136 if (uuid.empty()) { | 126 if (uuid.empty()) { |
| 137 SetError(kInvalidUuid); | 127 SetError(kInvalidUuid); |
| 138 return false; | 128 return false; |
| 139 } | 129 } |
| 140 } | 130 } |
| 141 | 131 |
| 132 ListValue* matches = new ListValue; |
| 133 SetResult(matches); |
| 134 |
| 142 CHECK_EQ(0, callbacks_pending_); | 135 CHECK_EQ(0, callbacks_pending_); |
| 143 | 136 |
| 144 chromeos::BluetoothAdapter::DeviceList devices = | 137 chromeos::BluetoothAdapter::DeviceList devices = |
| 145 GetMutableAdapter(profile())->GetDevices(); | 138 GetMutableAdapter(profile())->GetDevices(); |
| 146 for (chromeos::BluetoothAdapter::DeviceList::iterator i = devices.begin(); | 139 for (chromeos::BluetoothAdapter::DeviceList::iterator i = devices.begin(); |
| 147 i != devices.end(); ++i) { | 140 i != devices.end(); ++i) { |
| 148 chromeos::BluetoothDevice* device = *i; | 141 chromeos::BluetoothDevice* device = *i; |
| 149 CHECK(device); | |
| 150 | 142 |
| 151 if (!uuid.empty() && !(device->ProvidesServiceWithUUID(uuid))) | 143 if (!uuid.empty() && !(device->ProvidesServiceWithUUID(uuid))) |
| 152 continue; | 144 continue; |
| 153 | 145 |
| 154 if (options.name.get() == NULL) { | 146 if (options.name.get() == NULL) { |
| 155 DispatchDeviceSearchResult(*device); | 147 matches->Append(experimental_bluetooth::BluetoothDeviceToValue(*device)); |
| 156 continue; | 148 continue; |
| 157 } | 149 } |
| 158 | 150 |
| 159 callbacks_pending_++; | 151 callbacks_pending_++; |
| 160 device->ProvidesServiceWithName( | 152 device->ProvidesServiceWithName( |
| 161 *(options.name), | 153 *(options.name), |
| 162 base::Bind(&BluetoothGetDevicesFunction::ProvidesServiceCallback, | 154 base::Bind(&BluetoothGetDevicesFunction::AddDeviceIfTrueCallback, |
| 163 this, | 155 this, |
| 156 matches, |
| 164 device)); | 157 device)); |
| 165 } | 158 } |
| 166 callbacks_pending_--; | 159 callbacks_pending_--; |
| 167 | 160 |
| 168 // The count is checked for -1 because of the extra decrement after the | 161 // The count is checked for -1 because of the extra decrement after the |
| 169 // for-loop, which ensures that all requests have been made before | 162 // for-loop, which ensures that all requests have been made before |
| 170 // SendResponse happens. | 163 // SendResponse happens. |
| 171 if (callbacks_pending_ == -1) | 164 if (callbacks_pending_ == -1) |
| 172 SendResponse(true); | 165 SendResponse(true); |
| 173 | 166 |
| (...skipping 434 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 608 #endif | 601 #endif |
| 609 | 602 |
| 610 BluetoothReadFunction::BluetoothReadFunction() {} | 603 BluetoothReadFunction::BluetoothReadFunction() {} |
| 611 BluetoothReadFunction::~BluetoothReadFunction() {} | 604 BluetoothReadFunction::~BluetoothReadFunction() {} |
| 612 | 605 |
| 613 BluetoothWriteFunction::BluetoothWriteFunction() {} | 606 BluetoothWriteFunction::BluetoothWriteFunction() {} |
| 614 BluetoothWriteFunction::~BluetoothWriteFunction() {} | 607 BluetoothWriteFunction::~BluetoothWriteFunction() {} |
| 615 | 608 |
| 616 } // namespace api | 609 } // namespace api |
| 617 } // namespace extensions | 610 } // namespace extensions |
| OLD | NEW |