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/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/memory/ref_counted.h" |
15 #include "base/synchronization/lock.h" | 15 #include "base/synchronization/lock.h" |
16 #include "chrome/browser/chromeos/bluetooth/bluetooth_adapter.h" | 16 #include "chrome/browser/chromeos/bluetooth/bluetooth_adapter.h" |
17 #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" | 18 #include "chrome/browser/chromeos/bluetooth/bluetooth_socket.h" |
19 #include "chrome/browser/chromeos/extensions/bluetooth_event_router.h" | 19 #include "chrome/browser/chromeos/extensions/bluetooth_event_router.h" |
20 | 20 |
21 using chromeos::BluetoothAdapter; | 21 using chromeos::BluetoothAdapter; |
22 using chromeos::BluetoothDevice; | 22 using chromeos::BluetoothDevice; |
23 | 23 |
| 24 namespace { |
| 25 |
| 26 chromeos::ExtensionBluetoothEventRouter* GetEventRouter(Profile* profile) { |
| 27 return profile->GetExtensionService()->bluetooth_event_router(); |
| 28 } |
| 29 |
| 30 const chromeos::BluetoothAdapter* GetAdapter(Profile* profile) { |
| 31 return GetEventRouter(profile)->adapter(); |
| 32 } |
| 33 |
| 34 chromeos::BluetoothAdapter* GetMutableAdapter(Profile* profile) { |
| 35 return GetEventRouter(profile)->GetMutableAdapter(); |
| 36 } |
| 37 |
| 38 } // namespace |
24 #endif | 39 #endif |
25 | 40 |
26 namespace GetDevicesWithServiceUUID = | 41 namespace GetDevicesWithServiceUUID = |
27 extensions::api::experimental_bluetooth::GetDevicesWithServiceUUID; | 42 extensions::api::experimental_bluetooth::GetDevicesWithServiceUUID; |
28 namespace GetDevicesWithServiceName = | 43 namespace GetDevicesWithServiceName = |
29 extensions::api::experimental_bluetooth::GetDevicesWithServiceName; | 44 extensions::api::experimental_bluetooth::GetDevicesWithServiceName; |
30 namespace Connect = extensions::api::experimental_bluetooth::Connect; | 45 namespace Connect = extensions::api::experimental_bluetooth::Connect; |
31 namespace Disconnect = extensions::api::experimental_bluetooth::Disconnect; | 46 namespace Disconnect = extensions::api::experimental_bluetooth::Disconnect; |
32 | 47 |
33 namespace extensions { | 48 namespace extensions { |
34 namespace api { | 49 namespace api { |
35 | 50 |
36 #if defined(OS_CHROMEOS) | 51 #if defined(OS_CHROMEOS) |
37 | 52 |
38 chromeos::ExtensionBluetoothEventRouter* | |
39 BluetoothExtensionFunction::event_router() { | |
40 return profile()->GetExtensionService()->bluetooth_event_router(); | |
41 } | |
42 | |
43 const chromeos::BluetoothAdapter* BluetoothExtensionFunction::adapter() const { | |
44 const chromeos::ExtensionBluetoothEventRouter* bluetooth_event_router = | |
45 profile()->GetExtensionService()->bluetooth_event_router(); | |
46 return bluetooth_event_router->adapter(); | |
47 } | |
48 | |
49 chromeos::BluetoothAdapter* BluetoothExtensionFunction::GetMutableAdapter() { | |
50 return event_router()->GetMutableAdapter(); | |
51 } | |
52 | |
53 chromeos::ExtensionBluetoothEventRouter* | |
54 AsyncBluetoothExtensionFunction::event_router() { | |
55 return profile()->GetExtensionService()->bluetooth_event_router(); | |
56 } | |
57 | |
58 const chromeos::BluetoothAdapter* | |
59 AsyncBluetoothExtensionFunction::adapter() const { | |
60 const chromeos::ExtensionBluetoothEventRouter* bluetooth_event_router = | |
61 profile()->GetExtensionService()->bluetooth_event_router(); | |
62 return bluetooth_event_router->adapter(); | |
63 } | |
64 | |
65 chromeos::BluetoothAdapter* | |
66 AsyncBluetoothExtensionFunction::GetMutableAdapter() { | |
67 return event_router()->GetMutableAdapter(); | |
68 } | |
69 | |
70 bool BluetoothIsAvailableFunction::RunImpl() { | 53 bool BluetoothIsAvailableFunction::RunImpl() { |
71 result_.reset(Value::CreateBooleanValue(adapter()->IsPresent())); | 54 result_.reset(Value::CreateBooleanValue(GetAdapter(profile())->IsPresent())); |
72 return true; | 55 return true; |
73 } | 56 } |
74 | 57 |
75 bool BluetoothIsPoweredFunction::RunImpl() { | 58 bool BluetoothIsPoweredFunction::RunImpl() { |
76 result_.reset(Value::CreateBooleanValue(adapter()->IsPowered())); | 59 result_.reset(Value::CreateBooleanValue(GetAdapter(profile())->IsPowered())); |
77 return true; | 60 return true; |
78 } | 61 } |
79 | 62 |
80 bool BluetoothGetAddressFunction::RunImpl() { | 63 bool BluetoothGetAddressFunction::RunImpl() { |
81 result_.reset(Value::CreateStringValue(adapter()->address())); | 64 result_.reset(Value::CreateStringValue(GetAdapter(profile())->address())); |
82 return true; | 65 return true; |
83 } | 66 } |
84 | 67 |
85 bool BluetoothGetDevicesWithServiceUUIDFunction::RunImpl() { | 68 bool BluetoothGetDevicesWithServiceUUIDFunction::RunImpl() { |
86 scoped_ptr<GetDevicesWithServiceUUID::Params> params( | 69 scoped_ptr<GetDevicesWithServiceUUID::Params> params( |
87 GetDevicesWithServiceUUID::Params::Create(*args_)); | 70 GetDevicesWithServiceUUID::Params::Create(*args_)); |
88 | 71 |
89 const BluetoothAdapter::ConstDeviceList& devices = adapter()->GetDevices(); | 72 const BluetoothAdapter::ConstDeviceList& devices = |
| 73 GetAdapter(profile())->GetDevices(); |
90 ListValue* matches = new ListValue; | 74 ListValue* matches = new ListValue; |
91 for (BluetoothAdapter::ConstDeviceList::const_iterator i = | 75 for (BluetoothAdapter::ConstDeviceList::const_iterator i = |
92 devices.begin(); i != devices.end(); ++i) { | 76 devices.begin(); i != devices.end(); ++i) { |
93 if ((*i)->ProvidesServiceWithUUID(params->uuid)) { | 77 if ((*i)->ProvidesServiceWithUUID(params->uuid)) { |
94 experimental_bluetooth::Device device; | 78 experimental_bluetooth::Device device; |
95 device.name = UTF16ToUTF8((*i)->GetName()); | 79 device.name = UTF16ToUTF8((*i)->GetName()); |
96 device.address = (*i)->address(); | 80 device.address = (*i)->address(); |
97 matches->Append(device.ToValue().release()); | 81 matches->Append(device.ToValue().release()); |
98 } | 82 } |
99 } | 83 } |
(...skipping 25 matching lines...) Expand all Loading... |
125 | 109 |
126 bool BluetoothGetDevicesWithServiceNameFunction::RunImpl() { | 110 bool BluetoothGetDevicesWithServiceNameFunction::RunImpl() { |
127 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 111 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
128 | 112 |
129 scoped_ptr<GetDevicesWithServiceName::Params> params( | 113 scoped_ptr<GetDevicesWithServiceName::Params> params( |
130 GetDevicesWithServiceName::Params::Create(*args_)); | 114 GetDevicesWithServiceName::Params::Create(*args_)); |
131 | 115 |
132 ListValue* matches = new ListValue; | 116 ListValue* matches = new ListValue; |
133 result_.reset(matches); | 117 result_.reset(matches); |
134 | 118 |
135 BluetoothAdapter::DeviceList devices = GetMutableAdapter()->GetDevices(); | 119 BluetoothAdapter::DeviceList devices = |
| 120 GetMutableAdapter(profile())->GetDevices(); |
136 callbacks_pending_ = 0; | 121 callbacks_pending_ = 0; |
137 for (BluetoothAdapter::DeviceList::iterator i = devices.begin(); | 122 for (BluetoothAdapter::DeviceList::iterator i = devices.begin(); |
138 i != devices.end(); ++i) { | 123 i != devices.end(); ++i) { |
139 (*i)->ProvidesServiceWithName(params->name, | 124 (*i)->ProvidesServiceWithName(params->name, |
140 base::Bind(&BluetoothGetDevicesWithServiceNameFunction::AddDeviceIfTrue, | 125 base::Bind(&BluetoothGetDevicesWithServiceNameFunction::AddDeviceIfTrue, |
141 this, | 126 this, |
142 matches, | 127 matches, |
143 *i)); | 128 *i)); |
144 callbacks_pending_++; | 129 callbacks_pending_++; |
145 } | 130 } |
146 | 131 |
147 if (callbacks_pending_) | 132 if (callbacks_pending_) |
148 AddRef(); // Released in AddDeviceIfTrue when callbacks_pending_ == 0 | 133 AddRef(); // Released in AddDeviceIfTrue when callbacks_pending_ == 0 |
149 else | 134 else |
150 SendResponse(true); | 135 SendResponse(true); |
151 | 136 |
152 return true; | 137 return true; |
153 } | 138 } |
154 | 139 |
155 void BluetoothConnectFunction::ConnectToServiceCallback( | 140 void BluetoothConnectFunction::ConnectToServiceCallback( |
156 const chromeos::BluetoothDevice* device, | 141 const chromeos::BluetoothDevice* device, |
157 const std::string& service_uuid, | 142 const std::string& service_uuid, |
158 scoped_refptr<chromeos::BluetoothSocket> socket) { | 143 scoped_refptr<chromeos::BluetoothSocket> socket) { |
159 if (socket.get()) { | 144 if (socket.get()) { |
160 int socket_id = event_router()->RegisterSocket(socket); | 145 int socket_id = GetEventRouter(profile())->RegisterSocket(socket); |
161 | 146 |
162 experimental_bluetooth::Socket result_socket; | 147 experimental_bluetooth::Socket result_socket; |
163 result_socket.device.address = device->address(); | 148 result_socket.device.address = device->address(); |
164 result_socket.device.name = UTF16ToUTF8(device->GetName()); | 149 result_socket.device.name = UTF16ToUTF8(device->GetName()); |
165 result_socket.service_uuid = service_uuid; | 150 result_socket.service_uuid = service_uuid; |
166 result_socket.id = socket_id; | 151 result_socket.id = socket_id; |
167 result_.reset(result_socket.ToValue().release()); | 152 result_.reset(result_socket.ToValue().release()); |
168 SendResponse(true); | 153 SendResponse(true); |
169 } else { | 154 } else { |
170 SendResponse(false); | 155 SendResponse(false); |
171 } | 156 } |
172 | 157 |
173 Release(); // Added in RunImpl | 158 Release(); // Added in RunImpl |
174 } | 159 } |
175 | 160 |
176 bool BluetoothConnectFunction::RunImpl() { | 161 bool BluetoothConnectFunction::RunImpl() { |
177 scoped_ptr<Connect::Params> params(Connect::Params::Create(*args_)); | 162 scoped_ptr<Connect::Params> params(Connect::Params::Create(*args_)); |
178 | 163 |
179 chromeos::BluetoothDevice* device = | 164 chromeos::BluetoothDevice* device = |
180 GetMutableAdapter()->GetDevice(params->device.address); | 165 GetMutableAdapter(profile())->GetDevice(params->device.address); |
181 if (!device) { | 166 if (!device) { |
182 SendResponse(false); | 167 SendResponse(false); |
183 return false; | 168 return false; |
184 } | 169 } |
185 | 170 |
186 AddRef(); | 171 AddRef(); |
187 device->ConnectToService(params->service, | 172 device->ConnectToService(params->service, |
188 base::Bind(&BluetoothConnectFunction::ConnectToServiceCallback, | 173 base::Bind(&BluetoothConnectFunction::ConnectToServiceCallback, |
189 this, | 174 this, |
190 device, | 175 device, |
191 params->service)); | 176 params->service)); |
192 return true; | 177 return true; |
193 } | 178 } |
194 | 179 |
195 bool BluetoothDisconnectFunction::RunImpl() { | 180 bool BluetoothDisconnectFunction::RunImpl() { |
196 scoped_ptr<Disconnect::Params> params(Disconnect::Params::Create(*args_)); | 181 scoped_ptr<Disconnect::Params> params(Disconnect::Params::Create(*args_)); |
197 return event_router()->ReleaseSocket(params->socket.id); | 182 return GetEventRouter(profile())->ReleaseSocket(params->socket.id); |
198 } | 183 } |
199 | 184 |
200 #else | 185 #else |
201 | 186 |
202 // ----------------------------------------------------------------------------- | 187 // ----------------------------------------------------------------------------- |
203 // NIY stubs | 188 // NIY stubs |
204 // ----------------------------------------------------------------------------- | 189 // ----------------------------------------------------------------------------- |
205 bool BluetoothIsAvailableFunction::RunImpl() { | 190 bool BluetoothIsAvailableFunction::RunImpl() { |
206 NOTREACHED() << "Not implemented yet"; | 191 NOTREACHED() << "Not implemented yet"; |
207 return false; | 192 return false; |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
254 return false; | 239 return false; |
255 } | 240 } |
256 | 241 |
257 bool BluetoothWriteFunction::RunImpl() { | 242 bool BluetoothWriteFunction::RunImpl() { |
258 NOTREACHED() << "Not implemented yet"; | 243 NOTREACHED() << "Not implemented yet"; |
259 return false; | 244 return false; |
260 } | 245 } |
261 | 246 |
262 } // namespace api | 247 } // namespace api |
263 } // namespace extensions | 248 } // namespace extensions |
OLD | NEW |