OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "mojo/shell/dbus_service_loader_linux.h" | 5 #include "mojo/shell/dbus_service_loader_linux.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/location.h" | 10 #include "base/location.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/task_runner_util.h" | 12 #include "base/task_runner_util.h" |
13 #include "base/threading/thread_restrictions.h" | 13 #include "base/threading/thread_restrictions.h" |
14 #include "dbus/bus.h" | 14 #include "dbus/bus.h" |
15 #include "dbus/file_descriptor.h" | 15 #include "dbus/file_descriptor.h" |
16 #include "dbus/message.h" | 16 #include "dbus/message.h" |
17 #include "dbus/object_path.h" | 17 #include "dbus/object_path.h" |
18 #include "dbus/object_proxy.h" | 18 #include "dbus/object_proxy.h" |
19 #include "mojo/common/channel_init.h" | 19 #include "mojo/common/channel_init.h" |
20 #include "mojo/dbus/dbus_external_service.h" | 20 #include "mojo/dbus/dbus_external_service.h" |
21 #include "mojo/embedder/platform_channel_pair.h" | 21 #include "mojo/embedder/platform_channel_pair.h" |
22 #include "mojo/public/cpp/bindings/allocation_scope.h" | 22 #include "mojo/public/cpp/bindings/allocation_scope.h" |
23 #include "mojo/public/cpp/bindings/interface.h" | |
24 #include "mojo/public/cpp/bindings/remote_ptr.h" | |
25 #include "mojo/shell/context.h" | 23 #include "mojo/shell/context.h" |
26 #include "mojo/shell/external_service.mojom.h" | 24 #include "mojo/shell/external_service.mojom.h" |
27 #include "mojo/shell/keep_alive.h" | 25 #include "mojo/shell/keep_alive.h" |
28 | 26 |
29 namespace mojo { | 27 namespace mojo { |
30 namespace shell { | 28 namespace shell { |
31 | 29 |
32 // Manages the connection to a single externally-running service. | 30 // Manages the connection to a single externally-running service. |
33 class DBusServiceLoader::LoadContext : public mojo::ExternalServiceHost { | 31 class DBusServiceLoader::LoadContext { |
34 public: | 32 public: |
35 // Kicks off the attempt to bootstrap a connection to the externally-running | 33 // Kicks off the attempt to bootstrap a connection to the externally-running |
36 // service specified by url_. | 34 // service specified by url_. |
37 // Creates a MessagePipe and passes one end over DBus to the service. Then, | 35 // Creates a MessagePipe and passes one end over DBus to the service. Then, |
38 // calls ExternalService::Activate(ShellHandle) over the now-shared pipe. | 36 // calls ExternalService::Activate(ShellHandle) over the now-shared pipe. |
39 LoadContext(DBusServiceLoader* loader, | 37 LoadContext(DBusServiceLoader* loader, |
40 const scoped_refptr<dbus::Bus>& bus, | 38 const scoped_refptr<dbus::Bus>& bus, |
41 const GURL& url, | 39 const GURL& url, |
42 ScopedShellHandle shell_handle) | 40 ScopedMessagePipeHandle shell_handle) |
43 : loader_(loader), | 41 : loader_(loader), |
44 bus_(bus), | 42 bus_(bus), |
45 service_dbus_proxy_(NULL), | 43 service_dbus_proxy_(NULL), |
46 url_(url), | 44 url_(url), |
47 shell_handle_(shell_handle.Pass()), | 45 shell_handle_(shell_handle.Pass()), |
48 keep_alive_(loader->context_) { | 46 keep_alive_(loader->context_) { |
49 base::PostTaskAndReplyWithResult( | 47 base::PostTaskAndReplyWithResult( |
50 loader_->context_->task_runners()->io_runner(), | 48 loader_->context_->task_runners()->io_runner(), |
51 FROM_HERE, | 49 FROM_HERE, |
52 base::Bind(&LoadContext::CreateChannelOnIOThread, | 50 base::Bind(&LoadContext::CreateChannelOnIOThread, |
(...skipping 13 matching lines...) Expand all Loading... |
66 CHECK(bus_->Connect()); | 64 CHECK(bus_->Connect()); |
67 CHECK(bus_->SetUpAsyncOperations()); | 65 CHECK(bus_->SetUpAsyncOperations()); |
68 | 66 |
69 embedder::PlatformChannelPair channel_pair; | 67 embedder::PlatformChannelPair channel_pair; |
70 channel_init_.reset(new common::ChannelInit); | 68 channel_init_.reset(new common::ChannelInit); |
71 mojo::ScopedMessagePipeHandle bootstrap_message_pipe = | 69 mojo::ScopedMessagePipeHandle bootstrap_message_pipe = |
72 channel_init_->Init(channel_pair.PassServerHandle().release().fd, | 70 channel_init_->Init(channel_pair.PassServerHandle().release().fd, |
73 loader_->context_->task_runners()->io_runner()); | 71 loader_->context_->task_runners()->io_runner()); |
74 CHECK(bootstrap_message_pipe.is_valid()); | 72 CHECK(bootstrap_message_pipe.is_valid()); |
75 | 73 |
76 external_service_.reset( | 74 external_service_.Bind(bootstrap_message_pipe.Pass()); |
77 mojo::ScopedExternalServiceHandle::From(bootstrap_message_pipe.Pass()), | |
78 this); | |
79 | 75 |
80 scoped_ptr<dbus::FileDescriptor> client_fd(new dbus::FileDescriptor); | 76 scoped_ptr<dbus::FileDescriptor> client_fd(new dbus::FileDescriptor); |
81 client_fd->PutValue(channel_pair.PassClientHandle().release().fd); | 77 client_fd->PutValue(channel_pair.PassClientHandle().release().fd); |
82 client_fd->CheckValidity(); // Must be run on an IO thread. | 78 client_fd->CheckValidity(); // Must be run on an IO thread. |
83 return client_fd.Pass(); | 79 return client_fd.Pass(); |
84 } | 80 } |
85 | 81 |
86 // Sends client_fd over to the externally-running service. If that | 82 // Sends client_fd over to the externally-running service. If that |
87 // attempt is successful, the service will then be "activated" by | 83 // attempt is successful, the service will then be "activated" by |
88 // sending it a ShellHandle. | 84 // sending it a ShellHandle. |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
129 FROM_HERE, | 125 FROM_HERE, |
130 base::Bind(&DBusServiceLoader::ForgetService, | 126 base::Bind(&DBusServiceLoader::ForgetService, |
131 base::Unretained(loader_), url_)); | 127 base::Unretained(loader_), url_)); |
132 } | 128 } |
133 } | 129 } |
134 | 130 |
135 DBusServiceLoader* const loader_; | 131 DBusServiceLoader* const loader_; |
136 scoped_refptr<dbus::Bus> bus_; | 132 scoped_refptr<dbus::Bus> bus_; |
137 dbus::ObjectProxy* service_dbus_proxy_; // Owned by bus_; | 133 dbus::ObjectProxy* service_dbus_proxy_; // Owned by bus_; |
138 const GURL url_; | 134 const GURL url_; |
139 ScopedShellHandle shell_handle_; | 135 ScopedMessagePipeHandle shell_handle_; |
140 KeepAlive keep_alive_; | 136 KeepAlive keep_alive_; |
141 scoped_ptr<common::ChannelInit> channel_init_; | 137 scoped_ptr<common::ChannelInit> channel_init_; |
142 mojo::RemotePtr<mojo::ExternalService> external_service_; | 138 ExternalServicePtr external_service_; |
143 | 139 |
144 DISALLOW_COPY_AND_ASSIGN(LoadContext); | 140 DISALLOW_COPY_AND_ASSIGN(LoadContext); |
145 }; | 141 }; |
146 | 142 |
147 DBusServiceLoader::DBusServiceLoader(Context* context) : context_(context) { | 143 DBusServiceLoader::DBusServiceLoader(Context* context) : context_(context) { |
148 dbus::Bus::Options options; | 144 dbus::Bus::Options options; |
149 options.bus_type = dbus::Bus::SESSION; | 145 options.bus_type = dbus::Bus::SESSION; |
150 options.dbus_task_runner = context_->task_runners()->io_runner(); | 146 options.dbus_task_runner = context_->task_runners()->io_runner(); |
151 bus_ = new dbus::Bus(options); | 147 bus_ = new dbus::Bus(options); |
152 } | 148 } |
153 | 149 |
154 DBusServiceLoader::~DBusServiceLoader() { | 150 DBusServiceLoader::~DBusServiceLoader() { |
155 DCHECK(url_to_load_context_.empty()); | 151 DCHECK(url_to_load_context_.empty()); |
156 } | 152 } |
157 | 153 |
158 void DBusServiceLoader::LoadService(ServiceManager* manager, | 154 void DBusServiceLoader::LoadService(ServiceManager* manager, |
159 const GURL& url, | 155 const GURL& url, |
160 ScopedShellHandle service_handle) { | 156 ScopedMessagePipeHandle service_handle) { |
161 DCHECK(url.SchemeIs("dbus")); | 157 DCHECK(url.SchemeIs("dbus")); |
162 DCHECK(url_to_load_context_.find(url) == url_to_load_context_.end()); | 158 DCHECK(url_to_load_context_.find(url) == url_to_load_context_.end()); |
163 url_to_load_context_[url] = | 159 url_to_load_context_[url] = |
164 new LoadContext(this, bus_, url, service_handle.Pass()); | 160 new LoadContext(this, bus_, url, service_handle.Pass()); |
165 } | 161 } |
166 | 162 |
167 void DBusServiceLoader::OnServiceError(ServiceManager* manager, | 163 void DBusServiceLoader::OnServiceError(ServiceManager* manager, |
168 const GURL& url) { | 164 const GURL& url) { |
169 // TODO(cmasone): Anything at all in this method here. | 165 // TODO(cmasone): Anything at all in this method here. |
170 } | 166 } |
171 | 167 |
172 void DBusServiceLoader::ForgetService(const GURL& url) { | 168 void DBusServiceLoader::ForgetService(const GURL& url) { |
173 DCHECK(context_->task_runners()->ui_runner()->BelongsToCurrentThread()); | 169 DCHECK(context_->task_runners()->ui_runner()->BelongsToCurrentThread()); |
174 DVLOG(2) << "Forgetting service (url: " << url << ")"; | 170 DVLOG(2) << "Forgetting service (url: " << url << ")"; |
175 | 171 |
176 LoadContextMap::iterator it = url_to_load_context_.find(url); | 172 LoadContextMap::iterator it = url_to_load_context_.find(url); |
177 DCHECK(it != url_to_load_context_.end()) << url; | 173 DCHECK(it != url_to_load_context_.end()) << url; |
178 | 174 |
179 LoadContext* doomed = it->second; | 175 LoadContext* doomed = it->second; |
180 url_to_load_context_.erase(it); | 176 url_to_load_context_.erase(it); |
181 | 177 |
182 delete doomed; | 178 delete doomed; |
183 } | 179 } |
184 | 180 |
185 } // namespace shell | 181 } // namespace shell |
186 } // namespace mojo | 182 } // namespace mojo |
OLD | NEW |