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_OBJECT_PROXY_H_ | |
6 #define DBUS_OBJECT_PROXY_H_ | |
7 #pragma once | |
8 | |
9 #include <string> | |
10 #include <vector> | |
11 #include <dbus/dbus.h> | |
12 | |
13 #include "base/callback.h" | |
14 #include "base/memory/ref_counted.h" | |
15 | |
16 class MessageLoop; | |
17 | |
18 namespace dbus { | |
19 | |
20 class Bus; | |
21 class MethodCall; | |
22 class Response; | |
23 | |
24 // ObjectProxy is used to communicate with remote objects, mainly for | |
25 // calling methods of these objects. | |
26 // | |
27 // ObjectProxy is a ref counted object, to ensure that |this| of the | |
28 // object is is alive when callbacks referencing |this| are called. | |
29 class ObjectProxy : public base::RefCountedThreadSafe<ObjectProxy> { | |
30 public: | |
31 // Client code should use Bus::GetObjectProxy() instead of this | |
32 // constructor. | |
33 ObjectProxy(Bus* bus, | |
34 const std::string& service_name, | |
35 const std::string& object_path); | |
36 | |
37 // Special timeout constants. | |
38 // | |
39 // The constants correspond to DBUS_TIMEOUT_USE_DEFAULT and | |
40 // DBUS_TIMEOUT_INFINITE. Here we use literal numbers instead of these | |
41 // macros as these aren't defined with D-Bus earlier than 1.4.12. | |
42 enum { | |
43 TIMEOUT_USE_DEFAULT = -1, | |
44 TIMEOUT_INFINITE = 0x7fffffff, | |
45 }; | |
46 | |
47 // Called when the response is returned. Used for CallMethod(). | |
48 typedef base::Callback<void(Response*)> ResponseCallback; | |
49 | |
50 // Calls the method of the remote object and blocks until the response | |
51 // is returned. | |
52 // | |
53 // BLOCKING CALL. | |
54 virtual bool CallMethodAndBlock(MethodCall* method_call, | |
55 int timeout_ms, | |
56 Response* response); | |
57 | |
58 // Requests to call the method of the remote object. | |
59 // | |
60 // |callback| will be called in the origin thread, once the method call | |
61 // is complete. As it's called in the origin thread, |callback| can | |
62 // safely reference objects in the origin thread (i.e. UI thread in most | |
63 // cases). | |
64 // | |
65 // If the method call is successful, a pointer to Response object will | |
66 // be passed to the callback, and the callback needs to delete the | |
67 // response object. If unsuccessful, NULL will be passed to the | |
68 // callback. | |
69 // | |
70 // Must be called in the origin thread. | |
71 virtual void CallMethod(MethodCall* method_call, | |
72 int timeout_ms, | |
73 ResponseCallback callback); | |
74 | |
75 private: | |
76 friend class base::RefCountedThreadSafe<ObjectProxy>; | |
77 virtual ~ObjectProxy(); | |
78 | |
79 // Struct of data we'll be passing from StartAsyncMethodCall() to | |
80 // OnPendingCallIsCompleteThunk(). | |
81 struct OnPendingCallIsCompleteData { | |
82 OnPendingCallIsCompleteData(ObjectProxy* in_object_proxy, | |
83 ResponseCallback in_response_callback); | |
stevenjb
2011/08/10 19:40:07
struct constructors can generally be inlined.
satorux1
2011/08/10 21:40:55
As mentioned before, this may make the clang bot u
stevenjb
2011/08/10 21:57:54
Yep, cool.
| |
84 ObjectProxy* object_proxy; | |
85 ResponseCallback response_callback; | |
86 }; | |
87 | |
88 // Starts the async method call. This is a helper function to implement | |
89 // CallMethod(). | |
90 void StartAsyncMethodCall(int timeout_ms, | |
91 void* request_message, | |
92 ResponseCallback response_callback); | |
93 | |
94 // Called when the pending call is complete. | |
95 void OnPendingCallIsComplete(DBusPendingCall* pending_call, | |
96 ResponseCallback response_callback); | |
97 | |
98 // Runs the response callback with the given response object. | |
99 void RunResponseCallback(ResponseCallback response_callback, | |
100 Response* response); | |
101 | |
102 // Redirects the function call to OnPendingCallIsComplete(). | |
103 static void OnPendingCallIsCompleteThunk(DBusPendingCall* pending_call, | |
104 void* user_data); | |
105 | |
106 Bus* bus_; | |
107 std::string service_name_; | |
108 std::string object_path_; | |
109 | |
110 DISALLOW_COPY_AND_ASSIGN(ObjectProxy); | |
111 }; | |
112 | |
113 } // namespace dbus | |
114 | |
115 #endif // DBUS_OBJECT_PROXY_H_ | |
OLD | NEW |