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

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 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 23 matching lines...) Expand all
34 } 34 }
35 35
36 ExtensionBluetoothEventRouter::~ExtensionBluetoothEventRouter() { 36 ExtensionBluetoothEventRouter::~ExtensionBluetoothEventRouter() {
37 CHECK(socket_map_.size() == 0); 37 CHECK(socket_map_.size() == 0);
38 if (adapter_) { 38 if (adapter_) {
39 adapter_->RemoveObserver(this); 39 adapter_->RemoveObserver(this);
40 adapter_ = NULL; 40 adapter_ = NULL;
41 } 41 }
42 } 42 }
43 43
44 scoped_refptr<device::BluetoothAdapter> 44 void ExtensionBluetoothEventRouter::RunCallbackOnAdapterReady(
45 ExtensionBluetoothEventRouter::GetAdapter() { 45 const device::BluetoothAdapterFactory::AdapterCallback& callback) {
46 if (adapter_) 46 device::BluetoothAdapterFactory::RunCallbackOnAdapterReady(callback);
bryeung 2013/01/08 22:24:27 Since this is just a pass-through method now...why
youngki 2013/01/09 15:12:03 We could.. But then we cannot mock the bluetooth a
bryeung 2013/01/09 15:28:01 Okay.
47 return adapter_;
48
49 return device::BluetoothAdapterFactory::DefaultAdapter();
50 } 47 }
51 48
52 void ExtensionBluetoothEventRouter::OnListenerAdded() { 49 void ExtensionBluetoothEventRouter::OnListenerAdded() {
53 num_event_listeners_++; 50 num_event_listeners_++;
54 InitializeAdapterIfNeeded(); 51 InitializeAdapterIfNeeded();
55 } 52 }
56 53
57 void ExtensionBluetoothEventRouter::OnListenerRemoved() { 54 void ExtensionBluetoothEventRouter::OnListenerRemoved() {
58 num_event_listeners_--; 55 num_event_listeners_--;
59 CHECK(num_event_listeners_ >= 0); 56 CHECK(num_event_listeners_ >= 0);
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after
171 168
172 if (!send_discovery_events_) 169 if (!send_discovery_events_)
173 return; 170 return;
174 171
175 DispatchDeviceEvent(extensions::event_names::kBluetoothOnDeviceDiscovered, 172 DispatchDeviceEvent(extensions::event_names::kBluetoothOnDeviceDiscovered,
176 *extension_device); 173 *extension_device);
177 } 174 }
178 175
179 void ExtensionBluetoothEventRouter::InitializeAdapterIfNeeded() { 176 void ExtensionBluetoothEventRouter::InitializeAdapterIfNeeded() {
180 if (!adapter_) { 177 if (!adapter_) {
181 adapter_ = GetAdapter(); 178 RunCallbackOnAdapterReady(
182 adapter_->AddObserver(this); 179 base::Bind(&ExtensionBluetoothEventRouter::InitializeAdapter,
180 base::Unretained(this)));
183 } 181 }
184 } 182 }
185 183
184 bool ExtensionBluetoothEventRouter::InitializeAdapter(
185 scoped_refptr<device::BluetoothAdapter> adapter) {
186 adapter_ = adapter;
187 adapter_->AddObserver(this);
188 return true;
189 }
190
186 void ExtensionBluetoothEventRouter::MaybeReleaseAdapter() { 191 void ExtensionBluetoothEventRouter::MaybeReleaseAdapter() {
187 if (adapter_ && num_event_listeners_ == 0) { 192 if (adapter_ && num_event_listeners_ == 0) {
188 adapter_->RemoveObserver(this); 193 adapter_->RemoveObserver(this);
189 adapter_ = NULL; 194 adapter_ = NULL;
190 } 195 }
191 } 196 }
192 197
193 void ExtensionBluetoothEventRouter::DispatchAdapterStateEvent() { 198 void ExtensionBluetoothEventRouter::DispatchAdapterStateEvent() {
194 api::bluetooth::AdapterState state; 199 api::bluetooth::AdapterState state;
195 PopulateAdapterState(*adapter_, &state); 200 PopulateAdapterState(*adapter_, &state);
196 201
197 scoped_ptr<ListValue> args(new ListValue()); 202 scoped_ptr<ListValue> args(new ListValue());
198 args->Append(state.ToValue().release()); 203 args->Append(state.ToValue().release());
199 scoped_ptr<Event> event(new Event( 204 scoped_ptr<Event> event(new Event(
200 extensions::event_names::kBluetoothOnAdapterStateChanged, 205 extensions::event_names::kBluetoothOnAdapterStateChanged,
201 args.Pass())); 206 args.Pass()));
202 ExtensionSystem::Get(profile_)->event_router()->BroadcastEvent(event.Pass()); 207 ExtensionSystem::Get(profile_)->event_router()->BroadcastEvent(event.Pass());
203 } 208 }
204 209
205 } // namespace extensions 210 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698