15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef DBUS_BUS_H_
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define DBUS_BUS_H_
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
82a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include <dbus/dbus.h>
92a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <map>
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <set>
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string>
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <utility>
14868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include <vector>
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/callback.h"
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/ref_counted.h"
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/synchronization/waitable_event.h"
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/threading/platform_thread.h"
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "dbus/dbus_export.h"
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "dbus/object_path.h"
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace base {
242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class SequencedTaskRunner;
252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class SingleThreadTaskRunner;
2668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)class TaskRunner;
272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}
282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)namespace tracked_objects {
302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class Location;
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace dbus {
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class ExportedObject;
362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class ObjectManager;
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class ObjectProxy;
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Bus is used to establish a connection with D-Bus, create object
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// proxies, and export objects.
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// For asynchronous operations such as an asynchronous method call, the
432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// bus object will use a task runner to monitor the underlying file
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// descriptor used for D-Bus communication. By default, the bus will use
452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// the current thread's task runner. If |dbus_task_runner| option is
462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// specified, the bus will use that task runner instead.
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// THREADING
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// In the D-Bus library, we use the two threads:
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// - The origin thread: the thread that created the Bus object.
532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)// - The D-Bus thread: the thread servicing |dbus_task_runner|.
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The origin thread is usually Chrome's UI thread. The D-Bus thread is
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// usually a dedicated thread for the D-Bus library.
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// BLOCKING CALLS
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Functions that issue blocking calls are marked "BLOCKING CALL" and
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// these functions should be called in the D-Bus thread (if
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// supplied). AssertOnDBusThread() is placed in these functions.
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Note that it's hard to tell if a libdbus function is actually blocking
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// or not (ex. dbus_bus_request_name() internally calls
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// dbus_connection_send_with_reply_and_block(), which is a blocking
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// call). To err on the safe side, we consider all libdbus functions that
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// deal with the connection to dbus-daemon to be blocking.
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// SHUTDOWN
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The Bus object must be shut down manually by ShutdownAndBlock() and
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// friends. We require the manual shutdown to make the operation explicit
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// rather than doing it silently in the destructor.
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// EXAMPLE USAGE:
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Synchronous method call:
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   dbus::Bus::Options options;
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   // Set up the bus options here.
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   ...
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   dbus::Bus bus(options);
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   dbus::ObjectProxy* object_proxy =
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//       bus.GetObjectProxy(service_name, object_path);
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   dbus::MethodCall method_call(interface_name, method_name);
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   scoped_ptr<dbus::Response> response(
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//       object_proxy.CallMethodAndBlock(&method_call, timeout_ms));
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   if (response.get() != NULL) {  // Success.
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//     ...
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   }
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Asynchronous method call:
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   void OnResponse(dbus::Response* response) {
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//     // response is NULL if the method call failed.
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//     if (!response)
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//       return;
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   }
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   ...
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   object_proxy.CallMethod(&method_call, timeout_ms,
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//                           base::Bind(&OnResponse));
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Exporting a method:
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   void Echo(dbus::MethodCall* method_call,
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//             dbus::ExportedObject::ResponseSender response_sender) {
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//     // Do something with method_call.
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//     Response* response = Response::FromMethodCall(method_call);
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//     // Build response here.
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//     // Can send an immediate response here to implement a synchronous service
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//     // or store the response_sender and send a response later to implement an
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//     // asynchronous service.
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//     response_sender.Run(response);
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   }
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   void OnExported(const std::string& interface_name,
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//                   const ObjectPath& object_path,
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//                   bool success) {
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//     // success is true if the method was exported successfully.
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   }
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   ...
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   dbus::ExportedObject* exported_object =
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//       bus.GetExportedObject(service_name, object_path);
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   exported_object.ExportMethod(interface_name, method_name,
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//                                base::Bind(&Echo),
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//                                base::Bind(&OnExported));
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// WHY IS THIS A REF COUNTED OBJECT?
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Bus is a ref counted object, to ensure that |this| of the object is
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// alive when callbacks referencing |this| are called. However, after the
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// bus is shut down, |connection_| can be NULL. Hence, callbacks should
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// not rely on that |connection_| is alive.
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class CHROME_DBUS_EXPORT Bus : public base::RefCountedThreadSafe<Bus> {
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Specifies the bus type. SESSION is used to communicate with per-user
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // services like GNOME applications. SYSTEM is used to communicate with
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // system-wide services like NetworkManager. CUSTOM_ADDRESS is used to
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // communicate with an user specified address.
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  enum BusType {
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    SESSION = DBUS_BUS_SESSION,
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    SYSTEM = DBUS_BUS_SYSTEM,
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    CUSTOM_ADDRESS,
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Specifies the connection type. PRIVATE should usually be used unless
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // you are sure that SHARED is safe for you, which is unlikely the case
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // in Chrome.
1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // PRIVATE gives you a private connection, that won't be shared with
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // other Bus objects.
1575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // SHARED gives you a connection shared among other Bus objects, which
1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // is unsafe if the connection is shared with multiple threads.
1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  enum ConnectionType {
1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    PRIVATE,
1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    SHARED,
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
165c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Specifies whether the GetServiceOwnerAndBlock call should report or
166c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // suppress errors.
167c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  enum GetServiceOwnerOption {
168c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    REPORT_ERRORS,
169c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)    SUPPRESS_ERRORS,
170c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  };
171c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
172a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // Specifies service ownership options.
173a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  //
174a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // REQUIRE_PRIMARY indicates that you require primary ownership of the
175a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // service name.
176a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  //
177a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // ALLOW_REPLACEMENT indicates that you'll allow another connection to
178a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // steal ownership of this service name from you.
179a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  //
180a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  // REQUIRE_PRIMARY_ALLOW_REPLACEMENT does the obvious.
181a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  enum ServiceOwnershipOptions {
182a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    REQUIRE_PRIMARY = (DBUS_NAME_FLAG_DO_NOT_QUEUE |
183a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)                       DBUS_NAME_FLAG_REPLACE_EXISTING),
184a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)    REQUIRE_PRIMARY_ALLOW_REPLACEMENT = (REQUIRE_PRIMARY |
185a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)                                         DBUS_NAME_FLAG_ALLOW_REPLACEMENT),
186a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  };
187a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)
1885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Options used to create a Bus object.
1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  struct CHROME_DBUS_EXPORT Options {
1905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    Options();
1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ~Options();
1925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    BusType bus_type;  // SESSION by default.
1945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    ConnectionType connection_type;  // PRIVATE by default.
1952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // If dbus_task_runner is set, the bus object will use that
1962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // task runner to process asynchronous operations.
1975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    //
1982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // The thread servicing the task runner should meet the following
1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // requirements:
2005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // 1) Already running.
2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // 2) Has a MessageLoopForIO.
2022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    scoped_refptr<base::SequencedTaskRunner> dbus_task_runner;
2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Specifies the server addresses to be connected. If you want to
2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // communicate with non dbus-daemon such as ibus-daemon, set |bus_type| to
2065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // CUSTOM_ADDRESS, and |address| to the D-Bus server address you want to
2075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // connect to. The format of this address value is the dbus address style
2085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // which is described in
2095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // http://dbus.freedesktop.org/doc/dbus-specification.html#addresses
2105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    //
2115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // EXAMPLE USAGE:
2125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    //   dbus::Bus::Options options;
2135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    //   options.bus_type = CUSTOM_ADDRESS;
2145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    //   options.address.assign("unix:path=/tmp/dbus-XXXXXXX");
2155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    //   // Set up other options
2165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    //   dbus::Bus bus(options);
2175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    //
2185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    //   // Do something.
2195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    //
2205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    std::string address;
2212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // If the connection with dbus-daemon is closed, |disconnected_callback|
2232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // will be called on the origin thread. This is also called when the
2242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // disonnection by ShutdownAndBlock. |disconnected_callback| can be null
2252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    // callback
2262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    base::Closure disconnected_callback;
2275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
2285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Creates a Bus object. The actual connection will be established when
2305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Connect() is called.
2315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  explicit Bus(const Options& options);
2325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Called when an ownership request is complete.
2345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Parameters:
2355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // - the requested service name.
2365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // - whether ownership has been obtained or not.
2375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  typedef base::Callback<void (const std::string&, bool)> OnOwnershipCallback;
238c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
239c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Called when GetServiceOwner() completes.
240c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // |service_owner| is the return value from GetServiceOwnerAndBlock().
241c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  typedef base::Callback<void (const std::string& service_owner)>
242c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      GetServiceOwnerCallback;
243c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
2442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // TODO(satorux): Remove the service name parameter as the caller of
2452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // RequestOwnership() knows the service name.
2465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Gets the object proxy for the given service name and the object path.
2485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The caller must not delete the returned object.
2495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
2505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns an existing object proxy if the bus object already owns the
2515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // object proxy for the given service name and the object path.
2525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Never returns NULL.
2535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
2545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The bus will own all object proxies created by the bus, to ensure
2555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // that the object proxies are detached from remote objects at the
2565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // shutdown time of the bus.
2575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
2585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The object proxy is used to call methods of remote objects, and
2595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // receive signals from them.
2605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
2615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |service_name| looks like "org.freedesktop.NetworkManager", and
2625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |object_path| looks like "/org/freedesktop/NetworkManager/Devices/0".
2635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
2645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Must be called in the origin thread.
2655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ObjectProxy* GetObjectProxy(const std::string& service_name,
2665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                      const ObjectPath& object_path);
2675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Same as above, but also takes a bitfield of ObjectProxy::Options.
2695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // See object_proxy.h for available options.
2705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ObjectProxy* GetObjectProxyWithOptions(
2715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const std::string& service_name,
2725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const ObjectPath& object_path,
2735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      int options);
2745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Removes the previously created object proxy for the given service
2762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // name and the object path and releases its memory.
2772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
2782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // If and object proxy for the given service name and object was
2792a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // created with GetObjectProxy, this function removes it from the
2802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // bus object and detaches the ObjectProxy, invalidating any pointer
2812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // previously acquired for it with GetObjectProxy. A subsequent call
2822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // to GetObjectProxy will return a new object.
2832a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
2842a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // All the object proxies are detached from remote objects at the
2852a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // shutdown time of the bus, but they can be detached early to reduce
2862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // memory footprint and used match rules for the bus connection.
2872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
2882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // |service_name| looks like "org.freedesktop.NetworkManager", and
2892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // |object_path| looks like "/org/freedesktop/NetworkManager/Devices/0".
2902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // |callback| is called when the object proxy is successfully removed and
2912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // detached.
2922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
2932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // The function returns true when there is an object proxy matching the
2942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // |service_name| and |object_path| to remove, and calls |callback| when it
2952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // is removed. Otherwise, it returns false and the |callback| function is
2962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // never called. The |callback| argument must not be null.
2972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
2982a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Must be called in the origin thread.
2992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual bool RemoveObjectProxy(const std::string& service_name,
3002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                 const ObjectPath& object_path,
3012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                 const base::Closure& callback);
3022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Same as above, but also takes a bitfield of ObjectProxy::Options.
3042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // See object_proxy.h for available options.
3052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual bool RemoveObjectProxyWithOptions(
3062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      const std::string& service_name,
3072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      const ObjectPath& object_path,
3082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      int options,
3092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      const base::Closure& callback);
3102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Gets the exported object for the given object path.
3125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The caller must not delete the returned object.
3135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
3145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns an existing exported object if the bus object already owns
3155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the exported object for the given object path. Never returns NULL.
3165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
3175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The bus will own all exported objects created by the bus, to ensure
3185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // that the exported objects are unregistered at the shutdown time of
3195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the bus.
3205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
3215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The exported object is used to export methods of local objects, and
3225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // send signal from them.
3235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
3245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Must be called in the origin thread.
3255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ExportedObject* GetExportedObject(const ObjectPath& object_path);
3265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Unregisters the exported object for the given object path |object_path|.
3285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
3295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Getting an exported object for the same object path after this call
3305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // will return a new object, method calls on any remaining copies of the
3315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // previous object will not be called.
3325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
3335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Must be called in the origin thread.
3345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void UnregisterExportedObject(const ObjectPath& object_path);
3355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Gets an object manager for the given remote object path |object_path|
3382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // exported by the service |service_name|.
3392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
3402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Returns an existing object manager if the bus object already owns a
3412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // matching object manager, never returns NULL.
3422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
3432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // The caller must not delete the returned object, the bus retains ownership
3442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // of all object managers.
3452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
3462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Must be called in the origin thread.
3472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual ObjectManager* GetObjectManager(const std::string& service_name,
3482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                          const ObjectPath& object_path);
3492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Unregisters the object manager for the given remote object path
3512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // |object_path| exported by the srevice |service_name|.
3522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
3532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Getting an object manager for the same remote object after this call
3542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // will return a new object, method calls on any remaining copies of the
3552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // previous object are not permitted.
3562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
3571320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // This method will asynchronously clean up any match rules that have been
3581320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // added for the object manager and invoke |callback| when the operation is
3591320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // complete. If this method returns false, then |callback| is never called.
3601320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // The |callback| argument must not be null.
3611320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  //
3622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Must be called in the origin thread.
3631320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  virtual bool RemoveObjectManager(const std::string& service_name,
3641320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                                   const ObjectPath& object_path,
3651320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                                   const base::Closure& callback);
3662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Instructs all registered object managers to retrieve their set of managed
3682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // objects from their respective remote objects. There is no need to call this
3692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // manually, this is called automatically by the D-Bus thread manager once
3702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // implementation classes are registered.
3712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual void GetManagedObjects();
3722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Shuts down the bus and blocks until it's done. More specifically, this
3745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // function does the following:
3755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
3765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // - Unregisters the object paths
3775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // - Releases the service names
3785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // - Closes the connection to dbus-daemon.
3795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
3802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // This function can be called multiple times and it is no-op for the 2nd time
3812a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // calling.
3822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
3835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // BLOCKING CALL.
3845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void ShutdownAndBlock();
3855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Similar to ShutdownAndBlock(), but this function is used to
3875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // synchronously shut down the bus that uses the D-Bus thread. This
3885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // function is intended to be used at the very end of the browser
3895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // shutdown, where it makes more sense to shut down the bus
3905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // synchronously, than trying to make it asynchronous.
3915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
3925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // BLOCKING CALL, but must be called in the origin thread.
3935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void ShutdownOnDBusThreadAndBlock();
3945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true if the shutdown has been completed.
3965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool shutdown_completed() { return shutdown_completed_; }
3975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
3995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The public functions below are not intended to be used in client
4005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // code. These are used to implement ObjectProxy and ExportedObject.
4015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
4025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Connects the bus to the dbus-daemon.
4045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true on success, or the bus is already connected.
4055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
4065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // BLOCKING CALL.
4075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool Connect();
4085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Disconnects the bus from the dbus-daemon.
4102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Safe to call multiple times and no operation after the first call.
4112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Do not call for shared connection it will be released by libdbus.
4122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  //
4132a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // BLOCKING CALL.
4142a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual void ClosePrivateConnection();
4152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
4165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Requests the ownership of the service name given by |service_name|.
4175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // See also RequestOwnershipAndBlock().
4185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
4195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |on_ownership_callback| is called when the service name is obtained
4205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // or failed to be obtained, in the origin thread.
4215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
4225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Must be called in the origin thread.
4235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void RequestOwnership(const std::string& service_name,
424a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)                                ServiceOwnershipOptions options,
4255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                OnOwnershipCallback on_ownership_callback);
4265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Requests the ownership of the given service name.
4285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true on success, or the the service name is already obtained.
4295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
4305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Note that it's important to expose methods before requesting a service
4315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // name with this method.  See also ExportedObject::ExportMethodAndBlock()
4325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // for details.
4335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  //
4345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // BLOCKING CALL.
435a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)  virtual bool RequestOwnershipAndBlock(const std::string& service_name,
436a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)                                        ServiceOwnershipOptions options);
4375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Releases the ownership of the given service name.
4395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true on success.
4405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
4415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // BLOCKING CALL.
4425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool ReleaseOwnership(const std::string& service_name);
4435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Sets up async operations.
4455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true on success, or it's already set up.
4465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // This function needs to be called before starting async operations.
4475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
4485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // BLOCKING CALL.
4495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool SetUpAsyncOperations();
4505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Sends a message to the bus and blocks until the response is
4525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // received. Used to implement synchronous method calls.
4535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
4545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // BLOCKING CALL.
4555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual DBusMessage* SendWithReplyAndBlock(DBusMessage* request,
4565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                             int timeout_ms,
4575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                             DBusError* error);
4585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Requests to send a message to the bus. The reply is handled with
4605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |pending_call| at a later time.
4615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
4625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // BLOCKING CALL.
4635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void SendWithReply(DBusMessage* request,
4645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             DBusPendingCall** pending_call,
4655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             int timeout_ms);
4665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Requests to send a message to the bus. The message serial number will
4685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // be stored in |serial|.
4695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
4705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // BLOCKING CALL.
4715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void Send(DBusMessage* request, uint32* serial);
4725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Adds the message filter function. |filter_function| will be called
4745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // when incoming messages are received. Returns true on success.
4755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
4765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // When a new incoming message arrives, filter functions are called in
4775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the order that they were added until the the incoming message is
4785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // handled by a filter function.
4795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
4805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The same filter function associated with the same user data cannot be
4815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // added more than once. Returns false for this case.
4825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
4835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // BLOCKING CALL.
4845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool AddFilterFunction(DBusHandleMessageFunction filter_function,
4855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                 void* user_data);
4865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Removes the message filter previously added by AddFilterFunction().
4885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true on success.
4895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
4905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // BLOCKING CALL.
4915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool RemoveFilterFunction(DBusHandleMessageFunction filter_function,
4925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                    void* user_data);
4935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
4945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Adds the match rule. Messages that match the rule will be processed
4955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // by the filter functions added by AddFilterFunction().
4965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
4975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // You cannot specify which filter function to use for a match rule.
4985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Instead, you should check if an incoming message is what you are
4995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // interested in, in the filter functions.
5005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
5012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // The same match rule can be added more than once and should be removed
5022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // as many times as it was added.
5035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
5045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The match rule looks like:
5055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // "type='signal', interface='org.chromium.SomeInterface'".
5065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
5075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // See "Message Bus Message Routing" section in the D-Bus specification
5085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // for details about match rules:
5095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // http://dbus.freedesktop.org/doc/dbus-specification.html#message-bus-routing
5105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
5115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // BLOCKING CALL.
5125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void AddMatch(const std::string& match_rule, DBusError* error);
5135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Removes the match rule previously added by AddMatch().
5152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Returns false if the requested match rule is unknown or has already been
5162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // removed. Otherwise, returns true and sets |error| accordingly.
5175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
5185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // BLOCKING CALL.
5192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  virtual bool RemoveMatch(const std::string& match_rule, DBusError* error);
5205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Tries to register the object path. Returns true on success.
5225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns false if the object path is already registered.
5235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
5245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |message_function| in |vtable| will be called every time when a new
5255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |message sent to the object path arrives.
5265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
5275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The same object path must not be added more than once.
5285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
5295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // See also documentation of |dbus_connection_try_register_object_path| at
5305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // http://dbus.freedesktop.org/doc/api/html/group__DBusConnection.html
5315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
5325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // BLOCKING CALL.
5335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool TryRegisterObjectPath(const ObjectPath& object_path,
5345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                     const DBusObjectPathVTable* vtable,
5355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                     void* user_data,
5365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                     DBusError* error);
5375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Unregister the object path.
5395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
5405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // BLOCKING CALL.
5415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void UnregisterObjectPath(const ObjectPath& object_path);
5425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
54368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  // Returns the task runner of the D-Bus thread.
54468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  virtual base::TaskRunner* GetDBusTaskRunner();
54568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
54668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  // Returns the task runner of the thread that created the bus.
54768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  virtual base::TaskRunner* GetOriginTaskRunner();
5485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true if the bus has the D-Bus thread.
5505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool HasDBusThread();
5515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Check whether the current thread is on the origin thread (the thread
5535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // that created the bus). If not, DCHECK will fail.
5545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void AssertOnOriginThread();
5555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Check whether the current thread is on the D-Bus thread. If not,
5575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // DCHECK will fail. If the D-Bus thread is not supplied, it calls
5585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // AssertOnOriginThread().
5595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void AssertOnDBusThread();
5605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
561c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Gets the owner for |service_name| via org.freedesktop.DBus.GetNameOwner.
562c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Returns the owner name, if any, or an empty string on failure.
563c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // |options| specifies where to printing error messages or not.
564c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  //
565c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // BLOCKING CALL.
566c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  virtual std::string GetServiceOwnerAndBlock(const std::string& service_name,
567c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                                              GetServiceOwnerOption options);
568c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
569c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // A non-blocking version of GetServiceOwnerAndBlock().
570c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Must be called in the origin thread.
571c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  virtual void GetServiceOwner(const std::string& service_name,
572c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                               const GetServiceOwnerCallback& callback);
573c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
574868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Whenever the owner for |service_name| changes, run |callback| with the
575868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // name of the new owner. If the owner goes away, then |callback| receives
576868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // an empty string.
577868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  //
578868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Any unique (service_name, callback) can be used. Duplicate are ignored.
579868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // |service_name| must not be empty and |callback| must not be null.
580868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  //
581868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Must be called in the origin thread.
582868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  virtual void ListenForServiceOwnerChange(
583868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      const std::string& service_name,
584868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      const GetServiceOwnerCallback& callback);
585868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
586868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Stop listening for |service_name| owner changes for |callback|.
587868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Any unique (service_name, callback) can be used. Non-registered callbacks
588868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // for a given service name are ignored.
589868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // |service_name| must not be empty and |callback| must not be null.
590868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  //
591868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Must be called in the origin thread.
592868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  virtual void UnlistenForServiceOwnerChange(
593868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      const std::string& service_name,
594868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      const GetServiceOwnerCallback& callback);
595868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
5965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true if the bus is connected to D-Bus.
5975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool is_connected() { return connection_ != NULL; }
5985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) protected:
6005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // This is protected, so we can define sub classes.
6015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ~Bus();
6025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
6045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  friend class base::RefCountedThreadSafe<Bus>;
6055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Helper function used for RemoveObjectProxy().
6072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void RemoveObjectProxyInternal(scoped_refptr<dbus::ObjectProxy> object_proxy,
6082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                                 const base::Closure& callback);
6092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
6101320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // Helper functions used for RemoveObjectManager().
6111320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  void RemoveObjectManagerInternal(
6121320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      scoped_refptr<dbus::ObjectManager> object_manager,
6131320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      const base::Closure& callback);
6141320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  void RemoveObjectManagerInternalHelper(
6151320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      scoped_refptr<dbus::ObjectManager> object_manager,
6161320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      const base::Closure& callback);
6171320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
6185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Helper function used for UnregisterExportedObject().
6195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void UnregisterExportedObjectInternal(
6205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      scoped_refptr<dbus::ExportedObject> exported_object);
6215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Helper function used for ShutdownOnDBusThreadAndBlock().
6235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void ShutdownOnDBusThreadAndBlockInternal();
6245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Helper function used for RequestOwnership().
6265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void RequestOwnershipInternal(const std::string& service_name,
627a36e5920737c6adbddd3e43b760e5de8431db6e0Torne (Richard Coles)                                ServiceOwnershipOptions options,
6285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                OnOwnershipCallback on_ownership_callback);
6295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
630c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  // Helper function used for GetServiceOwner().
631c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  void GetServiceOwnerInternal(const std::string& service_name,
632c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)                               const GetServiceOwnerCallback& callback);
633c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
634868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Helper function used for ListenForServiceOwnerChange().
635868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  void ListenForServiceOwnerChangeInternal(
636868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      const std::string& service_name,
637868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      const GetServiceOwnerCallback& callback);
638868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
639868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Helper function used for UnListenForServiceOwnerChange().
640868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  void UnlistenForServiceOwnerChangeInternal(
641868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      const std::string& service_name,
642868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      const GetServiceOwnerCallback& callback);
643868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
6445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Processes the all incoming data to the connection, if any.
6455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
6465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // BLOCKING CALL.
6475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void ProcessAllIncomingDataIfAny();
6485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Called when a watch object is added. Used to start monitoring the
6505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // file descriptor used for D-Bus communication.
6515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  dbus_bool_t OnAddWatch(DBusWatch* raw_watch);
6525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Called when a watch object is removed.
6545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void OnRemoveWatch(DBusWatch* raw_watch);
6555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Called when the "enabled" status of |raw_watch| is toggled.
6575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void OnToggleWatch(DBusWatch* raw_watch);
6585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Called when a timeout object is added. Used to start monitoring
6605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // timeout for method calls.
6615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  dbus_bool_t OnAddTimeout(DBusTimeout* raw_timeout);
6625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Called when a timeout object is removed.
6645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void OnRemoveTimeout(DBusTimeout* raw_timeout);
6655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Called when the "enabled" status of |raw_timeout| is toggled.
6675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void OnToggleTimeout(DBusTimeout* raw_timeout);
6685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Called when the dispatch status (i.e. if any incoming data is
6705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // available) is changed.
6715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void OnDispatchStatusChanged(DBusConnection* connection,
6725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                               DBusDispatchStatus status);
6735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
6742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Called when the connection is diconnected.
6752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void OnConnectionDisconnected(DBusConnection* connection);
6762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
677868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Called when a service owner change occurs.
678868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  void OnServiceOwnerChanged(DBusMessage* message);
679868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
6805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Callback helper functions. Redirects to the corresponding member function.
6815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static dbus_bool_t OnAddWatchThunk(DBusWatch* raw_watch, void* data);
6825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static void OnRemoveWatchThunk(DBusWatch* raw_watch, void* data);
6835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static void OnToggleWatchThunk(DBusWatch* raw_watch, void* data);
6845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static dbus_bool_t OnAddTimeoutThunk(DBusTimeout* raw_timeout, void* data);
6855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static void OnRemoveTimeoutThunk(DBusTimeout* raw_timeout, void* data);
6865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static void OnToggleTimeoutThunk(DBusTimeout* raw_timeout, void* data);
6875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static void OnDispatchStatusChangedThunk(DBusConnection* connection,
6885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                           DBusDispatchStatus status,
6895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                           void* data);
6902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
691868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Calls OnConnectionDisconnected if the Disconnected signal is received.
6922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  static DBusHandlerResult OnConnectionDisconnectedFilter(
6932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      DBusConnection* connection,
6942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      DBusMessage* message,
6952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      void* user_data);
6962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
697868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Calls OnServiceOwnerChanged for a NameOwnerChanged signal.
698868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  static DBusHandlerResult OnServiceOwnerChangedFilter(
699868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      DBusConnection* connection,
700868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      DBusMessage* message,
701868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      void* user_data);
702868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
7035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const BusType bus_type_;
7045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const ConnectionType connection_type_;
7052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  scoped_refptr<base::SequencedTaskRunner> dbus_task_runner_;
7065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  base::WaitableEvent on_shutdown_;
7075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DBusConnection* connection_;
7085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  scoped_refptr<base::SingleThreadTaskRunner> origin_task_runner_;
7105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  base::PlatformThreadId origin_thread_id_;
7115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::set<std::string> owned_service_names_;
7135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The following sets are used to check if rules/object_paths/filters
7145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // are properly cleaned up before destruction of the bus object.
7152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Since it's not an error to add the same match rule twice, the repeated
7162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // match rules are counted in a map.
7172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  std::map<std::string, int> match_rules_added_;
7185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::set<ObjectPath> registered_object_paths_;
7195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::set<std::pair<DBusHandleMessageFunction, void*> >
7205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      filter_functions_added_;
7215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // ObjectProxyTable is used to hold the object proxies created by the
7235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // bus object. Key is a pair; the first part is a concatenated string of
7245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // service name + object path, like
7255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // "org.chromium.TestService/org/chromium/TestObject".
7265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The second part is the ObjectProxy::Options for the proxy.
7275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  typedef std::map<std::pair<std::string, int>,
7285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                   scoped_refptr<dbus::ObjectProxy> > ObjectProxyTable;
7295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ObjectProxyTable object_proxy_table_;
7305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // ExportedObjectTable is used to hold the exported objects created by
7325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the bus object. Key is a concatenated string of service name +
7335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // object path, like "org.chromium.TestService/org/chromium/TestObject".
7345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  typedef std::map<const dbus::ObjectPath,
7355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                   scoped_refptr<dbus::ExportedObject> > ExportedObjectTable;
7365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ExportedObjectTable exported_object_table_;
7375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7382a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // ObjectManagerTable is used to hold the object managers created by the
7392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // bus object. Key is a concatenated string of service name + object path,
7402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // like "org.chromium.TestService/org/chromium/TestObject".
7412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  typedef std::map<std::string,
7422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                   scoped_refptr<dbus::ObjectManager> > ObjectManagerTable;
7432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  ObjectManagerTable object_manager_table_;
7442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
745868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // A map of NameOwnerChanged signals to listen for and the callbacks to run
746868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // on the origin thread when the owner changes.
747868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Only accessed on the DBus thread.
748868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Key: Service name
749868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Value: Vector of callbacks. Unique and expected to be small. Not using
750868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  //        std::set here because base::Callbacks don't have a '<' operator.
751868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  typedef std::map<std::string, std::vector<GetServiceOwnerCallback> >
752868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)      ServiceOwnerChangedListenerMap;
753868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  ServiceOwnerChangedListenerMap service_owner_changed_listener_map_;
754868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
7555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool async_operations_set_up_;
7565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool shutdown_completed_;
7575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Counters to make sure that OnAddWatch()/OnRemoveWatch() and
7595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // OnAddTimeout()/OnRemoveTimeou() are balanced.
7605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int num_pending_watches_;
7615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int num_pending_timeouts_;
7625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string address_;
7642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::Closure on_disconnected_closure_;
7655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(Bus);
7675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
7685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace dbus
7705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
7715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // DBUS_BUS_H_
772