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_event_router.h" | 5 #include "chrome/browser/extensions/api/bluetooth/bluetooth_event_router.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/json/json_writer.h" | 11 #include "base/json/json_writer.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
14 #include "base/memory/scoped_vector.h" | 14 #include "base/memory/scoped_vector.h" |
15 #include "base/strings/utf_string_conversions.h" | 15 #include "base/strings/utf_string_conversions.h" |
16 #include "chrome/browser/chrome_notification_types.h" | 16 #include "chrome/browser/chrome_notification_types.h" |
17 #include "chrome/browser/extensions/api/bluetooth/bluetooth_api_utils.h" | 17 #include "chrome/browser/extensions/api/bluetooth/bluetooth_api_utils.h" |
18 #include "chrome/browser/extensions/event_names.h" | 18 #include "chrome/browser/extensions/event_names.h" |
19 #include "chrome/common/extensions/api/bluetooth.h" | 19 #include "chrome/common/extensions/api/bluetooth.h" |
20 #include "content/public/browser/notification_details.h" | 20 #include "content/public/browser/notification_details.h" |
21 #include "content/public/browser/notification_source.h" | 21 #include "content/public/browser/notification_source.h" |
22 #include "device/bluetooth/bluetooth_adapter.h" | 22 #include "device/bluetooth/bluetooth_adapter.h" |
23 #include "device/bluetooth/bluetooth_adapter_factory.h" | 23 #include "device/bluetooth/bluetooth_adapter_factory.h" |
24 #include "device/bluetooth/bluetooth_device.h" | 24 #include "device/bluetooth/bluetooth_device.h" |
25 #include "device/bluetooth/bluetooth_discovery_session.h" | |
26 #include "device/bluetooth/bluetooth_profile.h" | 25 #include "device/bluetooth/bluetooth_profile.h" |
27 #include "device/bluetooth/bluetooth_socket.h" | 26 #include "device/bluetooth/bluetooth_socket.h" |
28 #include "extensions/browser/event_router.h" | 27 #include "extensions/browser/event_router.h" |
29 #include "extensions/browser/extension_system.h" | 28 #include "extensions/browser/extension_system.h" |
30 | 29 |
31 namespace extensions { | 30 namespace extensions { |
32 | 31 |
33 namespace bluetooth = api::bluetooth; | 32 namespace bluetooth = api::bluetooth; |
34 | 33 |
35 // A struct storing a Bluetooth socket and the extension that added it. | 34 // A struct storing a Bluetooth socket and the extension that added it. |
36 struct ExtensionBluetoothSocketRecord { | 35 struct ExtensionBluetoothSocketRecord { |
37 std::string extension_id; | 36 std::string extension_id; |
38 scoped_refptr<device::BluetoothSocket> socket; | 37 scoped_refptr<device::BluetoothSocket> socket; |
39 }; | 38 }; |
40 | 39 |
41 // A struct storing a Bluetooth profile and the extension that added it. | 40 // A struct storing a Bluetooth profile and the extension that added it. |
42 struct ExtensionBluetoothProfileRecord { | 41 struct ExtensionBluetoothProfileRecord { |
43 std::string extension_id; | 42 std::string extension_id; |
44 device::BluetoothProfile* profile; | 43 device::BluetoothProfile* profile; |
45 }; | 44 }; |
46 | 45 |
47 ExtensionBluetoothEventRouter::ExtensionBluetoothEventRouter( | 46 ExtensionBluetoothEventRouter::ExtensionBluetoothEventRouter( |
48 content::BrowserContext* context) | 47 content::BrowserContext* context) |
49 : browser_context_(context), | 48 : send_discovery_events_(false), |
| 49 responsible_for_discovery_(false), |
| 50 browser_context_(context), |
50 adapter_(NULL), | 51 adapter_(NULL), |
51 num_event_listeners_(0), | 52 num_event_listeners_(0), |
52 next_socket_id_(1), | 53 next_socket_id_(1), |
53 weak_ptr_factory_(this) { | 54 weak_ptr_factory_(this) { |
54 DCHECK(browser_context_); | 55 DCHECK(browser_context_); |
55 registrar_.Add(this, | 56 registrar_.Add(this, |
56 chrome::NOTIFICATION_EXTENSION_UNLOADED, | 57 chrome::NOTIFICATION_EXTENSION_UNLOADED, |
57 content::Source<content::BrowserContext>(browser_context_)); | 58 content::Source<content::BrowserContext>(browser_context_)); |
58 } | 59 } |
59 | 60 |
60 ExtensionBluetoothEventRouter::~ExtensionBluetoothEventRouter() { | 61 ExtensionBluetoothEventRouter::~ExtensionBluetoothEventRouter() { |
61 if (adapter_.get()) { | 62 if (adapter_.get()) { |
62 adapter_->RemoveObserver(this); | 63 adapter_->RemoveObserver(this); |
63 adapter_ = NULL; | 64 adapter_ = NULL; |
64 } | 65 } |
65 DLOG_IF(WARNING, socket_map_.size() != 0) | 66 DLOG_IF(WARNING, socket_map_.size() != 0) |
66 << "Bluetooth sockets are still open."; | 67 << "Bluetooth sockets are still open."; |
67 socket_map_.clear(); | 68 socket_map_.clear(); |
68 | 69 |
69 for (BluetoothProfileMap::iterator iter = bluetooth_profile_map_.begin(); | 70 for (BluetoothProfileMap::iterator iter = bluetooth_profile_map_.begin(); |
70 iter != bluetooth_profile_map_.end(); | 71 iter != bluetooth_profile_map_.end(); |
71 ++iter) { | 72 ++iter) { |
72 iter->second.profile->Unregister(); | 73 iter->second.profile->Unregister(); |
73 } | 74 } |
74 for (DiscoverySessionMap::iterator iter = discovery_session_map_.begin(); | |
75 iter != discovery_session_map_.end(); | |
76 ++iter) { | |
77 delete iter->second; | |
78 } | |
79 } | 75 } |
80 | 76 |
81 bool ExtensionBluetoothEventRouter::IsBluetoothSupported() const { | 77 bool ExtensionBluetoothEventRouter::IsBluetoothSupported() const { |
82 return adapter_.get() || | 78 return adapter_.get() || |
83 device::BluetoothAdapterFactory::IsBluetoothAdapterAvailable(); | 79 device::BluetoothAdapterFactory::IsBluetoothAdapterAvailable(); |
84 } | 80 } |
85 | 81 |
86 void ExtensionBluetoothEventRouter::GetAdapter( | 82 void ExtensionBluetoothEventRouter::GetAdapter( |
87 const device::BluetoothAdapterFactory::AdapterCallback& callback) { | 83 const device::BluetoothAdapterFactory::AdapterCallback& callback) { |
88 if (adapter_.get()) { | 84 if (adapter_.get()) { |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
142 device::BluetoothProfile* bluetooth_profile = iter->second.profile; | 138 device::BluetoothProfile* bluetooth_profile = iter->second.profile; |
143 bluetooth_profile_map_.erase(iter); | 139 bluetooth_profile_map_.erase(iter); |
144 bluetooth_profile->Unregister(); | 140 bluetooth_profile->Unregister(); |
145 } | 141 } |
146 } | 142 } |
147 | 143 |
148 bool ExtensionBluetoothEventRouter::HasProfile(const std::string& uuid) const { | 144 bool ExtensionBluetoothEventRouter::HasProfile(const std::string& uuid) const { |
149 return bluetooth_profile_map_.find(uuid) != bluetooth_profile_map_.end(); | 145 return bluetooth_profile_map_.find(uuid) != bluetooth_profile_map_.end(); |
150 } | 146 } |
151 | 147 |
152 void ExtensionBluetoothEventRouter::StartDiscoverySession( | |
153 device::BluetoothAdapter* adapter, | |
154 const std::string& extension_id, | |
155 const base::Closure& callback, | |
156 const base::Closure& error_callback) { | |
157 if (adapter != adapter_.get()) { | |
158 error_callback.Run(); | |
159 return; | |
160 } | |
161 DiscoverySessionMap::iterator iter = | |
162 discovery_session_map_.find(extension_id); | |
163 if (iter != discovery_session_map_.end() && iter->second->IsActive()) { | |
164 DVLOG(1) << "An active discovery session exists for extension."; | |
165 error_callback.Run(); | |
166 return; | |
167 } | |
168 adapter->StartDiscoverySession( | |
169 base::Bind(&ExtensionBluetoothEventRouter::OnStartDiscoverySession, | |
170 weak_ptr_factory_.GetWeakPtr(), | |
171 extension_id, | |
172 callback), | |
173 error_callback); | |
174 } | |
175 | |
176 void ExtensionBluetoothEventRouter::StopDiscoverySession( | |
177 device::BluetoothAdapter* adapter, | |
178 const std::string& extension_id, | |
179 const base::Closure& callback, | |
180 const base::Closure& error_callback) { | |
181 if (adapter != adapter_.get()) { | |
182 error_callback.Run(); | |
183 return; | |
184 } | |
185 DiscoverySessionMap::iterator iter = | |
186 discovery_session_map_.find(extension_id); | |
187 if (iter == discovery_session_map_.end() || !iter->second->IsActive()) { | |
188 DVLOG(1) << "No active discovery session exists for extension."; | |
189 error_callback.Run(); | |
190 return; | |
191 } | |
192 device::BluetoothDiscoverySession* session = iter->second; | |
193 session->Stop(callback, error_callback); | |
194 } | |
195 | |
196 device::BluetoothProfile* ExtensionBluetoothEventRouter::GetProfile( | 148 device::BluetoothProfile* ExtensionBluetoothEventRouter::GetProfile( |
197 const std::string& uuid) const { | 149 const std::string& uuid) const { |
198 BluetoothProfileMap::const_iterator iter = bluetooth_profile_map_.find(uuid); | 150 BluetoothProfileMap::const_iterator iter = bluetooth_profile_map_.find(uuid); |
199 if (iter != bluetooth_profile_map_.end()) | 151 if (iter != bluetooth_profile_map_.end()) |
200 return iter->second.profile; | 152 return iter->second.profile; |
201 | 153 |
202 return NULL; | 154 return NULL; |
203 } | 155 } |
204 | 156 |
205 scoped_refptr<device::BluetoothSocket> | 157 scoped_refptr<device::BluetoothSocket> |
206 ExtensionBluetoothEventRouter::GetSocket(int id) { | 158 ExtensionBluetoothEventRouter::GetSocket(int id) { |
207 SocketMap::iterator socket_entry = socket_map_.find(id); | 159 SocketMap::iterator socket_entry = socket_map_.find(id); |
208 if (socket_entry == socket_map_.end()) | 160 if (socket_entry == socket_map_.end()) |
209 return NULL; | 161 return NULL; |
210 return socket_entry->second.socket; | 162 return socket_entry->second.socket; |
211 } | 163 } |
212 | 164 |
| 165 void ExtensionBluetoothEventRouter::SetResponsibleForDiscovery( |
| 166 bool responsible) { |
| 167 responsible_for_discovery_ = responsible; |
| 168 } |
| 169 |
| 170 bool ExtensionBluetoothEventRouter::IsResponsibleForDiscovery() const { |
| 171 return responsible_for_discovery_; |
| 172 } |
| 173 |
| 174 void ExtensionBluetoothEventRouter::SetSendDiscoveryEvents(bool should_send) { |
| 175 // At the transition into sending devices, also send past devices that |
| 176 // were discovered as they will not be discovered again. |
| 177 if (should_send && !send_discovery_events_) { |
| 178 for (DeviceList::const_iterator i = discovered_devices_.begin(); |
| 179 i != discovered_devices_.end(); ++i) { |
| 180 DispatchDeviceEvent(extensions::event_names::kBluetoothOnDeviceDiscovered, |
| 181 **i); |
| 182 } |
| 183 } |
| 184 |
| 185 send_discovery_events_ = should_send; |
| 186 } |
| 187 |
213 void ExtensionBluetoothEventRouter::DispatchDeviceEvent( | 188 void ExtensionBluetoothEventRouter::DispatchDeviceEvent( |
214 const std::string& event_name, const bluetooth::Device& device) { | 189 const std::string& event_name, const bluetooth::Device& device) { |
215 scoped_ptr<base::ListValue> args(new base::ListValue()); | 190 scoped_ptr<base::ListValue> args(new base::ListValue()); |
216 args->Append(device.ToValue().release()); | 191 args->Append(device.ToValue().release()); |
217 scoped_ptr<Event> event(new Event(event_name, args.Pass())); | 192 scoped_ptr<Event> event(new Event(event_name, args.Pass())); |
218 ExtensionSystem::Get(browser_context_)->event_router()->BroadcastEvent( | 193 ExtensionSystem::Get(browser_context_)->event_router()->BroadcastEvent( |
219 event.Pass()); | 194 event.Pass()); |
220 } | 195 } |
221 | 196 |
222 void ExtensionBluetoothEventRouter::DispatchConnectionEvent( | 197 void ExtensionBluetoothEventRouter::DispatchConnectionEvent( |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
261 } | 236 } |
262 | 237 |
263 void ExtensionBluetoothEventRouter::AdapterDiscoveringChanged( | 238 void ExtensionBluetoothEventRouter::AdapterDiscoveringChanged( |
264 device::BluetoothAdapter* adapter, bool discovering) { | 239 device::BluetoothAdapter* adapter, bool discovering) { |
265 if (adapter != adapter_.get()) { | 240 if (adapter != adapter_.get()) { |
266 DVLOG(1) << "Ignoring event for adapter " << adapter->GetAddress(); | 241 DVLOG(1) << "Ignoring event for adapter " << adapter->GetAddress(); |
267 return; | 242 return; |
268 } | 243 } |
269 | 244 |
270 if (!discovering) { | 245 if (!discovering) { |
271 // If any discovery sessions are inactive, clean them up. | 246 send_discovery_events_ = false; |
272 DiscoverySessionMap active_session_map; | 247 responsible_for_discovery_ = false; |
273 for (DiscoverySessionMap::iterator iter = discovery_session_map_.begin(); | 248 discovered_devices_.clear(); |
274 iter != discovery_session_map_.end(); | |
275 ++iter) { | |
276 device::BluetoothDiscoverySession* session = iter->second; | |
277 if (session->IsActive()) { | |
278 active_session_map[iter->first] = session; | |
279 continue; | |
280 } | |
281 delete session; | |
282 } | |
283 discovery_session_map_.swap(active_session_map); | |
284 } | 249 } |
285 | 250 |
286 DispatchAdapterStateEvent(); | 251 DispatchAdapterStateEvent(); |
287 } | 252 } |
288 | 253 |
289 void ExtensionBluetoothEventRouter::DeviceAdded( | 254 void ExtensionBluetoothEventRouter::DeviceAdded( |
290 device::BluetoothAdapter* adapter, | 255 device::BluetoothAdapter* adapter, |
291 device::BluetoothDevice* device) { | 256 device::BluetoothDevice* device) { |
292 if (adapter != adapter_.get()) { | 257 if (adapter != adapter_.get()) { |
293 DVLOG(1) << "Ignoring event for adapter " << adapter->GetAddress(); | 258 DVLOG(1) << "Ignoring event for adapter " << adapter->GetAddress(); |
294 return; | 259 return; |
295 } | 260 } |
296 | 261 |
297 bluetooth::Device* extension_device = | 262 bluetooth::Device* extension_device = |
298 new bluetooth::Device(); | 263 new bluetooth::Device(); |
299 bluetooth::BluetoothDeviceToApiDevice( | 264 bluetooth::BluetoothDeviceToApiDevice( |
300 *device, extension_device); | 265 *device, extension_device); |
| 266 discovered_devices_.push_back(extension_device); |
| 267 |
| 268 if (!send_discovery_events_) |
| 269 return; |
301 | 270 |
302 DispatchDeviceEvent(extensions::event_names::kBluetoothOnDeviceDiscovered, | 271 DispatchDeviceEvent(extensions::event_names::kBluetoothOnDeviceDiscovered, |
303 *extension_device); | 272 *extension_device); |
304 } | 273 } |
305 | 274 |
306 void ExtensionBluetoothEventRouter::InitializeAdapterIfNeeded() { | 275 void ExtensionBluetoothEventRouter::InitializeAdapterIfNeeded() { |
307 if (!adapter_.get()) { | 276 if (!adapter_.get()) { |
308 GetAdapter(base::Bind(&ExtensionBluetoothEventRouter::InitializeAdapter, | 277 GetAdapter(base::Bind(&ExtensionBluetoothEventRouter::InitializeAdapter, |
309 weak_ptr_factory_.GetWeakPtr())); | 278 weak_ptr_factory_.GetWeakPtr())); |
310 } | 279 } |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
355 // Remove all sockets opened by the extension. | 324 // Remove all sockets opened by the extension. |
356 SocketMap::iterator socket_iter = socket_map_.begin(); | 325 SocketMap::iterator socket_iter = socket_map_.begin(); |
357 while (socket_iter != socket_map_.end()) { | 326 while (socket_iter != socket_map_.end()) { |
358 int socket_id = socket_iter->first; | 327 int socket_id = socket_iter->first; |
359 ExtensionBluetoothSocketRecord record = socket_iter->second; | 328 ExtensionBluetoothSocketRecord record = socket_iter->second; |
360 socket_iter++; | 329 socket_iter++; |
361 if (record.extension_id == extension_id) { | 330 if (record.extension_id == extension_id) { |
362 ReleaseSocket(socket_id); | 331 ReleaseSocket(socket_id); |
363 } | 332 } |
364 } | 333 } |
365 | |
366 // Remove any discovery session initiated by the extension. | |
367 DiscoverySessionMap::iterator session_iter = | |
368 discovery_session_map_.find(extension_id); | |
369 if (session_iter == discovery_session_map_.end()) | |
370 return; | |
371 delete session_iter->second; | |
372 discovery_session_map_.erase(session_iter); | |
373 } | |
374 | |
375 void ExtensionBluetoothEventRouter::OnStartDiscoverySession( | |
376 const std::string& extension_id, | |
377 const base::Closure& callback, | |
378 scoped_ptr<device::BluetoothDiscoverySession> discovery_session) { | |
379 // Clean up any existing session instance for the extension. | |
380 DiscoverySessionMap::iterator iter = | |
381 discovery_session_map_.find(extension_id); | |
382 if (iter != discovery_session_map_.end()) | |
383 delete iter->second; | |
384 discovery_session_map_[extension_id] = discovery_session.release(); | |
385 callback.Run(); | |
386 } | 334 } |
387 | 335 |
388 void ExtensionBluetoothEventRouter::Observe( | 336 void ExtensionBluetoothEventRouter::Observe( |
389 int type, | 337 int type, |
390 const content::NotificationSource& source, | 338 const content::NotificationSource& source, |
391 const content::NotificationDetails& details) { | 339 const content::NotificationDetails& details) { |
392 switch (type) { | 340 switch (type) { |
393 case chrome::NOTIFICATION_EXTENSION_UNLOADED: { | 341 case chrome::NOTIFICATION_EXTENSION_UNLOADED: { |
394 extensions::UnloadedExtensionInfo* info = | 342 extensions::UnloadedExtensionInfo* info = |
395 content::Details<extensions::UnloadedExtensionInfo>(details).ptr(); | 343 content::Details<extensions::UnloadedExtensionInfo>(details).ptr(); |
396 CleanUpForExtension(info->extension->id()); | 344 CleanUpForExtension(info->extension->id()); |
397 break; | 345 break; |
398 } | 346 } |
399 } | 347 } |
400 } | 348 } |
401 | 349 |
402 } // namespace extensions | 350 } // namespace extensions |
OLD | NEW |