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