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