Roll src/third_party/WebKit 787a07c:716df21 (svn 201034:201036)
[chromium-blink-merge.git] / device / bluetooth / bluetooth_advertisement.h
blob46f19b1410d068d9221bb84163a982b89ae05c6c
1 // Copyright 2015 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 #ifndef DEVICE_BLUETOOTH_BLUETOOTH_ADVERTISEMENT_H_
6 #define DEVICE_BLUETOOTH_BLUETOOTH_ADVERTISEMENT_H_
8 #include <map>
9 #include <string>
10 #include <vector>
12 #include "base/callback.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/observer_list.h"
16 #include "device/bluetooth/bluetooth_export.h"
18 namespace device {
20 // BluetoothAdvertisement represents an advertisement which advertises over the
21 // LE channel during its lifetime.
22 class DEVICE_BLUETOOTH_EXPORT BluetoothAdvertisement
23 : public base::RefCounted<BluetoothAdvertisement> {
24 public:
25 // Possible types of error raised while registering or unregistering
26 // advertisements.
27 enum ErrorCode {
28 ERROR_UNSUPPORTED_PLATFORM, // Bluetooth advertisement not supported on
29 // current platform.
30 ERROR_ADVERTISEMENT_ALREADY_EXISTS, // An advertisement is already
31 // registered.
32 ERROR_ADVERTISEMENT_DOES_NOT_EXIST, // Unregistering an advertisement which
33 // is not registered.
34 ERROR_ADVERTISEMENT_INVALID_LENGTH, // Advertisement is not of a valid
35 // length.
36 INVALID_ADVERTISEMENT_ERROR_CODE
39 // Type of advertisement.
40 enum AdvertisementType {
41 // This advertises with the type set to ADV_NONCONN_IND, which indicates
42 // to receivers that our device is not connectable.
43 ADVERTISEMENT_TYPE_BROADCAST,
44 // This advertises with the type set to ADV_IND or ADV_SCAN_IND, which
45 // indicates to receivers that our device is connectable.
46 ADVERTISEMENT_TYPE_PERIPHERAL
49 using UUIDList = std::vector<std::string>;
50 using ManufacturerData = std::map<uint16_t, std::vector<uint8_t>>;
51 using ServiceData = std::map<std::string, std::vector<uint8_t>>;
53 // Structure that holds the data for an advertisement.
54 class DEVICE_BLUETOOTH_EXPORT Data {
55 public:
56 explicit Data(AdvertisementType type);
57 ~Data();
59 AdvertisementType type() { return type_; }
60 scoped_ptr<UUIDList> service_uuids() { return service_uuids_.Pass(); }
61 scoped_ptr<ManufacturerData> manufacturer_data() {
62 return manufacturer_data_.Pass();
64 scoped_ptr<UUIDList> solicit_uuids() { return solicit_uuids_.Pass(); }
65 scoped_ptr<ServiceData> service_data() { return service_data_.Pass(); }
67 void set_service_uuids(scoped_ptr<UUIDList> service_uuids) {
68 service_uuids_ = service_uuids.Pass();
70 void set_manufacturer_data(scoped_ptr<ManufacturerData> manufacturer_data) {
71 manufacturer_data_ = manufacturer_data.Pass();
73 void set_solicit_uuids(scoped_ptr<UUIDList> solicit_uuids) {
74 solicit_uuids = solicit_uuids_.Pass();
76 void set_service_data(scoped_ptr<ServiceData> service_data) {
77 service_data = service_data_.Pass();
80 void set_include_tx_power(bool include_tx_power) {
81 include_tx_power_ = include_tx_power;
84 private:
85 Data();
87 AdvertisementType type_;
88 scoped_ptr<UUIDList> service_uuids_;
89 scoped_ptr<ManufacturerData> manufacturer_data_;
90 scoped_ptr<UUIDList> solicit_uuids_;
91 scoped_ptr<ServiceData> service_data_;
92 bool include_tx_power_;
94 DISALLOW_COPY_AND_ASSIGN(Data);
97 // Interface for observing changes to this advertisement.
98 class Observer {
99 public:
100 virtual ~Observer() {}
102 // Called when this advertisement is released and is no longer advertising.
103 virtual void AdvertisementReleased(
104 BluetoothAdvertisement* advertisement) = 0;
107 // Adds and removes observers for events for this advertisement.
108 void AddObserver(BluetoothAdvertisement::Observer* observer);
109 void RemoveObserver(BluetoothAdvertisement::Observer* observer);
111 // Unregisters this advertisement. Called on destruction of this object
112 // automatically but can be called directly to explicitly unregister this
113 // object.
114 using SuccessCallback = base::Closure;
115 using ErrorCallback = base::Callback<void(ErrorCode)>;
116 virtual void Unregister(const SuccessCallback& success_callback,
117 const ErrorCallback& error_callback) = 0;
119 protected:
120 friend class base::RefCounted<BluetoothAdvertisement>;
122 BluetoothAdvertisement();
124 // The destructor will unregister this advertisement.
125 virtual ~BluetoothAdvertisement();
127 // List of observers interested in event notifications from us. Objects in
128 // |observers_| are expected to outlive a BluetoothAdvertisement object.
129 base::ObserverList<BluetoothAdvertisement::Observer> observers_;
131 private:
132 DISALLOW_COPY_AND_ASSIGN(BluetoothAdvertisement);
135 } // namespace device
137 #endif // DEVICE_BLUETOOTH_BLUETOOTH_ADVERTISEMENT_H_