Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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. | |
| 4 | |
| 5 #include "device/bluetooth/bluetooth_adapter_profile_chromeos.h" | |
| 6 | |
| 7 #include <string> | |
| 8 | |
| 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" | |
| 18 | |
| 19 namespace chromeos { | |
| 20 | |
| 21 // static | |
| 22 BluetoothAdapterProfileChromeOS* BluetoothAdapterProfileChromeOS::Register( | |
| 23 BluetoothAdapterChromeOS* adapter, | |
| 24 const device::BluetoothUUID& uuid, | |
| 25 const BluetoothProfileManagerClient::Options* options, | |
| 26 const base::Closure& success_callback, | |
| 27 const BluetoothProfileManagerClient::ErrorCallback& error_callback) { | |
| 28 BluetoothAdapterProfileChromeOS* profile = | |
| 29 new BluetoothAdapterProfileChromeOS(adapter, uuid); | |
| 30 | |
| 31 VLOG(1) << profile->object_path().value() << ": Registering profile."; | |
|
armansito
2015/01/21 01:44:47
nit: I'd turn this log line around: "Registering p
Marie Janssen
2015/01/22 21:55:32
Done.
| |
| 32 DBusThreadManager::Get()->GetBluetoothProfileManagerClient()->RegisterProfile( | |
| 33 profile->object_path(), uuid.canonical_value(), *options, | |
| 34 success_callback, error_callback); | |
| 35 | |
| 36 return profile; | |
| 37 } | |
| 38 | |
| 39 BluetoothAdapterProfileChromeOS::BluetoothAdapterProfileChromeOS( | |
| 40 BluetoothAdapterChromeOS* adapter, | |
| 41 const device::BluetoothUUID& uuid) | |
| 42 : uuid_(uuid), adapter_(adapter), 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); | |
| 47 | |
| 48 dbus::Bus* system_bus = DBusThreadManager::Get()->GetSystemBus(); | |
| 49 profile_.reset( | |
| 50 BluetoothProfileServiceProvider::Create(system_bus, object_path_, this)); | |
| 51 DCHECK(profile_.get()); | |
| 52 } | |
| 53 | |
| 54 BluetoothAdapterProfileChromeOS::~BluetoothAdapterProfileChromeOS() { | |
| 55 profile_.reset(); | |
| 56 } | |
| 57 | |
| 58 bool BluetoothAdapterProfileChromeOS::SetDelegate( | |
| 59 const dbus::ObjectPath& device_path, | |
| 60 BluetoothProfileServiceProvider::Delegate* delegate) { | |
|
armansito
2015/01/21 01:44:47
DCHECK(delegate);
Marie Janssen
2015/01/22 21:55:32
Done.
| |
| 61 VLOG(1) << object_path_.value() << " dev " << device_path.value() | |
| 62 << ": SetDelegate"; | |
| 63 | |
| 64 if (delegates_.find(device_path.value()) != delegates_.end()) { | |
| 65 return false; | |
| 66 } | |
| 67 | |
| 68 delegates_[device_path.value()] = delegate; | |
| 69 return true; | |
| 70 } | |
| 71 | |
| 72 void BluetoothAdapterProfileChromeOS::RemoveDelegate( | |
| 73 const dbus::ObjectPath& device_path) { | |
| 74 DCHECK(adapter_); | |
| 75 VLOG(1) << object_path_.value() << " dev " << device_path.value() | |
| 76 << ": RemoveDelegate"; | |
| 77 | |
| 78 if (delegates_.find(device_path.value()) == delegates_.end()) | |
| 79 return; | |
| 80 | |
| 81 VLOG(1) << device_path.value() << "Delegate found"; | |
| 82 | |
| 83 delegates_.erase(device_path.value()); | |
| 84 | |
| 85 if (delegates_.size() != 0) | |
| 86 return; | |
| 87 | |
| 88 // No users left, release the profile. | |
| 89 DBusThreadManager::Get() | |
| 90 ->GetBluetoothProfileManagerClient() | |
| 91 ->UnregisterProfile( | |
| 92 object_path_, | |
| 93 base::Bind(&BluetoothAdapterProfileChromeOS::OnUnregisterProfile, | |
| 94 weak_ptr_factory_.GetWeakPtr()), | |
| 95 base::Bind(&BluetoothAdapterProfileChromeOS::OnUnregisterProfileError, | |
| 96 weak_ptr_factory_.GetWeakPtr())); | |
| 97 } | |
| 98 | |
| 99 void BluetoothAdapterProfileChromeOS::OnUnregisterProfile() { | |
| 100 VLOG(1) << object_path().value() << ": Profile unregistered"; | |
| 101 | |
| 102 adapter_->ReleaseProfile(uuid_); | |
| 103 } | |
| 104 | |
| 105 void BluetoothAdapterProfileChromeOS::OnUnregisterProfileError( | |
| 106 const std::string& error_name, | |
| 107 const std::string& error_message) { | |
| 108 LOG(WARNING) << this->object_path().value() | |
| 109 << ": Failed to unregister profile: " << error_name << ": " | |
| 110 << error_message; | |
| 111 | |
| 112 adapter_->ReleaseProfile(uuid_); | |
| 113 } | |
| 114 | |
| 115 // BluetoothProfileServiceProvider::Delegate: | |
| 116 void BluetoothAdapterProfileChromeOS::Released() { | |
| 117 VLOG(1) << object_path_.value() << ": Release"; | |
| 118 } | |
| 119 | |
| 120 void BluetoothAdapterProfileChromeOS::NewConnection( | |
| 121 const dbus::ObjectPath& device_path, | |
| 122 scoped_ptr<dbus::FileDescriptor> fd, | |
| 123 const BluetoothProfileServiceProvider::Delegate::Options& options, | |
| 124 const ConfirmationCallback& callback) { | |
| 125 dbus::ObjectPath delegate_path = device_path; | |
| 126 | |
| 127 if (delegates_.find(device_path.value()) == delegates_.end()) | |
| 128 delegate_path = dbus::ObjectPath(""); | |
| 129 | |
| 130 if (delegates_.find(delegate_path.value()) == delegates_.end()) { | |
| 131 VLOG(1) << object_path_.value() << ": New connection for device " | |
| 132 << device_path.value() << " which has no delegates!"; | |
| 133 callback.Run(REJECTED); | |
| 134 return; | |
| 135 } | |
| 136 | |
| 137 delegates_[delegate_path.value()]->NewConnection(device_path, fd.Pass(), | |
| 138 options, callback); | |
| 139 } | |
| 140 | |
| 141 void BluetoothAdapterProfileChromeOS::RequestDisconnection( | |
| 142 const dbus::ObjectPath& device_path, | |
| 143 const ConfirmationCallback& callback) { | |
| 144 dbus::ObjectPath delegate_path = device_path; | |
| 145 | |
| 146 if (delegates_.find(device_path.value()) == delegates_.end()) | |
| 147 delegate_path = dbus::ObjectPath(""); | |
| 148 | |
| 149 if (delegates_.find(delegate_path.value()) == delegates_.end()) { | |
| 150 VLOG(1) << object_path_.value() << ": RequestDisconnection for device " | |
| 151 << device_path.value() << " which has no delegates!"; | |
| 152 return; | |
| 153 } | |
| 154 | |
| 155 delegates_[delegate_path.value()]->RequestDisconnection(device_path, | |
| 156 callback); | |
| 157 } | |
| 158 | |
| 159 void BluetoothAdapterProfileChromeOS::Cancel() { | |
| 160 // Cancel() should only go to a delegate accepting connections. | |
| 161 if (delegates_.find("") == delegates_.end()) { | |
| 162 VLOG(1) << object_path_.value() << ": Cancel with no delegate!"; | |
| 163 return; | |
| 164 } | |
| 165 | |
| 166 delegates_[""]->Cancel(); | |
| 167 } | |
| 168 | |
| 169 } // namespace chromeos | |
| OLD | NEW |