Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(393)

Side by Side Diff: chrome/browser/extensions/api/bluetooth/bluetooth_api.cc

Issue 10381120: Implement chrome.experimental.bluetooth.connect (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/utf_string_conversions.h" 7 #include "base/utf_string_conversions.h"
8 #include "chrome/browser/extensions/extension_service.h" 8 #include "chrome/browser/extensions/extension_service.h"
9 #include "chrome/browser/profiles/profile.h" 9 #include "chrome/browser/profiles/profile.h"
10 #include "chrome/common/extensions/api/experimental_bluetooth.h" 10 #include "chrome/common/extensions/api/experimental_bluetooth.h"
11 #include "content/public/browser/browser_thread.h" 11 #include "content/public/browser/browser_thread.h"
12 12
13 #if defined(OS_CHROMEOS) 13 #if defined(OS_CHROMEOS)
14 #include "base/memory/ref_counted.h"
14 #include "base/synchronization/lock.h" 15 #include "base/synchronization/lock.h"
15 #include "chrome/browser/chromeos/bluetooth/bluetooth_adapter.h" 16 #include "chrome/browser/chromeos/bluetooth/bluetooth_adapter.h"
16 #include "chrome/browser/chromeos/bluetooth/bluetooth_device.h" 17 #include "chrome/browser/chromeos/bluetooth/bluetooth_device.h"
18 #include "chrome/browser/chromeos/bluetooth/bluetooth_socket.h"
17 #include "chrome/browser/chromeos/extensions/bluetooth_event_router.h" 19 #include "chrome/browser/chromeos/extensions/bluetooth_event_router.h"
18 20
19 using chromeos::BluetoothAdapter; 21 using chromeos::BluetoothAdapter;
20 using chromeos::BluetoothDevice; 22 using chromeos::BluetoothDevice;
21 23
22 #endif 24 #endif
23 25
24 namespace GetDevicesWithServiceUUID = 26 namespace GetDevicesWithServiceUUID =
25 extensions::api::experimental_bluetooth::GetDevicesWithServiceUUID; 27 extensions::api::experimental_bluetooth::GetDevicesWithServiceUUID;
26 namespace GetDevicesWithServiceName = 28 namespace GetDevicesWithServiceName =
27 extensions::api::experimental_bluetooth::GetDevicesWithServiceName; 29 extensions::api::experimental_bluetooth::GetDevicesWithServiceName;
30 namespace Connect = extensions::api::experimental_bluetooth::Connect;
31 namespace Disconnect = extensions::api::experimental_bluetooth::Disconnect;
28 32
29 namespace extensions { 33 namespace extensions {
30 namespace api { 34 namespace api {
31 35
32 #if defined(OS_CHROMEOS) 36 #if defined(OS_CHROMEOS)
33 37
38 chromeos::ExtensionBluetoothEventRouter*
39 BluetoothExtensionFunction::event_router() {
40 return profile()->GetExtensionService()->bluetooth_event_router();
41 }
42
34 const chromeos::BluetoothAdapter* BluetoothExtensionFunction::adapter() const { 43 const chromeos::BluetoothAdapter* BluetoothExtensionFunction::adapter() const {
35 return profile()->GetExtensionService()->bluetooth_event_router()->adapter(); 44 const chromeos::ExtensionBluetoothEventRouter* bluetooth_event_router =
45 profile()->GetExtensionService()->bluetooth_event_router();
46 return bluetooth_event_router->adapter();
36 } 47 }
37 48
38 chromeos::BluetoothAdapter* BluetoothExtensionFunction::GetMutableAdapter() { 49 chromeos::BluetoothAdapter* BluetoothExtensionFunction::GetMutableAdapter() {
39 return profile()->GetExtensionService()->bluetooth_event_router()-> 50 return event_router()->GetMutableAdapter();
40 GetMutableAdapter(); 51 }
52
53 chromeos::ExtensionBluetoothEventRouter*
54 AsyncBluetoothExtensionFunction::event_router() {
55 return profile()->GetExtensionService()->bluetooth_event_router();
41 } 56 }
42 57
43 const chromeos::BluetoothAdapter* 58 const chromeos::BluetoothAdapter*
44 AsyncBluetoothExtensionFunction::adapter() const { 59 AsyncBluetoothExtensionFunction::adapter() const {
45 return profile()->GetExtensionService()->bluetooth_event_router()->adapter(); 60 const chromeos::ExtensionBluetoothEventRouter* bluetooth_event_router =
61 profile()->GetExtensionService()->bluetooth_event_router();
62 return bluetooth_event_router->adapter();
46 } 63 }
47 64
48 chromeos::BluetoothAdapter* 65 chromeos::BluetoothAdapter*
49 AsyncBluetoothExtensionFunction::GetMutableAdapter() { 66 AsyncBluetoothExtensionFunction::GetMutableAdapter() {
50 return profile()->GetExtensionService()->bluetooth_event_router()-> 67 return event_router()->GetMutableAdapter();
51 GetMutableAdapter();
52 } 68 }
53 69
54 bool BluetoothIsAvailableFunction::RunImpl() { 70 bool BluetoothIsAvailableFunction::RunImpl() {
55 result_.reset(Value::CreateBooleanValue(adapter()->IsPresent())); 71 result_.reset(Value::CreateBooleanValue(adapter()->IsPresent()));
56 return true; 72 return true;
57 } 73 }
58 74
59 bool BluetoothIsPoweredFunction::RunImpl() { 75 bool BluetoothIsPoweredFunction::RunImpl() {
60 result_.reset(Value::CreateBooleanValue(adapter()->IsPowered())); 76 result_.reset(Value::CreateBooleanValue(adapter()->IsPowered()));
61 return true; 77 return true;
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
129 } 145 }
130 146
131 if (callbacks_pending_) 147 if (callbacks_pending_)
132 AddRef(); // Released in AddDeviceIfTrue when callbacks_pending_ == 0 148 AddRef(); // Released in AddDeviceIfTrue when callbacks_pending_ == 0
133 else 149 else
134 SendResponse(true); 150 SendResponse(true);
135 151
136 return true; 152 return true;
137 } 153 }
138 154
155 void BluetoothConnectFunction::ConnectToServiceCallback(
156 const chromeos::BluetoothDevice* device,
157 const std::string& service_uuid,
158 scoped_refptr<chromeos::BluetoothSocket> socket) {
159 if (socket.get()) {
160 int socket_id = event_router()->RegisterSocket(socket);
161
162 experimental_bluetooth::Socket result_socket;
163 result_socket.device.address = device->address();
164 result_socket.device.name = UTF16ToUTF8(device->GetName());
165 result_socket.service_uuid = service_uuid;
166 result_socket.id = socket_id;
167 result_.reset(result_socket.ToValue().release());
168 SendResponse(true);
169 } else {
170 SendResponse(false);
171 }
172
173 Release(); // Added in RunImpl
174 }
175
176 bool BluetoothConnectFunction::RunImpl() {
177 scoped_ptr<Connect::Params> params(Connect::Params::Create(*args_));
178
179 chromeos::BluetoothDevice* device =
180 GetMutableAdapter()->GetDevice(params->device.address);
181 if (!device) {
182 SendResponse(false);
183 return false;
184 }
185
186 AddRef();
187 device->ConnectToService(params->service,
188 base::Bind(&BluetoothConnectFunction::ConnectToServiceCallback,
189 this,
190 device,
191 params->service));
192 return true;
193 }
194
195 bool BluetoothDisconnectFunction::RunImpl() {
196 scoped_ptr<Disconnect::Params> params(Disconnect::Params::Create(*args_));
197 return event_router()->ReleaseSocket(params->socket.id);
198 }
199
139 #else 200 #else
140 201
141 // ----------------------------------------------------------------------------- 202 // -----------------------------------------------------------------------------
142 // NIY stubs 203 // NIY stubs
143 // ----------------------------------------------------------------------------- 204 // -----------------------------------------------------------------------------
144 bool BluetoothIsAvailableFunction::RunImpl() { 205 bool BluetoothIsAvailableFunction::RunImpl() {
145 NOTREACHED() << "Not implemented yet"; 206 NOTREACHED() << "Not implemented yet";
146 return false; 207 return false;
147 } 208 }
148 209
(...skipping 10 matching lines...) Expand all
159 bool BluetoothGetDevicesWithServiceUUIDFunction::RunImpl() { 220 bool BluetoothGetDevicesWithServiceUUIDFunction::RunImpl() {
160 NOTREACHED() << "Not implemented yet"; 221 NOTREACHED() << "Not implemented yet";
161 return false; 222 return false;
162 } 223 }
163 224
164 bool BluetoothGetDevicesWithServiceNameFunction::RunImpl() { 225 bool BluetoothGetDevicesWithServiceNameFunction::RunImpl() {
165 NOTREACHED() << "Not implemented yet"; 226 NOTREACHED() << "Not implemented yet";
166 return false; 227 return false;
167 } 228 }
168 229
169 #endif 230 bool BluetoothConnectFunction::RunImpl() {
231 NOTREACHED() << "Not implemented yet";
232 return false;
233 }
170 234
171 bool BluetoothDisconnectFunction::RunImpl() { 235 bool BluetoothDisconnectFunction::RunImpl() {
172 NOTREACHED() << "Not implemented yet"; 236 NOTREACHED() << "Not implemented yet";
173 return false; 237 return false;
174 } 238 }
175 239
240 #endif
241
176 bool BluetoothReadFunction::RunImpl() { 242 bool BluetoothReadFunction::RunImpl() {
177 NOTREACHED() << "Not implemented yet"; 243 NOTREACHED() << "Not implemented yet";
178 return false; 244 return false;
179 } 245 }
180 246
181 bool BluetoothSetOutOfBandPairingDataFunction::RunImpl() { 247 bool BluetoothSetOutOfBandPairingDataFunction::RunImpl() {
182 NOTREACHED() << "Not implemented yet"; 248 NOTREACHED() << "Not implemented yet";
183 return false; 249 return false;
184 } 250 }
185 251
186 bool BluetoothGetOutOfBandPairingDataFunction::RunImpl() { 252 bool BluetoothGetOutOfBandPairingDataFunction::RunImpl() {
187 NOTREACHED() << "Not implemented yet"; 253 NOTREACHED() << "Not implemented yet";
188 return false; 254 return false;
189 } 255 }
190 256
191 bool BluetoothWriteFunction::RunImpl() { 257 bool BluetoothWriteFunction::RunImpl() {
192 NOTREACHED() << "Not implemented yet"; 258 NOTREACHED() << "Not implemented yet";
193 return false; 259 return false;
194 } 260 }
195 261
196 bool BluetoothConnectFunction::RunImpl() {
197 NOTREACHED() << "Not implemented yet";
198 return false;
199 }
200
201 } // namespace api 262 } // namespace api
202 } // namespace extensions 263 } // namespace extensions
OLDNEW
« no previous file with comments | « chrome/browser/extensions/api/bluetooth/bluetooth_api.h ('k') | chrome/common/extensions/api/experimental_bluetooth.idl » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698