summaryrefslogtreecommitdiffstats
path: root/device/bluetooth/bluetooth_socket_thread.h
blob: 0e0aea6b231c7b2a0a422be5ab8dc4a90d1ea2f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef DEVICE_BLUETOOTH_BLUETOOTH_SOCKET_THREAD_H_
#define DEVICE_BLUETOOTH_BLUETOOTH_SOCKET_THREAD_H_

#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/threading/thread_checker.h"
#include "device/bluetooth/bluetooth_export.h"

namespace base {
class SequencedTaskRunner;
class Thread;
}  // namespace base

namespace device {

// Thread abstraction used by |BluetoothSocketBlueZ| and |BluetoothSocketWin|
// to perform IO operations on the underlying platform sockets. An instance of
// this class can be shared by many active sockets.
class DEVICE_BLUETOOTH_EXPORT BluetoothSocketThread
    : public base::RefCountedThreadSafe<BluetoothSocketThread> {
 public:
  static scoped_refptr<BluetoothSocketThread> Get();
  static void CleanupForTesting();

  void OnSocketActivate();
  void OnSocketDeactivate();

  scoped_refptr<base::SequencedTaskRunner> task_runner() const;

 private:
  friend class base::RefCountedThreadSafe<BluetoothSocketThread>;
  BluetoothSocketThread();
  virtual ~BluetoothSocketThread();

  void EnsureStarted();

  base::ThreadChecker thread_checker_;
  int active_socket_count_;
  scoped_ptr<base::Thread> thread_;
  scoped_refptr<base::SequencedTaskRunner> task_runner_;

  DISALLOW_COPY_AND_ASSIGN(BluetoothSocketThread);
};

}  // namespace device

#endif  // DEVICE_BLUETOOTH_BLUETOOTH_SOCKET_THREAD_H_