| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/browser/chromeos/dbus/bluetooth_manager_client.h" | 5 #include "chrome/browser/chromeos/dbus/bluetooth_manager_client.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "chrome/browser/chromeos/system/runtime_environment.h" | 9 #include "chrome/browser/chromeos/system/runtime_environment.h" |
| 10 #include "dbus/bus.h" | 10 #include "dbus/bus.h" |
| 11 #include "dbus/message.h" | 11 #include "dbus/message.h" |
| 12 #include "dbus/object_path.h" |
| 12 #include "dbus/object_proxy.h" | 13 #include "dbus/object_proxy.h" |
| 13 #include "third_party/cros_system_api/dbus/service_constants.h" | 14 #include "third_party/cros_system_api/dbus/service_constants.h" |
| 14 | 15 |
| 15 namespace chromeos { | 16 namespace chromeos { |
| 16 | 17 |
| 17 // The BluetoothManagerClient implementation used in production. | 18 // The BluetoothManagerClient implementation used in production. |
| 18 class BluetoothManagerClientImpl : public BluetoothManagerClient { | 19 class BluetoothManagerClientImpl : public BluetoothManagerClient { |
| 19 public: | 20 public: |
| 20 explicit BluetoothManagerClientImpl(dbus::Bus* bus) | 21 explicit BluetoothManagerClientImpl(dbus::Bus* bus) |
| 21 : weak_ptr_factory_(this), | 22 : weak_ptr_factory_(this), |
| 22 bluetooth_manager_proxy_(NULL) { | 23 bluetooth_manager_proxy_(NULL) { |
| 23 VLOG(1) << "Creating BluetoothManagerClientImpl"; | 24 VLOG(1) << "Creating BluetoothManagerClientImpl"; |
| 24 | 25 |
| 25 DCHECK(bus); | 26 DCHECK(bus); |
| 26 | 27 |
| 27 bluetooth_manager_proxy_ = bus->GetObjectProxy( | 28 bluetooth_manager_proxy_ = bus->GetObjectProxy( |
| 28 bluetooth_manager::kBluetoothManagerServiceName, | 29 bluetooth_manager::kBluetoothManagerServiceName, |
| 29 bluetooth_manager::kBluetoothManagerServicePath); | 30 dbus::ObjectPath(bluetooth_manager::kBluetoothManagerServicePath)); |
| 30 | 31 |
| 31 bluetooth_manager_proxy_->ConnectToSignal( | 32 bluetooth_manager_proxy_->ConnectToSignal( |
| 32 bluetooth_manager::kBluetoothManagerInterface, | 33 bluetooth_manager::kBluetoothManagerInterface, |
| 33 bluetooth_manager::kAdapterAddedSignal, | 34 bluetooth_manager::kAdapterAddedSignal, |
| 34 base::Bind(&BluetoothManagerClientImpl::AdapterAddedReceived, | 35 base::Bind(&BluetoothManagerClientImpl::AdapterAddedReceived, |
| 35 weak_ptr_factory_.GetWeakPtr()), | 36 weak_ptr_factory_.GetWeakPtr()), |
| 36 base::Bind(&BluetoothManagerClientImpl::AdapterAddedConnected, | 37 base::Bind(&BluetoothManagerClientImpl::AdapterAddedConnected, |
| 37 weak_ptr_factory_.GetWeakPtr())); | 38 weak_ptr_factory_.GetWeakPtr())); |
| 38 | 39 |
| 39 bluetooth_manager_proxy_->ConnectToSignal( | 40 bluetooth_manager_proxy_->ConnectToSignal( |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 84 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | 85 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
| 85 base::Bind(&BluetoothManagerClientImpl::OnDefaultAdapter, | 86 base::Bind(&BluetoothManagerClientImpl::OnDefaultAdapter, |
| 86 weak_ptr_factory_.GetWeakPtr(), callback)); | 87 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 87 } | 88 } |
| 88 | 89 |
| 89 private: | 90 private: |
| 90 // Called by dbus:: when an AdapterAdded signal is received. | 91 // Called by dbus:: when an AdapterAdded signal is received. |
| 91 void AdapterAddedReceived(dbus::Signal* signal) { | 92 void AdapterAddedReceived(dbus::Signal* signal) { |
| 92 DCHECK(signal); | 93 DCHECK(signal); |
| 93 dbus::MessageReader reader(signal); | 94 dbus::MessageReader reader(signal); |
| 94 std::string object_path; | 95 dbus::ObjectPath object_path; |
| 95 if (!reader.PopObjectPath(&object_path)) { | 96 if (!reader.PopObjectPath(&object_path)) { |
| 96 LOG(ERROR) << "AdapterAdded signal has incorrect parameters: " | 97 LOG(ERROR) << "AdapterAdded signal has incorrect parameters: " |
| 97 << signal->ToString(); | 98 << signal->ToString(); |
| 98 return; | 99 return; |
| 99 } | 100 } |
| 100 VLOG(1) << "Adapter added: " << object_path; | 101 VLOG(1) << "Adapter added: " << object_path.value(); |
| 101 FOR_EACH_OBSERVER(Observer, observers_, AdapterAdded(object_path)); | 102 FOR_EACH_OBSERVER(Observer, observers_, AdapterAdded(object_path)); |
| 102 } | 103 } |
| 103 | 104 |
| 104 // Called by dbus:: when the AdapterAdded signal is initially connected. | 105 // Called by dbus:: when the AdapterAdded signal is initially connected. |
| 105 void AdapterAddedConnected(const std::string& interface_name, | 106 void AdapterAddedConnected(const std::string& interface_name, |
| 106 const std::string& signal_name, | 107 const std::string& signal_name, |
| 107 bool success) { | 108 bool success) { |
| 108 LOG_IF(WARNING, !success) << "Failed to connect to AdapterAdded signal."; | 109 LOG_IF(WARNING, !success) << "Failed to connect to AdapterAdded signal."; |
| 109 } | 110 } |
| 110 | 111 |
| 111 // Called by dbus:: when an AdapterRemoved signal is received. | 112 // Called by dbus:: when an AdapterRemoved signal is received. |
| 112 void AdapterRemovedReceived(dbus::Signal* signal) { | 113 void AdapterRemovedReceived(dbus::Signal* signal) { |
| 113 DCHECK(signal); | 114 DCHECK(signal); |
| 114 dbus::MessageReader reader(signal); | 115 dbus::MessageReader reader(signal); |
| 115 std::string object_path; | 116 dbus::ObjectPath object_path; |
| 116 if (!reader.PopObjectPath(&object_path)) { | 117 if (!reader.PopObjectPath(&object_path)) { |
| 117 LOG(ERROR) << "AdapterRemoved signal has incorrect parameters: " | 118 LOG(ERROR) << "AdapterRemoved signal has incorrect parameters: " |
| 118 << signal->ToString(); | 119 << signal->ToString(); |
| 119 return; | 120 return; |
| 120 } | 121 } |
| 121 VLOG(1) << "Adapter removed: " << object_path; | 122 VLOG(1) << "Adapter removed: " << object_path.value(); |
| 122 FOR_EACH_OBSERVER(Observer, observers_, AdapterRemoved(object_path)); | 123 FOR_EACH_OBSERVER(Observer, observers_, AdapterRemoved(object_path)); |
| 123 } | 124 } |
| 124 | 125 |
| 125 // Called by dbus:: when the AdapterRemoved signal is initially connected. | 126 // Called by dbus:: when the AdapterRemoved signal is initially connected. |
| 126 void AdapterRemovedConnected(const std::string& interface_name, | 127 void AdapterRemovedConnected(const std::string& interface_name, |
| 127 const std::string& signal_name, | 128 const std::string& signal_name, |
| 128 bool success) { | 129 bool success) { |
| 129 LOG_IF(WARNING, !success) << "Failed to connect to AdapterRemoved signal."; | 130 LOG_IF(WARNING, !success) << "Failed to connect to AdapterRemoved signal."; |
| 130 } | 131 } |
| 131 | 132 |
| 132 // Called by dbus:: when a DefaultAdapterChanged signal is received. | 133 // Called by dbus:: when a DefaultAdapterChanged signal is received. |
| 133 void DefaultAdapterChangedReceived(dbus::Signal* signal) { | 134 void DefaultAdapterChangedReceived(dbus::Signal* signal) { |
| 134 DCHECK(signal); | 135 DCHECK(signal); |
| 135 dbus::MessageReader reader(signal); | 136 dbus::MessageReader reader(signal); |
| 136 std::string adapter; | 137 dbus::ObjectPath object_path; |
| 137 if (!reader.PopObjectPath(&adapter)) { | 138 if (!reader.PopObjectPath(&object_path)) { |
| 138 LOG(ERROR) << "DefaultAdapterChanged signal has incorrect parameters: " | 139 LOG(ERROR) << "DefaultAdapterChanged signal has incorrect parameters: " |
| 139 << signal->ToString(); | 140 << signal->ToString(); |
| 140 return; | 141 return; |
| 141 } | 142 } |
| 142 VLOG(1) << "Default adapter changed: " << adapter; | 143 VLOG(1) << "Default adapter changed: " << object_path.value(); |
| 143 FOR_EACH_OBSERVER(Observer, observers_, DefaultAdapterChanged(adapter)); | 144 FOR_EACH_OBSERVER(Observer, observers_, DefaultAdapterChanged(object_path)); |
| 144 } | 145 } |
| 145 | 146 |
| 146 // Called by dbus:: when the DefaultAdapterChanged signal is initially | 147 // Called by dbus:: when the DefaultAdapterChanged signal is initially |
| 147 // connected. | 148 // connected. |
| 148 void DefaultAdapterChangedConnected(const std::string& interface_name, | 149 void DefaultAdapterChangedConnected(const std::string& interface_name, |
| 149 const std::string& signal_name, | 150 const std::string& signal_name, |
| 150 bool success) { | 151 bool success) { |
| 151 LOG_IF(WARNING, !success) | 152 LOG_IF(WARNING, !success) |
| 152 << "Failed to connect to DefaultAdapterChanged signal."; | 153 << "Failed to connect to DefaultAdapterChanged signal."; |
| 153 } | 154 } |
| 154 | 155 |
| 155 // Called when a response for DefaultAdapter() is received. | 156 // Called when a response for DefaultAdapter() is received. |
| 156 void OnDefaultAdapter(const DefaultAdapterCallback& callback, | 157 void OnDefaultAdapter(const DefaultAdapterCallback& callback, |
| 157 dbus::Response* response) { | 158 dbus::Response* response) { |
| 158 // Parse response. | 159 // Parse response. |
| 159 bool success = false; | 160 bool success = false; |
| 160 std::string adapter; | 161 dbus::ObjectPath adapter; |
| 161 if (response != NULL) { | 162 if (response != NULL) { |
| 162 dbus::MessageReader reader(response); | 163 dbus::MessageReader reader(response); |
| 163 if (!reader.PopObjectPath(&adapter)) { | 164 if (!reader.PopObjectPath(&adapter)) { |
| 164 LOG(ERROR) << "DefaultAdapter response has incorrect parameters: " | 165 LOG(ERROR) << "DefaultAdapter response has incorrect parameters: " |
| 165 << response->ToString(); | 166 << response->ToString(); |
| 166 } else { | 167 } else { |
| 167 success = true; | 168 success = true; |
| 168 LOG(INFO) << "OnDefaultAdapter: " << adapter; | 169 LOG(INFO) << "OnDefaultAdapter: " << adapter.value(); |
| 169 } | 170 } |
| 170 } else { | 171 } else { |
| 171 LOG(ERROR) << "Failed to get default adapter."; | 172 LOG(ERROR) << "Failed to get default adapter."; |
| 172 } | 173 } |
| 173 | 174 |
| 174 // Notify client. | 175 // Notify client. |
| 175 callback.Run(adapter, success); | 176 callback.Run(adapter, success); |
| 176 } | 177 } |
| 177 | 178 |
| 178 // Weak pointer factory for generating 'this' pointers that might live longer | 179 // Weak pointer factory for generating 'this' pointers that might live longer |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 214 | 215 |
| 215 BluetoothManagerClient* BluetoothManagerClient::Create(dbus::Bus* bus) { | 216 BluetoothManagerClient* BluetoothManagerClient::Create(dbus::Bus* bus) { |
| 216 if (system::runtime_environment::IsRunningOnChromeOS()) { | 217 if (system::runtime_environment::IsRunningOnChromeOS()) { |
| 217 return new BluetoothManagerClientImpl(bus); | 218 return new BluetoothManagerClientImpl(bus); |
| 218 } else { | 219 } else { |
| 219 return new BluetoothManagerClientStubImpl(); | 220 return new BluetoothManagerClientStubImpl(); |
| 220 } | 221 } |
| 221 } | 222 } |
| 222 | 223 |
| 223 } // namespace chromeos | 224 } // namespace chromeos |
| OLD | NEW |