OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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_socket_thread_win.h" | 5 #include "device/bluetooth/bluetooth_socket_thread.h" |
6 | 6 |
7 #include "base/lazy_instance.h" | 7 #include "base/lazy_instance.h" |
8 #include "base/sequenced_task_runner.h" | 8 #include "base/sequenced_task_runner.h" |
9 #include "base/threading/thread.h" | 9 #include "base/threading/thread.h" |
10 | 10 |
11 namespace device { | 11 namespace device { |
12 | 12 |
13 base::LazyInstance<scoped_refptr<BluetoothSocketThreadWin> > g_instance = | 13 base::LazyInstance<scoped_refptr<BluetoothSocketThread> > g_instance = |
14 LAZY_INSTANCE_INITIALIZER; | 14 LAZY_INSTANCE_INITIALIZER; |
15 | 15 |
16 // static | 16 // static |
17 scoped_refptr<BluetoothSocketThreadWin> BluetoothSocketThreadWin::Get() { | 17 scoped_refptr<BluetoothSocketThread> BluetoothSocketThread::Get() { |
18 if (!g_instance.Get().get()) { | 18 if (!g_instance.Get().get()) { |
19 g_instance.Get() = new BluetoothSocketThreadWin(); | 19 g_instance.Get() = new BluetoothSocketThread(); |
20 } | 20 } |
21 return g_instance.Get(); | 21 return g_instance.Get(); |
22 } | 22 } |
23 | 23 |
24 BluetoothSocketThreadWin::BluetoothSocketThreadWin() | 24 BluetoothSocketThread::BluetoothSocketThread() |
25 : active_socket_count_(0) {} | 25 : active_socket_count_(0) {} |
26 | 26 |
27 BluetoothSocketThreadWin::~BluetoothSocketThreadWin() {} | 27 BluetoothSocketThread::~BluetoothSocketThread() {} |
28 | 28 |
29 void BluetoothSocketThreadWin::OnSocketActivate() { | 29 void BluetoothSocketThread::OnSocketActivate() { |
30 DCHECK(thread_checker_.CalledOnValidThread()); | 30 DCHECK(thread_checker_.CalledOnValidThread()); |
31 active_socket_count_++; | 31 active_socket_count_++; |
32 EnsureStarted(); | 32 EnsureStarted(); |
33 } | 33 } |
34 | 34 |
35 void BluetoothSocketThreadWin::OnSocketDeactivate() { | 35 void BluetoothSocketThread::OnSocketDeactivate() { |
36 DCHECK(thread_checker_.CalledOnValidThread()); | 36 DCHECK(thread_checker_.CalledOnValidThread()); |
37 active_socket_count_--; | 37 active_socket_count_--; |
38 if (active_socket_count_ == 0 && thread_) { | 38 if (active_socket_count_ == 0 && thread_) { |
39 thread_->Stop(); | 39 thread_->Stop(); |
40 thread_.reset(NULL); | 40 thread_.reset(NULL); |
41 task_runner_ = NULL; | 41 task_runner_ = NULL; |
42 } | 42 } |
43 } | 43 } |
44 | 44 |
45 void BluetoothSocketThreadWin::EnsureStarted() { | 45 void BluetoothSocketThread::EnsureStarted() { |
46 DCHECK(thread_checker_.CalledOnValidThread()); | 46 DCHECK(thread_checker_.CalledOnValidThread()); |
47 if (thread_) | 47 if (thread_) |
48 return; | 48 return; |
49 | 49 |
50 base::Thread::Options thread_options; | 50 base::Thread::Options thread_options; |
51 thread_options.message_loop_type = base::MessageLoop::TYPE_IO; | 51 thread_options.message_loop_type = base::MessageLoop::TYPE_IO; |
52 thread_.reset(new base::Thread("BluetoothSocketThreadWin")); | 52 thread_.reset(new base::Thread("BluetoothSocketThread")); |
53 thread_->StartWithOptions(thread_options); | 53 thread_->StartWithOptions(thread_options); |
54 task_runner_ = thread_->message_loop_proxy(); | 54 task_runner_ = thread_->message_loop_proxy(); |
55 } | 55 } |
56 | 56 |
57 scoped_refptr<base::SequencedTaskRunner> BluetoothSocketThreadWin::task_runner() | 57 scoped_refptr<base::SequencedTaskRunner> BluetoothSocketThread::task_runner() |
58 const { | 58 const { |
59 DCHECK(active_socket_count_ > 0); | 59 DCHECK(active_socket_count_ > 0); |
60 DCHECK(thread_); | 60 DCHECK(thread_); |
61 DCHECK(task_runner_); | 61 DCHECK(task_runner_); |
62 | 62 |
63 return task_runner_; | 63 return task_runner_; |
64 } | 64 } |
65 | 65 |
66 } // namespace device | 66 } // namespace device |
OLD | NEW |