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. If unsuccessful, NULL will be passed to |
| 67 // the callback. |
| 68 // |
| 69 // Must be called in the origin thread. |
| 70 virtual void CallMethod(MethodCall* method_call, |
| 71 int timeout_ms, |
| 72 ResponseCallback callback); |
| 73 |
| 74 private: |
| 75 friend class base::RefCountedThreadSafe<ObjectProxy>; |
| 76 virtual ~ObjectProxy(); |
| 77 |
| 78 // Struct of data we'll be passing from StartAsyncMethodCall() to |
| 79 // OnPendingCallIsCompleteThunk(). |
| 80 struct OnPendingCallIsCompleteData { |
| 81 OnPendingCallIsCompleteData(ObjectProxy* in_object_proxy, |
| 82 ResponseCallback in_response_callback); |
| 83 ~OnPendingCallIsCompleteData(); |
| 84 |
| 85 ObjectProxy* object_proxy; |
| 86 ResponseCallback response_callback; |
| 87 }; |
| 88 |
| 89 // Starts the async method call. This is a helper function to implement |
| 90 // CallMethod(). |
| 91 void StartAsyncMethodCall(int timeout_ms, |
| 92 void* request_message, |
| 93 ResponseCallback response_callback); |
| 94 |
| 95 // Called when the pending call is complete. |
| 96 void OnPendingCallIsComplete(DBusPendingCall* pending_call, |
| 97 ResponseCallback response_callback); |
| 98 |
| 99 // Runs the response callback with the given response object. |
| 100 void RunResponseCallback(ResponseCallback response_callback, |
| 101 Response* response); |
| 102 |
| 103 // Redirects the function call to OnPendingCallIsComplete(). |
| 104 static void OnPendingCallIsCompleteThunk(DBusPendingCall* pending_call, |
| 105 void* user_data); |
| 106 |
| 107 Bus* bus_; |
| 108 std::string service_name_; |
| 109 std::string object_path_; |
| 110 |
| 111 DISALLOW_COPY_AND_ASSIGN(ObjectProxy); |
| 112 }; |
| 113 |
| 114 } // namespace dbus |
| 115 |
| 116 #endif // DBUS_OBJECT_PROXY_H_ |
OLD | NEW |