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_APPLICATION_LOADER_LINUX_H_
6#define MOJO_SHELL_DBUS_APPLICATION_LOADER_LINUX_H_
7
8#include <map>
9
10#include "base/macros.h"
11#include "base/memory/ref_counted.h"
12#include "mojo/application_manager/application_loader.h"
13#include "mojo/public/cpp/system/core.h"
14#include "url/gurl.h"
15
16namespace dbus {
17class Bus;
18}  // namespace dbus
19
20namespace mojo {
21namespace shell {
22
23class Context;
24
25// An implementation of ApplicationLoader that contacts a system service
26// and bootstraps a Mojo connection to it over DBus.
27//
28// In order to allow the externally-running service to accept connections from
29// a Mojo shell, we need to get it a ShellHandle. This class creates a
30// dedicated MessagePipe, passes a handle to one end to the desired service
31// over DBus, and then passes the ShellHandle over that pipe.
32//
33// This class assumes the following:
34// 1) Your service is already running.
35// 2) Your service implements the Mojo ExternalService API
36//    (from external_service.mojom).
37// 3) Your service exports an object that implements the org.chromium.Mojo DBus
38//    interface:
39//    <interface name="org.chromium.Mojo">
40//      <method name="ConnectChannel">
41//        <arg type="h" name="file_descriptor" direction="in" />
42//      </method>
43//    </interface>
44class DBusApplicationLoader : public ApplicationLoader {
45 public:
46  DBusApplicationLoader(Context* context);
47  virtual ~DBusApplicationLoader();
48
49  // URL for DBus services are of the following format:
50  // dbus:tld.domain.ServiceName/path/to/DBusObject
51  //
52  // This is simply the scheme (dbus:) and then the DBus service name followed
53  // by the DBus object path of an object that implements the org.chromium.Mojo
54  // interface as discussed above.
55  //
56  // Example:
57  //   dbus:org.chromium.EchoService/org/chromium/MojoImpl
58  //
59  // This will tell DBusApplicationLoader to reach out to a service with
60  // the name "org.chromium.EchoService" and invoke the method
61  // "org.chromium.Mojo.ConnectChannel" on the object exported at
62  // "/org/chromium/MojoImpl".
63  virtual void Load(ApplicationManager* manager,
64                    const GURL& url,
65                    scoped_refptr<LoadCallbacks> callbacks) OVERRIDE;
66
67  virtual void OnApplicationError(ApplicationManager* manager,
68                                  const GURL& url) OVERRIDE;
69
70 private:
71  class LoadContext;
72
73  // Tosses out connection-related state to service at given URL.
74  void ForgetService(const GURL& url);
75
76  Context* const context_;
77  scoped_refptr<dbus::Bus> bus_;
78
79  typedef std::map<GURL, LoadContext*> LoadContextMap;
80  LoadContextMap url_to_load_context_;
81
82  DISALLOW_COPY_AND_ASSIGN(DBusApplicationLoader);
83};
84
85}  // namespace shell
86}  // namespace mojo
87
88#endif  // MOJO_SHELL_DBUS_APPLICATION_LOADER_LINUX_H_
89