| 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 #ifndef DBUS_PROPERTY_H_ | 5 #ifndef DBUS_PROPERTY_H_ |
| 6 #define DBUS_PROPERTY_H_ | 6 #define DBUS_PROPERTY_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <map> | 9 #include <map> |
| 10 #include <string> | 10 #include <string> |
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 150 // Properties& properties = GetProperties(object_path); | 150 // Properties& properties = GetProperties(object_path); |
| 151 // if (property_name == properties.version.name()) { | 151 // if (property_name == properties.version.name()) { |
| 152 // // Handle version property changing | 152 // // Handle version property changing |
| 153 // } | 153 // } |
| 154 // } | 154 // } |
| 155 const std::string& name() const { return name_; } | 155 const std::string& name() const { return name_; } |
| 156 | 156 |
| 157 // Method used by PropertySet to retrieve the value from a MessageReader, | 157 // Method used by PropertySet to retrieve the value from a MessageReader, |
| 158 // no knowledge of the contained type is required, this method returns | 158 // no knowledge of the contained type is required, this method returns |
| 159 // true if its expected type was found, false if not. | 159 // true if its expected type was found, false if not. |
| 160 // Implementation provided by specialization. |
| 160 virtual bool PopValueFromReader(MessageReader*) = 0; | 161 virtual bool PopValueFromReader(MessageReader*) = 0; |
| 161 | 162 |
| 163 // Method used by PropertySet to append the set value to a MessageWriter, |
| 164 // no knowledge of the contained type is required. |
| 165 // Implementation provided by specialization. |
| 166 virtual void AppendSetValueToWriter(MessageWriter* writer) = 0; |
| 167 |
| 162 protected: | 168 protected: |
| 163 // Retrieves the associated property set. | 169 // Retrieves the associated property set. |
| 164 PropertySet* property_set() { return property_set_; } | 170 PropertySet* property_set() { return property_set_; } |
| 165 | 171 |
| 166 private: | 172 private: |
| 167 // Pointer to the PropertySet instance that this instance is a member of, | 173 // Pointer to the PropertySet instance that this instance is a member of, |
| 168 // no ownership is taken and |property_set_| must outlive this class. | 174 // no ownership is taken and |property_set_| must outlive this class. |
| 169 PropertySet* property_set_; | 175 PropertySet* property_set_; |
| 170 | 176 |
| 171 // Name of the property. | 177 // Name of the property. |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 216 virtual void ConnectSignals(); | 222 virtual void ConnectSignals(); |
| 217 | 223 |
| 218 // Methods connected by ConnectSignals() and called by dbus:: when | 224 // Methods connected by ConnectSignals() and called by dbus:: when |
| 219 // a property is changed. Sub-classes may override if the property | 225 // a property is changed. Sub-classes may override if the property |
| 220 // changed signal provides different arguments. | 226 // changed signal provides different arguments. |
| 221 virtual void ChangedReceived(Signal*); | 227 virtual void ChangedReceived(Signal*); |
| 222 virtual void ChangedConnected(const std::string& interface_name, | 228 virtual void ChangedConnected(const std::string& interface_name, |
| 223 const std::string& signal_name, | 229 const std::string& signal_name, |
| 224 bool success); | 230 bool success); |
| 225 | 231 |
| 232 // Callback for Get() method, |success| indicates whether or not the |
| 233 // value could be retrived, if true the new value can be obtained by |
| 234 // calling value() on the property. |
| 235 typedef base::Callback<void(bool success)> GetCallback; |
| 236 |
| 237 // Requests an updated value from the remote object for |property| |
| 238 // incurring a round-trip. |callback| will be called when the new |
| 239 // value is available. This may not be implemented by some interfaces, |
| 240 // and may be overriden by sub-classes if interfaces use different |
| 241 // method calls. |
| 242 virtual void Get(PropertyBase* property, GetCallback callback); |
| 243 virtual void OnGet(PropertyBase* property, GetCallback callback, |
| 244 Response* response); |
| 245 |
| 226 // Queries the remote object for values of all properties and updates | 246 // Queries the remote object for values of all properties and updates |
| 227 // initial values. Sub-classes may override to use a different D-Bus | 247 // initial values. Sub-classes may override to use a different D-Bus |
| 228 // method, or if the remote object does not support retrieving all | 248 // method, or if the remote object does not support retrieving all |
| 229 // properties, either ignore or obtain each property value individually. | 249 // properties, either ignore or obtain each property value individually. |
| 230 virtual void GetAll(); | 250 virtual void GetAll(); |
| 231 virtual void OnGetAll(Response* response); | 251 virtual void OnGetAll(Response* response); |
| 232 | 252 |
| 253 // Callback for Set() method, |success| indicates whether or not the |
| 254 // new property value was accepted by the remote object. |
| 255 typedef base::Callback<void(bool success)> SetCallback; |
| 256 |
| 257 // Requests that the remote object for |property| change the property to |
| 258 // its new value. |callback| will be called to indicate the success or |
| 259 // failure of the request, however the new value may not be available |
| 260 // depending on the remote object. This method may be overridden by |
| 261 // sub-classes if interfaces use different method calls. |
| 262 virtual void Set(PropertyBase* property, SetCallback callback); |
| 263 virtual void OnSet(PropertyBase* property, SetCallback callback, |
| 264 Response* response); |
| 265 |
| 233 // Update properties by reading an array of dictionary entries, each | 266 // Update properties by reading an array of dictionary entries, each |
| 234 // containing a string with the name and a variant with the value, from | 267 // containing a string with the name and a variant with the value, from |
| 235 // |message_reader|. Returns false if message is in incorrect format. | 268 // |message_reader|. Returns false if message is in incorrect format. |
| 236 bool UpdatePropertiesFromReader(MessageReader* reader); | 269 bool UpdatePropertiesFromReader(MessageReader* reader); |
| 237 | 270 |
| 238 // Updates a single property by reading a string with the name and a | 271 // Updates a single property by reading a string with the name and a |
| 239 // variant with the value from |message_reader|. Returns false if message | 272 // variant with the value from |message_reader|. Returns false if message |
| 240 // is in incorrect format, or property type doesn't match. | 273 // is in incorrect format, or property type doesn't match. |
| 241 bool UpdatePropertyFromReader(MessageReader* reader); | 274 bool UpdatePropertyFromReader(MessageReader* reader); |
| 242 | 275 |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 286 base::WeakPtrFactory<PropertySet> weak_ptr_factory_; | 319 base::WeakPtrFactory<PropertySet> weak_ptr_factory_; |
| 287 | 320 |
| 288 DISALLOW_COPY_AND_ASSIGN(PropertySet); | 321 DISALLOW_COPY_AND_ASSIGN(PropertySet); |
| 289 }; | 322 }; |
| 290 | 323 |
| 291 // Property template, this defines the type-specific and type-safe methods | 324 // Property template, this defines the type-specific and type-safe methods |
| 292 // of properties that can be accessed as members of a PropertySet structure. | 325 // of properties that can be accessed as members of a PropertySet structure. |
| 293 // | 326 // |
| 294 // Properties provide a cached value that has an initial sensible default | 327 // Properties provide a cached value that has an initial sensible default |
| 295 // until the reply to PropertySet::GetAll() is retrieved and is updated by | 328 // until the reply to PropertySet::GetAll() is retrieved and is updated by |
| 296 // all calls to that method, Property<>::Get() and property changed signals | 329 // all calls to that method, PropertySet::Get() and property changed signals |
| 297 // handled by PropertySet. It can be obtained by calling value() on the | 330 // also handled by PropertySet. It can be obtained by calling value() on the |
| 298 // property. | 331 // property. |
| 299 // | 332 // |
| 300 // It is recommended that this cached value be used where necessary, with | 333 // It is recommended that this cached value be used where necessary, with |
| 301 // code using PropertySet::PropertyChangedCallback to be notified of changes, | 334 // code using PropertySet::PropertyChangedCallback to be notified of changes, |
| 302 // rather than incurring a round-trip to the remote object for each property | 335 // rather than incurring a round-trip to the remote object for each property |
| 303 // access. | 336 // access. |
| 304 // | 337 // |
| 305 // Where a round-trip is necessary, the Get() method is provided. And to | 338 // Where a round-trip is necessary, the Get() method is provided. And to |
| 306 // update the remote object value, the Set() method is also provided. | 339 // update the remote object value, the Set() method is also provided; these |
| 340 // both simply call methods on PropertySet. |
| 307 // | 341 // |
| 308 // Handling of particular D-Bus types is performed via specialization, | 342 // Handling of particular D-Bus types is performed via specialization, |
| 309 // typically the PopValueFromReader() and AppendToWriter() methods will need | 343 // typically the PopValueFromReader() and AppendSetValueToWriter() methods |
| 310 // to be provided, and in rare cases a constructor to provide a default value. | 344 // will need to be provided, and in rare cases a constructor to provide a |
| 311 // Specializations for basic D-Bus types, strings, object paths and arrays | 345 // default value. Specializations for basic D-Bus types, strings, object |
| 312 // are provided for you. | 346 // paths and arrays are provided for you. |
| 313 template <class T> | 347 template <class T> |
| 314 class Property : public PropertyBase { | 348 class Property : public PropertyBase { |
| 315 public: | 349 public: |
| 316 // Callback for Get() method, |success| indicates whether or not the | 350 Property() {} |
| 317 // value could be retrived, if true the new value can be obtained by | |
| 318 // calling value() on the property. | |
| 319 typedef base::Callback<void(bool success)> GetCallback; | |
| 320 | |
| 321 // Callback for Set() method, |success| indicates whether or not the | |
| 322 // new property value was accepted by the remote object. | |
| 323 typedef base::Callback<void(bool success)> SetCallback; | |
| 324 | |
| 325 Property() : weak_ptr_factory_(this) {} | |
| 326 | 351 |
| 327 // Retrieves the cached value. | 352 // Retrieves the cached value. |
| 328 const T& value() const { return value_; } | 353 const T& value() const { return value_; } |
| 329 | 354 |
| 330 // Requests an updated value from the remote object incurring a | 355 // Requests an updated value from the remote object incurring a |
| 331 // round-trip. |callback| will be called when the new value is available. | 356 // round-trip. |callback| will be called when the new value is available. |
| 332 // This may not be implemented by some interfaces, and may be overriden | 357 // This may not be implemented by some interfaces. |
| 333 // by sub-classes if interfaces use different method calls. | 358 virtual void Get(dbus::PropertySet::GetCallback callback) { |
| 334 virtual void Get(GetCallback callback) { | 359 property_set()->Get(this, callback); |
| 335 MethodCall method_call(kPropertiesInterface, kPropertiesGet); | |
| 336 MessageWriter writer(&method_call); | |
| 337 writer.AppendString(property_set()->interface()); | |
| 338 writer.AppendString(name()); | |
| 339 | |
| 340 ObjectProxy* object_proxy = property_set()->object_proxy(); | |
| 341 DCHECK(object_proxy); | |
| 342 object_proxy->CallMethod(&method_call, | |
| 343 ObjectProxy::TIMEOUT_USE_DEFAULT, | |
| 344 base::Bind(&Property<T>::OnGet, | |
| 345 GetWeakPtr(), | |
| 346 callback)); | |
| 347 } | |
| 348 | |
| 349 // Callback for Get(), may be overriden by sub-classes if interfaces | |
| 350 // use different response arguments. | |
| 351 virtual void OnGet(SetCallback callback, Response* response) { | |
| 352 if (!response) { | |
| 353 LOG(WARNING) << name() << ": Get: failed."; | |
| 354 return; | |
| 355 } | |
| 356 | |
| 357 MessageReader reader(response); | |
| 358 if (PopValueFromReader(&reader)) | |
| 359 property_set()->NotifyPropertyChanged(name()); | |
| 360 | |
| 361 if (!callback.is_null()) | |
| 362 callback.Run(response); | |
| 363 } | 360 } |
| 364 | 361 |
| 365 // Requests that the remote object change the property value to |value|, | 362 // Requests that the remote object change the property value to |value|, |
| 366 // |callback| will be called to indicate the success or failure of the | 363 // |callback| will be called to indicate the success or failure of the |
| 367 // request, however the new value may not be available depending on the | 364 // request, however the new value may not be available depending on the |
| 368 // remote object. This method may be overridden by sub-classes if | 365 // remote object. |
| 369 // interfaces use different method calls. | 366 virtual void Set(const T& value, dbus::PropertySet::SetCallback callback) { |
| 370 virtual void Set(const T& value, SetCallback callback) { | 367 set_value_ = value; |
| 371 MethodCall method_call(kPropertiesInterface, kPropertiesSet); | 368 property_set()->Set(this, callback); |
| 372 MessageWriter writer(&method_call); | |
| 373 writer.AppendString(property_set()->interface()); | |
| 374 writer.AppendString(name()); | |
| 375 AppendToWriter(&writer, value); | |
| 376 | |
| 377 ObjectProxy* object_proxy = property_set()->object_proxy(); | |
| 378 DCHECK(object_proxy); | |
| 379 object_proxy->CallMethod(&method_call, | |
| 380 ObjectProxy::TIMEOUT_USE_DEFAULT, | |
| 381 base::Bind(&Property<T>::OnSet, | |
| 382 GetWeakPtr(), | |
| 383 callback)); | |
| 384 } | 369 } |
| 385 | 370 |
| 386 // Callback for Set(), may be overriden by sub-classes if interfaces | 371 // Method used by PropertySet to retrieve the value from a MessageReader, |
| 387 // use different response arguments. | 372 // no knowledge of the contained type is required, this method returns |
| 388 virtual void OnSet(SetCallback callback, Response* response) { | 373 // true if its expected type was found, false if not. |
| 389 LOG_IF(WARNING, !response) << name() << ": Set: failed."; | 374 virtual bool PopValueFromReader(MessageReader*); |
| 390 if (!callback.is_null()) | |
| 391 callback.Run(response); | |
| 392 } | |
| 393 | 375 |
| 394 // Updates the cached property value, replacing any previous value | 376 // Method used by PropertySet to append the set value to a MessageWriter, |
| 395 // entirely, by popping from |reader| which should be positioned at the | 377 // no knowledge of the contained type is required. |
| 396 // property value, generally of variant type. | |
| 397 // Implementation provided by specialization. | 378 // Implementation provided by specialization. |
| 398 virtual bool PopValueFromReader(MessageReader* reader); | 379 virtual void AppendSetValueToWriter(MessageWriter* writer); |
| 399 | |
| 400 // Appends the passed |value| to |writer|, generally as a variant type. | |
| 401 // Implementation provided by specialization. | |
| 402 virtual void AppendToWriter(MessageWriter* writer, const T& value); | |
| 403 | |
| 404 protected: | |
| 405 // Get a weak pointer to this propertyt, provided so that sub-classes | |
| 406 // overriding methods that make D-Bus calls may use the existing (or | |
| 407 // override) callbacks without providing their own weak pointer factory. | |
| 408 base::WeakPtr<Property<T> > GetWeakPtr() { | |
| 409 return weak_ptr_factory_.GetWeakPtr(); | |
| 410 } | |
| 411 | 380 |
| 412 private: | 381 private: |
| 413 // Current cached value of the property. | 382 // Current cached value of the property. |
| 414 T value_; | 383 T value_; |
| 415 | 384 |
| 416 // Weak pointer factory as D-Bus callbacks may last longer than these | 385 // Replacement value of the property. |
| 417 // objects. | 386 T set_value_; |
| 418 base::WeakPtrFactory<Property<T> > weak_ptr_factory_; | |
| 419 }; | 387 }; |
| 420 | 388 |
| 421 } // namespace dbus | 389 } // namespace dbus |
| 422 | 390 |
| 423 #endif // DBUS_PROPERTY_H_ | 391 #endif // DBUS_PROPERTY_H_ |
| OLD | NEW |