Revert of Roll src/third_party/WebKit e0eac24:489c548 (svn 193311:193320) (patchset...
[chromium-blink-merge.git] / chromeos / dbus / fake_bluetooth_media_client.cc
blob4c8f7fdfd38168246236c8c7bae5d02ea692017d
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"
7 #include <string>
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;
17 namespace {
19 // Except for |kFailedError|, the other error is defined in BlueZ D-Bus Media
20 // API.
21 const char kFailedError[] = "org.chromium.Error.Failed";
22 const char kInvalidArgumentsError[] = "org.chromium.Error.InvalidArguments";
24 } // namespace
26 namespace chromeos {
28 // static
29 const uint8_t FakeBluetoothMediaClient::kDefaultCodec = 0x00;
31 FakeBluetoothMediaClient::FakeBluetoothMediaClient()
32 : visible_(true),
33 object_path_(ObjectPath(FakeBluetoothAdapterClient::kAdapterPath)) {
36 FakeBluetoothMediaClient::~FakeBluetoothMediaClient() {
39 void FakeBluetoothMediaClient::Init(dbus::Bus* bus) {
42 void FakeBluetoothMediaClient::AddObserver(
43 BluetoothMediaClient::Observer* observer) {
44 DCHECK(observer);
45 observers_.AddObserver(observer);
48 void FakeBluetoothMediaClient::RemoveObserver(
49 BluetoothMediaClient::Observer* observer) {
50 DCHECK(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) {
60 if (!visible_)
61 return;
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, "");
70 return;
73 callback.Run();
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");
86 return;
89 SetEndpointRegistered(endpoints_[endpoint_path], false);
90 callback.Run();
93 void FakeBluetoothMediaClient::SetVisible(bool visible) {
94 visible_ = visible;
96 if (visible_)
97 return;
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,
112 bool registered) {
113 if (registered) {
114 endpoints_[endpoint->object_path()] = endpoint;
115 return;
118 if (!IsRegistered(endpoint->object_path()))
119 return;
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);
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