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_EXPORTED_OBJECT_H_ | 5 #ifndef DBUS_EXPORTED_OBJECT_H_ |
6 #define DBUS_EXPORTED_OBJECT_H_ | 6 #define DBUS_EXPORTED_OBJECT_H_ |
7 | 7 |
8 #include <dbus/dbus.h> | 8 #include <dbus/dbus.h> |
9 | 9 |
10 #include <map> | 10 #include <map> |
| 11 #include <memory> |
11 #include <string> | 12 #include <string> |
12 #include <utility> | 13 #include <utility> |
13 | 14 |
14 #include "base/callback.h" | 15 #include "base/callback.h" |
15 #include "base/memory/ref_counted.h" | 16 #include "base/memory/ref_counted.h" |
16 #include "base/memory/scoped_ptr.h" | |
17 #include "base/synchronization/waitable_event.h" | 17 #include "base/synchronization/waitable_event.h" |
18 #include "base/threading/platform_thread.h" | 18 #include "base/threading/platform_thread.h" |
19 #include "base/time/time.h" | 19 #include "base/time/time.h" |
20 #include "dbus/dbus_export.h" | 20 #include "dbus/dbus_export.h" |
21 #include "dbus/object_path.h" | 21 #include "dbus/object_path.h" |
22 | 22 |
23 namespace dbus { | 23 namespace dbus { |
24 | 24 |
25 class Bus; | 25 class Bus; |
26 class MethodCall; | 26 class MethodCall; |
27 class Response; | 27 class Response; |
28 class Signal; | 28 class Signal; |
29 | 29 |
30 // ExportedObject is used to export objects and methods to other D-Bus | 30 // ExportedObject is used to export objects and methods to other D-Bus |
31 // clients. | 31 // clients. |
32 // | 32 // |
33 // ExportedObject is a ref counted object, to ensure that |this| of the | 33 // ExportedObject is a ref counted object, to ensure that |this| of the |
34 // object is alive when callbacks referencing |this| are called. | 34 // object is alive when callbacks referencing |this| are called. |
35 class CHROME_DBUS_EXPORT ExportedObject | 35 class CHROME_DBUS_EXPORT ExportedObject |
36 : public base::RefCountedThreadSafe<ExportedObject> { | 36 : public base::RefCountedThreadSafe<ExportedObject> { |
37 public: | 37 public: |
38 // Client code should use Bus::GetExportedObject() instead of this | 38 // Client code should use Bus::GetExportedObject() instead of this |
39 // constructor. | 39 // constructor. |
40 ExportedObject(Bus* bus, const ObjectPath& object_path); | 40 ExportedObject(Bus* bus, const ObjectPath& object_path); |
41 | 41 |
42 // Called to send a response from an exported method. |response| is the | 42 // Called to send a response from an exported method. |response| is the |
43 // response message. Callers should pass NULL in the event of an error that | 43 // response message. Callers should pass NULL in the event of an error that |
44 // prevents the sending of a response. | 44 // prevents the sending of a response. |
45 typedef base::Callback<void (scoped_ptr<Response> response)> ResponseSender; | 45 typedef base::Callback<void(std::unique_ptr<Response> response)> |
| 46 ResponseSender; |
46 | 47 |
47 // Called when an exported method is called. |method_call| is the request | 48 // Called when an exported method is called. |method_call| is the request |
48 // message. |sender| is the callback that's used to send a response. | 49 // message. |sender| is the callback that's used to send a response. |
49 // | 50 // |
50 // |method_call| is owned by ExportedObject, hence client code should not | 51 // |method_call| is owned by ExportedObject, hence client code should not |
51 // delete |method_call|. | 52 // delete |method_call|. |
52 typedef base::Callback<void (MethodCall* method_call, ResponseSender sender)> | 53 typedef base::Callback<void (MethodCall* method_call, ResponseSender sender)> |
53 MethodCallCallback; | 54 MethodCallCallback; |
54 | 55 |
55 // Called when method exporting is done. | 56 // Called when method exporting is done. |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
132 // BLOCKING CALL. | 133 // BLOCKING CALL. |
133 bool Register(); | 134 bool Register(); |
134 | 135 |
135 // Handles the incoming request messages and dispatches to the exported | 136 // Handles the incoming request messages and dispatches to the exported |
136 // methods. | 137 // methods. |
137 DBusHandlerResult HandleMessage(DBusConnection* connection, | 138 DBusHandlerResult HandleMessage(DBusConnection* connection, |
138 DBusMessage* raw_message); | 139 DBusMessage* raw_message); |
139 | 140 |
140 // Runs the method. Helper function for HandleMessage(). | 141 // Runs the method. Helper function for HandleMessage(). |
141 void RunMethod(MethodCallCallback method_call_callback, | 142 void RunMethod(MethodCallCallback method_call_callback, |
142 scoped_ptr<MethodCall> method_call, | 143 std::unique_ptr<MethodCall> method_call, |
143 base::TimeTicks start_time); | 144 base::TimeTicks start_time); |
144 | 145 |
145 // Callback invoked by service provider to send a response to a method call. | 146 // Callback invoked by service provider to send a response to a method call. |
146 // Can be called immediately from a MethodCallCallback to implement a | 147 // Can be called immediately from a MethodCallCallback to implement a |
147 // synchronous service or called later to implement an asynchronous service. | 148 // synchronous service or called later to implement an asynchronous service. |
148 void SendResponse(base::TimeTicks start_time, | 149 void SendResponse(base::TimeTicks start_time, |
149 scoped_ptr<MethodCall> method_call, | 150 std::unique_ptr<MethodCall> method_call, |
150 scoped_ptr<Response> response); | 151 std::unique_ptr<Response> response); |
151 | 152 |
152 // Called on completion of the method run from SendResponse(). | 153 // Called on completion of the method run from SendResponse(). |
153 // Takes ownership of |method_call| and |response|. | 154 // Takes ownership of |method_call| and |response|. |
154 void OnMethodCompleted(scoped_ptr<MethodCall> method_call, | 155 void OnMethodCompleted(std::unique_ptr<MethodCall> method_call, |
155 scoped_ptr<Response> response, | 156 std::unique_ptr<Response> response, |
156 base::TimeTicks start_time); | 157 base::TimeTicks start_time); |
157 | 158 |
158 // Called when the object is unregistered. | 159 // Called when the object is unregistered. |
159 void OnUnregistered(DBusConnection* connection); | 160 void OnUnregistered(DBusConnection* connection); |
160 | 161 |
161 // Redirects the function call to HandleMessage(). | 162 // Redirects the function call to HandleMessage(). |
162 static DBusHandlerResult HandleMessageThunk(DBusConnection* connection, | 163 static DBusHandlerResult HandleMessageThunk(DBusConnection* connection, |
163 DBusMessage* raw_message, | 164 DBusMessage* raw_message, |
164 void* user_data); | 165 void* user_data); |
165 | 166 |
166 // Redirects the function call to OnUnregistered(). | 167 // Redirects the function call to OnUnregistered(). |
167 static void OnUnregisteredThunk(DBusConnection* connection, | 168 static void OnUnregisteredThunk(DBusConnection* connection, |
168 void* user_data); | 169 void* user_data); |
169 | 170 |
170 scoped_refptr<Bus> bus_; | 171 scoped_refptr<Bus> bus_; |
171 ObjectPath object_path_; | 172 ObjectPath object_path_; |
172 bool object_is_registered_; | 173 bool object_is_registered_; |
173 | 174 |
174 // The method table where keys are absolute method names (i.e. interface | 175 // The method table where keys are absolute method names (i.e. interface |
175 // name + method name), and values are the corresponding callbacks. | 176 // name + method name), and values are the corresponding callbacks. |
176 typedef std::map<std::string, MethodCallCallback> MethodTable; | 177 typedef std::map<std::string, MethodCallCallback> MethodTable; |
177 MethodTable method_table_; | 178 MethodTable method_table_; |
178 }; | 179 }; |
179 | 180 |
180 } // namespace dbus | 181 } // namespace dbus |
181 | 182 |
182 #endif // DBUS_EXPORTED_OBJECT_H_ | 183 #endif // DBUS_EXPORTED_OBJECT_H_ |
OLD | NEW |