Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(224)

Side by Side Diff: net/dbus/dbus.h

Issue 7413004: Uploading for backup... (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: backup Created 9 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « content/browser/geolocation/wifi_data_provider_linux.cc ('k') | net/dbus/dbus.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 NET_DBUS_DBUS_H_
8 #define NET_DBUS_DBUS_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/memory/scoped_ptr.h"
18 #include "base/tracked_objects.h"
19
20 class MessageLoop;
21
22 namespace net {
23 namespace dbus {
24
25 class ObjectProxy;
26
27 // Bus is used to establish a connection with D-Bus, and create object
28 // proxies.
29 class Bus {
30 public:
31 // Specifies the bus type. SESSION is used to communicate with per-user
32 // services like GNOME applications. SYSTEM is used to communicate with
33 // system-wide services like NetworkManager.
34 enum BusType {
35 SESSION = DBUS_BUS_SESSION,
36 SYSTEM = DBUS_BUS_SYSTEM,
37 };
38
39 // Specifies the connection type. PRIVATE should usually be used unless
40 // you are sure that SHARED is safe for you. PRIVATE gives you a private
41 // connection, that won't be shared with other Bus objects. SHARED gives
42 // you a connection shared among other Bus objects, which is unsafe if
43 // the connection is shared with multiple threads.
44 enum ConnectionType {
45 PRIVATE,
46 SHARED,
47 };
48
49 // Options used to create a Bus object.
50 struct Options {
51 Options() : bus_type(SESSION), connection_type(PRIVATE) {}
52
53 BusType bus_type;
54 ConnectionType connection_type;
55 // May add other options like timeout.
56 };
57
58 // Creates a Bus object. The actual connection will be established when
59 // Init() is called.
60 Bus(const Options& options);
61 virtual ~Bus();
62
63 // Gets the object proxy for the given service name and the object path.
64 // |service_name| looks like "org.freedesktop.NetworkManager", and
65 // |object_path| looks like "/org/freedesktop/NetworkManager/Devices/0".
66 //
67 // This method never returns NULL. The caller should delete the object.
68 virtual ObjectProxy* GetObjectProxy(const std::string& service_name,
69 const std::string& object_path);
70
71 private:
72 class IOThread;
73
74 friend class ObjectProxy;
75 DBusConnection* connection() { return connection_; }
76
77 // Initializes the bus by establishing a connection with the D-Bus.
78 // Returns true on success.
79 //
80 // This is a bit tricky but we off calling this function until when we
81 // first send a message to the D-Bus, as connecting to D-Bus is
82 // expensive(actually a blocking call), and we don't want to do this
83 // when the Bus object is created (that can be in the UI thread).
84 bool Init();
85
86 // ...
87 bool InitForAsync();
88
89 // ...
90 virtual void StartIOThreadIfNeeded();
91
92 // ...
93 virtual void PostTask(const tracked_objects::Location& from_here,
94 const base::Closure& task);
95
96 static dbus_bool_t OnAddWatch(DBusWatch* watch, void* data);
97 static void OnRemoveWatch(DBusWatch* watch, void* data);
98 static void OnToggleWatch(DBusWatch* watch, void* data);
99
100 const BusType bus_type_;
101 const ConnectionType connection_type_;
102 DBusConnection* connection_;
103 // The thread is used for asynchronous method calls, and shared by the
104 // object proxies created from the bus.
105 scoped_ptr<IOThread> io_thread_;
106
107 DISALLOW_COPY_AND_ASSIGN(Bus);
108 };
109
110 class MethodCall;
111 class Response;
112
113 // ObjectProxy is used to communicate with remote objects, such as calling
114 // methods and sending signals.
115 class ObjectProxy {
116 public:
117 virtual ~ObjectProxy();
118 // Used for CallMethodAsync().
119 typedef base::Callback<void(Response*)> ResponseCallback;
120
121 // Synchronously calls the method of the remote object. This is a
122 // blocking call hence shouldn't be called from the UI thread.
123 virtual bool CallMethodSync(MethodCall* method_call,
124 Response* response);
125
126 // Asynchronously calls the method of the remote object. |callback| will
127 // be called in message loop of the same thread that calls this
128 // function. If the method call is successful, a valid Response object
129 // will be passed to the callback, and the callback needs to delete it.
130 // If the method call is unsuccessful, NULL will be passed to the callback.
131 //
132 // This is not a blocking call (this function just posts a task to the
133 // message loop in another thread), hence it's safe to be called from
134 // the UI thread.
135 virtual void CallMethodAsync(MethodCall* method_call,
136 ResponseCallback callback);
137
138 const std::string& service_name() const { return service_name_; }
139 const std::string& object_path() const { return object_path_; }
140
141 private:
142 // Starts the async method call.
143 void StartAsyncMethodCall(void* request_message,
144 MessageLoop* origin_loop,
145 ResponseCallback response_callback);
146
147 // Runs the response callback with the given response object.
148 static void RunResponseCallback(ResponseCallback response_callback,
149 Response* response);
150
151 // ...
152 struct OnPendingCallIsCompleteData {
153 OnPendingCallIsCompleteData(MessageLoop* in_origin_loop,
154 ResponseCallback in_response_callback);
155 MessageLoop* origin_loop;
156 ResponseCallback response_callback;
157 };
158
159 // ...
160 static void OnPendingCallIsComplete(DBusPendingCall* pending_call,
161 void* user_data);
162
163 friend class Bus;
164
165 // The object proxy can only be created by Bus.
166 ObjectProxy(Bus* bus,
167 const std::string& service_name,
168 const std::string& object_path);
169
170 Bus* bus_;
171 std::string service_name_;
172 std::string object_path_;
173
174 DISALLOW_COPY_AND_ASSIGN(ObjectProxy);
175 };
176
177 class MessageWriter;
178 class MessageReader;
179
180 // Message is the base class of D-Bus message types. Client code should
181 // usually use sub classes such as MethodCall and Response instead.
182 class Message {
183 public:
184 // The data type used in the D-Bus message system.
185 // Redefined here so clients don't need to use raw D-Bus macros.
186 enum Type {
187 BYTE = DBUS_TYPE_BYTE,
188 BOOL = DBUS_TYPE_BOOLEAN,
189 INT16 = DBUS_TYPE_INT16,
190 UINT16 = DBUS_TYPE_UINT16,
191 INT32 = DBUS_TYPE_INT32,
192 UINT32 = DBUS_TYPE_UINT32,
193 INT64 = DBUS_TYPE_INT64,
194 UINT64 = DBUS_TYPE_UINT64,
195 DOUBLE = DBUS_TYPE_DOUBLE,
196 STRING = DBUS_TYPE_STRING,
197 OBJECT_PATH = DBUS_TYPE_OBJECT_PATH,
198 ARRAY = DBUS_TYPE_ARRAY,
199 STRUCT = DBUS_TYPE_STRUCT,
200 DICT_ENTRY = DBUS_TYPE_DICT_ENTRY,
201 VARIANT = DBUS_TYPE_VARIANT,
202 };
203
204 // Creates a Message. The internal raw message is NULL until it's set
205 // from outside by reset_raw_message().
206 Message();
207 virtual ~Message();
208
209 DBusMessage* raw_message() { return raw_message_; }
210 // Takes the ownership of raw_message.
211 void reset_raw_message(DBusMessage* raw_message);
212
213 // Returns the string representation of this message for debugging.
214 std::string ToString();
215
216 private:
217 // Helper function used in ToString().
218 std::string ToStringInternal(const std::string& indent,
219 MessageReader* reader);
220
221 DBusMessage* raw_message_;
222 DISALLOW_COPY_AND_ASSIGN(Message);
223 };
224
225 // MessageCall is a type of message used for calling a method via D-Bus.
226 class MethodCall : public Message {
227 public:
228 // Creates a method call message for the specified interface name and
229 // the method name.
230 //
231 // For instance, to call "Get" method of DBUS_INTERFACE_INTROSPECTABLE
232 // interface ("org.freedesktop.DBus.Introspectable"), create a method
233 // call like this:
234 //
235 // MethodCall method_call(DBUS_INTERFACE_INTROSPECTABLE, "Get");
236 //
237 MethodCall(const std::string& interface_name,
238 const std::string& method_name);
239
240 private:
241 friend class ObjectProxy;
242
243 // Sets the service name. This will be handled by the object proxy.
244 void SetServiceName(const std::string& service_name);
245 // Sets the object path. This will be handled by the object proxy.
246 void SetObjectPath(const std::string& object_path);
247
248 DISALLOW_COPY_AND_ASSIGN(MethodCall);
249 };
250
251 // Response is a type of message used for receiving a response from a
252 // method via D-Bus.
253 class Response : public Message {
254 public:
255 Response();
256
257 private:
258 DISALLOW_COPY_AND_ASSIGN(Response);
259 };
260
261 // MessageWriter is used to create outgoing messages for calling methods
262 // and sending signals.
263 class MessageWriter {
264 public:
265 // The data will be written into the given message.
266 MessageWriter(Message* message);
267 ~MessageWriter();
268
269 // Appends a byte to the message.
270 void AppendByte(uint8 value);
271 // The following functions do the same for other basic types.
272 void AppendBool(bool value);
273 void AppendInt16(int16 value);
274 void AppendUint16(uint16 value);
275 void AppendInt32(int32 value);
276 void AppendUint32(uint32 value);
277 void AppendInt64(int64 value);
278 void AppendUint64(uint64 value);
279 void AppendDouble(double value);
280 void AppendString(const std::string& value);
281 void AppendObjectPath(const std::string& value);
282
283 // Ideally, client shouldn't need to supply the signature string, but
284 // the underlying D-Bus system requires us to supply this before
285 // appending contents to array and variant. For instance, if you want an
286 // array of strings, or a variant of a string, then you pass "s" as the
287 // signature.
288 //
289 // See the spec for details about the type signatures.
290 // http://dbus.freedesktop.org/doc/dbus-specification.html
291 // #message-protocol-signatures
292 //
293 void OpenArray(const std::string& signature, MessageWriter* sub_writer);
294 void OpenVariant(const std::string& signature, MessageWriter* sub_writer);
295 // Struct and dict entry don't need the type signature.
296 void OpenStruct(MessageWriter* sub_writer);
297 void OpenDictEntry(MessageWriter* sub_writer);
298 // ...
299 void CloseContainer(MessageWriter* sub_writer);
300
301 private:
302 Message* message_;
303 DBusMessageIter raw_message_iter_;
304
305 DISALLOW_COPY_AND_ASSIGN(MessageWriter);
306 };
307
308 // MessageReader is used to read incoming messages such as responses for
309 // method calls.
310 //
311 // MessageReader manages an internal iterator to read data. All functions
312 // starting with Pop advance the iterator on success.
313 class MessageReader {
314 public:
315 // The data will be read from the given message.
316 MessageReader(Message* message);
317 ~MessageReader();
318
319 // Returns true if the reader has more data to read. The function is
320 // used to iterate contents in a container like:
321 //
322 // while (reader.HasMore())
323 // reader.PopString(&value);
324 bool HasMore();
325
326 // Gets the byte at the current iterator position. On success, advances
327 // the iterator and returns true. It's an error if the actual data type
328 // is not a byte, so false will be returned in this case.
329 bool PopByte(uint8* value);
330 // The following functions do the same for other basic types.
331 bool PopBool(bool* value);
332 bool PopInt16(int16* value);
333 bool PopUint16(uint16* value);
334 bool PopInt32(int32* value);
335 bool PopUint32(uint32* value);
336 bool PopInt64(int64* value);
337 bool PopUint64(uint64* value);
338 bool PopDouble(double* value);
339 bool PopString(std::string* value);
340 bool PopObjectPath(std::string* value);
341
342 // Sets up the given message reader to read an array at the current
343 // iterator position. On success, advances the iterator and returns
344 // true. It's an error if the actual data type is not an array, so false
345 // will be returned in this case.
346 bool PopArray(MessageReader* sub_reader);
347 // The following functions do the same for other container types.
348 bool PopStruct(MessageReader* sub_reader);
349 bool PopDictEntry(MessageReader* sub_reader);
350 bool PopVariant(MessageReader* sub_reader);
351
352 // Gets the array of bytes at the current iterator positio. On success,
353 // advances the iterator and returns true. Arrays of bytes are often
354 // used for exchanging binary blobs hence it's worth having a
355 // specialized function.
356 bool PopArrayOfBytes(std::vector<uint8>* bytes);
357
358 // Gets the array of object paths at the current iterator positio. On
359 // success, advances the iterator and returns true. Arrays of object
360 // paths are often used to communicate with D-Bus services like
361 // NetworkManager, hence it's worth having a specialized function.
362 bool PopArrayOfObjectPaths(std::vector<std::string>* object_paths);
363
364 // Gets the byte from the variant data container at the current iterator
365 // position. On success, returns true and advances the
366 // iterator. Variants are widely used in D-Bus services so it's worth
367 // having a specialized function. For instance, The return value type of
368 // "org.freedesktop.DBus.Introspectable.Get" is a variant.
369 bool PopVariantOfByte(uint8* value);
370 // The following functions do the same for other basic types.
371 bool PopVariantOfBool(bool* value);
372 bool PopVariantOfInt16(int16* value);
373 bool PopVariantOfUint16(uint16* value);
374 bool PopVariantOfInt32(int32* value);
375 bool PopVariantOfUint32(uint32* value);
376 bool PopVariantOfInt64(int64* value);
377 bool PopVariantOfUint64(uint64* value);
378 bool PopVariantOfDouble(double* value);
379 bool PopVariantOfString(std::string* value);
380 bool PopVariantOfObjectPath(std::string* value);
381
382 // Get the type of the value at the current iterator position. INVALID
383 // will be returned if the iterator points to the end of the message.
384 Message::Type GetType();
385
386 private:
387 // Returns true if the data type at the current iterator position
388 // matches the given D-Bus type, such as DBUS_TYPE_BYTE.
389 bool CheckType(int dbus_type);
390
391 // Helper function used to implement PopByte() etc.
392 bool PopBasic(int dbus_type, void *value);
393
394 // Helper function used to implement PopArray() etc.
395 bool PopContainer(int dbus_type, MessageReader* sub_reader);
396
397 // Helper function used to implement PopVariantOfByte() etc.
398 bool PopVariantOfBasic(int dbus_type, void* value);
399
400 Message* message_;
401 DBusMessageIter raw_message_iter_;
402
403 DISALLOW_COPY_AND_ASSIGN(MessageReader);
404 };
405
406 } // namespace dbus
407 } // namespace net
408
409 #endif // NET_DBUS_DBUS_H_
OLDNEW
« no previous file with comments | « content/browser/geolocation/wifi_data_provider_linux.cc ('k') | net/dbus/dbus.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698