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

Side by Side Diff: device/bluetooth/bluetooth_adapter_factory.cc

Issue 12018024: Implemented Asynchronous Initialization of BluetoothAdapter. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Turned adapter_callbacks to a lazy instance. 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
« no previous file with comments | « device/bluetooth/bluetooth_adapter_factory.h ('k') | device/bluetooth/bluetooth_adapter_win.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 "device/bluetooth/bluetooth_adapter_factory.h" 5 #include "device/bluetooth/bluetooth_adapter_factory.h"
6 6
7 #include <vector>
8
9 #include "base/bind.h"
7 #include "base/lazy_instance.h" 10 #include "base/lazy_instance.h"
8 #include "base/memory/ref_counted.h" 11 #include "base/memory/ref_counted.h"
9 #include "base/memory/weak_ptr.h" 12 #include "base/memory/weak_ptr.h"
10 #include "device/bluetooth/bluetooth_adapter.h" 13 #include "device/bluetooth/bluetooth_adapter.h"
11 14
12 #if defined(OS_CHROMEOS) 15 #if defined(OS_CHROMEOS)
13 #include "device/bluetooth/bluetooth_adapter_chromeos.h" 16 #include "device/bluetooth/bluetooth_adapter_chromeos.h"
14 #elif defined(OS_WIN) 17 #elif defined(OS_WIN)
15 #include "device/bluetooth/bluetooth_adapter_win.h" 18 #include "device/bluetooth/bluetooth_adapter_win.h"
16 #endif 19 #endif
17 20
18 namespace { 21 namespace {
19 22
23 using device::BluetoothAdapter;
24 using device::BluetoothAdapterFactory;
25
20 // Shared default adapter instance, we don't want to keep this class around 26 // Shared default adapter instance, we don't want to keep this class around
21 // if nobody is using it so use a WeakPtr and create the object when needed; 27 // if nobody is using it so use a WeakPtr and create the object when needed;
22 // since Google C++ Style (and clang's static analyzer) forbids us having 28 // since Google C++ Style (and clang's static analyzer) forbids us having
23 // exit-time destructors we use a leaky lazy instance for it. 29 // exit-time destructors we use a leaky lazy instance for it.
24 base::LazyInstance<base::WeakPtr<device::BluetoothAdapter> >::Leaky 30 base::LazyInstance<base::WeakPtr<device::BluetoothAdapter> >::Leaky
25 default_adapter = LAZY_INSTANCE_INITIALIZER; 31 default_adapter = LAZY_INSTANCE_INITIALIZER;
26 32
33 typedef std::vector<BluetoothAdapterFactory::AdapterCallback>
34 AdapterCallbackList;
35 base::LazyInstance<AdapterCallbackList> adapter_callbacks =
36 LAZY_INSTANCE_INITIALIZER;
37
38 void RunAdapterCallbacks() {
39 CHECK(default_adapter.Get().get());
40 scoped_refptr<BluetoothAdapter> adapter(default_adapter.Get());
41 for (std::vector<BluetoothAdapterFactory::AdapterCallback>::const_iterator
42 iter = adapter_callbacks.Get().begin();
43 iter != adapter_callbacks.Get().end();
44 ++iter) {
45 iter->Run(adapter);
46 }
47 adapter_callbacks.Get().clear();
48 }
49
27 } // namespace 50 } // namespace
28 51
29 namespace device { 52 namespace device {
30 53
31 // static 54 // static
32 bool BluetoothAdapterFactory::IsBluetoothAdapterAvailable() { 55 bool BluetoothAdapterFactory::IsBluetoothAdapterAvailable() {
33 #if defined(OS_CHROMEOS) 56 #if defined(OS_CHROMEOS)
34 return true; 57 return true;
35 #elif defined(OS_WIN) 58 #elif defined(OS_WIN)
36 return true; 59 return true;
37 #endif 60 #endif
38 return false; 61 return false;
39 } 62 }
40 63
41 // static 64 // static
42 void BluetoothAdapterFactory::RunCallbackOnAdapterReady( 65 void BluetoothAdapterFactory::GetAdapter(const AdapterCallback& callback) {
43 const BluetoothAdapter::AdapterCallback& callback) {
44 if (!default_adapter.Get().get()) { 66 if (!default_adapter.Get().get()) {
45 #if defined(OS_CHROMEOS) 67 #if defined(OS_CHROMEOS)
46 chromeos::BluetoothAdapterChromeOs* new_adapter = 68 chromeos::BluetoothAdapterChromeOs* new_adapter =
47 new chromeos::BluetoothAdapterChromeOs; 69 new chromeos::BluetoothAdapterChromeOs;
48 new_adapter->TrackDefaultAdapter(); 70 new_adapter->TrackDefaultAdapter();
49 default_adapter.Get() = new_adapter->weak_ptr_factory_.GetWeakPtr(); 71 default_adapter.Get() = new_adapter->weak_ptr_factory_.GetWeakPtr();
50 #elif defined(OS_WIN) 72 #elif defined(OS_WIN)
51 BluetoothAdapterWin* new_adapter = new BluetoothAdapterWin(); 73 BluetoothAdapterWin* new_adapter = new BluetoothAdapterWin(
74 base::Bind(&RunAdapterCallbacks));
52 new_adapter->TrackDefaultAdapter(); 75 new_adapter->TrackDefaultAdapter();
53 default_adapter.Get() = new_adapter->weak_ptr_factory_.GetWeakPtr(); 76 default_adapter.Get() = new_adapter->weak_ptr_factory_.GetWeakPtr();
54 #endif 77 #endif
55 } 78 }
56 79
57 if (default_adapter.Get()->IsInitialized()) { 80 if (default_adapter.Get()->IsInitialized()) {
58 callback.Run(scoped_refptr<BluetoothAdapter>(default_adapter.Get())); 81 callback.Run(scoped_refptr<BluetoothAdapter>(default_adapter.Get()));
59 } else { 82 } else {
60 default_adapter.Get()->QueueAdapterCallback(callback); 83 adapter_callbacks.Get().push_back(callback);
61 } 84 }
62 } 85 }
63 86
64 // static 87 // static
65 scoped_refptr<BluetoothAdapter> BluetoothAdapterFactory::GetAdapter() { 88 scoped_refptr<BluetoothAdapter> BluetoothAdapterFactory::MaybeGetAdapter() {
66 return scoped_refptr<BluetoothAdapter>(default_adapter.Get()); 89 return scoped_refptr<BluetoothAdapter>(default_adapter.Get());
67 } 90 }
68 91
69 // static 92 // static
70 BluetoothAdapter* BluetoothAdapterFactory::Create(const std::string& address) { 93 BluetoothAdapter* BluetoothAdapterFactory::Create(const std::string& address) {
71 BluetoothAdapter* adapter = NULL; 94 BluetoothAdapter* adapter = NULL;
72 #if defined(OS_CHROMEOS) 95 #if defined(OS_CHROMEOS)
73 chromeos::BluetoothAdapterChromeOs* adapter_chromeos = 96 chromeos::BluetoothAdapterChromeOs* adapter_chromeos =
74 new chromeos::BluetoothAdapterChromeOs; 97 new chromeos::BluetoothAdapterChromeOs;
75 adapter_chromeos->FindAdapter(address); 98 adapter_chromeos->FindAdapter(address);
76 adapter = adapter_chromeos; 99 adapter = adapter_chromeos;
77 #elif defined(OS_WIN) 100 #elif defined(OS_WIN)
78 adapter = new BluetoothAdapterWin(); 101 adapter = new BluetoothAdapterWin(base::Bind(&RunAdapterCallbacks));
79 #endif 102 #endif
80 return adapter; 103 return adapter;
81 } 104 }
82 105
83 } // namespace device 106 } // namespace device
OLDNEW
« no previous file with comments | « device/bluetooth/bluetooth_adapter_factory.h ('k') | device/bluetooth/bluetooth_adapter_win.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698