diff options
author | cmasone@chromium.org <cmasone@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-04-24 16:17:57 +0000 |
---|---|---|
committer | cmasone@chromium.org <cmasone@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-04-24 16:17:57 +0000 |
commit | 196c60bbf6fb74be48726da4f96097490826f67d (patch) | |
tree | 5fa0d20ff75307add5f3c2752c1b3b1bb0d673f2 /mojo/examples/dbus_echo | |
parent | 4765890cc2e2b69bbb356f238bf26f8a6d2bf574 (diff) | |
download | chromium_src-196c60bbf6fb74be48726da4f96097490826f67d.zip chromium_src-196c60bbf6fb74be48726da4f96097490826f67d.tar.gz chromium_src-196c60bbf6fb74be48726da4f96097490826f67d.tar.bz2 |
Implement a trivial Mojo EchoService that can be connected to over DBus
Some systems might like to use Mojo IPC to talk to services that are
running outside the auspices of the Mojo shell. DBus is one potential
channel to use for bootstrapping such a connection.
In order to allow the externally-running service to accept connections
from a Mojo shell, we need to get it a ShellHandle. This change
defines a DBusServiceLoader that implements the ServiceLoader
interface. DBusServiceLoader creates a dedicated MessagePipe, passes
a handle to one end to the desired service over DBus, and then passes
the ShellHandle over that pipe.
This class assumes the following:
1) Your service is already running,
2) Your service implements the Mojo ExternalService API
(from external_service.mojom).
3) Your service exports an object that implements the org.chromium.Mojo
DBus interface:
<interface name="org.chromium.Mojo">
<method name="ConnectChannel">
<arg type="h" name="file_descriptor" direction="in" />
</method>
</interface>
This change also includes a trivial DBusEchoService that can be
"loaded" using the DBusServiceLoader, as well as a mojo example app
that connects to this service.
BUG=364903
TEST=run mojo_dbus_echo_service in one shell, and then load mojo_dbus_echo in the mojo shell. "who" should successfully echo between the two.
Review URL: https://codereview.chromium.org/242763003
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@265927 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'mojo/examples/dbus_echo')
-rw-r--r-- | mojo/examples/dbus_echo/dbus_echo_app.cc | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/mojo/examples/dbus_echo/dbus_echo_app.cc b/mojo/examples/dbus_echo/dbus_echo_app.cc new file mode 100644 index 0000000..fefd72c --- /dev/null +++ b/mojo/examples/dbus_echo/dbus_echo_app.cc @@ -0,0 +1,74 @@ +// Copyright 2014 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. + +#include <stdio.h> +#include <string> + +#include "base/bind.h" +#include "base/logging.h" +#include "mojo/public/cpp/bindings/allocation_scope.h" +#include "mojo/public/cpp/bindings/remote_ptr.h" +#include "mojo/public/cpp/environment/environment.h" +#include "mojo/public/cpp/shell/application.h" +#include "mojo/public/cpp/system/core.h" +#include "mojo/public/cpp/system/macros.h" +#include "mojo/public/cpp/utility/run_loop.h" +#include "mojo/public/interfaces/shell/shell.mojom.h" +#include "mojo/services/dbus_echo/echo.mojom.h" + +#if defined(WIN32) +#if !defined(CDECL) +#define CDECL __cdecl +#endif +#define DBUS_ECHO_APP_EXPORT __declspec(dllexport) +#else +#define CDECL +#define DBUS_ECHO_APP_EXPORT __attribute__((visibility("default"))) +#endif + +namespace mojo { +namespace examples { + +class DBusEchoApp : public Application, public mojo::EchoClient { + public: + explicit DBusEchoApp(MojoHandle shell_handle) : Application(shell_handle) { + InterfacePipe<EchoService, AnyInterface> echo_pipe; + mojo::AllocationScope scope; + shell()->Connect("dbus:org.chromium.EchoService/org/chromium/MojoImpl", + echo_pipe.handle_to_peer.Pass()); + echo_service_.reset(echo_pipe.handle_to_self.Pass(), this); + echo_service_->Echo("who", base::Bind(&DBusEchoApp::OnEcho, + base::Unretained(this))); + } + + virtual ~DBusEchoApp() { + } + + virtual void OnCreated() MOJO_OVERRIDE { + } + + virtual void OnDestroyed() MOJO_OVERRIDE { + RunLoop::current()->Quit(); + } + + private: + void OnEcho(const mojo::String& echoed) { + LOG(INFO) << "echo'd " << echoed.To<std::string>(); + } + + RemotePtr<EchoService> echo_service_; +}; + +} // namespace examples +} // namespace mojo + +extern "C" DBUS_ECHO_APP_EXPORT MojoResult CDECL MojoMain( + MojoHandle shell_handle) { + mojo::Environment env; + mojo::RunLoop loop; + + mojo::examples::DBusEchoApp app(shell_handle); + loop.Run(); + return MOJO_RESULT_OK; +} |