• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #ifndef MOJO_SHELL_DBUS_SERVICE_LOADER_H_
6 #define MOJO_SHELL_DBUS_SERVICE_LOADER_H_
7 
8 #include <map>
9 
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"
15 #include "url/gurl.h"
16 
17 namespace dbus {
18 class Bus;
19 }  // namespace dbus
20 
21 namespace mojo {
22 namespace shell {
23 
24 class Context;
25 
26 // An implementation of ServiceLoader that contacts a system service
27 // and bootstraps a Mojo connection to it over DBus.
28 //
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.
33 //
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
39 //    interface:
40 //    <interface name="org.chromium.Mojo">
41 //      <method name="ConnectChannel">
42 //        <arg type="h" name="file_descriptor" direction="in" />
43 //      </method>
44 //    </interface>
45 class DBusServiceLoader : public ServiceLoader {
46  public:
47   DBusServiceLoader(Context* context);
48   virtual ~DBusServiceLoader();
49 
50   // URL for DBus services are of the following format:
51   // dbus:tld.domain.ServiceName/path/to/DBusObject
52   //
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.
56   //
57   // Example:
58   //   dbus:org.chromium.EchoService/org/chromium/MojoImpl
59   //
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,
65                            const GURL& url,
66                            ScopedMessagePipeHandle service_handle) OVERRIDE;
67 
68   virtual void OnServiceError(ServiceManager* manager, const GURL& url)
69       OVERRIDE;
70 
71  private:
72   class LoadContext;
73 
74   // Tosses out connection-related state to service at given URL.
75   void ForgetService(const GURL& url);
76 
77   Context* const context_;
78   scoped_refptr<dbus::Bus> bus_;
79 
80   typedef std::map<GURL, LoadContext*> LoadContextMap;
81   LoadContextMap url_to_load_context_;
82 
83   DISALLOW_COPY_AND_ASSIGN(DBusServiceLoader);
84 };
85 
86 }  // namespace shell
87 }  // namespace mojo
88 
89 #endif  // MOJO_SHELL_DBUS_SERVICE_LOADER_H_
90