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/json/json_writer.h" | 10 #include "base/json/json_writer.h" |
(...skipping 11 matching lines...) Expand all Loading... |
22 #include "device/bluetooth/bluetooth_socket.h" | 22 #include "device/bluetooth/bluetooth_socket.h" |
23 | 23 |
24 namespace extensions { | 24 namespace extensions { |
25 | 25 |
26 ExtensionBluetoothEventRouter::ExtensionBluetoothEventRouter(Profile* profile) | 26 ExtensionBluetoothEventRouter::ExtensionBluetoothEventRouter(Profile* profile) |
27 : send_discovery_events_(false), | 27 : send_discovery_events_(false), |
28 responsible_for_discovery_(false), | 28 responsible_for_discovery_(false), |
29 profile_(profile), | 29 profile_(profile), |
30 adapter_(NULL), | 30 adapter_(NULL), |
31 num_event_listeners_(0), | 31 num_event_listeners_(0), |
32 next_socket_id_(1) { | 32 next_socket_id_(1), |
| 33 ALLOW_THIS_IN_INITIALIZER_LIST(weak_ptr_factory_(this)) { |
33 DCHECK(profile_); | 34 DCHECK(profile_); |
34 } | 35 } |
35 | 36 |
36 ExtensionBluetoothEventRouter::~ExtensionBluetoothEventRouter() { | 37 ExtensionBluetoothEventRouter::~ExtensionBluetoothEventRouter() { |
37 CHECK(socket_map_.size() == 0); | 38 CHECK(socket_map_.size() == 0); |
38 if (adapter_) { | 39 if (adapter_) { |
39 adapter_->RemoveObserver(this); | 40 adapter_->RemoveObserver(this); |
40 adapter_ = NULL; | 41 adapter_ = NULL; |
41 } | 42 } |
42 } | 43 } |
43 | 44 |
44 scoped_refptr<device::BluetoothAdapter> | 45 void ExtensionBluetoothEventRouter::RunCallbackOnAdapterReady( |
45 ExtensionBluetoothEventRouter::GetAdapter() { | 46 const device::BluetoothAdapterFactory::AdapterCallback& callback) { |
46 if (adapter_) | 47 device::BluetoothAdapterFactory::RunCallbackOnAdapterReady(callback); |
47 return adapter_; | |
48 | |
49 return device::BluetoothAdapterFactory::DefaultAdapter(); | |
50 } | 48 } |
51 | 49 |
52 void ExtensionBluetoothEventRouter::OnListenerAdded() { | 50 void ExtensionBluetoothEventRouter::OnListenerAdded() { |
53 num_event_listeners_++; | 51 num_event_listeners_++; |
54 InitializeAdapterIfNeeded(); | 52 InitializeAdapterIfNeeded(); |
55 } | 53 } |
56 | 54 |
57 void ExtensionBluetoothEventRouter::OnListenerRemoved() { | 55 void ExtensionBluetoothEventRouter::OnListenerRemoved() { |
58 num_event_listeners_--; | 56 num_event_listeners_--; |
59 CHECK(num_event_listeners_ >= 0); | 57 CHECK(num_event_listeners_ >= 0); |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
171 | 169 |
172 if (!send_discovery_events_) | 170 if (!send_discovery_events_) |
173 return; | 171 return; |
174 | 172 |
175 DispatchDeviceEvent(extensions::event_names::kBluetoothOnDeviceDiscovered, | 173 DispatchDeviceEvent(extensions::event_names::kBluetoothOnDeviceDiscovered, |
176 *extension_device); | 174 *extension_device); |
177 } | 175 } |
178 | 176 |
179 void ExtensionBluetoothEventRouter::InitializeAdapterIfNeeded() { | 177 void ExtensionBluetoothEventRouter::InitializeAdapterIfNeeded() { |
180 if (!adapter_) { | 178 if (!adapter_) { |
181 adapter_ = GetAdapter(); | 179 RunCallbackOnAdapterReady( |
| 180 base::Bind(&ExtensionBluetoothEventRouter::InitializeAdapter, |
| 181 weak_ptr_factory_.GetWeakPtr())); |
| 182 } |
| 183 } |
| 184 |
| 185 void ExtensionBluetoothEventRouter::InitializeAdapter( |
| 186 scoped_refptr<device::BluetoothAdapter> adapter) { |
| 187 adapter_ = adapter; |
| 188 if (adapter_) |
182 adapter_->AddObserver(this); | 189 adapter_->AddObserver(this); |
183 } | |
184 } | 190 } |
185 | 191 |
186 void ExtensionBluetoothEventRouter::MaybeReleaseAdapter() { | 192 void ExtensionBluetoothEventRouter::MaybeReleaseAdapter() { |
187 if (adapter_ && num_event_listeners_ == 0) { | 193 if (adapter_ && num_event_listeners_ == 0) { |
188 adapter_->RemoveObserver(this); | 194 adapter_->RemoveObserver(this); |
189 adapter_ = NULL; | 195 adapter_ = NULL; |
190 } | 196 } |
191 } | 197 } |
192 | 198 |
193 void ExtensionBluetoothEventRouter::DispatchAdapterStateEvent() { | 199 void ExtensionBluetoothEventRouter::DispatchAdapterStateEvent() { |
194 api::bluetooth::AdapterState state; | 200 api::bluetooth::AdapterState state; |
195 PopulateAdapterState(*adapter_, &state); | 201 PopulateAdapterState(*adapter_, &state); |
196 | 202 |
197 scoped_ptr<ListValue> args(new ListValue()); | 203 scoped_ptr<ListValue> args(new ListValue()); |
198 args->Append(state.ToValue().release()); | 204 args->Append(state.ToValue().release()); |
199 scoped_ptr<Event> event(new Event( | 205 scoped_ptr<Event> event(new Event( |
200 extensions::event_names::kBluetoothOnAdapterStateChanged, | 206 extensions::event_names::kBluetoothOnAdapterStateChanged, |
201 args.Pass())); | 207 args.Pass())); |
202 ExtensionSystem::Get(profile_)->event_router()->BroadcastEvent(event.Pass()); | 208 ExtensionSystem::Get(profile_)->event_router()->BroadcastEvent(event.Pass()); |
203 } | 209 } |
204 | 210 |
205 } // namespace extensions | 211 } // namespace extensions |
OLD | NEW |