OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/chromeos/extensions/bluetooth_event_router.h" | |
6 | |
7 #include <map> | |
8 | |
9 #include "base/json/json_writer.h" | |
10 #include "base/memory/ref_counted.h" | |
11 #include "base/memory/scoped_vector.h" | |
12 #include "base/utf_string_conversions.h" | |
13 #include "chrome/browser/chromeos/bluetooth/bluetooth_adapter.h" | |
14 #include "chrome/browser/chromeos/bluetooth/bluetooth_adapter_factory.h" | |
15 #include "chrome/browser/chromeos/bluetooth/bluetooth_device_chromeos.h" | |
16 #include "chrome/browser/chromeos/bluetooth/bluetooth_socket.h" | |
17 #include "chrome/browser/extensions/api/bluetooth/bluetooth_api_utils.h" | |
18 #include "chrome/browser/extensions/event_names.h" | |
19 #include "chrome/browser/extensions/event_router.h" | |
20 #include "chrome/common/extensions/api/experimental_bluetooth.h" | |
21 | |
22 namespace experimental_bluetooth = extensions::api::experimental_bluetooth; | |
23 | |
24 namespace chromeos { | |
25 | |
26 ExtensionBluetoothEventRouter::ExtensionBluetoothEventRouter(Profile* profile) | |
27 : profile_(profile), | |
28 adapter_(chromeos::BluetoothAdapterFactory::DefaultAdapter()), | |
29 next_socket_id_(1) { | |
30 DCHECK(profile_); | |
31 DCHECK(adapter_.get()); | |
32 adapter_->AddObserver(this); | |
33 } | |
34 | |
35 ExtensionBluetoothEventRouter::~ExtensionBluetoothEventRouter() { | |
36 adapter_->RemoveObserver(this); | |
37 socket_map_.clear(); | |
38 } | |
39 | |
40 int ExtensionBluetoothEventRouter::RegisterSocket( | |
41 scoped_refptr<BluetoothSocket> socket) { | |
42 // If there is a socket registered with the same fd, just return it's id | |
43 for (SocketMap::const_iterator i = socket_map_.begin(); | |
44 i != socket_map_.end(); ++i) { | |
45 if (i->second->fd() == socket->fd()) { | |
46 return i->first; | |
47 } | |
48 } | |
49 int return_id = next_socket_id_++; | |
50 socket_map_[return_id] = socket; | |
51 return return_id; | |
52 } | |
53 | |
54 bool ExtensionBluetoothEventRouter::ReleaseSocket(int id) { | |
55 SocketMap::iterator socket_entry = socket_map_.find(id); | |
56 if (socket_entry == socket_map_.end()) | |
57 return false; | |
58 socket_map_.erase(socket_entry); | |
59 return true; | |
60 } | |
61 | |
62 scoped_refptr<BluetoothSocket> ExtensionBluetoothEventRouter::GetSocket( | |
63 int id) { | |
64 SocketMap::iterator socket_entry = socket_map_.find(id); | |
65 if (socket_entry == socket_map_.end()) | |
66 return NULL; | |
67 return socket_entry->second; | |
68 } | |
69 | |
70 void ExtensionBluetoothEventRouter::SetResponsibleForDiscovery( | |
71 bool responsible) { | |
72 responsible_for_discovery_ = responsible; | |
73 } | |
74 | |
75 bool ExtensionBluetoothEventRouter::IsResponsibleForDiscovery() const { | |
76 return responsible_for_discovery_; | |
77 } | |
78 | |
79 void ExtensionBluetoothEventRouter::SetSendDiscoveryEvents(bool should_send) { | |
80 // At the transition into sending devices, also send past devices that | |
81 // were discovered as they will not be discovered again. | |
82 if (should_send && !send_discovery_events_) { | |
83 for (DeviceList::const_iterator i = discovered_devices_.begin(); | |
84 i != discovered_devices_.end(); ++i) { | |
85 DispatchDeviceEvent(extensions::event_names::kBluetoothOnDeviceDiscovered, | |
86 **i); | |
87 } | |
88 } | |
89 | |
90 send_discovery_events_ = should_send; | |
91 } | |
92 | |
93 void ExtensionBluetoothEventRouter::DispatchDeviceEvent( | |
94 const char* event_name, const experimental_bluetooth::Device& device) { | |
95 scoped_ptr<ListValue> args(new ListValue()); | |
96 args->Append(device.ToValue().release()); | |
97 profile_->GetExtensionEventRouter()->DispatchEventToRenderers( | |
98 event_name, | |
99 args.Pass(), | |
100 NULL, | |
101 GURL()); | |
102 } | |
103 | |
104 void ExtensionBluetoothEventRouter::AdapterPresentChanged( | |
105 chromeos::BluetoothAdapter* adapter, bool present) { | |
106 if (adapter != adapter_.get()) { | |
107 DVLOG(1) << "Ignoring event for adapter " << adapter->address(); | |
108 return; | |
109 } | |
110 | |
111 DispatchBooleanValueEvent( | |
112 extensions::event_names::kBluetoothOnAvailabilityChanged, | |
113 present); | |
114 } | |
115 | |
116 void ExtensionBluetoothEventRouter::AdapterPoweredChanged( | |
117 chromeos::BluetoothAdapter* adapter, bool has_power) { | |
118 if (adapter != adapter_.get()) { | |
119 DVLOG(1) << "Ignoring event for adapter " << adapter->address(); | |
120 return; | |
121 } | |
122 | |
123 DispatchBooleanValueEvent( | |
124 extensions::event_names::kBluetoothOnPowerChanged, | |
125 has_power); | |
126 } | |
127 | |
128 void ExtensionBluetoothEventRouter::AdapterDiscoveringChanged( | |
129 chromeos::BluetoothAdapter* adapter, bool discovering) { | |
130 if (adapter != adapter_.get()) { | |
131 DVLOG(1) << "Ignoring event for adapter " << adapter->address(); | |
132 return; | |
133 } | |
134 | |
135 if (!discovering) { | |
136 send_discovery_events_ = false; | |
137 responsible_for_discovery_ = false; | |
138 discovered_devices_.clear(); | |
139 } | |
140 | |
141 DispatchBooleanValueEvent( | |
142 extensions::event_names::kBluetoothOnDiscoveringChanged, | |
143 discovering); | |
144 } | |
145 | |
146 void ExtensionBluetoothEventRouter::DeviceAdded( | |
147 chromeos::BluetoothAdapter* adapter, chromeos::BluetoothDevice* device) { | |
148 if (adapter != adapter_.get()) { | |
149 DVLOG(1) << "Ignoring event for adapter " << adapter->address(); | |
150 return; | |
151 } | |
152 | |
153 experimental_bluetooth::Device* extension_device = | |
154 new experimental_bluetooth::Device(); | |
155 experimental_bluetooth::BluetoothDeviceToApiDevice(*device, extension_device); | |
156 discovered_devices_.push_back(extension_device); | |
157 | |
158 if (!send_discovery_events_) | |
159 return; | |
160 | |
161 DispatchDeviceEvent(extensions::event_names::kBluetoothOnDeviceDiscovered, | |
162 *extension_device); | |
163 } | |
164 | |
165 void ExtensionBluetoothEventRouter::DispatchBooleanValueEvent( | |
166 const char* event_name, bool value) { | |
167 scoped_ptr<ListValue> args(new ListValue()); | |
168 args->Append(Value::CreateBooleanValue(value)); | |
169 profile_->GetExtensionEventRouter()->DispatchEventToRenderers( | |
170 event_name, args.Pass(), NULL, GURL()); | |
171 } | |
172 | |
173 } // namespace chromeos | |
OLD | NEW |