blob: 4c8f7fdfd38168246236c8c7bae5d02ea692017d [file] [log] [blame]
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chromeos/dbus/fake_bluetooth_media_client.h"
#include <string>
#include "base/stl_util.h"
#include "chromeos/dbus/dbus_thread_manager.h"
#include "chromeos/dbus/fake_bluetooth_adapter_client.h"
#include "chromeos/dbus/fake_bluetooth_media_endpoint_service_provider.h"
#include "chromeos/dbus/fake_bluetooth_media_transport_client.h"
using dbus::ObjectPath;
namespace {
// Except for |kFailedError|, the other error is defined in BlueZ D-Bus Media
// API.
const char kFailedError[] = "org.chromium.Error.Failed";
const char kInvalidArgumentsError[] = "org.chromium.Error.InvalidArguments";
} // namespace
namespace chromeos {
// static
const uint8_t FakeBluetoothMediaClient::kDefaultCodec = 0x00;
FakeBluetoothMediaClient::FakeBluetoothMediaClient()
: visible_(true),
object_path_(ObjectPath(FakeBluetoothAdapterClient::kAdapterPath)) {
}
FakeBluetoothMediaClient::~FakeBluetoothMediaClient() {
}
void FakeBluetoothMediaClient::Init(dbus::Bus* bus) {
}
void FakeBluetoothMediaClient::AddObserver(
BluetoothMediaClient::Observer* observer) {
DCHECK(observer);
observers_.AddObserver(observer);
}
void FakeBluetoothMediaClient::RemoveObserver(
BluetoothMediaClient::Observer* observer) {
DCHECK(observer);
observers_.RemoveObserver(observer);
}
void FakeBluetoothMediaClient::RegisterEndpoint(
const ObjectPath& object_path,
const ObjectPath& endpoint_path,
const EndpointProperties& properties,
const base::Closure& callback,
const ErrorCallback& error_callback) {
if (!visible_)
return;
VLOG(1) << "RegisterEndpoint: " << endpoint_path.value();
// The media client and adapter client should have the same object path.
if (object_path != object_path_ ||
properties.uuid != BluetoothMediaClient::kBluetoothAudioSinkUUID ||
properties.codec != kDefaultCodec || properties.capabilities.empty()) {
error_callback.Run(kInvalidArgumentsError, "");
return;
}
callback.Run();
}
void FakeBluetoothMediaClient::UnregisterEndpoint(
const ObjectPath& object_path,
const ObjectPath& endpoint_path,
const base::Closure& callback,
const ErrorCallback& error_callback) {
// TODO(mcchou): Come up with some corresponding actions.
VLOG(1) << "UnregisterEndpoint: " << endpoint_path.value();
if (!ContainsKey(endpoints_, endpoint_path)) {
error_callback.Run(kFailedError, "Unknown media endpoint");
return;
}
SetEndpointRegistered(endpoints_[endpoint_path], false);
callback.Run();
}
void FakeBluetoothMediaClient::SetVisible(bool visible) {
visible_ = visible;
if (visible_)
return;
// If the media object becomes invisible, an update chain will unregister all
// endpoints and set the associated transport objects to be invalid.
// SetEndpointRegistered will remove the endpoint entry from |endpoints_|.
while (endpoints_.begin() != endpoints_.end())
SetEndpointRegistered(endpoints_.begin()->second, false);
// Notifies observers about the change on |visible_|.
FOR_EACH_OBSERVER(BluetoothMediaClient::Observer, observers_,
MediaRemoved(object_path_));
}
void FakeBluetoothMediaClient::SetEndpointRegistered(
FakeBluetoothMediaEndpointServiceProvider* endpoint,
bool registered) {
if (registered) {
endpoints_[endpoint->object_path()] = endpoint;
return;
}
if (!IsRegistered(endpoint->object_path()))
return;
// Once a media endpoint object becomes invalid, invalidate the associated
// transport.
FakeBluetoothMediaTransportClient* transport =
static_cast<FakeBluetoothMediaTransportClient*>(
DBusThreadManager::Get()->GetBluetoothMediaTransportClient());
transport->SetValid(endpoint, false);
endpoints_.erase(endpoint->object_path());
endpoint->Released();
}
bool FakeBluetoothMediaClient::IsRegistered(
const dbus::ObjectPath& endpoint_path) {
return ContainsKey(endpoints_, endpoint_path);
}
} // namespace chromeos