Christopher Wiley | cec927c | 2014-04-15 16:26:47 -0700 | [diff] [blame] | 1 | // Copyright 2014 The Chromium OS 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 | |
Alex Vakulenko | 3379706 | 2014-05-12 15:55:25 -0700 | [diff] [blame] | 5 | #ifndef BUFFET_EXPORTED_OBJECT_MANAGER_H_ |
| 6 | #define BUFFET_EXPORTED_OBJECT_MANAGER_H_ |
Christopher Wiley | cec927c | 2014-04-15 16:26:47 -0700 | [diff] [blame] | 7 | |
| 8 | #include <map> |
| 9 | #include <string> |
| 10 | |
| 11 | #include <base/memory/weak_ptr.h> |
| 12 | #include <dbus/bus.h> |
| 13 | #include <dbus/exported_object.h> |
| 14 | #include <dbus/object_path.h> |
| 15 | |
| 16 | #include "buffet/exported_property_set.h" |
| 17 | |
| 18 | namespace buffet { |
| 19 | |
| 20 | namespace dbus_utils { |
| 21 | |
| 22 | // ExportedObjectManager is a delegate that implements the |
| 23 | // org.freedesktop.DBus.ObjectManager interface on behalf of another |
| 24 | // object. It handles sending signals when new interfaces are added. |
| 25 | // |
| 26 | // This class is very similar to the ExportedPropertySet class, except that |
| 27 | // it allows objects to expose an object manager interface rather than the |
| 28 | // properties interface. |
| 29 | // |
| 30 | // Example usage: |
| 31 | // |
| 32 | // class ExampleObjectManager { |
| 33 | // public: |
| 34 | // ExampleObjectManager(dbus::Bus* bus) |
| 35 | // : object_manager_(bus, "/my/objects/path") { } |
| 36 | // |
| 37 | // void Init(const OnInitFinish& cb) { object_manager_.Init(cb); } |
| 38 | // void ClaimInterface(const dbus::ObjectPath& path, |
| 39 | // const std::string& interface_name, |
| 40 | // const PropertyWriter& writer) { |
| 41 | // object_manager_->ClaimInterface(...); |
| 42 | // } |
| 43 | // void ReleaseInterface(const dbus::ObjectPath& path, |
| 44 | // const std::string& interface_name) { |
| 45 | // object_manager_->ReleaseInterface(...); |
| 46 | // } |
| 47 | // |
| 48 | // private: |
| 49 | // ExportedObjectManager object_manager_; |
| 50 | // }; |
| 51 | // |
| 52 | // class MyObjectClaimingAnInterface { |
| 53 | // public: |
| 54 | // MyObjectClaimingAnInterface(ExampleObjectManager* object_manager) |
| 55 | // : object_manager_(object_manager) {} |
| 56 | // |
| 57 | // void OnInitFinish(bool success) { |
| 58 | // if (!success) { /* handle that */ } |
| 59 | // object_manager_->ClaimInterface( |
| 60 | // my_path_, my_interface_, my_properties_.GetWriter()); |
| 61 | // } |
| 62 | // |
| 63 | // private: |
| 64 | // struct Properties : public ExportedPropertySet { |
| 65 | // public: |
| 66 | // /* Lots of interesting properties. */ |
| 67 | // }; |
| 68 | // |
| 69 | // Properties my_properties_; |
| 70 | // ExampleObjectManager* object_manager_; |
| 71 | // }; |
Christopher Wiley | adb901d | 2014-05-07 09:58:45 -0700 | [diff] [blame] | 72 | class ExportedObjectManager |
| 73 | : public base::SupportsWeakPtr<ExportedObjectManager> { |
Christopher Wiley | cec927c | 2014-04-15 16:26:47 -0700 | [diff] [blame] | 74 | public: |
| 75 | // Writes a dictionary of property name to property value variants to writer. |
| 76 | typedef base::Callback<void(dbus::MessageWriter* writer)> PropertyWriter; |
| 77 | typedef base::Callback<void(bool success)> OnInitFinish; |
| 78 | typedef std::map<std::string, PropertyWriter> InterfaceProperties; |
| 79 | |
Christopher Wiley | adb901d | 2014-05-07 09:58:45 -0700 | [diff] [blame] | 80 | ExportedObjectManager(scoped_refptr<dbus::Bus> bus, |
| 81 | const dbus::ObjectPath& path); |
Christopher Wiley | cec927c | 2014-04-15 16:26:47 -0700 | [diff] [blame] | 82 | |
| 83 | // Registers methods implementing the ObjectManager interface on the object |
| 84 | // exported on the path given in the constructor. Must be called on the |
| 85 | // origin thread. |
| 86 | void Init(const OnInitFinish& cb); |
| 87 | |
| 88 | // Trigger a signal that |path| has added an interface |interface_name| |
| 89 | // with properties as given by |writer|. |
| 90 | void ClaimInterface(const dbus::ObjectPath& path, |
| 91 | const std::string& interface_name, |
| 92 | const PropertyWriter& writer); |
| 93 | |
| 94 | // Trigger a signal that |path| has removed an interface |interface_name|. |
| 95 | void ReleaseInterface(const dbus::ObjectPath& path, |
| 96 | const std::string& interface_name); |
| 97 | |
| 98 | private: |
| 99 | void HandleGetManagedObjects( |
| 100 | dbus::MethodCall* method_call, |
| 101 | dbus::ExportedObject::ResponseSender response_sender) const; |
| 102 | |
| 103 | // Both |bus_| and |exported_object_| outlive *this. |
| 104 | dbus::Bus* const bus_; |
| 105 | dbus::ExportedObject* const exported_object_; |
| 106 | // Tracks all objects currently known to the ExportedObjectManager. |
| 107 | std::map<dbus::ObjectPath, InterfaceProperties> registered_objects_; |
| 108 | |
Christopher Wiley | cec927c | 2014-04-15 16:26:47 -0700 | [diff] [blame] | 109 | friend class ExportedObjectManagerTest; |
| 110 | DISALLOW_COPY_AND_ASSIGN(ExportedObjectManager); |
| 111 | }; |
| 112 | |
| 113 | } // namespace dbus_utils |
| 114 | |
| 115 | } // namespace buffet |
| 116 | |
Alex Vakulenko | 3379706 | 2014-05-12 15:55:25 -0700 | [diff] [blame] | 117 | #endif // BUFFET_EXPORTED_OBJECT_MANAGER_H_ |