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 // D-Bus library for Chrome. |
| 6 |
| 7 #ifndef DBUS_OBJECT_PROXY_H_ |
| 8 #define DBUS_OBJECT_PROXY_H_ |
| 9 #pragma once |
| 10 |
| 11 #include <string> |
| 12 #include <vector> |
| 13 #include <dbus/dbus.h> |
| 14 |
| 15 #include "base/callback.h" |
| 16 #include "base/memory/ref_counted.h" |
| 17 #include "base/threading/platform_thread.h" |
| 18 |
| 19 class MessageLoop; |
| 20 |
| 21 namespace dbus { |
| 22 |
| 23 class Bus; |
| 24 class MethodCall; |
| 25 class Response; |
| 26 |
| 27 // ObjectProxy is used to communicate with remote objects, such as calling |
| 28 // methods and sending signals. |
| 29 // |
| 30 // TODO(satorux): Add support for sending signals. |
| 31 class ObjectProxy { |
| 32 public: |
| 33 virtual ~ObjectProxy(); |
| 34 |
| 35 // The constants correspond to DBUS_TIMEOUT_USE_DEFAULT and |
| 36 // DBUS_TIMEOUT_INFINITE. Here we use literal numbers instead of these |
| 37 // macros as these aren't defined with D-Bus earlier than 1.4.12. |
| 38 // http://dbus.freedesktop.org/doc/api/html/group__DBusPendingCall.html |
| 39 enum { |
| 40 // Note: the default timeout is set to 25 secs per |
| 41 // dbus-1.2.16/dbus/dbus-connection-internal.h |
| 42 // #define _DBUS_DEFAULT_TIMEOUT_VALUE (25 * 1000) |
| 43 TIMEOUT_USE_DEFAULT = -1, |
| 44 TIMEOUT_INFINITE = 0x7fffffff, |
| 45 }; |
| 46 |
| 47 // Used for CallMethodAsync(). |
| 48 typedef base::Callback<void(Response*)> ResponseCallback; |
| 49 |
| 50 // Calls the method of the remote object and waits until the response is |
| 51 // returned. This is a blocking call hence shouldn't be called from the |
| 52 // UI thread. |
| 53 virtual bool CallMethodAndWait(MethodCall* method_call, |
| 54 Response* response, |
| 55 int timeout_ms); |
| 56 |
| 57 // Calls the method of the remote object. |callback| will be called in |
| 58 // the message loop of the thread that calls this function. If the |
| 59 // method call is successful, a valid Response object will be passed to |
| 60 // the callback, and the callback needs to delete the response object. |
| 61 // If the method call is unsuccessful, NULL will be passed to the |
| 62 // callback. |
| 63 // |
| 64 // This is not a blocking call (this function just posts a task to the |
| 65 // message loop in another thread maintained by Bus object), hence it's |
| 66 // safe to be called from the UI thread. |
| 67 // |
| 68 // TODO(satorux): Implement timeout. timeout_ms is currently unused. |
| 69 virtual void CallMethod(MethodCall* method_call, |
| 70 ResponseCallback callback, |
| 71 int timeout_ms); |
| 72 |
| 73 const std::string& service_name() const { return service_name_; } |
| 74 const std::string& object_path() const { return object_path_; } |
| 75 |
| 76 private: |
| 77 // Struct of data we'll be passing from StartAsyncMethodCall() to |
| 78 // OnPendingCallIsCompleteStub(). |
| 79 struct OnPendingCallIsCompleteData { |
| 80 OnPendingCallIsCompleteData(ObjectProxy* in_object_proxy, |
| 81 MessageLoop* in_origin_loop, |
| 82 ResponseCallback in_response_callback); |
| 83 ObjectProxy* object_proxy; |
| 84 MessageLoop* origin_loop; |
| 85 ResponseCallback response_callback; |
| 86 }; |
| 87 |
| 88 // Starts the async method call. |
| 89 void StartAsyncMethodCall(int timeout_ms, |
| 90 void* request_message, |
| 91 MessageLoop* origin_loop, |
| 92 ResponseCallback response_callback); |
| 93 |
| 94 // Called when the pending call is complete. |
| 95 void OnPendingCallIsComplete(DBusPendingCall* pending_call, |
| 96 ResponseCallback response_callback, |
| 97 MessageLoop* origin_loop); |
| 98 |
| 99 // Runs the response callback with the given response object. |
| 100 void RunResponseCallback(ResponseCallback response_callback, |
| 101 Response* response); |
| 102 |
| 103 // Checks whether the current thread is on the same thread that created |
| 104 // the object proxy. If not, DCHECK() will fail. |
| 105 void AssertOnOriginThread(); |
| 106 |
| 107 // Checks whether the current thread is on the IO thread, if the |
| 108 // underlying bus is configured to use it. If not, DCHECK() will fail. |
| 109 // |
| 110 // The function does the same with AssertOnOriginThread() if the bus is |
| 111 // NOT configured to use the IO thread. |
| 112 void AssertOnIoThreadIfConfigured(); |
| 113 |
| 114 // Redirects the function call to OnPendingCallIsComplete(). |
| 115 static void OnPendingCallIsCompleteStub(DBusPendingCall* pending_call, |
| 116 void* user_data); |
| 117 |
| 118 friend class Bus; |
| 119 |
| 120 // The object proxy can only be created by Bus. |
| 121 ObjectProxy(Bus* bus, |
| 122 const std::string& service_name, |
| 123 const std::string& object_path); |
| 124 |
| 125 Bus* bus_; |
| 126 std::string service_name_; |
| 127 std::string object_path_; |
| 128 const base::PlatformThreadId origin_thread_id_; |
| 129 |
| 130 DISALLOW_COPY_AND_ASSIGN(ObjectProxy); |
| 131 }; |
| 132 |
| 133 } // namespace dbus |
| 134 |
| 135 #endif // DBUS_OBJECT_PROXY_H_ |
OLD | NEW |