Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / device / bluetooth / bluetooth_socket_thread.cc
blobca2dfa4adcef0337b6c4a79ea7fcd4f7e6c33c09
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
3 // found in the LICENSE file.
5 #include "device/bluetooth/bluetooth_socket_thread.h"
7 #include "base/lazy_instance.h"
8 #include "base/sequenced_task_runner.h"
9 #include "base/threading/thread.h"
11 namespace device {
13 base::LazyInstance<scoped_refptr<BluetoothSocketThread> > g_instance =
14 LAZY_INSTANCE_INITIALIZER;
16 // static
17 scoped_refptr<BluetoothSocketThread> BluetoothSocketThread::Get() {
18 if (!g_instance.Get().get()) {
19 g_instance.Get() = new BluetoothSocketThread();
21 return g_instance.Get();
24 // static
25 void BluetoothSocketThread::CleanupForTesting() {
26 DCHECK(g_instance.Get().get());
27 g_instance.Get() = NULL;
30 BluetoothSocketThread::BluetoothSocketThread()
31 : active_socket_count_(0) {}
33 BluetoothSocketThread::~BluetoothSocketThread() {
34 if (thread_) {
35 thread_->Stop();
36 thread_.reset(NULL);
37 task_runner_ = NULL;
41 void BluetoothSocketThread::OnSocketActivate() {
42 DCHECK(thread_checker_.CalledOnValidThread());
43 active_socket_count_++;
44 EnsureStarted();
47 void BluetoothSocketThread::OnSocketDeactivate() {
48 DCHECK(thread_checker_.CalledOnValidThread());
49 active_socket_count_--;
52 void BluetoothSocketThread::EnsureStarted() {
53 DCHECK(thread_checker_.CalledOnValidThread());
54 if (thread_)
55 return;
57 base::Thread::Options thread_options;
58 thread_options.message_loop_type = base::MessageLoop::TYPE_IO;
59 thread_.reset(new base::Thread("BluetoothSocketThread"));
60 thread_->StartWithOptions(thread_options);
61 task_runner_ = thread_->task_runner();
64 scoped_refptr<base::SequencedTaskRunner> BluetoothSocketThread::task_runner()
65 const {
66 DCHECK(active_socket_count_ > 0);
67 DCHECK(thread_);
68 DCHECK(task_runner_.get());
70 return task_runner_;
73 } // namespace device