Extensions: Store disable reasons in Sync
[chromium-blink-merge.git] / device / bluetooth / bluetooth_adapter_profile_chromeos.cc
blob207c55c4bad2bd952aba95776ead895cfde3303a
1 // Copyright 2015 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 "device/bluetooth/bluetooth_adapter_profile_chromeos.h"
7 #include <string>
9 #include "base/bind.h"
10 #include "base/logging.h"
11 #include "base/strings/string_util.h"
12 #include "chromeos/dbus/bluetooth_profile_service_provider.h"
13 #include "chromeos/dbus/dbus_thread_manager.h"
14 #include "dbus/bus.h"
15 #include "dbus/object_path.h"
16 #include "device/bluetooth/bluetooth_adapter_chromeos.h"
17 #include "device/bluetooth/bluetooth_uuid.h"
19 namespace chromeos {
21 // static
22 void BluetoothAdapterProfileChromeOS::Register(
23 const device::BluetoothUUID& uuid,
24 const BluetoothProfileManagerClient::Options& options,
25 const ProfileRegisteredCallback& success_callback,
26 const BluetoothProfileManagerClient::ErrorCallback& error_callback) {
27 scoped_ptr<BluetoothAdapterProfileChromeOS> profile(
28 new BluetoothAdapterProfileChromeOS(uuid));
30 VLOG(1) << "Registering profile: " << profile->object_path().value();
31 const dbus::ObjectPath& object_path = profile->object_path();
32 DBusThreadManager::Get()->GetBluetoothProfileManagerClient()->RegisterProfile(
33 object_path,
34 uuid.canonical_value(),
35 options,
36 base::Bind(success_callback, base::Passed(&profile)),
37 error_callback);
40 BluetoothAdapterProfileChromeOS::BluetoothAdapterProfileChromeOS(
41 const device::BluetoothUUID& uuid)
42 : uuid_(uuid), weak_ptr_factory_(this) {
43 std::string uuid_path;
44 base::ReplaceChars(uuid.canonical_value(), ":-", "_", &uuid_path);
45 object_path_ =
46 dbus::ObjectPath("/org/chromium/bluetooth_profile/" + uuid_path);
48 dbus::Bus* system_bus = DBusThreadManager::Get()->GetSystemBus();
49 profile_.reset(
50 BluetoothProfileServiceProvider::Create(system_bus, object_path_, this));
51 DCHECK(profile_.get());
54 BluetoothAdapterProfileChromeOS::~BluetoothAdapterProfileChromeOS() {
57 bool BluetoothAdapterProfileChromeOS::SetDelegate(
58 const dbus::ObjectPath& device_path,
59 BluetoothProfileServiceProvider::Delegate* delegate) {
60 DCHECK(delegate);
61 VLOG(1) << "SetDelegate: " << object_path_.value() << " dev "
62 << device_path.value();
64 if (delegates_.find(device_path.value()) != delegates_.end()) {
65 return false;
68 delegates_[device_path.value()] = delegate;
69 return true;
72 void BluetoothAdapterProfileChromeOS::RemoveDelegate(
73 const dbus::ObjectPath& device_path,
74 const base::Closure& unregistered_callback) {
75 VLOG(1) << object_path_.value() << " dev " << device_path.value()
76 << ": RemoveDelegate";
78 if (delegates_.find(device_path.value()) == delegates_.end())
79 return;
81 delegates_.erase(device_path.value());
83 if (delegates_.size() != 0)
84 return;
86 VLOG(1) << device_path.value() << " No delegates left, unregistering.";
88 // No users left, release the profile.
89 DBusThreadManager::Get()
90 ->GetBluetoothProfileManagerClient()
91 ->UnregisterProfile(
92 object_path_, unregistered_callback,
93 base::Bind(&BluetoothAdapterProfileChromeOS::OnUnregisterProfileError,
94 weak_ptr_factory_.GetWeakPtr(), unregistered_callback));
97 void BluetoothAdapterProfileChromeOS::OnUnregisterProfileError(
98 const base::Closure& unregistered_callback,
99 const std::string& error_name,
100 const std::string& error_message) {
101 LOG(WARNING) << this->object_path().value()
102 << ": Failed to unregister profile: " << error_name << ": "
103 << error_message;
105 unregistered_callback.Run();
108 // BluetoothProfileServiceProvider::Delegate:
109 void BluetoothAdapterProfileChromeOS::Released() {
110 VLOG(1) << object_path_.value() << ": Release";
113 void BluetoothAdapterProfileChromeOS::NewConnection(
114 const dbus::ObjectPath& device_path,
115 scoped_ptr<dbus::FileDescriptor> fd,
116 const BluetoothProfileServiceProvider::Delegate::Options& options,
117 const ConfirmationCallback& callback) {
118 dbus::ObjectPath delegate_path = device_path;
120 if (delegates_.find(device_path.value()) == delegates_.end())
121 delegate_path = dbus::ObjectPath("");
123 if (delegates_.find(delegate_path.value()) == delegates_.end()) {
124 VLOG(1) << object_path_.value() << ": New connection for device "
125 << device_path.value() << " which has no delegates!";
126 callback.Run(REJECTED);
127 return;
130 delegates_[delegate_path.value()]->NewConnection(device_path, fd.Pass(),
131 options, callback);
134 void BluetoothAdapterProfileChromeOS::RequestDisconnection(
135 const dbus::ObjectPath& device_path,
136 const ConfirmationCallback& callback) {
137 dbus::ObjectPath delegate_path = device_path;
139 if (delegates_.find(device_path.value()) == delegates_.end())
140 delegate_path = dbus::ObjectPath("");
142 if (delegates_.find(delegate_path.value()) == delegates_.end()) {
143 VLOG(1) << object_path_.value() << ": RequestDisconnection for device "
144 << device_path.value() << " which has no delegates!";
145 return;
148 delegates_[delegate_path.value()]->RequestDisconnection(device_path,
149 callback);
152 void BluetoothAdapterProfileChromeOS::Cancel() {
153 // Cancel() should only go to a delegate accepting connections.
154 if (delegates_.find("") == delegates_.end()) {
155 VLOG(1) << object_path_.value() << ": Cancel with no delegate!";
156 return;
159 delegates_[""]->Cancel();
162 } // namespace chromeos