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