OLD | NEW |
| (Empty) |
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 CHROMEOS_DBUS_FAKE_BLUETOOTH_MEDIA_TRANSPORT_CLIENT_H_ | |
6 #define CHROMEOS_DBUS_FAKE_BLUETOOTH_MEDIA_TRANSPORT_CLIENT_H_ | |
7 | |
8 #include <map> | |
9 #include <string> | |
10 #include <vector> | |
11 | |
12 #include "base/files/file.h" | |
13 #include "base/memory/scoped_ptr.h" | |
14 #include "base/observer_list.h" | |
15 #include "chromeos/chromeos_export.h" | |
16 #include "chromeos/dbus/bluetooth_media_transport_client.h" | |
17 #include "dbus/object_path.h" | |
18 | |
19 namespace chromeos { | |
20 | |
21 class FakeBluetoothMediaEndpointServiceProvider; | |
22 | |
23 class CHROMEOS_EXPORT FakeBluetoothMediaTransportClient | |
24 : public BluetoothMediaTransportClient { | |
25 public: | |
26 struct Properties : public BluetoothMediaTransportClient::Properties { | |
27 explicit Properties(const PropertyChangedCallback& callback); | |
28 ~Properties() override; | |
29 | |
30 void Get(dbus::PropertyBase* property, | |
31 dbus::PropertySet::GetCallback callback) override; | |
32 void GetAll() override; | |
33 void Set(dbus::PropertyBase* property, | |
34 dbus::PropertySet::SetCallback callback) override; | |
35 }; | |
36 | |
37 // The default path of the transport object. | |
38 static const char kTransportPath[]; | |
39 | |
40 // The default properties including device, codec, configuration, state, delay | |
41 // and volume, owned by a fake media transport object we emulate. | |
42 static const char kTransportDevicePath[]; | |
43 static const uint8_t kTransportCodec; | |
44 static const std::vector<uint8_t> kTransportConfiguration; | |
45 static const uint16_t kTransportDelay; | |
46 static const uint16_t kTransportVolume; | |
47 | |
48 // The default MTUs for read and write. | |
49 static const uint16_t kDefaultReadMtu; | |
50 static const uint16_t kDefaultWriteMtu; | |
51 | |
52 FakeBluetoothMediaTransportClient(); | |
53 ~FakeBluetoothMediaTransportClient() override; | |
54 | |
55 // DBusClient override. | |
56 void Init(dbus::Bus* bus) override; | |
57 | |
58 // BluetoothMediaTransportClient override. | |
59 void AddObserver(Observer* observer) override; | |
60 void RemoveObserver(Observer* observer) override; | |
61 Properties* GetProperties(const dbus::ObjectPath& object_path) override; | |
62 void Acquire(const dbus::ObjectPath& object_path, | |
63 const AcquireCallback& callback, | |
64 const ErrorCallback& error_callback) override; | |
65 void TryAcquire(const dbus::ObjectPath& object_path, | |
66 const AcquireCallback& callback, | |
67 const ErrorCallback& error_callback) override; | |
68 void Release(const dbus::ObjectPath& object_path, | |
69 const base::Closure& callback, | |
70 const ErrorCallback& error_callback) override; | |
71 | |
72 // Makes the transport valid/invalid for a given media endpoint. The transport | |
73 // object is assigned to the given endpoint if valid is true, false | |
74 // otherwise. | |
75 void SetValid(FakeBluetoothMediaEndpointServiceProvider* endpoint, | |
76 bool valid); | |
77 | |
78 // Set state/volume property to a certain value. | |
79 void SetState(const dbus::ObjectPath& endpoint_path, | |
80 const std::string& state); | |
81 void SetVolume(const dbus::ObjectPath& endpoint_path, | |
82 const uint16_t& volume); | |
83 | |
84 // Writes bytes to the input file descriptor, |input_fd|, associated with a | |
85 // transport object which is bound to |endpoint_path|. | |
86 void WriteData(const dbus::ObjectPath& endpoint_path, | |
87 const std::vector<char>& bytes); | |
88 | |
89 // Retrieves the transport object path bound to |endpoint_path|. | |
90 dbus::ObjectPath GetTransportPath(const dbus::ObjectPath& endpoint_path); | |
91 | |
92 private: | |
93 // This class is used for simulating the scenario where each media endpoint | |
94 // has a corresponding transport path and properties. Once an endpoint is | |
95 // assigned with a transport path, an object of Transport is created. | |
96 struct Transport { | |
97 Transport(const dbus::ObjectPath& transport_path, | |
98 Properties* transport_properties); | |
99 ~Transport(); | |
100 | |
101 // An unique transport path. | |
102 dbus::ObjectPath path; | |
103 | |
104 // The property set bound with |path|. | |
105 scoped_ptr<Properties> properties; | |
106 | |
107 // This is the internal end of socketpair created for simulation purposes. | |
108 // |input_fd| will be initialized when Acquire/TryAcquire is called. | |
109 scoped_ptr<base::File> input_fd; | |
110 }; | |
111 | |
112 // Property callback passed while a Properties structure is created. | |
113 void OnPropertyChanged(const std::string& property_name); | |
114 | |
115 // Gets the endpoint path associated with the given transport_path. | |
116 dbus::ObjectPath GetEndpointPath(const dbus::ObjectPath& transport_path); | |
117 | |
118 // Retrieves the transport structure bound to |endpoint_path|. | |
119 Transport* GetTransport(const dbus::ObjectPath& endpoint_path); | |
120 | |
121 // Retrieves the transport structure with |transport_path|. | |
122 Transport* GetTransportByPath(const dbus::ObjectPath& transport_path); | |
123 | |
124 // Helper function used by Acquire and TryAcquire to set up the sockpair and | |
125 // invoke callback/error_callback. | |
126 void AcquireInternal(bool try_flag, | |
127 const dbus::ObjectPath& object_path, | |
128 const AcquireCallback& callback, | |
129 const ErrorCallback& error_callback); | |
130 | |
131 // Map of endpoints with valid transport. Each pair is composed of an endpoint | |
132 // path and a Transport structure containing a transport path and its | |
133 // properties. | |
134 std::map<dbus::ObjectPath, Transport*> endpoint_to_transport_map_; | |
135 | |
136 // Map of valid transports. Each pair is composed of a transport path as the | |
137 // key and an endpoint path as the value. This map is used to get the | |
138 // corresponding endpoint path when GetProperties() is called. | |
139 std::map<dbus::ObjectPath, dbus::ObjectPath> transport_to_endpoint_map_; | |
140 | |
141 base::ObserverList<BluetoothMediaTransportClient::Observer> observers_; | |
142 | |
143 DISALLOW_COPY_AND_ASSIGN(FakeBluetoothMediaTransportClient); | |
144 }; | |
145 | |
146 } // namespace chromeos | |
147 | |
148 #endif // CHROMEOS_DBUS_FAKE_BLUETOOTH_MEDIA_TRANSPORT_CLIENT_H_ | |
OLD | NEW |