OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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 "chrome/browser/chromeos/dbus/bluetooth_manager_client.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/logging.h" |
| 9 #include "chrome/browser/chromeos/system/runtime_environment.h" |
| 10 #include "dbus/bus.h" |
| 11 #include "dbus/message.h" |
| 12 #include "dbus/object_proxy.h" |
| 13 #include "third_party/cros_system_api/dbus/service_constants.h" |
| 14 |
| 15 namespace chromeos { |
| 16 |
| 17 // The BluetoothManagerClient implementation used in production. |
| 18 class BluetoothManagerClientImpl : public BluetoothManagerClient { |
| 19 public: |
| 20 explicit BluetoothManagerClientImpl(dbus::Bus* bus) |
| 21 : weak_ptr_factory_(this), |
| 22 bluetooth_manager_proxy_(NULL) { |
| 23 VLOG(1) << "Creating BluetoothManagerClientImpl"; |
| 24 |
| 25 DCHECK(bus); |
| 26 |
| 27 bluetooth_manager_proxy_ = bus->GetObjectProxy( |
| 28 bluetooth_manager::kBluetoothManagerServiceName, |
| 29 bluetooth_manager::kBluetoothManagerServicePath); |
| 30 |
| 31 bluetooth_manager_proxy_->ConnectToSignal( |
| 32 bluetooth_manager::kBluetoothManagerInterface, |
| 33 bluetooth_manager::kAdapterRemovedSignal, |
| 34 base::Bind(&BluetoothManagerClientImpl::AdapterRemovedReceived, |
| 35 weak_ptr_factory_.GetWeakPtr()), |
| 36 base::Bind(&BluetoothManagerClientImpl::AdapterRemovedConnected, |
| 37 weak_ptr_factory_.GetWeakPtr())); |
| 38 |
| 39 bluetooth_manager_proxy_->ConnectToSignal( |
| 40 bluetooth_manager::kBluetoothManagerInterface, |
| 41 bluetooth_manager::kDefaultAdapterChangedSignal, |
| 42 base::Bind(&BluetoothManagerClientImpl::DefaultAdapterChangedReceived, |
| 43 weak_ptr_factory_.GetWeakPtr()), |
| 44 base::Bind(&BluetoothManagerClientImpl::DefaultAdapterChangedConnected, |
| 45 weak_ptr_factory_.GetWeakPtr())); |
| 46 } |
| 47 |
| 48 virtual ~BluetoothManagerClientImpl() { |
| 49 } |
| 50 |
| 51 // BluetoothManagerClient override. |
| 52 virtual void AddObserver(Observer* observer) { |
| 53 VLOG(1) << "AddObserver"; |
| 54 DCHECK(observer); |
| 55 observers_.AddObserver(observer); |
| 56 } |
| 57 |
| 58 // BluetoothManagerClient override. |
| 59 virtual void RemoveObserver(Observer* observer) { |
| 60 VLOG(1) << "RemoveObserver"; |
| 61 DCHECK(observer); |
| 62 observers_.RemoveObserver(observer); |
| 63 } |
| 64 |
| 65 // BluetoothManagerClient override. |
| 66 virtual void DefaultAdapter(const DefaultAdapterCallback& callback) { |
| 67 LOG(INFO) << "DefaultAdapter"; |
| 68 |
| 69 dbus::MethodCall method_call( |
| 70 bluetooth_manager::kBluetoothManagerInterface, |
| 71 bluetooth_manager::kDefaultAdapter); |
| 72 |
| 73 DCHECK(bluetooth_manager_proxy_); |
| 74 bluetooth_manager_proxy_->CallMethod( |
| 75 &method_call, |
| 76 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, |
| 77 base::Bind(&BluetoothManagerClientImpl::OnDefaultAdapter, |
| 78 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 79 } |
| 80 |
| 81 private: |
| 82 // Called by dbus:: when an AdapterRemoved signal is received. |
| 83 void AdapterRemovedReceived(dbus::Signal* signal) { |
| 84 DCHECK(signal); |
| 85 dbus::MessageReader reader(signal); |
| 86 std::string adapter; |
| 87 if (!reader.PopObjectPath(&adapter)) { |
| 88 LOG(ERROR) << "AdapterRemoved signal has incorrect parameters: " |
| 89 << signal->ToString(); |
| 90 return; |
| 91 } |
| 92 VLOG(1) << "Adapter removed: " << adapter; |
| 93 FOR_EACH_OBSERVER(Observer, observers_, AdapterRemoved(adapter)); |
| 94 } |
| 95 |
| 96 // Called by dbus:: when the AdapterRemoved signal is initially connected. |
| 97 void AdapterRemovedConnected(const std::string& interface_name, |
| 98 const std::string& signal_name, |
| 99 bool success) { |
| 100 LOG_IF(WARNING, !success) << "Failed to connect to AdapterRemoved signal."; |
| 101 } |
| 102 |
| 103 // Called by dbus:: when a DefaultAdapterChanged signal is received. |
| 104 void DefaultAdapterChangedReceived(dbus::Signal* signal) { |
| 105 DCHECK(signal); |
| 106 dbus::MessageReader reader(signal); |
| 107 std::string adapter; |
| 108 if (!reader.PopObjectPath(&adapter)) { |
| 109 LOG(ERROR) << "DefaultAdapterChanged signal has incorrect parameters: " |
| 110 << signal->ToString(); |
| 111 return; |
| 112 } |
| 113 VLOG(1) << "Default adapter changed: " << adapter; |
| 114 FOR_EACH_OBSERVER(Observer, observers_, DefaultAdapterChanged(adapter)); |
| 115 } |
| 116 |
| 117 // Called by dbus:: when the DefaultAdapterChanged signal is initially |
| 118 // connected. |
| 119 void DefaultAdapterChangedConnected(const std::string& interface_name, |
| 120 const std::string& signal_name, |
| 121 bool success) { |
| 122 LOG_IF(WARNING, !success) |
| 123 << "Failed to connect to DefaultAdapterChanged signal."; |
| 124 } |
| 125 |
| 126 // Called when a response for DefaultAdapter() is received. |
| 127 void OnDefaultAdapter(const DefaultAdapterCallback& callback, |
| 128 dbus::Response* response) { |
| 129 // Parse response. |
| 130 bool success = false; |
| 131 std::string adapter; |
| 132 if (response != NULL) { |
| 133 dbus::MessageReader reader(response); |
| 134 if (!reader.PopObjectPath(&adapter)) { |
| 135 LOG(ERROR) << "DefaultAdapter response has incorrect parameters: " |
| 136 << response->ToString(); |
| 137 } else { |
| 138 success = true; |
| 139 LOG(INFO) << "OnDefaultAdapter: " << adapter; |
| 140 } |
| 141 } else { |
| 142 LOG(ERROR) << "Failed to get default adapter."; |
| 143 } |
| 144 |
| 145 // Notify client. |
| 146 callback.Run(adapter, success); |
| 147 } |
| 148 |
| 149 // Weak pointer factory for generating 'this' pointers that might live longer |
| 150 // than we do. |
| 151 base::WeakPtrFactory<BluetoothManagerClientImpl> weak_ptr_factory_; |
| 152 |
| 153 // D-Bus proxy for BlueZ Manager interface. |
| 154 dbus::ObjectProxy* bluetooth_manager_proxy_; |
| 155 |
| 156 // List of observers interested in event notifications from us. |
| 157 ObserverList<Observer> observers_; |
| 158 |
| 159 DISALLOW_COPY_AND_ASSIGN(BluetoothManagerClientImpl); |
| 160 }; |
| 161 |
| 162 // The BluetoothManagerClient implementation used on Linux desktop, which does |
| 163 // nothing. |
| 164 class BluetoothManagerClientStubImpl : public BluetoothManagerClient { |
| 165 public: |
| 166 // BluetoothManagerClient override. |
| 167 virtual void AddObserver(Observer* observer) { |
| 168 } |
| 169 |
| 170 // BluetoothManagerClient override. |
| 171 virtual void RemoveObserver(Observer* observer) { |
| 172 } |
| 173 |
| 174 // BluetoothManagerClient override. |
| 175 virtual void DefaultAdapter(const DefaultAdapterCallback& callback) { |
| 176 VLOG(1) << "Requested default adapter."; |
| 177 } |
| 178 }; |
| 179 |
| 180 BluetoothManagerClient::BluetoothManagerClient() { |
| 181 } |
| 182 |
| 183 BluetoothManagerClient::~BluetoothManagerClient() { |
| 184 } |
| 185 |
| 186 BluetoothManagerClient* BluetoothManagerClient::Create(dbus::Bus* bus) { |
| 187 if (system::runtime_environment::IsRunningOnChromeOS()) { |
| 188 return new BluetoothManagerClientImpl(bus); |
| 189 } else { |
| 190 return new BluetoothManagerClientStubImpl(); |
| 191 } |
| 192 } |
| 193 |
| 194 } // namespace chromeos |
OLD | NEW |