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

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: Rebase to switch MojoChannelInit to ChannelInit Created 6 years, 7 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
« no previous file with comments | « mojo/shell/dbus_service_loader_linux.h ('k') | mojo/shell/external_service.mojom » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/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::ChannelInit);
70 mojo::ScopedMessagePipeHandle bootstrap_message_pipe =
71 channel_init_->Init(channel_pair.PassServerHandle().release().fd,
72 loader_->context_->task_runners()->io_runner());
73 CHECK(bootstrap_message_pipe.is_valid());
74
75 external_service_.reset(
76 mojo::ScopedExternalServiceHandle::From(bootstrap_message_pipe.Pass()),
77 this);
78
79 scoped_ptr<dbus::FileDescriptor> client_fd(new dbus::FileDescriptor);
80 client_fd->PutValue(channel_pair.PassClientHandle().release().fd);
81 client_fd->CheckValidity(); // Must be run on an IO thread.
82 return client_fd.Pass();
83 }
84
85 // Sends client_fd over to the externally-running service. If that
86 // attempt is successful, the service will then be "activated" by
87 // sending it a ShellHandle.
88 void ConnectChannel(scoped_ptr<dbus::FileDescriptor> client_fd) {
89 size_t first_slash = url_.path().find_first_of('/');
90 DCHECK_NE(first_slash, std::string::npos);
91
92 const std::string service_name = url_.path().substr(0, first_slash);
93 const std::string object_path = url_.path().substr(first_slash);
94 service_dbus_proxy_ =
95 bus_->GetObjectProxy(service_name, dbus::ObjectPath(object_path));
96
97 dbus::MethodCall call("org.chromium.Mojo", "ConnectChannel");
98 dbus::MessageWriter writer(&call);
99 writer.AppendFileDescriptor(*client_fd.get());
100
101 // TODO(cmasone): handle errors!
102 service_dbus_proxy_->CallMethod(
103 &call,
104 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
105 base::Bind(&LoadContext::ActivateService, base::Unretained(this)));
106 }
107
108 // Sends a ShellHandle over to the now-connected externally-running service,
109 // using the Mojo ExternalService API.
110 void ActivateService(dbus::Response* response) {
111 mojo::AllocationScope scope;
112 external_service_->Activate(
113 mojo::ScopedMessagePipeHandle(
114 mojo::MessagePipeHandle(shell_handle_.release().value())));
115 }
116
117 // Should the ExternalService disappear completely, destroy connection state.
118 // NB: This triggers off of the service disappearing from
119 // DBus. Perhaps there's a way to watch at the Mojo layer instead,
120 // and that would be superior?
121 void HandleNameOwnerChanged(const std::string& old_owner,
122 const std::string& new_owner) {
123 DCHECK(loader_->context_->task_runners()->ui_runner()->
124 BelongsToCurrentThread());
125
126 if (new_owner.empty()) {
127 loader_->context_->task_runners()->ui_runner()->PostTask(
128 FROM_HERE,
129 base::Bind(&DBusServiceLoader::ForgetService,
130 base::Unretained(loader_), url_));
131 }
132 }
133
134 DBusServiceLoader* const loader_;
135 scoped_refptr<dbus::Bus> bus_;
136 dbus::ObjectProxy* service_dbus_proxy_; // Owned by bus_;
137 const GURL url_;
138 ScopedShellHandle shell_handle_;
139 KeepAlive keep_alive_;
140 scoped_ptr<common::ChannelInit> channel_init_;
141 mojo::RemotePtr<mojo::ExternalService> external_service_;
142
143 DISALLOW_COPY_AND_ASSIGN(LoadContext);
144 };
145
146 DBusServiceLoader::DBusServiceLoader(Context* context) : context_(context) {
147 dbus::Bus::Options options;
148 options.bus_type = dbus::Bus::SESSION;
149 options.dbus_task_runner = context_->task_runners()->io_runner();
150 bus_ = new dbus::Bus(options);
151 }
152
153 DBusServiceLoader::~DBusServiceLoader() {
154 DCHECK(url_to_load_context_.empty());
155 }
156
157 void DBusServiceLoader::LoadService(ServiceManager* manager,
158 const GURL& url,
159 ScopedShellHandle service_handle) {
160 DCHECK(url.SchemeIs("dbus"));
161 DCHECK(url_to_load_context_.find(url) == url_to_load_context_.end());
162 url_to_load_context_[url] =
163 new LoadContext(this, bus_, url, service_handle.Pass());
164 }
165
166 void DBusServiceLoader::OnServiceError(ServiceManager* manager,
167 const GURL& url) {
168 // TODO(cmasone): Anything at all in this method here.
169 }
170
171 void DBusServiceLoader::ForgetService(const GURL& url) {
172 DCHECK(context_->task_runners()->ui_runner()->BelongsToCurrentThread());
173 DVLOG(2) << "Forgetting service (url: " << url << ")";
174
175 LoadContextMap::iterator it = url_to_load_context_.find(url);
176 DCHECK(it != url_to_load_context_.end()) << url;
177
178 LoadContext* doomed = it->second;
179 url_to_load_context_.erase(it);
180
181 delete doomed;
182 }
183
184 } // namespace shell
185 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/shell/dbus_service_loader_linux.h ('k') | mojo/shell/external_service.mojom » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698