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.
4
5#ifndef ASH_SYSTEM_CHROMEOS_BLUETOOTH_BLUETOOTH_NOTIFICATION_CONTROLLER_H_
6#define ASH_SYSTEM_CHROMEOS_BLUETOOTH_BLUETOOTH_NOTIFICATION_CONTROLLER_H_
7
8#include <set>
9
10#include "ash/ash_export.h"
11#include "base/basictypes.h"
12#include "base/compiler_specific.h"
13#include "base/memory/ref_counted.h"
14#include "base/memory/weak_ptr.h"
15#include "base/strings/string16.h"
16#include "device/bluetooth/bluetooth_adapter.h"
17#include "device/bluetooth/bluetooth_device.h"
18
19namespace ash {
20
21// The BluetoothNotificationController receives incoming pairing requests from
22// the BluetoothAdapter, and notifications of changes to the adapter state and
23// set of paired devices. It presents incoming pairing requests in the form of
24// rich notifications that the user can interact with to approve the request.
25class ASH_EXPORT BluetoothNotificationController
26    : public device::BluetoothAdapter::Observer,
27      public device::BluetoothDevice::PairingDelegate {
28 public:
29  BluetoothNotificationController();
30  virtual ~BluetoothNotificationController();
31
32  // device::BluetoothAdapter::Observer override.
33  virtual void AdapterDiscoverableChanged(device::BluetoothAdapter* adapter,
34                                          bool discoverable) OVERRIDE;
35  virtual void DeviceAdded(device::BluetoothAdapter* adapter,
36                           device::BluetoothDevice* device) OVERRIDE;
37  virtual void DeviceChanged(device::BluetoothAdapter* adapter,
38                             device::BluetoothDevice* device) OVERRIDE;
39  virtual void DeviceRemoved(device::BluetoothAdapter* adapter,
40                             device::BluetoothDevice* device) OVERRIDE;
41
42  // device::BluetoothDevice::PairingDelegate override.
43  virtual void RequestPinCode(device::BluetoothDevice* device) OVERRIDE;
44  virtual void RequestPasskey(device::BluetoothDevice* device) OVERRIDE;
45  virtual void DisplayPinCode(device::BluetoothDevice* device,
46                              const std::string& pincode) OVERRIDE;
47  virtual void DisplayPasskey(device::BluetoothDevice* device,
48                              uint32 passkey) OVERRIDE;
49  virtual void KeysEntered(device::BluetoothDevice* device,
50                           uint32 entered) OVERRIDE;
51  virtual void ConfirmPasskey(device::BluetoothDevice* device,
52                              uint32 passkey) OVERRIDE;
53  virtual void AuthorizePairing(device::BluetoothDevice* device) OVERRIDE;
54
55 private:
56  // Internal method called by BluetoothAdapterFactory to provide the adapter
57  // object.
58  void OnGetAdapter(scoped_refptr<device::BluetoothAdapter> adapter);
59
60  // Presents a notification to the user when the adapter becomes discoverable
61  // to other nearby devices.
62  void NotifyAdapterDiscoverable();
63
64  // Presents a notification to the user that a device |device| is making a
65  // pairing request. The exact message to display is given in |message| and
66  // should include all relevant instructions, if |with_buttons| is true then
67  // the notification will have Accept and Reject buttons, if false only the
68  // usual cancel/dismiss button will be present on the notification.
69  void NotifyPairing(device::BluetoothDevice* device,
70                     const base::string16& message,
71                     bool with_buttons);
72
73  // Clears any shown pairing notification now that the device has been paired.
74  void NotifyPairedDevice(device::BluetoothDevice* device);
75
76  // Reference to the underlying BluetoothAdapter object, holding this reference
77  // ensures we stay around as the pairing delegate for that adapter.
78  scoped_refptr<device::BluetoothAdapter> adapter_;
79
80  // Set of currently paired devices, stored by Bluetooth address, used to
81  // filter out property changes for devices that were previously paired.
82  std::set<std::string> paired_devices_;
83
84  // Note: This should remain the last member so it'll be destroyed and
85  // invalidate its weak pointers before any other members are destroyed.
86  base::WeakPtrFactory<BluetoothNotificationController> weak_ptr_factory_;
87
88  DISALLOW_COPY_AND_ASSIGN(BluetoothNotificationController);
89};
90
91}  // namespace ash
92
93#endif  // ASH_SYSTEM_CHROMEOS_BLUETOOTH_BLUETOOTH_NOTIFICATION_CONTROLLER_H_
94