satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 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" |
satorux@chromium.org | 01fbb89 | 2011-08-20 10:07:17 +0900 | [diff] [blame] | 17 | #include "base/synchronization/waitable_event.h" |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 18 | #include "base/threading/platform_thread.h" |
satorux@chromium.org | 5a92cf3 | 2011-09-07 05:53:30 +0900 | [diff] [blame] | 19 | #include "base/time.h" |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 20 | |
| 21 | class MessageLoop; |
| 22 | |
| 23 | namespace dbus { |
| 24 | |
| 25 | class Bus; |
| 26 | class MethodCall; |
| 27 | class Response; |
satorux@chromium.org | 7f0c451 | 2011-08-23 16:29:21 +0900 | [diff] [blame] | 28 | class Signal; |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 29 | |
| 30 | // ExportedObject is used to export objects and methods to other D-Bus |
| 31 | // clients. |
| 32 | // |
| 33 | // ExportedObject is a ref counted object, to ensure that |this| of the |
| 34 | // object is alive when callbacks referencing |this| are called. |
| 35 | class ExportedObject : public base::RefCountedThreadSafe<ExportedObject> { |
| 36 | public: |
| 37 | // Client code should use Bus::GetExportedObject() instead of this |
| 38 | // constructor. |
| 39 | ExportedObject(Bus* bus, |
| 40 | const std::string& service_name, |
| 41 | const std::string& object_path); |
| 42 | |
| 43 | // Called when an exported method is called. MethodCall* is the request |
| 44 | // message. |
| 45 | typedef base::Callback<Response* (MethodCall*)> MethodCallCallback; |
| 46 | |
| 47 | // Called when method exporting is done. |
| 48 | // Parameters: |
| 49 | // - the interface name. |
| 50 | // - the method name. |
| 51 | // - whether exporting was successful or not. |
| 52 | typedef base::Callback<void (const std::string&, const std::string&, bool)> |
| 53 | OnExportedCallback; |
| 54 | |
| 55 | // Exports the method specified by |interface_name| and |method_name|, |
| 56 | // and blocks until exporting is done. Returns true on success. |
| 57 | // |
| 58 | // |method_call_callback| will be called in the origin thread, when the |
| 59 | // exported method is called. As it's called in the origin thread, |
satorux@chromium.org | 7f0c451 | 2011-08-23 16:29:21 +0900 | [diff] [blame] | 60 | // |method_callback| can safely reference objects in the origin thread |
| 61 | // (i.e. UI thread in most cases). |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 62 | // |
| 63 | // BLOCKING CALL. |
| 64 | virtual bool ExportMethodAndBlock(const std::string& interface_name, |
| 65 | const std::string& method_name, |
| 66 | MethodCallCallback method_call_callback); |
| 67 | |
| 68 | // Requests to export the method specified by |interface_name| and |
| 69 | // |method_name|. See Also ExportMethodAndBlock(). |
| 70 | // |
| 71 | // |on_exported_callback| is called when the method is exported or |
| 72 | // failed to be exported, in the origin thread. |
| 73 | // |
| 74 | // Must be called in the origin thread. |
| 75 | virtual void ExportMethod(const std::string& interface_name, |
| 76 | const std::string& method_name, |
| 77 | MethodCallCallback method_call_callback, |
| 78 | OnExportedCallback on_exported_callback); |
| 79 | |
satorux@chromium.org | 7f0c451 | 2011-08-23 16:29:21 +0900 | [diff] [blame] | 80 | // Requests to send the signal from this object. The signal will be sent |
| 81 | // asynchronously from the message loop in the D-Bus thread. |
| 82 | virtual void SendSignal(Signal* signal); |
| 83 | |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 84 | // Unregisters the object from the bus. The Bus object will take care of |
| 85 | // unregistering so you don't have to do this manually. |
| 86 | // |
| 87 | // BLOCKING CALL. |
| 88 | virtual void Unregister(); |
| 89 | |
satorux@chromium.org | f77861f | 2011-08-25 14:18:29 +0900 | [diff] [blame] | 90 | protected: |
| 91 | // This is protected, so we can define sub classes. |
| 92 | virtual ~ExportedObject(); |
| 93 | |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 94 | private: |
| 95 | friend class base::RefCountedThreadSafe<ExportedObject>; |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 96 | |
| 97 | // Helper function for ExportMethod(). |
| 98 | void ExportMethodInternal(const std::string& interface_name, |
| 99 | const std::string& method_name, |
| 100 | MethodCallCallback method_call_callback, |
| 101 | OnExportedCallback exported_callback); |
| 102 | |
| 103 | // Called when the object is exported. |
| 104 | void OnExported(OnExportedCallback on_exported_callback, |
| 105 | const std::string& interface_name, |
| 106 | const std::string& method_name, |
| 107 | bool success); |
| 108 | |
satorux@chromium.org | 7f0c451 | 2011-08-23 16:29:21 +0900 | [diff] [blame] | 109 | // Helper function for SendSignal(). |
satorux@chromium.org | 5a92cf3 | 2011-09-07 05:53:30 +0900 | [diff] [blame] | 110 | void SendSignalInternal(base::TimeTicks start_time, |
satorux@chromium.org | 47d706b | 2011-10-04 22:47:21 +0900 | [diff] [blame] | 111 | DBusMessage* signal_message); |
satorux@chromium.org | 7f0c451 | 2011-08-23 16:29:21 +0900 | [diff] [blame] | 112 | |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 113 | // Registers this object to the bus. |
| 114 | // Returns true on success, or the object is already registered. |
| 115 | // |
| 116 | // BLOCKING CALL. |
| 117 | bool Register(); |
| 118 | |
| 119 | // Handles the incoming request messages and dispatches to the exported |
| 120 | // methods. |
| 121 | DBusHandlerResult HandleMessage(DBusConnection* connection, |
| 122 | DBusMessage* raw_message); |
| 123 | |
| 124 | // Runs the method. Helper function for HandleMessage(). |
| 125 | void RunMethod(MethodCallCallback method_call_callback, |
satorux@chromium.org | c9ebea2 | 2011-10-08 01:26:30 +0900 | [diff] [blame] | 126 | MethodCall* method_call, |
| 127 | base::TimeTicks start_time); |
| 128 | |
| 129 | // Called on completion of the method run from RunMethod(). |
| 130 | // Takes ownership of |method_call| and |response|. |
| 131 | void OnMethodCompleted(MethodCall* method_call, |
| 132 | Response* response, |
| 133 | base::TimeTicks start_time); |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 134 | |
| 135 | // Called when the object is unregistered. |
| 136 | void OnUnregistered(DBusConnection* connection); |
| 137 | |
| 138 | // Redirects the function call to HandleMessage(). |
| 139 | static DBusHandlerResult HandleMessageThunk(DBusConnection* connection, |
| 140 | DBusMessage* raw_message, |
| 141 | void* user_data); |
| 142 | |
| 143 | // Redirects the function call to OnUnregistered(). |
| 144 | static void OnUnregisteredThunk(DBusConnection* connection, |
| 145 | void* user_data); |
| 146 | |
satorux@chromium.org | f06eb89 | 2011-10-13 09:45:26 +0900 | [diff] [blame^] | 147 | scoped_refptr<Bus> bus_; |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 148 | std::string service_name_; |
| 149 | std::string object_path_; |
| 150 | bool object_is_registered_; |
satorux@chromium.org | 163f1cb | 2011-08-18 05:58:12 +0900 | [diff] [blame] | 151 | |
| 152 | // The method table where keys are absolute method names (i.e. interface |
| 153 | // name + method name), and values are the corresponding callbacks. |
| 154 | typedef std::map<std::string, MethodCallCallback> MethodTable; |
| 155 | MethodTable method_table_; |
| 156 | }; |
| 157 | |
| 158 | } // namespace dbus |
| 159 | |
| 160 | #endif // DBUS_EXPORTED_OBJECT_H_ |