diff options
author | satorux@chromium.org <satorux@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-08-17 20:58:12 +0000 |
---|---|---|
committer | satorux@chromium.org <satorux@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-08-17 20:58:12 +0000 |
commit | a510761104333ec7da3943399f059ff693839856 (patch) | |
tree | 2a9f81a60bccf4db3367ad1b8f71e9ea130efb7d /dbus/exported_object.h | |
parent | 07f93af15169ee054552a376ac4953abd1346cb2 (diff) | |
download | chromium_src-a510761104333ec7da3943399f059ff693839856.zip chromium_src-a510761104333ec7da3943399f059ff693839856.tar.gz chromium_src-a510761104333ec7da3943399f059ff693839856.tar.bz2 |
Implement Bus and ObjectProxy classes for our D-Bus library.
ObjectProxy is used to access remote objects.
ExportedObject is used to export objects to other D-Bus
BUG=90036
TEST=run unit tests. The code is not yet used in Chrome.
Review URL: http://codereview.chromium.org/7491029
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@97204 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'dbus/exported_object.h')
-rw-r--r-- | dbus/exported_object.h | 144 |
1 files changed, 144 insertions, 0 deletions
diff --git a/dbus/exported_object.h b/dbus/exported_object.h new file mode 100644 index 0000000..8e03118 --- /dev/null +++ b/dbus/exported_object.h @@ -0,0 +1,144 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef DBUS_EXPORTED_OBJECT_H_ +#define DBUS_EXPORTED_OBJECT_H_ +#pragma once + +#include <string> +#include <map> +#include <utility> + +#include <dbus/dbus.h> + +#include "base/callback.h" +#include "base/memory/ref_counted.h" +#include "base/synchronization/condition_variable.h" +#include "base/threading/platform_thread.h" + +class MessageLoop; + +namespace dbus { + +class Bus; +class MethodCall; +class Response; + +// ExportedObject is used to export objects and methods to other D-Bus +// clients. +// +// ExportedObject is a ref counted object, to ensure that |this| of the +// object is alive when callbacks referencing |this| are called. +class ExportedObject : public base::RefCountedThreadSafe<ExportedObject> { + public: + // Client code should use Bus::GetExportedObject() instead of this + // constructor. + ExportedObject(Bus* bus, + const std::string& service_name, + const std::string& object_path); + + // Called when an exported method is called. MethodCall* is the request + // message. + typedef base::Callback<Response* (MethodCall*)> MethodCallCallback; + + // Called when method exporting is done. + // Parameters: + // - the interface name. + // - the method name. + // - whether exporting was successful or not. + typedef base::Callback<void (const std::string&, const std::string&, bool)> + OnExportedCallback; + + // Exports the method specified by |interface_name| and |method_name|, + // and blocks until exporting is done. Returns true on success. + // + // |method_call_callback| will be called in the origin thread, when the + // exported method is called. As it's called in the origin thread, + // callback| can safely reference objects in the origin thread (i.e. UI + // thread in most cases). + // + // BLOCKING CALL. + virtual bool ExportMethodAndBlock(const std::string& interface_name, + const std::string& method_name, + MethodCallCallback method_call_callback); + + // Requests to export the method specified by |interface_name| and + // |method_name|. See Also ExportMethodAndBlock(). + // + // |on_exported_callback| is called when the method is exported or + // failed to be exported, in the origin thread. + // + // Must be called in the origin thread. + virtual void ExportMethod(const std::string& interface_name, + const std::string& method_name, + MethodCallCallback method_call_callback, + OnExportedCallback on_exported_callback); + + // Unregisters the object from the bus. The Bus object will take care of + // unregistering so you don't have to do this manually. + // + // BLOCKING CALL. + virtual void Unregister(); + + private: + friend class base::RefCountedThreadSafe<ExportedObject>; + virtual ~ExportedObject(); + + // Helper function for ExportMethod(). + void ExportMethodInternal(const std::string& interface_name, + const std::string& method_name, + MethodCallCallback method_call_callback, + OnExportedCallback exported_callback); + + // Called when the object is exported. + void OnExported(OnExportedCallback on_exported_callback, + const std::string& interface_name, + const std::string& method_name, + bool success); + + // Registers this object to the bus. + // Returns true on success, or the object is already registered. + // + // BLOCKING CALL. + bool Register(); + + // Handles the incoming request messages and dispatches to the exported + // methods. + DBusHandlerResult HandleMessage(DBusConnection* connection, + DBusMessage* raw_message); + + // Runs the method. Helper function for HandleMessage(). + void RunMethod(MethodCallCallback method_call_callback, + MethodCall* method_call); + + // Called when the object is unregistered. + void OnUnregistered(DBusConnection* connection); + + // Redirects the function call to HandleMessage(). + static DBusHandlerResult HandleMessageThunk(DBusConnection* connection, + DBusMessage* raw_message, + void* user_data); + + // Redirects the function call to OnUnregistered(). + static void OnUnregisteredThunk(DBusConnection* connection, + void* user_data); + + Bus* bus_; + std::string service_name_; + std::string object_path_; + bool object_is_registered_; + bool method_is_called_; + dbus::Response* response_from_method_; + base::Lock method_is_called_lock_; + base::ConditionVariable on_method_is_called_; + + // The method table where keys are absolute method names (i.e. interface + // name + method name), and values are the corresponding callbacks. + typedef std::map<std::string, MethodCallCallback> MethodTable; + MethodTable method_table_; +}; + +} // namespace dbus + +#endif // DBUS_EXPORTED_OBJECT_H_ |