1 // Copyright 2014 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.
5 #ifndef MOJO_SHELL_DBUS_SERVICE_LOADER_H_
6 #define MOJO_SHELL_DBUS_SERVICE_LOADER_H_
10 #include "base/macros.h"
11 #include "base/memory/ref_counted.h"
12 #include "mojo/public/cpp/system/core.h"
13 #include "mojo/service_manager/service_loader.h"
14 #include "mojo/shell/keep_alive.h"
26 // An implementation of ServiceLoader that contacts a system service
27 // and bootstraps a Mojo connection to it over DBus.
29 // In order to allow the externally-running service to accept connections from
30 // a Mojo shell, we need to get it a ShellHandle. This class creates a
31 // dedicated MessagePipe, passes a handle to one end to the desired service
32 // over DBus, and then passes the ShellHandle over that pipe.
34 // This class assumes the following:
35 // 1) Your service is already running.
36 // 2) Your service implements the Mojo ExternalService API
37 // (from external_service.mojom).
38 // 3) Your service exports an object that implements the org.chromium.Mojo DBus
40 // <interface name="org.chromium.Mojo">
41 // <method name="ConnectChannel">
42 // <arg type="h" name="file_descriptor" direction="in" />
45 class DBusServiceLoader
: public ServiceLoader
{
47 DBusServiceLoader(Context
* context
);
48 virtual ~DBusServiceLoader();
50 // URL for DBus services are of the following format:
51 // dbus:tld.domain.ServiceName/path/to/DBusObject
53 // This is simply the scheme (dbus:) and then the DBus service name followed
54 // by the DBus object path of an object that implements the org.chromium.Mojo
55 // interface as discussed above.
58 // dbus:org.chromium.EchoService/org/chromium/MojoImpl
60 // This will tell DBusServiceLoader to reach out to a service with
61 // the name "org.chromium.EchoService" and invoke the method
62 // "org.chromium.Mojo.ConnectChannel" on the object exported at
63 // "/org/chromium/MojoImpl".
64 virtual void LoadService(ServiceManager
* manager
,
66 ScopedMessagePipeHandle shell_handle
) OVERRIDE
;
68 virtual void OnServiceError(ServiceManager
* manager
, const GURL
& url
)
74 // Tosses out connection-related state to service at given URL.
75 void ForgetService(const GURL
& url
);
77 Context
* const context_
;
78 scoped_refptr
<dbus::Bus
> bus_
;
80 typedef std::map
<GURL
, LoadContext
*> LoadContextMap
;
81 LoadContextMap url_to_load_context_
;
83 DISALLOW_COPY_AND_ASSIGN(DBusServiceLoader
);
89 #endif // MOJO_SHELL_DBUS_SERVICE_LOADER_H_