summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/extensions/bluetooth_event_router.h
blob: 544c66a380cc98d80fab2acb9aa82dcc9dd4fcdf (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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Copyright (c) 2012 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 CHROME_BROWSER_CHROMEOS_EXTENSIONS_BLUETOOTH_EVENT_ROUTER_H_
#define CHROME_BROWSER_CHROMEOS_EXTENSIONS_BLUETOOTH_EVENT_ROUTER_H_

#include <map>

#include "base/memory/ref_counted.h"
#include "chrome/browser/chromeos/bluetooth/bluetooth_adapter.h"
#include "chrome/browser/chromeos/bluetooth/bluetooth_socket.h"
#include "chrome/browser/profiles/profile.h"

namespace chromeos {

class ExtensionBluetoothEventRouter
    : public chromeos::BluetoothAdapter::Observer {
 public:
  explicit ExtensionBluetoothEventRouter(Profile* profile);
  virtual ~ExtensionBluetoothEventRouter();

  const chromeos::BluetoothAdapter* adapter() const { return adapter_.get(); }
  chromeos::BluetoothAdapter* GetMutableAdapter() { return adapter_.get(); }

  // Register the BluetoothSocket |socket| for use by the extensions system.
  // This class will hold onto the socket for its lifetime, or until
  // ReleaseSocket is called for the socket.  Returns an id for the socket.
  int RegisterSocket(scoped_refptr<BluetoothSocket> socket);

  // Release the BluetoothSocket corresponding to |id|.  Returns true if
  // the socket was found and released, false otherwise.
  bool ReleaseSocket(int id);

  // Get the BluetoothSocket corresponding to |id|.
  scoped_refptr<BluetoothSocket> GetSocket(int id);

  // Sets whether or not DeviceAdded events will be dispatched to extensions.
  void SetSendDiscoveryEvents(bool should_send);

  // Override from chromeos::BluetoothAdapter::Observer
  virtual void AdapterPresentChanged(chromeos::BluetoothAdapter* adapter,
                                     bool present) OVERRIDE;
  virtual void AdapterPoweredChanged(chromeos::BluetoothAdapter* adapter,
                                     bool has_power) OVERRIDE;
  virtual void DeviceAdded(chromeos::BluetoothAdapter* adapter,
                           chromeos::BluetoothDevice* device) OVERRIDE;

  // Exposed for testing.
  void SetAdapterForTest(chromeos::BluetoothAdapter* adapter) {
    adapter_ = adapter;
  }
 private:
  void DispatchBooleanValueEvent(const char* event_name, bool value);

  bool send_discovery_events_;

  Profile* profile_;
  scoped_refptr<chromeos::BluetoothAdapter> adapter_;

  // The next id to use for referring to a BluetoothSocket.  We avoid using
  // the fd of the socket because we don't want to leak that information to
  // the extension javascript.
  int next_socket_id_;

  typedef std::map<int, scoped_refptr<BluetoothSocket> > SocketMap;
  SocketMap socket_map_;

  DISALLOW_COPY_AND_ASSIGN(ExtensionBluetoothEventRouter);
};

}  // namespace chromeos

#endif  // CHROME_BROWSER_CHROMEOS_EXTENSIONS_BLUETOOTH_EVENT_ROUTER_H_