| 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 "chromeos/dbus/privet_daemon_manager_client.h" | |
| 6 | |
| 7 #include <stddef.h> | |
| 8 #include <stdint.h> | |
| 9 | |
| 10 #include "base/bind.h" | |
| 11 #include "base/location.h" | |
| 12 #include "base/logging.h" | |
| 13 #include "base/macros.h" | |
| 14 #include "base/memory/weak_ptr.h" | |
| 15 #include "base/observer_list.h" | |
| 16 #include "base/single_thread_task_runner.h" | |
| 17 #include "base/threading/thread_task_runner_handle.h" | |
| 18 #include "dbus/bus.h" | |
| 19 #include "dbus/message.h" | |
| 20 #include "dbus/object_manager.h" | |
| 21 #include "dbus/object_proxy.h" | |
| 22 | |
| 23 namespace chromeos { | |
| 24 | |
| 25 namespace { | |
| 26 | |
| 27 // TODO(benchan): Move these constants to system_api. | |
| 28 const char kManagerInterfaceName[] = "org.chromium.privetd.Manager"; | |
| 29 const char kPrivetdManagerPath[] = "/org/chromium/privetd/Manager"; | |
| 30 const char kPrivetdServiceName[] = "org.chromium.privetd"; | |
| 31 const char kPrivetdServicePath[] = "/org/chromium/privetd"; | |
| 32 const char kSetDescriptionMethod[] = "SetDescription"; | |
| 33 | |
| 34 const char kDescriptionProperty[] = "Description"; | |
| 35 const char kGCDBootstrapStateProperty[] = "GCDBootstrapState"; | |
| 36 const char kNameProperty[] = "Name"; | |
| 37 const char kPairingInfoCodeProperty[] = "code"; | |
| 38 const char kPairingInfoModeProperty[] = "mode"; | |
| 39 const char kPairingInfoProperty[] = "PairingInfo"; | |
| 40 const char kPairingInfoSessionIdProperty[] = "sessionId"; | |
| 41 const char kWiFiBootstrapStateProperty[] = "WiFiBootstrapState"; | |
| 42 | |
| 43 // The PrivetDaemonManagerClient implementation used in production. | |
| 44 class PrivetDaemonManagerClientImpl : public PrivetDaemonManagerClient, | |
| 45 public dbus::ObjectManager::Interface { | |
| 46 public: | |
| 47 PrivetDaemonManagerClientImpl(); | |
| 48 ~PrivetDaemonManagerClientImpl() override; | |
| 49 | |
| 50 // PrivetDaemonManagerClient overrides. | |
| 51 void AddObserver(Observer* observer) override; | |
| 52 void RemoveObserver(Observer* observer) override; | |
| 53 void SetDescription(const std::string& description, | |
| 54 const VoidDBusMethodCallback& callback) override; | |
| 55 const Properties* GetProperties() override; | |
| 56 | |
| 57 // DBusClient overrides. | |
| 58 void Init(dbus::Bus* bus) override; | |
| 59 | |
| 60 // dbus::ObjectManager::Interface overrides. | |
| 61 dbus::PropertySet* CreateProperties( | |
| 62 dbus::ObjectProxy* object_proxy, | |
| 63 const dbus::ObjectPath& object_path, | |
| 64 const std::string& interface_name) override; | |
| 65 void ObjectAdded(const dbus::ObjectPath& object_path, | |
| 66 const std::string& interface_name) override; | |
| 67 void ObjectRemoved(const dbus::ObjectPath& object_path, | |
| 68 const std::string& interface_name) override; | |
| 69 | |
| 70 private: | |
| 71 // Called by dbus::PropertySet when a property value is changed, | |
| 72 // either by result of a signal or response to a GetAll() or Get() | |
| 73 // call. Informs observers. | |
| 74 void OnManagerPropertyChanged(const std::string& property_name); | |
| 75 void OnVoidDBusMethod(const VoidDBusMethodCallback& callback, | |
| 76 dbus::Response* response); | |
| 77 | |
| 78 // List of observers interested in event notifications from us. | |
| 79 base::ObserverList<Observer> observers_; | |
| 80 dbus::ObjectManager* object_manager_; | |
| 81 base::WeakPtrFactory<PrivetDaemonManagerClientImpl> weak_ptr_factory_; | |
| 82 | |
| 83 DISALLOW_COPY_AND_ASSIGN(PrivetDaemonManagerClientImpl); | |
| 84 }; | |
| 85 | |
| 86 PrivetDaemonManagerClientImpl::PrivetDaemonManagerClientImpl() | |
| 87 : object_manager_(nullptr), weak_ptr_factory_(this) { | |
| 88 } | |
| 89 | |
| 90 PrivetDaemonManagerClientImpl::~PrivetDaemonManagerClientImpl() { | |
| 91 if (object_manager_) { | |
| 92 object_manager_->UnregisterInterface(kManagerInterfaceName); | |
| 93 } | |
| 94 } | |
| 95 | |
| 96 void PrivetDaemonManagerClientImpl::AddObserver(Observer* observer) { | |
| 97 DCHECK(observer); | |
| 98 observers_.AddObserver(observer); | |
| 99 } | |
| 100 | |
| 101 void PrivetDaemonManagerClientImpl::RemoveObserver(Observer* observer) { | |
| 102 DCHECK(observer); | |
| 103 observers_.RemoveObserver(observer); | |
| 104 } | |
| 105 | |
| 106 void PrivetDaemonManagerClientImpl::SetDescription( | |
| 107 const std::string& description, | |
| 108 const VoidDBusMethodCallback& callback) { | |
| 109 dbus::ObjectProxy* object_proxy = | |
| 110 object_manager_->GetObjectProxy(dbus::ObjectPath(kPrivetdManagerPath)); | |
| 111 if (!object_proxy) { | |
| 112 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 113 FROM_HERE, | |
| 114 base::Bind(&PrivetDaemonManagerClientImpl::OnVoidDBusMethod, | |
| 115 weak_ptr_factory_.GetWeakPtr(), callback, nullptr)); | |
| 116 return; | |
| 117 } | |
| 118 | |
| 119 dbus::MethodCall method_call(kManagerInterfaceName, kSetDescriptionMethod); | |
| 120 dbus::MessageWriter writer(&method_call); | |
| 121 writer.AppendString(description); | |
| 122 object_proxy->CallMethod( | |
| 123 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
| 124 base::Bind(&PrivetDaemonManagerClientImpl::OnVoidDBusMethod, | |
| 125 weak_ptr_factory_.GetWeakPtr(), callback)); | |
| 126 } | |
| 127 | |
| 128 const PrivetDaemonManagerClient::Properties* | |
| 129 PrivetDaemonManagerClientImpl::GetProperties() { | |
| 130 return static_cast<Properties*>(object_manager_->GetProperties( | |
| 131 dbus::ObjectPath(kPrivetdManagerPath), kManagerInterfaceName)); | |
| 132 } | |
| 133 | |
| 134 void PrivetDaemonManagerClientImpl::Init(dbus::Bus* bus) { | |
| 135 object_manager_ = bus->GetObjectManager( | |
| 136 kPrivetdServiceName, dbus::ObjectPath(kPrivetdServicePath)); | |
| 137 object_manager_->RegisterInterface(kManagerInterfaceName, this); | |
| 138 } | |
| 139 | |
| 140 dbus::PropertySet* PrivetDaemonManagerClientImpl::CreateProperties( | |
| 141 dbus::ObjectProxy* object_proxy, | |
| 142 const dbus::ObjectPath& object_path, | |
| 143 const std::string& interface_name) { | |
| 144 dbus::PropertySet* properties = nullptr; | |
| 145 if (interface_name == kManagerInterfaceName) { | |
| 146 properties = new Properties( | |
| 147 object_proxy, interface_name, | |
| 148 base::Bind(&PrivetDaemonManagerClientImpl::OnManagerPropertyChanged, | |
| 149 weak_ptr_factory_.GetWeakPtr())); | |
| 150 } else { | |
| 151 NOTREACHED() << "Unhandled interface name " << interface_name; | |
| 152 } | |
| 153 return properties; | |
| 154 } | |
| 155 | |
| 156 void PrivetDaemonManagerClientImpl::ObjectAdded( | |
| 157 const dbus::ObjectPath& object_path, | |
| 158 const std::string& interface_name) { | |
| 159 if (interface_name == kManagerInterfaceName) { | |
| 160 FOR_EACH_OBSERVER(Observer, observers_, ManagerAdded()); | |
| 161 } else { | |
| 162 NOTREACHED() << "Unhandled interface name " << interface_name; | |
| 163 } | |
| 164 } | |
| 165 | |
| 166 void PrivetDaemonManagerClientImpl::ObjectRemoved( | |
| 167 const dbus::ObjectPath& object_path, | |
| 168 const std::string& interface_name) { | |
| 169 if (interface_name == kManagerInterfaceName) { | |
| 170 FOR_EACH_OBSERVER(Observer, observers_, ManagerRemoved()); | |
| 171 } else { | |
| 172 NOTREACHED() << "Unhandled interface name " << interface_name; | |
| 173 } | |
| 174 } | |
| 175 | |
| 176 void PrivetDaemonManagerClientImpl::OnManagerPropertyChanged( | |
| 177 const std::string& property_name) { | |
| 178 FOR_EACH_OBSERVER(Observer, observers_, | |
| 179 ManagerPropertyChanged(property_name)); | |
| 180 } | |
| 181 | |
| 182 void PrivetDaemonManagerClientImpl::OnVoidDBusMethod( | |
| 183 const VoidDBusMethodCallback& callback, | |
| 184 dbus::Response* response) { | |
| 185 callback.Run(response ? DBUS_METHOD_CALL_SUCCESS : DBUS_METHOD_CALL_FAILURE); | |
| 186 } | |
| 187 | |
| 188 } // namespace | |
| 189 | |
| 190 void PrivetDaemonManagerClient::PairingInfo::Clear() { | |
| 191 code_.clear(); | |
| 192 mode_.clear(); | |
| 193 session_id_.clear(); | |
| 194 } | |
| 195 | |
| 196 bool PrivetDaemonManagerClient::PairingInfoProperty::PopValueFromReader( | |
| 197 dbus::MessageReader* reader) { | |
| 198 dbus::MessageReader variant_reader(nullptr); | |
| 199 dbus::MessageReader array_reader(nullptr); | |
| 200 value_.Clear(); | |
| 201 if (!reader->PopVariant(&variant_reader) || | |
| 202 !variant_reader.PopArray(&array_reader)) { | |
| 203 return false; | |
| 204 } | |
| 205 while (array_reader.HasMoreData()) { | |
| 206 dbus::MessageReader dict_entry_reader(nullptr); | |
| 207 if (!array_reader.PopDictEntry(&dict_entry_reader)) | |
| 208 return false; | |
| 209 std::string key; | |
| 210 if (!dict_entry_reader.PopString(&key)) | |
| 211 return false; | |
| 212 | |
| 213 dbus::MessageReader field_reader(nullptr); | |
| 214 if (!dict_entry_reader.PopVariant(&field_reader)) | |
| 215 return false; | |
| 216 | |
| 217 if (field_reader.GetDataSignature() == "s") { | |
| 218 std::string string_value; | |
| 219 if (!field_reader.PopString(&string_value)) | |
| 220 return false; | |
| 221 if (key == kPairingInfoSessionIdProperty) { | |
| 222 value_.set_session_id(string_value); | |
| 223 } else if (key == kPairingInfoModeProperty) { | |
| 224 value_.set_mode(string_value); | |
| 225 } | |
| 226 } else if (field_reader.GetDataSignature() == "ay") { | |
| 227 const uint8_t* bytes = nullptr; | |
| 228 size_t length = 0; | |
| 229 if (!field_reader.PopArrayOfBytes(&bytes, &length)) | |
| 230 return false; | |
| 231 if (key == kPairingInfoCodeProperty) | |
| 232 value_.set_code(bytes, length); | |
| 233 } | |
| 234 } | |
| 235 return true; | |
| 236 } | |
| 237 | |
| 238 PrivetDaemonManagerClient::PairingInfo::PairingInfo() { | |
| 239 } | |
| 240 | |
| 241 PrivetDaemonManagerClient::PairingInfo::~PairingInfo() { | |
| 242 } | |
| 243 | |
| 244 void PrivetDaemonManagerClient::PairingInfoProperty::AppendSetValueToWriter( | |
| 245 dbus::MessageWriter* writer) { | |
| 246 NOTIMPLEMENTED(); | |
| 247 } | |
| 248 | |
| 249 void PrivetDaemonManagerClient::PairingInfoProperty:: | |
| 250 ReplaceValueWithSetValue() { | |
| 251 NOTIMPLEMENTED(); | |
| 252 } | |
| 253 | |
| 254 PrivetDaemonManagerClient::Properties::Properties( | |
| 255 dbus::ObjectProxy* object_proxy, | |
| 256 const std::string& interface_name, | |
| 257 const PropertyChangedCallback& callback) | |
| 258 : dbus::PropertySet(object_proxy, interface_name, callback) { | |
| 259 RegisterProperty(kWiFiBootstrapStateProperty, &wifi_bootstrap_state_); | |
| 260 RegisterProperty(kGCDBootstrapStateProperty, &gcd_bootstrap_state_); | |
| 261 RegisterProperty(kPairingInfoProperty, &pairing_info_); | |
| 262 RegisterProperty(kDescriptionProperty, &description_); | |
| 263 RegisterProperty(kNameProperty, &name_); | |
| 264 } | |
| 265 | |
| 266 PrivetDaemonManagerClient::Properties::~Properties() { | |
| 267 } | |
| 268 | |
| 269 PrivetDaemonManagerClient::Observer::~Observer() { | |
| 270 } | |
| 271 | |
| 272 PrivetDaemonManagerClient::PrivetDaemonManagerClient() { | |
| 273 } | |
| 274 | |
| 275 PrivetDaemonManagerClient::~PrivetDaemonManagerClient() { | |
| 276 } | |
| 277 | |
| 278 // static | |
| 279 PrivetDaemonManagerClient* PrivetDaemonManagerClient::Create() { | |
| 280 return new PrivetDaemonManagerClientImpl(); | |
| 281 } | |
| 282 | |
| 283 } // namespace chromeos | |
| OLD | NEW |