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

Side by Side Diff: device/bluetooth/bluetooth_task_manager_win.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
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_task_manager_win.h" 5 #include "device/bluetooth/bluetooth_task_manager_win.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/memory/ref_counted.h" 10 #include "base/memory/ref_counted.h"
11 #include "base/message_loop.h" 11 #include "base/message_loop.h"
12 #include "base/sequenced_task_runner.h" 12 #include "base/sequenced_task_runner.h"
13 #include "base/stringprintf.h" 13 #include "base/stringprintf.h"
14 #include "base/sys_string_conversions.h" 14 #include "base/sys_string_conversions.h"
15 #include "base/threading/sequenced_worker_pool.h" 15 #include "base/threading/sequenced_worker_pool.h"
16 #include "base/threading/thread_restrictions.h"
17 #include "base/win/scoped_handle.h" 16 #include "base/win/scoped_handle.h"
18 #include "device/bluetooth/bluetooth_includes_win.h" 17 #include "device/bluetooth/bluetooth_init_win.h"
19 18
20 namespace { 19 namespace {
21 20
22 const int kNumThreadsInWorkerPool = 3; 21 const int kNumThreadsInWorkerPool = 3;
23 const char kBluetoothThreadName[] = "BluetoothPollingThreadWin"; 22 const char kBluetoothThreadName[] = "BluetoothPollingThreadWin";
24 23
25 // A frame-based exception handler filter function for a handler for exceptions
26 // generated by the Visual C++ delay loader helper function.
27 int FilterVisualCPPExceptions(DWORD exception_code) {
28 return HRESULT_FACILITY(exception_code) == FACILITY_VISUALCPP ?
29 EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH;
30 } // namespace
31
32 // Populates bluetooth adapter state using adapter_handle. 24 // Populates bluetooth adapter state using adapter_handle.
33 void GetAdapterState(HANDLE adapter_handle, 25 void GetAdapterState(HANDLE adapter_handle,
34 device::BluetoothTaskManagerWin::AdapterState* state) { 26 device::BluetoothTaskManagerWin::AdapterState* state) {
35 std::string name; 27 std::string name;
36 std::string address; 28 std::string address;
37 bool powered = false; 29 bool powered = false;
38 BLUETOOTH_RADIO_INFO adapter_info = { sizeof(BLUETOOTH_RADIO_INFO), 0 }; 30 BLUETOOTH_RADIO_INFO adapter_info = { sizeof(BLUETOOTH_RADIO_INFO), 0 };
39 if (adapter_handle && 31 if (adapter_handle &&
40 ERROR_SUCCESS == BluetoothGetRadioInfo(adapter_handle, 32 ERROR_SUCCESS == BluetoothGetRadioInfo(adapter_handle,
41 &adapter_info)) { 33 &adapter_info)) {
(...skipping 12 matching lines...) Expand all
54 state->powered = powered; 46 state->powered = powered;
55 } 47 }
56 48
57 } 49 }
58 50
59 namespace device { 51 namespace device {
60 52
61 // static 53 // static
62 const int BluetoothTaskManagerWin::kPollIntervalMs = 500; 54 const int BluetoothTaskManagerWin::kPollIntervalMs = 500;
63 55
64 // static
65 bool BluetoothTaskManagerWin::HasBluetoothStack() {
66 static enum {
67 HBS_UNKNOWN,
68 HBS_YES,
69 HBS_NO,
70 } has_bluetooth_stack = HBS_UNKNOWN;
71
72 if (has_bluetooth_stack == HBS_UNKNOWN) {
73 base::ThreadRestrictions::AssertIOAllowed();
74 HRESULT hr = E_FAIL;
75 __try {
76 hr = __HrLoadAllImportsForDll("bthprops.cpl");
77 } __except(FilterVisualCPPExceptions(::GetExceptionCode())) {
78 hr = E_FAIL;
79 }
80 has_bluetooth_stack = SUCCEEDED(hr) ? HBS_YES : HBS_NO;
81 }
82
83 return has_bluetooth_stack == HBS_YES;
84 }
85
86 BluetoothTaskManagerWin::BluetoothTaskManagerWin( 56 BluetoothTaskManagerWin::BluetoothTaskManagerWin(
87 scoped_refptr<base::SequencedTaskRunner> ui_task_runner) 57 scoped_refptr<base::SequencedTaskRunner> ui_task_runner)
88 : ui_task_runner_(ui_task_runner), 58 : ui_task_runner_(ui_task_runner),
89 worker_pool_(new base::SequencedWorkerPool(kNumThreadsInWorkerPool, 59 worker_pool_(new base::SequencedWorkerPool(kNumThreadsInWorkerPool,
90 kBluetoothThreadName)), 60 kBluetoothThreadName)),
91 bluetooth_task_runner_( 61 bluetooth_task_runner_(
92 worker_pool_->GetSequencedTaskRunnerWithShutdownBehavior( 62 worker_pool_->GetSequencedTaskRunnerWithShutdownBehavior(
93 worker_pool_->GetSequenceToken(), 63 worker_pool_->GetSequenceToken(),
94 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN)) { 64 base::SequencedWorkerPool::CONTINUE_ON_SHUTDOWN)) {
95 } 65 }
(...skipping 25 matching lines...) Expand all
121 bluetooth_task_runner_->PostTask( 91 bluetooth_task_runner_->PostTask(
122 FROM_HERE, 92 FROM_HERE,
123 base::Bind(&BluetoothTaskManagerWin::StartPolling, 93 base::Bind(&BluetoothTaskManagerWin::StartPolling,
124 this)); 94 this));
125 } 95 }
126 96
127 void BluetoothTaskManagerWin::StartPolling() { 97 void BluetoothTaskManagerWin::StartPolling() {
128 DCHECK(bluetooth_task_runner_->RunsTasksOnCurrentThread()); 98 DCHECK(bluetooth_task_runner_->RunsTasksOnCurrentThread());
129 99
130 // TODO(youngki): Handle this check where BluetoothAdapter is initialized. 100 // TODO(youngki): Handle this check where BluetoothAdapter is initialized.
131 if (HasBluetoothStack()) { 101 if (device::bluetooth_init_win::HasBluetoothStack()) {
132 PollAdapter(); 102 PollAdapter();
103 } else {
104 // IF the bluetooth stack is not available, we still send an empty state
105 // to BluetoothAdapter so that it is marked initialized, but the adapter
106 // will not be present.
107 AdapterState* state = new AdapterState();
108 ui_task_runner_->PostTask(
109 FROM_HERE,
110 base::Bind(&BluetoothTaskManagerWin::OnAdapterStateChanged,
111 this,
112 base::Owned(state)));
133 } 113 }
134 } 114 }
135 115
136 void BluetoothTaskManagerWin::Shutdown() { 116 void BluetoothTaskManagerWin::Shutdown() {
137 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread()); 117 DCHECK(ui_task_runner_->RunsTasksOnCurrentThread());
138 worker_pool_->Shutdown(); 118 worker_pool_->Shutdown();
139 } 119 }
140 120
141 void BluetoothTaskManagerWin::PostSetPoweredBluetoothTask( 121 void BluetoothTaskManagerWin::PostSetPoweredBluetoothTask(
142 bool powered, 122 bool powered,
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
208 FROM_HERE, 188 FROM_HERE,
209 base::Bind(&BluetoothTaskManagerWin::OnAdapterStateChanged, 189 base::Bind(&BluetoothTaskManagerWin::OnAdapterStateChanged,
210 this, 190 this,
211 base::Owned(state))); 191 base::Owned(state)));
212 ui_task_runner_->PostTask(FROM_HERE, callback); 192 ui_task_runner_->PostTask(FROM_HERE, callback);
213 } else { 193 } else {
214 ui_task_runner_->PostTask(FROM_HERE, error_callback); 194 ui_task_runner_->PostTask(FROM_HERE, error_callback);
215 } 195 }
216 } 196 }
217 197
218 } // namespace device 198 } // namespace device
OLDNEW
« no previous file with comments | « device/bluetooth/bluetooth_task_manager_win.h ('k') | device/bluetooth/bluetooth_task_manager_win_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698