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