summaryrefslogtreecommitdiffstats
path: root/mojo/shell/dbus_service_loader_linux.cc
blob: 64854968937f41be54e4d2bb120dcff06b8101a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// 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 "mojo/shell/dbus_service_loader_linux.h"

#include <string>

#include "base/command_line.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/task_runner_util.h"
#include "base/threading/thread_restrictions.h"
#include "dbus/bus.h"
#include "dbus/file_descriptor.h"
#include "dbus/message.h"
#include "dbus/object_path.h"
#include "dbus/object_proxy.h"
#include "mojo/common/channel_init.h"
#include "mojo/dbus/dbus_external_service.h"
#include "mojo/embedder/platform_channel_pair.h"
#include "mojo/public/cpp/bindings/allocation_scope.h"
#include "mojo/public/cpp/bindings/interface.h"
#include "mojo/public/cpp/bindings/remote_ptr.h"
#include "mojo/shell/context.h"
#include "mojo/shell/external_service.mojom.h"
#include "mojo/shell/keep_alive.h"

namespace mojo {
namespace shell {

// Manages the connection to a single externally-running service.
class DBusServiceLoader::LoadContext : public mojo::ExternalServiceHost {
 public:
  // Kicks off the attempt to bootstrap a connection to the externally-running
  // service specified by url_.
  // Creates a MessagePipe and passes one end over DBus to the service. Then,
  // calls ExternalService::Activate(ShellHandle) over the now-shared pipe.
  LoadContext(DBusServiceLoader* loader,
              const scoped_refptr<dbus::Bus>& bus,
              const GURL& url,
              ScopedShellHandle shell_handle)
      : loader_(loader),
        bus_(bus),
        service_dbus_proxy_(NULL),
        url_(url),
        shell_handle_(shell_handle.Pass()),
        keep_alive_(loader->context_) {
    base::PostTaskAndReplyWithResult(
        loader_->context_->task_runners()->io_runner(),
        FROM_HERE,
        base::Bind(&LoadContext::CreateChannelOnIOThread,
                   base::Unretained(this)),
        base::Bind(&LoadContext::ConnectChannel, base::Unretained(this)));
  }

  virtual ~LoadContext() {
  }

 private:
  // Sets up a pipe to share with the externally-running service and returns
  // the endpoint that should be sent over DBus.
  // The FD for the endpoint must be validated on an IO thread.
  scoped_ptr<dbus::FileDescriptor> CreateChannelOnIOThread() {
    base::ThreadRestrictions::AssertIOAllowed();
    CHECK(bus_->Connect());
    CHECK(bus_->SetUpAsyncOperations());

    embedder::PlatformChannelPair channel_pair;
    channel_init_.reset(new common::ChannelInit);
    mojo::ScopedMessagePipeHandle bootstrap_message_pipe =
        channel_init_->Init(channel_pair.PassServerHandle().release().fd,
                            loader_->context_->task_runners()->io_runner());
    CHECK(bootstrap_message_pipe.is_valid());

    external_service_.reset(
        mojo::ScopedExternalServiceHandle::From(bootstrap_message_pipe.Pass()),
        this);

    scoped_ptr<dbus::FileDescriptor> client_fd(new dbus::FileDescriptor);
    client_fd->PutValue(channel_pair.PassClientHandle().release().fd);
    client_fd->CheckValidity();  // Must be run on an IO thread.
    return client_fd.Pass();
  }

  // Sends client_fd over to the externally-running service. If that
  // attempt is successful, the service will then be "activated" by
  // sending it a ShellHandle.
  void ConnectChannel(scoped_ptr<dbus::FileDescriptor> client_fd) {
    size_t first_slash = url_.path().find_first_of('/');
    DCHECK_NE(first_slash, std::string::npos);

    const std::string service_name = url_.path().substr(0, first_slash);
    const std::string object_path =  url_.path().substr(first_slash);
    service_dbus_proxy_ =
        bus_->GetObjectProxy(service_name, dbus::ObjectPath(object_path));

    dbus::MethodCall call(kMojoDBusInterface, kMojoDBusConnectMethod);
    dbus::MessageWriter writer(&call);
    writer.AppendFileDescriptor(*client_fd.get());

    // TODO(cmasone): handle errors!
    service_dbus_proxy_->CallMethod(
        &call,
        dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
        base::Bind(&LoadContext::ActivateService, base::Unretained(this)));
  }

  // Sends a ShellHandle over to the now-connected externally-running service,
  // using the Mojo ExternalService API.
  void ActivateService(dbus::Response* response) {
    mojo::AllocationScope scope;
    external_service_->Activate(
        mojo::ScopedMessagePipeHandle(
            mojo::MessagePipeHandle(shell_handle_.release().value())));
  }

  // Should the ExternalService disappear completely, destroy connection state.
  // NB: This triggers off of the service disappearing from
  // DBus. Perhaps there's a way to watch at the Mojo layer instead,
  // and that would be superior?
  void HandleNameOwnerChanged(const std::string& old_owner,
                              const std::string& new_owner) {
    DCHECK(loader_->context_->task_runners()->ui_runner()->
           BelongsToCurrentThread());

    if (new_owner.empty()) {
      loader_->context_->task_runners()->ui_runner()->PostTask(
          FROM_HERE,
          base::Bind(&DBusServiceLoader::ForgetService,
                     base::Unretained(loader_), url_));
    }
  }

  DBusServiceLoader* const loader_;
  scoped_refptr<dbus::Bus> bus_;
  dbus::ObjectProxy* service_dbus_proxy_;  // Owned by bus_;
  const GURL url_;
  ScopedShellHandle shell_handle_;
  KeepAlive keep_alive_;
  scoped_ptr<common::ChannelInit> channel_init_;
  mojo::RemotePtr<mojo::ExternalService> external_service_;

  DISALLOW_COPY_AND_ASSIGN(LoadContext);
};

DBusServiceLoader::DBusServiceLoader(Context* context) : context_(context) {
  dbus::Bus::Options options;
  options.bus_type = dbus::Bus::SESSION;
  options.dbus_task_runner = context_->task_runners()->io_runner();
  bus_ = new dbus::Bus(options);
}

DBusServiceLoader::~DBusServiceLoader() {
  DCHECK(url_to_load_context_.empty());
}

void DBusServiceLoader::LoadService(ServiceManager* manager,
                                    const GURL& url,
                                    ScopedShellHandle service_handle) {
  DCHECK(url.SchemeIs("dbus"));
  DCHECK(url_to_load_context_.find(url) == url_to_load_context_.end());
  url_to_load_context_[url] =
      new LoadContext(this, bus_, url, service_handle.Pass());
}

void DBusServiceLoader::OnServiceError(ServiceManager* manager,
                                       const GURL& url) {
  // TODO(cmasone): Anything at all in this method here.
}

void DBusServiceLoader::ForgetService(const GURL& url) {
  DCHECK(context_->task_runners()->ui_runner()->BelongsToCurrentThread());
  DVLOG(2) << "Forgetting service (url: " << url << ")";

  LoadContextMap::iterator it = url_to_load_context_.find(url);
  DCHECK(it != url_to_load_context_.end()) << url;

  LoadContext* doomed = it->second;
  url_to_load_context_.erase(it);

  delete doomed;
}

}  // namespace shell
}  // namespace mojo