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

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

Issue 177113013: Bluetooth: add Device events, and cleanup JS API (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@arman-patch
Patch Set: Created 6 years, 9 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
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_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"
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after
203 } 203 }
204 204
205 scoped_refptr<device::BluetoothSocket> 205 scoped_refptr<device::BluetoothSocket>
206 ExtensionBluetoothEventRouter::GetSocket(int id) { 206 ExtensionBluetoothEventRouter::GetSocket(int id) {
207 SocketMap::iterator socket_entry = socket_map_.find(id); 207 SocketMap::iterator socket_entry = socket_map_.find(id);
208 if (socket_entry == socket_map_.end()) 208 if (socket_entry == socket_map_.end())
209 return NULL; 209 return NULL;
210 return socket_entry->second.socket; 210 return socket_entry->second.socket;
211 } 211 }
212 212
213 void ExtensionBluetoothEventRouter::DispatchDeviceEvent(
214 const std::string& event_name, const bluetooth::Device& device) {
215 scoped_ptr<base::ListValue> args(new base::ListValue());
216 args->Append(device.ToValue().release());
217 scoped_ptr<Event> event(new Event(event_name, args.Pass()));
218 ExtensionSystem::Get(browser_context_)->event_router()->BroadcastEvent(
219 event.Pass());
220 }
221
222 void ExtensionBluetoothEventRouter::DispatchConnectionEvent( 213 void ExtensionBluetoothEventRouter::DispatchConnectionEvent(
223 const std::string& extension_id, 214 const std::string& extension_id,
224 const std::string& uuid, 215 const std::string& uuid,
225 const device::BluetoothDevice* device, 216 const device::BluetoothDevice* device,
226 scoped_refptr<device::BluetoothSocket> socket) { 217 scoped_refptr<device::BluetoothSocket> socket) {
227 if (!HasProfile(uuid)) 218 if (!HasProfile(uuid))
228 return; 219 return;
229 220
230 int socket_id = RegisterSocket(extension_id, socket); 221 int socket_id = RegisterSocket(extension_id, socket);
231 api::bluetooth::Socket result_socket; 222 api::bluetooth::Socket result_socket;
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
287 } 278 }
288 279
289 void ExtensionBluetoothEventRouter::DeviceAdded( 280 void ExtensionBluetoothEventRouter::DeviceAdded(
290 device::BluetoothAdapter* adapter, 281 device::BluetoothAdapter* adapter,
291 device::BluetoothDevice* device) { 282 device::BluetoothDevice* device) {
292 if (adapter != adapter_.get()) { 283 if (adapter != adapter_.get()) {
293 DVLOG(1) << "Ignoring event for adapter " << adapter->GetAddress(); 284 DVLOG(1) << "Ignoring event for adapter " << adapter->GetAddress();
294 return; 285 return;
295 } 286 }
296 287
297 bluetooth::Device* extension_device = 288 DispatchDeviceEvent(extensions::event_names::kBluetoothOnDeviceAdded,
298 new bluetooth::Device(); 289 device);
299 bluetooth::BluetoothDeviceToApiDevice( 290 }
300 *device, extension_device);
301 291
302 DispatchDeviceEvent(extensions::event_names::kBluetoothOnDeviceDiscovered, 292 void ExtensionBluetoothEventRouter::DeviceChanged(
303 *extension_device); 293 device::BluetoothAdapter* adapter,
294 device::BluetoothDevice* device) {
295 if (adapter != adapter_.get()) {
296 DVLOG(1) << "Ignoring event for adapter " << adapter->GetAddress();
297 return;
298 }
299
300 DispatchDeviceEvent(extensions::event_names::kBluetoothOnDeviceChanged,
301 device);
302 }
303
304 void ExtensionBluetoothEventRouter::DeviceRemoved(
305 device::BluetoothAdapter* adapter,
306 device::BluetoothDevice* device) {
307 if (adapter != adapter_.get()) {
308 DVLOG(1) << "Ignoring event for adapter " << adapter->GetAddress();
309 return;
310 }
311
312 DispatchDeviceEvent(extensions::event_names::kBluetoothOnDeviceRemoved,
313 device);
304 } 314 }
305 315
306 void ExtensionBluetoothEventRouter::InitializeAdapterIfNeeded() { 316 void ExtensionBluetoothEventRouter::InitializeAdapterIfNeeded() {
307 if (!adapter_.get()) { 317 if (!adapter_.get()) {
308 GetAdapter(base::Bind(&ExtensionBluetoothEventRouter::InitializeAdapter, 318 GetAdapter(base::Bind(&ExtensionBluetoothEventRouter::InitializeAdapter,
309 weak_ptr_factory_.GetWeakPtr())); 319 weak_ptr_factory_.GetWeakPtr()));
310 } 320 }
311 } 321 }
312 322
313 void ExtensionBluetoothEventRouter::InitializeAdapter( 323 void ExtensionBluetoothEventRouter::InitializeAdapter(
(...skipping 17 matching lines...) Expand all
331 341
332 scoped_ptr<base::ListValue> args(new base::ListValue()); 342 scoped_ptr<base::ListValue> args(new base::ListValue());
333 args->Append(state.ToValue().release()); 343 args->Append(state.ToValue().release());
334 scoped_ptr<Event> event(new Event( 344 scoped_ptr<Event> event(new Event(
335 bluetooth::OnAdapterStateChanged::kEventName, 345 bluetooth::OnAdapterStateChanged::kEventName,
336 args.Pass())); 346 args.Pass()));
337 ExtensionSystem::Get(browser_context_)->event_router()->BroadcastEvent( 347 ExtensionSystem::Get(browser_context_)->event_router()->BroadcastEvent(
338 event.Pass()); 348 event.Pass());
339 } 349 }
340 350
351 void ExtensionBluetoothEventRouter::DispatchDeviceEvent(
352 const std::string& event_name,
353 device::BluetoothDevice* device) {
354 bluetooth::Device extension_device;
355 bluetooth::BluetoothDeviceToApiDevice(*device, &extension_device);
356
357 scoped_ptr<base::ListValue> args(new base::ListValue());
rpaquay 2014/03/06 00:11:19 You should be able to use auto-generated code, i.e
keybuk 2014/03/06 21:33:44 Done.
358 args->Append(extension_device.ToValue().release());
359 scoped_ptr<Event> event(new Event(event_name, args.Pass()));
360 ExtensionSystem::Get(browser_context_)->event_router()->BroadcastEvent(
361 event.Pass());
362 }
363
341 void ExtensionBluetoothEventRouter::CleanUpForExtension( 364 void ExtensionBluetoothEventRouter::CleanUpForExtension(
342 const std::string& extension_id) { 365 const std::string& extension_id) {
343 // Remove all profiles added by the extension. 366 // Remove all profiles added by the extension.
344 BluetoothProfileMap::iterator profile_iter = bluetooth_profile_map_.begin(); 367 BluetoothProfileMap::iterator profile_iter = bluetooth_profile_map_.begin();
345 while (profile_iter != bluetooth_profile_map_.end()) { 368 while (profile_iter != bluetooth_profile_map_.end()) {
346 ExtensionBluetoothProfileRecord record = profile_iter->second; 369 ExtensionBluetoothProfileRecord record = profile_iter->second;
347 if (record.extension_id == extension_id) { 370 if (record.extension_id == extension_id) {
348 bluetooth_profile_map_.erase(profile_iter++); 371 bluetooth_profile_map_.erase(profile_iter++);
349 record.profile->Unregister(); 372 record.profile->Unregister();
350 } else { 373 } else {
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
393 case chrome::NOTIFICATION_EXTENSION_UNLOADED: { 416 case chrome::NOTIFICATION_EXTENSION_UNLOADED: {
394 extensions::UnloadedExtensionInfo* info = 417 extensions::UnloadedExtensionInfo* info =
395 content::Details<extensions::UnloadedExtensionInfo>(details).ptr(); 418 content::Details<extensions::UnloadedExtensionInfo>(details).ptr();
396 CleanUpForExtension(info->extension->id()); 419 CleanUpForExtension(info->extension->id());
397 break; 420 break;
398 } 421 }
399 } 422 }
400 } 423 }
401 424
402 } // namespace extensions 425 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698