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