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 #include "chromeos/dbus/fake_bluetooth_media_client.h"
9 #include "base/stl_util.h"
10 #include "chromeos/dbus/dbus_thread_manager.h"
11 #include "chromeos/dbus/fake_bluetooth_adapter_client.h"
12 #include "chromeos/dbus/fake_bluetooth_media_endpoint_service_provider.h"
13 #include "chromeos/dbus/fake_bluetooth_media_transport_client.h"
15 using dbus::ObjectPath
;
19 // Except for |kFailedError|, the other error is defined in BlueZ D-Bus Media
21 const char kFailedError
[] = "org.chromium.Error.Failed";
22 const char kInvalidArgumentsError
[] = "org.chromium.Error.InvalidArguments";
29 const uint8_t FakeBluetoothMediaClient::kDefaultCodec
= 0x00;
31 FakeBluetoothMediaClient::FakeBluetoothMediaClient()
33 object_path_(ObjectPath(FakeBluetoothAdapterClient::kAdapterPath
)) {
36 FakeBluetoothMediaClient::~FakeBluetoothMediaClient() {
39 void FakeBluetoothMediaClient::Init(dbus::Bus
* bus
) {
42 void FakeBluetoothMediaClient::AddObserver(
43 BluetoothMediaClient::Observer
* observer
) {
45 observers_
.AddObserver(observer
);
48 void FakeBluetoothMediaClient::RemoveObserver(
49 BluetoothMediaClient::Observer
* observer
) {
51 observers_
.RemoveObserver(observer
);
54 void FakeBluetoothMediaClient::RegisterEndpoint(
55 const ObjectPath
& object_path
,
56 const ObjectPath
& endpoint_path
,
57 const EndpointProperties
& properties
,
58 const base::Closure
& callback
,
59 const ErrorCallback
& error_callback
) {
63 VLOG(1) << "RegisterEndpoint: " << endpoint_path
.value();
65 // The media client and adapter client should have the same object path.
66 if (object_path
!= object_path_
||
67 properties
.uuid
!= BluetoothMediaClient::kBluetoothAudioSinkUUID
||
68 properties
.codec
!= kDefaultCodec
|| properties
.capabilities
.empty()) {
69 error_callback
.Run(kInvalidArgumentsError
, "");
76 void FakeBluetoothMediaClient::UnregisterEndpoint(
77 const ObjectPath
& object_path
,
78 const ObjectPath
& endpoint_path
,
79 const base::Closure
& callback
,
80 const ErrorCallback
& error_callback
) {
81 // TODO(mcchou): Come up with some corresponding actions.
82 VLOG(1) << "UnregisterEndpoint: " << endpoint_path
.value();
84 if (!ContainsKey(endpoints_
, endpoint_path
)) {
85 error_callback
.Run(kFailedError
, "Unknown media endpoint");
89 SetEndpointRegistered(endpoints_
[endpoint_path
], false);
93 void FakeBluetoothMediaClient::SetVisible(bool visible
) {
99 // If the media object becomes invisible, an update chain will unregister all
100 // endpoints and set the associated transport objects to be invalid.
101 // SetEndpointRegistered will remove the endpoint entry from |endpoints_|.
102 while (endpoints_
.begin() != endpoints_
.end())
103 SetEndpointRegistered(endpoints_
.begin()->second
, false);
105 // Notifies observers about the change on |visible_|.
106 FOR_EACH_OBSERVER(BluetoothMediaClient::Observer
, observers_
,
107 MediaRemoved(object_path_
));
110 void FakeBluetoothMediaClient::SetEndpointRegistered(
111 FakeBluetoothMediaEndpointServiceProvider
* endpoint
,
114 endpoints_
[endpoint
->object_path()] = endpoint
;
118 if (!IsRegistered(endpoint
->object_path()))
121 // Once a media endpoint object becomes invalid, invalidate the associated
123 FakeBluetoothMediaTransportClient
* transport
=
124 static_cast<FakeBluetoothMediaTransportClient
*>(
125 DBusThreadManager::Get()->GetBluetoothMediaTransportClient());
126 transport
->SetValid(endpoint
, false);
128 endpoints_
.erase(endpoint
->object_path());
129 endpoint
->Released();
132 bool FakeBluetoothMediaClient::IsRegistered(
133 const dbus::ObjectPath
& endpoint_path
) {
134 return ContainsKey(endpoints_
, endpoint_path
);
137 } // namespace chromeos