Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(431)

Side by Side Diff: device/bluetooth/bluetooth_adapter_profile_bluez.cc

Issue 1415573014: Reland "Add Linux support for the Bluetooth API" (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: build fix. Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "device/bluetooth/bluetooth_adapter_profile_chromeos.h" 5 #include "device/bluetooth/bluetooth_adapter_profile_bluez.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/logging.h" 10 #include "base/logging.h"
11 #include "base/strings/string_util.h" 11 #include "base/strings/string_util.h"
12 #include "dbus/bus.h" 12 #include "dbus/bus.h"
13 #include "dbus/object_path.h" 13 #include "dbus/object_path.h"
14 #include "device/bluetooth/bluetooth_adapter_chromeos.h" 14 #include "device/bluetooth/bluetooth_adapter_bluez.h"
15 #include "device/bluetooth/bluetooth_uuid.h" 15 #include "device/bluetooth/bluetooth_uuid.h"
16 #include "device/bluetooth/dbus/bluetooth_profile_service_provider.h" 16 #include "device/bluetooth/dbus/bluetooth_profile_service_provider.h"
17 #include "device/bluetooth/dbus/bluez_dbus_manager.h" 17 #include "device/bluetooth/dbus/bluez_dbus_manager.h"
18 18
19 namespace chromeos { 19 namespace bluez {
20 20
21 // static 21 // static
22 void BluetoothAdapterProfileChromeOS::Register( 22 void BluetoothAdapterProfileBlueZ::Register(
23 const device::BluetoothUUID& uuid, 23 const device::BluetoothUUID& uuid,
24 const bluez::BluetoothProfileManagerClient::Options& options, 24 const bluez::BluetoothProfileManagerClient::Options& options,
25 const ProfileRegisteredCallback& success_callback, 25 const ProfileRegisteredCallback& success_callback,
26 const bluez::BluetoothProfileManagerClient::ErrorCallback& error_callback) { 26 const bluez::BluetoothProfileManagerClient::ErrorCallback& error_callback) {
27 scoped_ptr<BluetoothAdapterProfileChromeOS> profile( 27 scoped_ptr<BluetoothAdapterProfileBlueZ> profile(
28 new BluetoothAdapterProfileChromeOS(uuid)); 28 new BluetoothAdapterProfileBlueZ(uuid));
29 29
30 VLOG(1) << "Registering profile: " << profile->object_path().value(); 30 VLOG(1) << "Registering profile: " << profile->object_path().value();
31 const dbus::ObjectPath& object_path = profile->object_path(); 31 const dbus::ObjectPath& object_path = profile->object_path();
32 bluez::BluezDBusManager::Get() 32 bluez::BluezDBusManager::Get()
33 ->GetBluetoothProfileManagerClient() 33 ->GetBluetoothProfileManagerClient()
34 ->RegisterProfile(object_path, uuid.canonical_value(), options, 34 ->RegisterProfile(object_path, uuid.canonical_value(), options,
35 base::Bind(success_callback, base::Passed(&profile)), 35 base::Bind(success_callback, base::Passed(&profile)),
36 error_callback); 36 error_callback);
37 } 37 }
38 38
39 BluetoothAdapterProfileChromeOS::BluetoothAdapterProfileChromeOS( 39 BluetoothAdapterProfileBlueZ::BluetoothAdapterProfileBlueZ(
40 const device::BluetoothUUID& uuid) 40 const device::BluetoothUUID& uuid)
41 : uuid_(uuid), weak_ptr_factory_(this) { 41 : uuid_(uuid), weak_ptr_factory_(this) {
42 std::string uuid_path; 42 std::string uuid_path;
43 base::ReplaceChars(uuid.canonical_value(), ":-", "_", &uuid_path); 43 base::ReplaceChars(uuid.canonical_value(), ":-", "_", &uuid_path);
44 object_path_ = 44 object_path_ =
45 dbus::ObjectPath("/org/chromium/bluetooth_profile/" + uuid_path); 45 dbus::ObjectPath("/org/chromium/bluetooth_profile/" + uuid_path);
46 46
47 dbus::Bus* system_bus = bluez::BluezDBusManager::Get()->GetSystemBus(); 47 dbus::Bus* system_bus = bluez::BluezDBusManager::Get()->GetSystemBus();
48 profile_.reset(bluez::BluetoothProfileServiceProvider::Create( 48 profile_.reset(bluez::BluetoothProfileServiceProvider::Create(
49 system_bus, object_path_, this)); 49 system_bus, object_path_, this));
50 DCHECK(profile_.get()); 50 DCHECK(profile_.get());
51 } 51 }
52 52
53 BluetoothAdapterProfileChromeOS::~BluetoothAdapterProfileChromeOS() { 53 BluetoothAdapterProfileBlueZ::~BluetoothAdapterProfileBlueZ() {}
54 }
55 54
56 bool BluetoothAdapterProfileChromeOS::SetDelegate( 55 bool BluetoothAdapterProfileBlueZ::SetDelegate(
57 const dbus::ObjectPath& device_path, 56 const dbus::ObjectPath& device_path,
58 bluez::BluetoothProfileServiceProvider::Delegate* delegate) { 57 bluez::BluetoothProfileServiceProvider::Delegate* delegate) {
59 DCHECK(delegate); 58 DCHECK(delegate);
60 VLOG(1) << "SetDelegate: " << object_path_.value() << " dev " 59 VLOG(1) << "SetDelegate: " << object_path_.value() << " dev "
61 << device_path.value(); 60 << device_path.value();
62 61
63 if (delegates_.find(device_path.value()) != delegates_.end()) { 62 if (delegates_.find(device_path.value()) != delegates_.end()) {
64 return false; 63 return false;
65 } 64 }
66 65
67 delegates_[device_path.value()] = delegate; 66 delegates_[device_path.value()] = delegate;
68 return true; 67 return true;
69 } 68 }
70 69
71 void BluetoothAdapterProfileChromeOS::RemoveDelegate( 70 void BluetoothAdapterProfileBlueZ::RemoveDelegate(
72 const dbus::ObjectPath& device_path, 71 const dbus::ObjectPath& device_path,
73 const base::Closure& unregistered_callback) { 72 const base::Closure& unregistered_callback) {
74 VLOG(1) << object_path_.value() << " dev " << device_path.value() 73 VLOG(1) << object_path_.value() << " dev " << device_path.value()
75 << ": RemoveDelegate"; 74 << ": RemoveDelegate";
76 75
77 if (delegates_.find(device_path.value()) == delegates_.end()) 76 if (delegates_.find(device_path.value()) == delegates_.end())
78 return; 77 return;
79 78
80 delegates_.erase(device_path.value()); 79 delegates_.erase(device_path.value());
81 80
82 if (delegates_.size() != 0) 81 if (delegates_.size() != 0)
83 return; 82 return;
84 83
85 VLOG(1) << device_path.value() << " No delegates left, unregistering."; 84 VLOG(1) << device_path.value() << " No delegates left, unregistering.";
86 85
87 // No users left, release the profile. 86 // No users left, release the profile.
88 bluez::BluezDBusManager::Get() 87 bluez::BluezDBusManager::Get()
89 ->GetBluetoothProfileManagerClient() 88 ->GetBluetoothProfileManagerClient()
90 ->UnregisterProfile( 89 ->UnregisterProfile(
91 object_path_, unregistered_callback, 90 object_path_, unregistered_callback,
92 base::Bind(&BluetoothAdapterProfileChromeOS::OnUnregisterProfileError, 91 base::Bind(&BluetoothAdapterProfileBlueZ::OnUnregisterProfileError,
93 weak_ptr_factory_.GetWeakPtr(), unregistered_callback)); 92 weak_ptr_factory_.GetWeakPtr(), unregistered_callback));
94 } 93 }
95 94
96 void BluetoothAdapterProfileChromeOS::OnUnregisterProfileError( 95 void BluetoothAdapterProfileBlueZ::OnUnregisterProfileError(
97 const base::Closure& unregistered_callback, 96 const base::Closure& unregistered_callback,
98 const std::string& error_name, 97 const std::string& error_name,
99 const std::string& error_message) { 98 const std::string& error_message) {
100 LOG(WARNING) << this->object_path().value() 99 LOG(WARNING) << this->object_path().value()
101 << ": Failed to unregister profile: " << error_name << ": " 100 << ": Failed to unregister profile: " << error_name << ": "
102 << error_message; 101 << error_message;
103 102
104 unregistered_callback.Run(); 103 unregistered_callback.Run();
105 } 104 }
106 105
107 // bluez::BluetoothProfileServiceProvider::Delegate: 106 // bluez::BluetoothProfileServiceProvider::Delegate:
108 void BluetoothAdapterProfileChromeOS::Released() { 107 void BluetoothAdapterProfileBlueZ::Released() {
109 VLOG(1) << object_path_.value() << ": Release"; 108 VLOG(1) << object_path_.value() << ": Release";
110 } 109 }
111 110
112 void BluetoothAdapterProfileChromeOS::NewConnection( 111 void BluetoothAdapterProfileBlueZ::NewConnection(
113 const dbus::ObjectPath& device_path, 112 const dbus::ObjectPath& device_path,
114 scoped_ptr<dbus::FileDescriptor> fd, 113 scoped_ptr<dbus::FileDescriptor> fd,
115 const bluez::BluetoothProfileServiceProvider::Delegate::Options& options, 114 const bluez::BluetoothProfileServiceProvider::Delegate::Options& options,
116 const ConfirmationCallback& callback) { 115 const ConfirmationCallback& callback) {
117 dbus::ObjectPath delegate_path = device_path; 116 dbus::ObjectPath delegate_path = device_path;
118 117
119 if (delegates_.find(device_path.value()) == delegates_.end()) 118 if (delegates_.find(device_path.value()) == delegates_.end())
120 delegate_path = dbus::ObjectPath(""); 119 delegate_path = dbus::ObjectPath("");
121 120
122 if (delegates_.find(delegate_path.value()) == delegates_.end()) { 121 if (delegates_.find(delegate_path.value()) == delegates_.end()) {
123 VLOG(1) << object_path_.value() << ": New connection for device " 122 VLOG(1) << object_path_.value() << ": New connection for device "
124 << device_path.value() << " which has no delegates!"; 123 << device_path.value() << " which has no delegates!";
125 callback.Run(REJECTED); 124 callback.Run(REJECTED);
126 return; 125 return;
127 } 126 }
128 127
129 delegates_[delegate_path.value()]->NewConnection(device_path, fd.Pass(), 128 delegates_[delegate_path.value()]->NewConnection(device_path, fd.Pass(),
130 options, callback); 129 options, callback);
131 } 130 }
132 131
133 void BluetoothAdapterProfileChromeOS::RequestDisconnection( 132 void BluetoothAdapterProfileBlueZ::RequestDisconnection(
134 const dbus::ObjectPath& device_path, 133 const dbus::ObjectPath& device_path,
135 const ConfirmationCallback& callback) { 134 const ConfirmationCallback& callback) {
136 dbus::ObjectPath delegate_path = device_path; 135 dbus::ObjectPath delegate_path = device_path;
137 136
138 if (delegates_.find(device_path.value()) == delegates_.end()) 137 if (delegates_.find(device_path.value()) == delegates_.end())
139 delegate_path = dbus::ObjectPath(""); 138 delegate_path = dbus::ObjectPath("");
140 139
141 if (delegates_.find(delegate_path.value()) == delegates_.end()) { 140 if (delegates_.find(delegate_path.value()) == delegates_.end()) {
142 VLOG(1) << object_path_.value() << ": RequestDisconnection for device " 141 VLOG(1) << object_path_.value() << ": RequestDisconnection for device "
143 << device_path.value() << " which has no delegates!"; 142 << device_path.value() << " which has no delegates!";
144 return; 143 return;
145 } 144 }
146 145
147 delegates_[delegate_path.value()]->RequestDisconnection(device_path, 146 delegates_[delegate_path.value()]->RequestDisconnection(device_path,
148 callback); 147 callback);
149 } 148 }
150 149
151 void BluetoothAdapterProfileChromeOS::Cancel() { 150 void BluetoothAdapterProfileBlueZ::Cancel() {
152 // Cancel() should only go to a delegate accepting connections. 151 // Cancel() should only go to a delegate accepting connections.
153 if (delegates_.find("") == delegates_.end()) { 152 if (delegates_.find("") == delegates_.end()) {
154 VLOG(1) << object_path_.value() << ": Cancel with no delegate!"; 153 VLOG(1) << object_path_.value() << ": Cancel with no delegate!";
155 return; 154 return;
156 } 155 }
157 156
158 delegates_[""]->Cancel(); 157 delegates_[""]->Cancel();
159 } 158 }
160 159
161 } // namespace chromeos 160 } // namespace bluez
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698