Pepper: Fix crash on allocation failure.
[chromium-blink-merge.git] / chromeos / dbus / fake_bluetooth_gatt_characteristic_client.h
blob1ae77cbb701adf0dab8b5e4d0b456bfea35ddcf5
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.
5 #ifndef CHROMEOS_DBUS_FAKE_BLUETOOTH_GATT_CHARACTERISTIC_CLIENT_H_
6 #define CHROMEOS_DBUS_FAKE_BLUETOOTH_GATT_CHARACTERISTIC_CLIENT_H_
8 #include <string>
9 #include <vector>
11 #include "base/basictypes.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/observer_list.h"
15 #include "chromeos/chromeos_export.h"
16 #include "chromeos/dbus/bluetooth_gatt_characteristic_client.h"
17 #include "dbus/object_path.h"
18 #include "dbus/property.h"
20 namespace chromeos {
22 // FakeBluetoothGattCharacteristicClient simulates the behavior of the
23 // Bluetooth Daemon GATT characteristic objects and is used in test cases in
24 // place of a mock and on the Linux desktop.
25 class CHROMEOS_EXPORT FakeBluetoothGattCharacteristicClient
26 : public BluetoothGattCharacteristicClient {
27 public:
28 struct Properties : public BluetoothGattCharacteristicClient::Properties {
29 explicit Properties(const PropertyChangedCallback& callback);
30 virtual ~Properties();
32 // dbus::PropertySet override
33 virtual void Get(dbus::PropertyBase* property,
34 dbus::PropertySet::GetCallback callback) OVERRIDE;
35 virtual void GetAll() OVERRIDE;
36 virtual void Set(dbus::PropertyBase* property,
37 dbus::PropertySet::SetCallback callback) OVERRIDE;
40 FakeBluetoothGattCharacteristicClient();
41 virtual ~FakeBluetoothGattCharacteristicClient();
43 // DBusClient override.
44 virtual void Init(dbus::Bus* bus) OVERRIDE;
46 // BluetoothGattCharacteristicClient overrides.
47 virtual void AddObserver(Observer* observer) OVERRIDE;
48 virtual void RemoveObserver(Observer* observer) OVERRIDE;
49 virtual std::vector<dbus::ObjectPath> GetCharacteristics() OVERRIDE;
50 virtual Properties* GetProperties(const dbus::ObjectPath& object_path)
51 OVERRIDE;
53 // Makes the group of characteristics belonging to a particular GATT based
54 // profile available under the GATT service with object path |service_path|.
55 // Characteristic paths are hierarchical to service paths.
56 void ExposeHeartRateCharacteristics(const dbus::ObjectPath& service_path);
57 void HideHeartRateCharacteristics();
59 // Returns whether or not the heart rate characteristics are visible and
60 // performs the appropriate assertions.
61 bool IsHeartRateVisible() const;
63 // Returns the current object paths of exposed characteristics. If the
64 // characteristic is not visible, returns an invalid empty path.
65 dbus::ObjectPath GetHeartRateMeasurementPath() const;
66 dbus::ObjectPath GetBodySensorLocationPath() const;
67 dbus::ObjectPath GetHeartRateControlPointPath() const;
69 // Object path components and UUIDs of GATT characteristics.
70 // Heart Rate Service:
71 static const char kHeartRateMeasurementPathComponent[];
72 static const char kHeartRateMeasurementUUID[];
73 static const char kBodySensorLocationPathComponent[];
74 static const char kBodySensorLocationUUID[];
75 static const char kHeartRateControlPointPathComponent[];
76 static const char kHeartRateControlPointUUID[];
78 private:
79 // Property callback passed when we create Properties structures.
80 void OnPropertyChanged(const dbus::ObjectPath& object_path,
81 const std::string& property_name);
83 // Notifies observers.
84 void NotifyCharacteristicAdded(const dbus::ObjectPath& object_path);
85 void NotifyCharacteristicRemoved(const dbus::ObjectPath& object_path);
87 // Schedules a heart rate measurement value change, if the heart rate
88 // characteristics are visible.
89 void ScheduleHeartRateMeasurementValueChange();
91 // Returns a random Heart Rate Measurement value. All the fields of the value
92 // are populated according to the the fake behavior. The measurement value
93 // is a random value within a reasonable range.
94 std::vector<uint8> GetHeartRateMeasurementValue();
96 // If true, characteristics of the Heart Rate Service are visible. Use
97 // IsHeartRateVisible() to check the value.
98 bool heart_rate_visible_;
100 // Total calories burned, used for the Heart Rate Measurement characteristic.
101 uint16 calories_burned_;
103 // Static properties returned for simulated characteristics for the Heart
104 // Rate Service. These pointers are not NULL only if the characteristics are
105 // actually exposed.
106 scoped_ptr<Properties> heart_rate_measurement_properties_;
107 scoped_ptr<Properties> body_sensor_location_properties_;
108 scoped_ptr<Properties> heart_rate_control_point_properties_;
110 // Object paths of the exposed characteristics. If a characteristic is not
111 // exposed, these will be empty.
112 std::string heart_rate_measurement_path_;
113 std::string heart_rate_measurement_ccc_desc_path_;
114 std::string body_sensor_location_path_;
115 std::string heart_rate_control_point_path_;
117 // List of observers interested in event notifications from us.
118 ObserverList<Observer> observers_;
120 // Weak pointer factory for generating 'this' pointers that might live longer
121 // than we do.
122 // Note: This should remain the last member so it'll be destroyed and
123 // invalidate its weak pointers before any other members are destroyed.
124 base::WeakPtrFactory<FakeBluetoothGattCharacteristicClient>
125 weak_ptr_factory_;
127 DISALLOW_COPY_AND_ASSIGN(FakeBluetoothGattCharacteristicClient);
130 } // namespace chromeos
132 #endif // CHROMEOS_DBUS_FAKE_BLUETOOTH_GATT_CHARACTERISTIC_CLIENT_H_