Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(186)

Side by Side Diff: mojo/shell/dbus_service_loader_linux.cc

Issue 242763003: Implement a trivial Mojo EchoService that can be connected to over DBus (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Automatically exclude DBusServiceLoader on non-Linux Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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 #include "mojo/shell/dbus_service_loader_linux.h"
6
7 #include <string>
8
9 #include "base/command_line.h"
10 #include "base/location.h"
11 #include "base/logging.h"
12 #include "base/task_runner_util.h"
13 #include "base/threading/thread_restrictions.h"
14 #include "dbus/bus.h"
15 #include "dbus/file_descriptor.h"
16 #include "dbus/message.h"
17 #include "dbus/object_path.h"
18 #include "dbus/object_proxy.h"
19 #include "mojo/common/mojo_channel_init.h"
20 #include "mojo/embedder/platform_channel_pair.h"
21 #include "mojo/public/cpp/bindings/allocation_scope.h"
22 #include "mojo/public/cpp/bindings/interface.h"
23 #include "mojo/public/cpp/bindings/remote_ptr.h"
24 #include "mojo/shell/context.h"
25 #include "mojo/shell/external_service.mojom.h"
26 #include "mojo/shell/keep_alive.h"
27
28 namespace mojo {
29 namespace shell {
30
31 // Manages the connection to a single externally-running service.
32 class DBusServiceLoader::LoadContext : public mojo::ExternalServiceHost {
33 public:
34 // Kicks off the attempt to bootstrap a connection to the externally-running
35 // service specified by url_.
36 // Creates a MessagePipe and passes one end over DBus to the service. Then,
37 // calls ExternalService::Activate(ShellHandle) over the now-shared pipe.
38 LoadContext(DBusServiceLoader* loader,
39 const scoped_refptr<dbus::Bus>& bus,
40 const GURL& url,
41 ScopedShellHandle shell_handle)
42 : loader_(loader),
43 bus_(bus),
44 service_dbus_proxy_(NULL),
45 url_(url),
46 shell_handle_(shell_handle.Pass()),
47 keep_alive_(loader->context_) {
48 base::PostTaskAndReplyWithResult(
49 loader_->context_->task_runners()->io_runner(),
50 FROM_HERE,
51 base::Bind(&LoadContext::CreateChannelOnIOThread,
52 base::Unretained(this)),
53 base::Bind(&LoadContext::ConnectChannel, base::Unretained(this)));
54 }
55
56 virtual ~LoadContext() {
57 }
58
59 private:
60 // Sets up a pipe to share with the externally-running service and returns
61 // the endpoint that should be sent over DBus.
62 // The FD for the endpoint must be validated on an IO thread.
63 scoped_ptr<dbus::FileDescriptor> CreateChannelOnIOThread() {
64 base::ThreadRestrictions::AssertIOAllowed();
65 CHECK(bus_->Connect());
66 CHECK(bus_->SetUpAsyncOperations());
67
68 embedder::PlatformChannelPair channel_pair;
69 channel_init_.reset(new common::MojoChannelInit);
70 channel_init_->Init(
71 channel_pair.PassServerHandle().release().fd,
72 loader_->context_->task_runners()->io_runner());
73 CHECK(channel_init_->is_handle_valid());
74
75 scoped_ptr<dbus::FileDescriptor> client_fd(new dbus::FileDescriptor);
76 client_fd->PutValue(channel_pair.PassClientHandle().release().fd);
77 client_fd->CheckValidity(); // Must be run on an IO thread.
78 return client_fd.Pass();
79 }
80
81 // Sends client_fd over to the externally-running service. If that
82 // attempt is successful, the service will then be "activated" by
83 // sending it a ShellHandle.
84 void ConnectChannel(scoped_ptr<dbus::FileDescriptor> client_fd) {
85 size_t first_slash = url_.path().find_first_of('/');
86 if (first_slash == std::string::npos)
87 NOTREACHED();
viettrungluu 2014/04/23 20:07:24 nit: You may as well do a DCHECK_NE here.
Chris Masone 2014/04/23 22:31:00 Done.
88
89 const std::string service_name = url_.path().substr(0, first_slash);
90 const std::string object_path = url_.path().substr(first_slash);
91 service_dbus_proxy_ =
92 bus_->GetObjectProxy(service_name, dbus::ObjectPath(object_path));
93
94 dbus::MethodCall call("org.chromium.Mojo", "ConnectChannel");
95 dbus::MessageWriter writer(&call);
96 writer.AppendFileDescriptor(*client_fd.get());
97
98 // TODO(cmasone): handle errors!
99 service_dbus_proxy_->CallMethod(
100 &call,
101 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
102 base::Bind(&LoadContext::ActivateService, base::Unretained(this)));
103 }
104
105 // Sends a ShellHandle over to the now-connected externally-running service,
106 // using the Mojo ExternalService API.
107 void ActivateService(dbus::Response* response) {
108 mojo::ScopedExternalServiceHandle external_service_handle(
109 mojo::ExternalServiceHandle(
110 channel_init_->bootstrap_message_pipe().release().value()));
111 external_service_.reset(external_service_handle.Pass(), this);
112
113 mojo::AllocationScope scope;
114 external_service_->Activate(
115 mojo::ScopedMessagePipeHandle(
116 mojo::MessagePipeHandle(shell_handle_.release().value())));
117 }
118
119 // Should the ExternalService disappear completely, destroy connection state.
120 // NB: This triggers off of the service disappearing from
121 // DBus. Perhaps there's a way to watch at the Mojo layer instead,
122 // and that would be superior?
123 void HandleNameOwnerChanged(const std::string& old_owner,
124 const std::string& new_owner) {
125 DCHECK(loader_->context_->task_runners()->ui_runner()->
126 BelongsToCurrentThread());
127
128 if (new_owner.empty()) {
129 loader_->context_->task_runners()->ui_runner()->PostTask(
130 FROM_HERE,
131 base::Bind(&DBusServiceLoader::ForgetService,
132 base::Unretained(loader_), url_));
133 }
134 }
135
136 DBusServiceLoader* const loader_;
137 scoped_refptr<dbus::Bus> bus_;
138 dbus::ObjectProxy* service_dbus_proxy_; // Owned by bus_;
139 const GURL url_;
140 ScopedShellHandle shell_handle_;
141 KeepAlive keep_alive_;
142 scoped_ptr<common::MojoChannelInit> channel_init_;
143 mojo::RemotePtr<mojo::ExternalService> external_service_;
144
145 DISALLOW_COPY_AND_ASSIGN(LoadContext);
146 };
147
148 DBusServiceLoader::DBusServiceLoader(Context* context) : context_(context) {
149 dbus::Bus::Options options;
150 options.bus_type = dbus::Bus::SESSION;
151 options.dbus_task_runner = context_->task_runners()->io_runner();
152 bus_ = new dbus::Bus(options);
153 }
154
155 DBusServiceLoader::~DBusServiceLoader() {
156 DCHECK(url_to_load_context_.empty());
157 }
158
159 void DBusServiceLoader::LoadService(ServiceManager* manager,
160 const GURL& url,
161 ScopedShellHandle service_handle) {
162 DCHECK(url.SchemeIs("dbus"));
163 DCHECK(url_to_load_context_.find(url) == url_to_load_context_.end());
164 url_to_load_context_[url] =
165 new LoadContext(this, bus_, url, service_handle.Pass());
166 }
167
168 void DBusServiceLoader::OnServiceError(ServiceManager* manager,
169 const GURL& url) {
170 // TODO(cmasone): Anything at all in this method here.
171 }
172
173 void DBusServiceLoader::ForgetService(const GURL& url) {
174 DCHECK(context_->task_runners()->ui_runner()->BelongsToCurrentThread());
175 DVLOG(2) << "Forgetting service (url: " << url << ")";
176
177 LoadContextMap::iterator it = url_to_load_context_.find(url);
178 DCHECK(it != url_to_load_context_.end()) << url;
179
180 LoadContext* doomed = it->second;
181 url_to_load_context_.erase(it);
182
183 delete doomed;
184 }
185
186 } // namespace shell
187 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698