OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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 #ifndef DBUS_EXPORTED_OBJECT_H_ |
| 6 #define DBUS_EXPORTED_OBJECT_H_ |
| 7 #pragma once |
| 8 |
| 9 #include <string> |
| 10 #include <map> |
| 11 #include <utility> |
| 12 |
| 13 #include <dbus/dbus.h> |
| 14 |
| 15 #include "base/callback.h" |
| 16 #include "base/memory/ref_counted.h" |
| 17 #include "base/synchronization/condition_variable.h" |
| 18 #include "base/threading/platform_thread.h" |
| 19 |
| 20 class MessageLoop; |
| 21 |
| 22 namespace dbus { |
| 23 |
| 24 class Bus; |
| 25 class MethodCall; |
| 26 class Response; |
| 27 |
| 28 // ExportedObject is used to export objects and methods to other D-Bus |
| 29 // clients. |
| 30 // |
| 31 // ExportedObject is a ref counted object, to ensure that |this| of the |
| 32 // object is alive when callbacks referencing |this| are called. |
| 33 class ExportedObject : public base::RefCountedThreadSafe<ExportedObject> { |
| 34 public: |
| 35 // Client code should use Bus::GetExportedObject() instead of this |
| 36 // constructor. |
| 37 ExportedObject(Bus* bus, |
| 38 const std::string& service_name, |
| 39 const std::string& object_path); |
| 40 |
| 41 // Called when an exported method is called. MethodCall* is the request |
| 42 // message. |
| 43 typedef base::Callback<Response* (MethodCall*)> MethodCallCallback; |
| 44 |
| 45 // Called when method exporting is done. |
| 46 // Parameters: |
| 47 // - the interface name. |
| 48 // - the method name. |
| 49 // - whether exporting was successful or not. |
| 50 typedef base::Callback<void (const std::string&, const std::string&, bool)> |
| 51 OnExportedCallback; |
| 52 |
| 53 // Exports the method specified by |interface_name| and |method_name|, |
| 54 // and blocks until exporting is done. Returns true on success. |
| 55 // |
| 56 // |method_call_callback| will be called in the origin thread, when the |
| 57 // exported method is called. As it's called in the origin thread, |
| 58 // callback| can safely reference objects in the origin thread (i.e. UI |
| 59 // thread in most cases). |
| 60 // |
| 61 // BLOCKING CALL. |
| 62 virtual bool ExportMethodAndBlock(const std::string& interface_name, |
| 63 const std::string& method_name, |
| 64 MethodCallCallback method_call_callback); |
| 65 |
| 66 // Requests to export the method specified by |interface_name| and |
| 67 // |method_name|. See Also ExportMethodAndBlock(). |
| 68 // |
| 69 // |on_exported_callback| is called when the method is exported or |
| 70 // failed to be exported, in the origin thread. |
| 71 // |
| 72 // Must be called in the origin thread. |
| 73 virtual void ExportMethod(const std::string& interface_name, |
| 74 const std::string& method_name, |
| 75 MethodCallCallback method_call_callback, |
| 76 OnExportedCallback on_exported_callback); |
| 77 |
| 78 // Unregisters the object from the bus. The Bus object will take care of |
| 79 // unregistering so you don't have to do this manually. |
| 80 // |
| 81 // BLOCKING CALL. |
| 82 virtual void Unregister(); |
| 83 |
| 84 private: |
| 85 friend class base::RefCountedThreadSafe<ExportedObject>; |
| 86 virtual ~ExportedObject(); |
| 87 |
| 88 // Helper function for ExportMethod(). |
| 89 void ExportMethodInternal(const std::string& interface_name, |
| 90 const std::string& method_name, |
| 91 MethodCallCallback method_call_callback, |
| 92 OnExportedCallback exported_callback); |
| 93 |
| 94 // Called when the object is exported. |
| 95 void OnExported(OnExportedCallback on_exported_callback, |
| 96 const std::string& interface_name, |
| 97 const std::string& method_name, |
| 98 bool success); |
| 99 |
| 100 // Registers this object to the bus. |
| 101 // Returns true on success, or the object is already registered. |
| 102 // |
| 103 // BLOCKING CALL. |
| 104 bool Register(); |
| 105 |
| 106 // Handles the incoming request messages and dispatches to the exported |
| 107 // methods. |
| 108 DBusHandlerResult HandleMessage(DBusConnection* connection, |
| 109 DBusMessage* raw_message); |
| 110 |
| 111 // Runs the method. Helper function for HandleMessage(). |
| 112 void RunMethod(MethodCallCallback method_call_callback, |
| 113 MethodCall* method_call); |
| 114 |
| 115 // Called when the object is unregistered. |
| 116 void OnUnregistered(DBusConnection* connection); |
| 117 |
| 118 // Redirects the function call to HandleMessage(). |
| 119 static DBusHandlerResult HandleMessageThunk(DBusConnection* connection, |
| 120 DBusMessage* raw_message, |
| 121 void* user_data); |
| 122 |
| 123 // Redirects the function call to OnUnregistered(). |
| 124 static void OnUnregisteredThunk(DBusConnection* connection, |
| 125 void* user_data); |
| 126 |
| 127 Bus* bus_; |
| 128 std::string service_name_; |
| 129 std::string object_path_; |
| 130 bool object_is_registered_; |
| 131 bool method_is_called_; |
| 132 dbus::Response* response_from_method_; |
| 133 base::Lock method_is_called_lock_; |
| 134 base::ConditionVariable on_method_is_called_; |
| 135 |
| 136 // The method table where keys are absolute method names (i.e. interface |
| 137 // name + method name), and values are the corresponding callbacks. |
| 138 typedef std::map<std::string, MethodCallCallback> MethodTable; |
| 139 MethodTable method_table_; |
| 140 }; |
| 141 |
| 142 } // namespace dbus |
| 143 |
| 144 #endif // DBUS_EXPORTED_OBJECT_H_ |
OLD | NEW |