OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chromeos/dbus/fake_bluetooth_media_client.h" | 5 #include "chromeos/dbus/fake_bluetooth_media_client.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
| 9 #include "base/stl_util.h" |
9 #include "chromeos/dbus/dbus_thread_manager.h" | 10 #include "chromeos/dbus/dbus_thread_manager.h" |
10 #include "chromeos/dbus/fake_bluetooth_adapter_client.h" | 11 #include "chromeos/dbus/fake_bluetooth_adapter_client.h" |
| 12 #include "chromeos/dbus/fake_bluetooth_media_endpoint_service_provider.h" |
11 #include "chromeos/dbus/fake_bluetooth_media_transport_client.h" | 13 #include "chromeos/dbus/fake_bluetooth_media_transport_client.h" |
12 | 14 |
13 using dbus::ObjectPath; | 15 using dbus::ObjectPath; |
14 | 16 |
15 namespace { | 17 namespace { |
16 | 18 |
17 // Except for |kFailedError|, the other error is defined in BlueZ D-Bus Media | 19 // Except for |kFailedError|, the other error is defined in BlueZ D-Bus Media |
18 // API. | 20 // API. |
19 const char kFailedError[] = "org.chromium.Error.Failed"; | 21 const char kFailedError[] = "org.chromium.Error.Failed"; |
20 const char kInvalidArgumentsError[] = "org.chromium.Error.InvalidArguments"; | 22 const char kInvalidArgumentsError[] = "org.chromium.Error.InvalidArguments"; |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
61 VLOG(1) << "RegisterEndpoint: " << endpoint_path.value(); | 63 VLOG(1) << "RegisterEndpoint: " << endpoint_path.value(); |
62 | 64 |
63 // The media client and adapter client should have the same object path. | 65 // The media client and adapter client should have the same object path. |
64 if (object_path != object_path_ || | 66 if (object_path != object_path_ || |
65 properties.uuid != BluetoothMediaClient::kBluetoothAudioSinkUUID || | 67 properties.uuid != BluetoothMediaClient::kBluetoothAudioSinkUUID || |
66 properties.codec != kDefaultCodec || properties.capabilities.empty()) { | 68 properties.codec != kDefaultCodec || properties.capabilities.empty()) { |
67 error_callback.Run(kInvalidArgumentsError, ""); | 69 error_callback.Run(kInvalidArgumentsError, ""); |
68 return; | 70 return; |
69 } | 71 } |
70 | 72 |
71 // Sets the state of a given endpoint path to true if it is registered. | |
72 SetEndpointRegistered(endpoint_path, true); | |
73 | |
74 callback.Run(); | 73 callback.Run(); |
75 } | 74 } |
76 | 75 |
77 void FakeBluetoothMediaClient::UnregisterEndpoint( | 76 void FakeBluetoothMediaClient::UnregisterEndpoint( |
78 const ObjectPath& object_path, | 77 const ObjectPath& object_path, |
79 const ObjectPath& endpoint_path, | 78 const ObjectPath& endpoint_path, |
80 const base::Closure& callback, | 79 const base::Closure& callback, |
81 const ErrorCallback& error_callback) { | 80 const ErrorCallback& error_callback) { |
82 // TODO(mcchou): Come up with some corresponding actions. | 81 // TODO(mcchou): Come up with some corresponding actions. |
83 error_callback.Run(kFailedError, ""); | 82 VLOG(1) << "UnregisterEndpoint: " << endpoint_path.value(); |
| 83 |
| 84 if (!ContainsKey(endpoints_, endpoint_path)) { |
| 85 error_callback.Run(kFailedError, "Unknown media endpoint"); |
| 86 return; |
| 87 } |
| 88 |
| 89 SetEndpointRegistered(endpoints_[endpoint_path], false); |
| 90 callback.Run(); |
84 } | 91 } |
85 | 92 |
86 void FakeBluetoothMediaClient::SetVisible(bool visible) { | 93 void FakeBluetoothMediaClient::SetVisible(bool visible) { |
87 visible_ = visible; | 94 visible_ = visible; |
88 | 95 |
89 if (visible_) | 96 if (visible_) |
90 return; | 97 return; |
91 | 98 |
92 // If the media object becomes invisible, an update chain will | 99 // If the media object becomes invisible, an update chain will unregister all |
93 // unregister all endpoints and set the associated transport | 100 // endpoints and set the associated transport objects to be invalid. |
94 // objects to be invalid. | 101 // SetEndpointRegistered will remove the endpoint entry from |endpoints_|. |
95 for (std::map<ObjectPath, bool>::iterator it = endpoints_.begin(); | 102 while (endpoints_.begin() != endpoints_.end()) |
96 it != endpoints_.end(); ++it) { | 103 SetEndpointRegistered(endpoints_.begin()->second, false); |
97 SetEndpointRegistered(it->first, false); | |
98 } | |
99 endpoints_.clear(); | |
100 | 104 |
101 // Notifies observers about the change on |visible_|. | 105 // Notifies observers about the change on |visible_|. |
102 FOR_EACH_OBSERVER(BluetoothMediaClient::Observer, observers_, | 106 FOR_EACH_OBSERVER(BluetoothMediaClient::Observer, observers_, |
103 MediaRemoved(object_path_)); | 107 MediaRemoved(object_path_)); |
104 } | 108 } |
105 | 109 |
106 void FakeBluetoothMediaClient::SetEndpointRegistered( | 110 void FakeBluetoothMediaClient::SetEndpointRegistered( |
107 const ObjectPath& endpoint_path, | 111 FakeBluetoothMediaEndpointServiceProvider* endpoint, |
108 bool registered) { | 112 bool registered) { |
109 if (registered) { | 113 if (registered) { |
110 endpoints_[endpoint_path] = registered; | 114 endpoints_[endpoint->object_path()] = endpoint; |
111 } else { | 115 return; |
112 // Once a media endpoint object becomes invalid, the associated transport | |
113 // becomes invalid. | |
114 FakeBluetoothMediaTransportClient* transport = | |
115 static_cast<FakeBluetoothMediaTransportClient*>( | |
116 DBusThreadManager::Get()->GetBluetoothMediaTransportClient()); | |
117 transport->SetValid(endpoint_path, true); | |
118 } | 116 } |
| 117 |
| 118 if (!IsRegistered(endpoint->object_path())) |
| 119 return; |
| 120 |
| 121 // Once a media endpoint object becomes invalid, invalidate the associated |
| 122 // transport. |
| 123 FakeBluetoothMediaTransportClient* transport = |
| 124 static_cast<FakeBluetoothMediaTransportClient*>( |
| 125 DBusThreadManager::Get()->GetBluetoothMediaTransportClient()); |
| 126 transport->SetValid(endpoint, false); |
| 127 |
| 128 endpoints_.erase(endpoint->object_path()); |
| 129 endpoint->Released(); |
| 130 } |
| 131 |
| 132 bool FakeBluetoothMediaClient::IsRegistered( |
| 133 const dbus::ObjectPath& endpoint_path) { |
| 134 return ContainsKey(endpoints_, endpoint_path); |
119 } | 135 } |
120 | 136 |
121 } // namespace chromeos | 137 } // namespace chromeos |
OLD | NEW |