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

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

Issue 11819007: Changed DefaultAdapter to RunCallbackOnAdapterReady function. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created IsBluetoothSupported in BluetoothEventRouter to fix browser_tests. Created 7 years, 11 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 | Annotate | Revision Log
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/json/json_writer.h" 10 #include "base/json/json_writer.h"
(...skipping 11 matching lines...) Expand all
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 bool ExtensionBluetoothEventRouter::IsBluetoothSupported() const {
45 ExtensionBluetoothEventRouter::GetAdapter() { 46 return adapter_ ||
46 if (adapter_) 47 device::BluetoothAdapterFactory::IsBluetoothAdapterAvailable();
47 return adapter_; 48 }
48 49
49 return device::BluetoothAdapterFactory::DefaultAdapter(); 50 void ExtensionBluetoothEventRouter::RunCallbackOnAdapterReady(
51 const device::BluetoothAdapterFactory::AdapterCallback& callback) {
52 if (adapter_) {
53 callback.Run(scoped_refptr<device::BluetoothAdapter>(adapter_));
54 return;
55 }
56
57 device::BluetoothAdapterFactory::RunCallbackOnAdapterReady(callback);
50 } 58 }
51 59
52 void ExtensionBluetoothEventRouter::OnListenerAdded() { 60 void ExtensionBluetoothEventRouter::OnListenerAdded() {
53 num_event_listeners_++; 61 num_event_listeners_++;
54 InitializeAdapterIfNeeded(); 62 InitializeAdapterIfNeeded();
55 } 63 }
56 64
57 void ExtensionBluetoothEventRouter::OnListenerRemoved() { 65 void ExtensionBluetoothEventRouter::OnListenerRemoved() {
58 num_event_listeners_--; 66 num_event_listeners_--;
59 CHECK(num_event_listeners_ >= 0); 67 CHECK(num_event_listeners_ >= 0);
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after
171 179
172 if (!send_discovery_events_) 180 if (!send_discovery_events_)
173 return; 181 return;
174 182
175 DispatchDeviceEvent(extensions::event_names::kBluetoothOnDeviceDiscovered, 183 DispatchDeviceEvent(extensions::event_names::kBluetoothOnDeviceDiscovered,
176 *extension_device); 184 *extension_device);
177 } 185 }
178 186
179 void ExtensionBluetoothEventRouter::InitializeAdapterIfNeeded() { 187 void ExtensionBluetoothEventRouter::InitializeAdapterIfNeeded() {
180 if (!adapter_) { 188 if (!adapter_) {
181 adapter_ = GetAdapter(); 189 RunCallbackOnAdapterReady(
190 base::Bind(&ExtensionBluetoothEventRouter::InitializeAdapter,
191 weak_ptr_factory_.GetWeakPtr()));
192 }
193 }
194
195 void ExtensionBluetoothEventRouter::InitializeAdapter(
196 scoped_refptr<device::BluetoothAdapter> adapter) {
197 adapter_ = adapter;
198 if (adapter_)
182 adapter_->AddObserver(this); 199 adapter_->AddObserver(this);
183 }
184 } 200 }
185 201
186 void ExtensionBluetoothEventRouter::MaybeReleaseAdapter() { 202 void ExtensionBluetoothEventRouter::MaybeReleaseAdapter() {
187 if (adapter_ && num_event_listeners_ == 0) { 203 if (adapter_ && num_event_listeners_ == 0) {
188 adapter_->RemoveObserver(this); 204 adapter_->RemoveObserver(this);
189 adapter_ = NULL; 205 adapter_ = NULL;
190 } 206 }
191 } 207 }
192 208
193 void ExtensionBluetoothEventRouter::DispatchAdapterStateEvent() { 209 void ExtensionBluetoothEventRouter::DispatchAdapterStateEvent() {
194 api::bluetooth::AdapterState state; 210 api::bluetooth::AdapterState state;
195 PopulateAdapterState(*adapter_, &state); 211 PopulateAdapterState(*adapter_, &state);
196 212
197 scoped_ptr<ListValue> args(new ListValue()); 213 scoped_ptr<ListValue> args(new ListValue());
198 args->Append(state.ToValue().release()); 214 args->Append(state.ToValue().release());
199 scoped_ptr<Event> event(new Event( 215 scoped_ptr<Event> event(new Event(
200 extensions::event_names::kBluetoothOnAdapterStateChanged, 216 extensions::event_names::kBluetoothOnAdapterStateChanged,
201 args.Pass())); 217 args.Pass()));
202 ExtensionSystem::Get(profile_)->event_router()->BroadcastEvent(event.Pass()); 218 ExtensionSystem::Get(profile_)->event_router()->BroadcastEvent(event.Pass());
203 } 219 }
204 220
205 } // namespace extensions 221 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698