| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/runner/child/runner_connection.h" | 5 #include "mojo/shell/runner/child/runner_connection.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 65 Unblocker GetUnblocker() { return Unblocker(this); } | 65 Unblocker GetUnblocker() { return Unblocker(this); } |
| 66 | 66 |
| 67 private: | 67 private: |
| 68 base::WaitableEvent event_; | 68 base::WaitableEvent event_; |
| 69 base::Closure run_after_; | 69 base::Closure run_after_; |
| 70 | 70 |
| 71 DISALLOW_COPY_AND_ASSIGN(Blocker); | 71 DISALLOW_COPY_AND_ASSIGN(Blocker); |
| 72 }; | 72 }; |
| 73 | 73 |
| 74 using GotApplicationRequestCallback = | 74 using GotApplicationRequestCallback = |
| 75 base::Callback<void(InterfaceRequest<mojom::Application>)>; | 75 base::Callback<void(InterfaceRequest<mojom::ShellClient>)>; |
| 76 | 76 |
| 77 void OnCreateMessagePipe(ScopedMessagePipeHandle* result, | 77 void OnCreateMessagePipe(ScopedMessagePipeHandle* result, |
| 78 Blocker::Unblocker unblocker, | 78 Blocker::Unblocker unblocker, |
| 79 ScopedMessagePipeHandle pipe) { | 79 ScopedMessagePipeHandle pipe) { |
| 80 *result = std::move(pipe); | 80 *result = std::move(pipe); |
| 81 unblocker.Unblock(base::Bind(&base::DoNothing)); | 81 unblocker.Unblock(base::Bind(&base::DoNothing)); |
| 82 } | 82 } |
| 83 | 83 |
| 84 void OnGotApplicationRequest(InterfaceRequest<mojom::Application>* out_request, | 84 void OnGotApplicationRequest(InterfaceRequest<mojom::ShellClient>* out_request, |
| 85 InterfaceRequest<mojom::Application> request) { | 85 InterfaceRequest<mojom::ShellClient> request) { |
| 86 *out_request = std::move(request); | 86 *out_request = std::move(request); |
| 87 } | 87 } |
| 88 | 88 |
| 89 class ChildControllerImpl; | 89 class ChildControllerImpl; |
| 90 | 90 |
| 91 class RunnerConnectionImpl : public RunnerConnection { | 91 class RunnerConnectionImpl : public RunnerConnection { |
| 92 public: | 92 public: |
| 93 RunnerConnectionImpl() : controller_thread_("controller_thread") { | 93 RunnerConnectionImpl() : controller_thread_("controller_thread") { |
| 94 StartControllerThread(); | 94 StartControllerThread(); |
| 95 } | 95 } |
| 96 ~RunnerConnectionImpl() override { | 96 ~RunnerConnectionImpl() override { |
| 97 controller_runner_->PostTask( | 97 controller_runner_->PostTask( |
| 98 FROM_HERE, base::Bind(&RunnerConnectionImpl::ShutdownOnControllerThread, | 98 FROM_HERE, base::Bind(&RunnerConnectionImpl::ShutdownOnControllerThread, |
| 99 base::Unretained(this))); | 99 base::Unretained(this))); |
| 100 controller_thread_.Stop(); | 100 controller_thread_.Stop(); |
| 101 } | 101 } |
| 102 | 102 |
| 103 // Returns true if a connection to the runner has been established and | 103 // Returns true if a connection to the runner has been established and |
| 104 // |request| has been modified, false if no connection was established. | 104 // |request| has been modified, false if no connection was established. |
| 105 bool WaitForApplicationRequest(InterfaceRequest<mojom::Application>* request, | 105 bool WaitForApplicationRequest(InterfaceRequest<mojom::ShellClient>* request, |
| 106 ScopedMessagePipeHandle handle); | 106 ScopedMessagePipeHandle handle); |
| 107 | 107 |
| 108 ChildControllerImpl* controller() const { return controller_.get(); } | 108 ChildControllerImpl* controller() const { return controller_.get(); } |
| 109 | 109 |
| 110 void set_controller(scoped_ptr<ChildControllerImpl> controller) { | 110 void set_controller(scoped_ptr<ChildControllerImpl> controller) { |
| 111 controller_ = std::move(controller); | 111 controller_ = std::move(controller); |
| 112 } | 112 } |
| 113 | 113 |
| 114 private: | 114 private: |
| 115 void StartControllerThread() { | 115 void StartControllerThread() { |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 166 } | 166 } |
| 167 | 167 |
| 168 void OnConnectionError() { | 168 void OnConnectionError() { |
| 169 // A connection error means the connection to the shell is lost. This is not | 169 // A connection error means the connection to the shell is lost. This is not |
| 170 // recoverable. | 170 // recoverable. |
| 171 DLOG(ERROR) << "Connection error to the shell."; | 171 DLOG(ERROR) << "Connection error to the shell."; |
| 172 _exit(1); | 172 _exit(1); |
| 173 } | 173 } |
| 174 | 174 |
| 175 // |mojom::ChildController| methods: | 175 // |mojom::ChildController| methods: |
| 176 void StartApp(InterfaceRequest<mojom::Application> application_request, | 176 void StartApp(InterfaceRequest<mojom::ShellClient> request, |
| 177 const StartAppCallback& on_app_complete) override { | 177 const StartAppCallback& on_app_complete) override { |
| 178 DCHECK(thread_checker_.CalledOnValidThread()); | 178 DCHECK(thread_checker_.CalledOnValidThread()); |
| 179 | 179 |
| 180 on_app_complete_ = on_app_complete; | 180 on_app_complete_ = on_app_complete; |
| 181 unblocker_.Unblock( | 181 unblocker_.Unblock( |
| 182 base::Bind(&ChildControllerImpl::ReturnApplicationRequestOnMainThread, | 182 base::Bind(&ChildControllerImpl::ReturnApplicationRequestOnMainThread, |
| 183 callback_, base::Passed(&application_request))); | 183 callback_, base::Passed(&request))); |
| 184 } | 184 } |
| 185 | 185 |
| 186 void ExitNow(int32_t exit_code) override { | 186 void ExitNow(int32_t exit_code) override { |
| 187 DVLOG(2) << "ChildControllerImpl::ExitNow(" << exit_code << ")"; | 187 DVLOG(2) << "ChildControllerImpl::ExitNow(" << exit_code << ")"; |
| 188 _exit(exit_code); | 188 _exit(exit_code); |
| 189 } | 189 } |
| 190 | 190 |
| 191 private: | 191 private: |
| 192 ChildControllerImpl(RunnerConnectionImpl* connection, | 192 ChildControllerImpl(RunnerConnectionImpl* connection, |
| 193 const GotApplicationRequestCallback& callback, | 193 const GotApplicationRequestCallback& callback, |
| 194 const Blocker::Unblocker& unblocker) | 194 const Blocker::Unblocker& unblocker) |
| 195 : connection_(connection), | 195 : connection_(connection), |
| 196 callback_(callback), | 196 callback_(callback), |
| 197 unblocker_(unblocker), | 197 unblocker_(unblocker), |
| 198 binding_(this) {} | 198 binding_(this) {} |
| 199 | 199 |
| 200 static void ReturnApplicationRequestOnMainThread( | 200 static void ReturnApplicationRequestOnMainThread( |
| 201 const GotApplicationRequestCallback& callback, | 201 const GotApplicationRequestCallback& callback, |
| 202 InterfaceRequest<mojom::Application> application_request) { | 202 InterfaceRequest<mojom::ShellClient> request) { |
| 203 callback.Run(std::move(application_request)); | 203 callback.Run(std::move(request)); |
| 204 } | 204 } |
| 205 | 205 |
| 206 base::ThreadChecker thread_checker_; | 206 base::ThreadChecker thread_checker_; |
| 207 RunnerConnectionImpl* const connection_; | 207 RunnerConnectionImpl* const connection_; |
| 208 GotApplicationRequestCallback callback_; | 208 GotApplicationRequestCallback callback_; |
| 209 Blocker::Unblocker unblocker_; | 209 Blocker::Unblocker unblocker_; |
| 210 StartAppCallback on_app_complete_; | 210 StartAppCallback on_app_complete_; |
| 211 | 211 |
| 212 Binding<ChildController> binding_; | 212 Binding<ChildController> binding_; |
| 213 | 213 |
| 214 DISALLOW_COPY_AND_ASSIGN(ChildControllerImpl); | 214 DISALLOW_COPY_AND_ASSIGN(ChildControllerImpl); |
| 215 }; | 215 }; |
| 216 | 216 |
| 217 bool RunnerConnectionImpl::WaitForApplicationRequest( | 217 bool RunnerConnectionImpl::WaitForApplicationRequest( |
| 218 InterfaceRequest<mojom::Application>* request, | 218 InterfaceRequest<mojom::ShellClient>* request, |
| 219 ScopedMessagePipeHandle handle) { | 219 ScopedMessagePipeHandle handle) { |
| 220 // If a valid message pipe to the runner was not provided, look for one on the | 220 // If a valid message pipe to the runner was not provided, look for one on the |
| 221 // command line. | 221 // command line. |
| 222 if (!handle.is_valid()) { | 222 if (!handle.is_valid()) { |
| 223 edk::ScopedPlatformHandle platform_channel = | 223 edk::ScopedPlatformHandle platform_channel = |
| 224 edk::PlatformChannelPair::PassClientHandleFromParentProcess( | 224 edk::PlatformChannelPair::PassClientHandleFromParentProcess( |
| 225 *base::CommandLine::ForCurrentProcess()); | 225 *base::CommandLine::ForCurrentProcess()); |
| 226 if (!platform_channel.is_valid()) | 226 if (!platform_channel.is_valid()) |
| 227 return false; | 227 return false; |
| 228 edk::SetParentPipeHandle(std::move(platform_channel)); | 228 edk::SetParentPipeHandle(std::move(platform_channel)); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 248 | 248 |
| 249 return true; | 249 return true; |
| 250 } | 250 } |
| 251 | 251 |
| 252 } // namespace | 252 } // namespace |
| 253 | 253 |
| 254 RunnerConnection::~RunnerConnection() {} | 254 RunnerConnection::~RunnerConnection() {} |
| 255 | 255 |
| 256 // static | 256 // static |
| 257 RunnerConnection* RunnerConnection::ConnectToRunner( | 257 RunnerConnection* RunnerConnection::ConnectToRunner( |
| 258 InterfaceRequest<mojom::Application>* request, | 258 InterfaceRequest<mojom::ShellClient>* request, |
| 259 ScopedMessagePipeHandle handle) { | 259 ScopedMessagePipeHandle handle) { |
| 260 RunnerConnectionImpl* connection = new RunnerConnectionImpl; | 260 RunnerConnectionImpl* connection = new RunnerConnectionImpl; |
| 261 if (!connection->WaitForApplicationRequest(request, std::move(handle))) { | 261 if (!connection->WaitForApplicationRequest(request, std::move(handle))) { |
| 262 delete connection; | 262 delete connection; |
| 263 return nullptr; | 263 return nullptr; |
| 264 } | 264 } |
| 265 return connection; | 265 return connection; |
| 266 } | 266 } |
| 267 | 267 |
| 268 RunnerConnection::RunnerConnection() {} | 268 RunnerConnection::RunnerConnection() {} |
| 269 | 269 |
| 270 } // namespace shell | 270 } // namespace shell |
| 271 } // namespace mojo | 271 } // namespace mojo |
| OLD | NEW |