| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 // This file implements the Windows service controlling Me2Me host processes | 5 // This file implements the Windows service controlling Me2Me host processes |
| 6 // running within user sessions. | 6 // running within user sessions. |
| 7 | 7 |
| 8 #include "remoting/host/win/wts_session_process_delegate.h" | 8 #include "remoting/host/win/wts_session_process_delegate.h" |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 147 scoped_ptr<base::CommandLine> target_command, | 147 scoped_ptr<base::CommandLine> target_command, |
| 148 bool launch_elevated, | 148 bool launch_elevated, |
| 149 const std::string& channel_security) | 149 const std::string& channel_security) |
| 150 : caller_task_runner_(base::ThreadTaskRunnerHandle::Get()), | 150 : caller_task_runner_(base::ThreadTaskRunnerHandle::Get()), |
| 151 io_task_runner_(io_task_runner), | 151 io_task_runner_(io_task_runner), |
| 152 channel_security_(channel_security), | 152 channel_security_(channel_security), |
| 153 event_handler_(nullptr), | 153 event_handler_(nullptr), |
| 154 get_named_pipe_client_pid_(nullptr), | 154 get_named_pipe_client_pid_(nullptr), |
| 155 launch_elevated_(launch_elevated), | 155 launch_elevated_(launch_elevated), |
| 156 launch_pending_(false), | 156 launch_pending_(false), |
| 157 target_command_(target_command.Pass()) { | 157 target_command_(std::move(target_command)) {} |
| 158 } | |
| 159 | 158 |
| 160 bool WtsSessionProcessDelegate::Core::Initialize(uint32 session_id) { | 159 bool WtsSessionProcessDelegate::Core::Initialize(uint32 session_id) { |
| 161 DCHECK(caller_task_runner_->BelongsToCurrentThread()); | 160 DCHECK(caller_task_runner_->BelongsToCurrentThread()); |
| 162 | 161 |
| 163 // Windows XP does not support elevation. | 162 // Windows XP does not support elevation. |
| 164 if (base::win::GetVersion() < base::win::VERSION_VISTA) | 163 if (base::win::GetVersion() < base::win::VERSION_VISTA) |
| 165 launch_elevated_ = false; | 164 launch_elevated_ = false; |
| 166 | 165 |
| 167 if (launch_elevated_) { | 166 if (launch_elevated_) { |
| 168 // GetNamedPipeClientProcessId() is available starting from Vista. | 167 // GetNamedPipeClientProcessId() is available starting from Vista. |
| (...skipping 24 matching lines...) Expand all Loading... |
| 193 JobObjectExtendedLimitInformation, | 192 JobObjectExtendedLimitInformation, |
| 194 &info, | 193 &info, |
| 195 sizeof(info))) { | 194 sizeof(info))) { |
| 196 PLOG(ERROR) << "Failed to set limits on the job object"; | 195 PLOG(ERROR) << "Failed to set limits on the job object"; |
| 197 return false; | 196 return false; |
| 198 } | 197 } |
| 199 | 198 |
| 200 // ScopedHandle is not compatible with base::Passed, so we wrap it to | 199 // ScopedHandle is not compatible with base::Passed, so we wrap it to |
| 201 // a scoped pointer. | 200 // a scoped pointer. |
| 202 scoped_ptr<ScopedHandle> job_wrapper(new ScopedHandle()); | 201 scoped_ptr<ScopedHandle> job_wrapper(new ScopedHandle()); |
| 203 *job_wrapper = job.Pass(); | 202 *job_wrapper = std::move(job); |
| 204 | 203 |
| 205 // To receive job object notifications the job object is registered with | 204 // To receive job object notifications the job object is registered with |
| 206 // the completion port represented by |io_task_runner|. The registration has | 205 // the completion port represented by |io_task_runner|. The registration has |
| 207 // to be done on the I/O thread because | 206 // to be done on the I/O thread because |
| 208 // MessageLoopForIO::RegisterJobObject() can only be called via | 207 // MessageLoopForIO::RegisterJobObject() can only be called via |
| 209 // MessageLoopForIO::current(). | 208 // MessageLoopForIO::current(). |
| 210 io_task_runner_->PostTask( | 209 io_task_runner_->PostTask( |
| 211 FROM_HERE, | 210 FROM_HERE, |
| 212 base::Bind(&Core::InitializeJob, this, base::Passed(&job_wrapper))); | 211 base::Bind(&Core::InitializeJob, this, base::Passed(&job_wrapper))); |
| 213 } | 212 } |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 323 | 322 |
| 324 DWORD desired_access = | 323 DWORD desired_access = |
| 325 SYNCHRONIZE | PROCESS_DUP_HANDLE | PROCESS_QUERY_INFORMATION; | 324 SYNCHRONIZE | PROCESS_DUP_HANDLE | PROCESS_QUERY_INFORMATION; |
| 326 ScopedHandle worker_process(OpenProcess(desired_access, false, pid)); | 325 ScopedHandle worker_process(OpenProcess(desired_access, false, pid)); |
| 327 if (!worker_process.IsValid()) { | 326 if (!worker_process.IsValid()) { |
| 328 PLOG(ERROR) << "Failed to open process " << pid; | 327 PLOG(ERROR) << "Failed to open process " << pid; |
| 329 ReportFatalError(); | 328 ReportFatalError(); |
| 330 return; | 329 return; |
| 331 } | 330 } |
| 332 | 331 |
| 333 ReportProcessLaunched(worker_process.Pass()); | 332 ReportProcessLaunched(std::move(worker_process)); |
| 334 } | 333 } |
| 335 | 334 |
| 336 if (event_handler_) | 335 if (event_handler_) |
| 337 event_handler_->OnChannelConnected(peer_pid); | 336 event_handler_->OnChannelConnected(peer_pid); |
| 338 } | 337 } |
| 339 | 338 |
| 340 void WtsSessionProcessDelegate::Core::OnChannelError() { | 339 void WtsSessionProcessDelegate::Core::OnChannelError() { |
| 341 DCHECK(caller_task_runner_->BelongsToCurrentThread()); | 340 DCHECK(caller_task_runner_->BelongsToCurrentThread()); |
| 342 | 341 |
| 343 event_handler_->OnChannelError(); | 342 event_handler_->OnChannelError(); |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 414 return; | 413 return; |
| 415 } | 414 } |
| 416 } | 415 } |
| 417 | 416 |
| 418 if (!ResumeThread(worker_thread.Get())) { | 417 if (!ResumeThread(worker_thread.Get())) { |
| 419 PLOG(ERROR) << "Failed to resume the worker thread"; | 418 PLOG(ERROR) << "Failed to resume the worker thread"; |
| 420 ReportFatalError(); | 419 ReportFatalError(); |
| 421 return; | 420 return; |
| 422 } | 421 } |
| 423 | 422 |
| 424 channel_ = channel.Pass(); | 423 channel_ = std::move(channel); |
| 425 pipe_ = pipe.Pass(); | 424 pipe_ = std::move(pipe); |
| 426 | 425 |
| 427 // Report success if the worker process is lauched directly. Otherwise, PID of | 426 // Report success if the worker process is lauched directly. Otherwise, PID of |
| 428 // the client connected to the pipe will be used later. See | 427 // the client connected to the pipe will be used later. See |
| 429 // OnChannelConnected(). | 428 // OnChannelConnected(). |
| 430 if (!launch_elevated_) | 429 if (!launch_elevated_) |
| 431 ReportProcessLaunched(worker_process.Pass()); | 430 ReportProcessLaunched(std::move(worker_process)); |
| 432 } | 431 } |
| 433 | 432 |
| 434 void WtsSessionProcessDelegate::Core::DrainJobNotifications() { | 433 void WtsSessionProcessDelegate::Core::DrainJobNotifications() { |
| 435 DCHECK(io_task_runner_->BelongsToCurrentThread()); | 434 DCHECK(io_task_runner_->BelongsToCurrentThread()); |
| 436 | 435 |
| 437 // DrainJobNotifications() is posted after the job object is destroyed, so | 436 // DrainJobNotifications() is posted after the job object is destroyed, so |
| 438 // by this time all notifications from the job object have been processed | 437 // by this time all notifications from the job object have been processed |
| 439 // already. Let the main thread know that the queue has been drained. | 438 // already. Let the main thread know that the queue has been drained. |
| 440 caller_task_runner_->PostTask(FROM_HERE, base::Bind( | 439 caller_task_runner_->PostTask(FROM_HERE, base::Bind( |
| 441 &Core::DrainJobNotificationsCompleted, this)); | 440 &Core::DrainJobNotificationsCompleted, this)); |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 499 WorkerProcessLauncher* event_handler = event_handler_; | 498 WorkerProcessLauncher* event_handler = event_handler_; |
| 500 event_handler_ = nullptr; | 499 event_handler_ = nullptr; |
| 501 event_handler->OnFatalError(); | 500 event_handler->OnFatalError(); |
| 502 } | 501 } |
| 503 | 502 |
| 504 void WtsSessionProcessDelegate::Core::ReportProcessLaunched( | 503 void WtsSessionProcessDelegate::Core::ReportProcessLaunched( |
| 505 base::win::ScopedHandle worker_process) { | 504 base::win::ScopedHandle worker_process) { |
| 506 DCHECK(caller_task_runner_->BelongsToCurrentThread()); | 505 DCHECK(caller_task_runner_->BelongsToCurrentThread()); |
| 507 DCHECK(!worker_process_.IsValid()); | 506 DCHECK(!worker_process_.IsValid()); |
| 508 | 507 |
| 509 worker_process_ = worker_process.Pass(); | 508 worker_process_ = std::move(worker_process); |
| 510 | 509 |
| 511 // Report a handle that can be used to wait for the worker process completion, | 510 // Report a handle that can be used to wait for the worker process completion, |
| 512 // query information about the process and duplicate handles. | 511 // query information about the process and duplicate handles. |
| 513 DWORD desired_access = | 512 DWORD desired_access = |
| 514 SYNCHRONIZE | PROCESS_DUP_HANDLE | PROCESS_QUERY_INFORMATION; | 513 SYNCHRONIZE | PROCESS_DUP_HANDLE | PROCESS_QUERY_INFORMATION; |
| 515 HANDLE temp_handle; | 514 HANDLE temp_handle; |
| 516 if (!DuplicateHandle(GetCurrentProcess(), | 515 if (!DuplicateHandle(GetCurrentProcess(), worker_process_.Get(), |
| 517 worker_process_.Get(), | 516 GetCurrentProcess(), &temp_handle, desired_access, FALSE, |
| 518 GetCurrentProcess(), | |
| 519 &temp_handle, | |
| 520 desired_access, | |
| 521 FALSE, | |
| 522 0)) { | 517 0)) { |
| 523 PLOG(ERROR) << "Failed to duplicate a handle"; | 518 PLOG(ERROR) << "Failed to duplicate a handle"; |
| 524 ReportFatalError(); | 519 ReportFatalError(); |
| 525 return; | 520 return; |
| 526 } | 521 } |
| 527 ScopedHandle limited_handle(temp_handle); | 522 ScopedHandle limited_handle(temp_handle); |
| 528 | 523 |
| 529 event_handler_->OnProcessLaunched(limited_handle.Pass()); | 524 event_handler_->OnProcessLaunched(std::move(limited_handle)); |
| 530 } | 525 } |
| 531 | 526 |
| 532 WtsSessionProcessDelegate::WtsSessionProcessDelegate( | 527 WtsSessionProcessDelegate::WtsSessionProcessDelegate( |
| 533 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner, | 528 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner, |
| 534 scoped_ptr<base::CommandLine> target_command, | 529 scoped_ptr<base::CommandLine> target_command, |
| 535 bool launch_elevated, | 530 bool launch_elevated, |
| 536 const std::string& channel_security) { | 531 const std::string& channel_security) { |
| 537 core_ = new Core(io_task_runner, | 532 core_ = new Core(io_task_runner, std::move(target_command), launch_elevated, |
| 538 target_command.Pass(), | |
| 539 launch_elevated, | |
| 540 channel_security); | 533 channel_security); |
| 541 } | 534 } |
| 542 | 535 |
| 543 WtsSessionProcessDelegate::~WtsSessionProcessDelegate() { | 536 WtsSessionProcessDelegate::~WtsSessionProcessDelegate() { |
| 544 core_->Stop(); | 537 core_->Stop(); |
| 545 } | 538 } |
| 546 | 539 |
| 547 bool WtsSessionProcessDelegate::Initialize(uint32 session_id) { | 540 bool WtsSessionProcessDelegate::Initialize(uint32 session_id) { |
| 548 return core_->Initialize(session_id); | 541 return core_->Initialize(session_id); |
| 549 } | 542 } |
| 550 | 543 |
| 551 void WtsSessionProcessDelegate::LaunchProcess( | 544 void WtsSessionProcessDelegate::LaunchProcess( |
| 552 WorkerProcessLauncher* event_handler) { | 545 WorkerProcessLauncher* event_handler) { |
| 553 core_->LaunchProcess(event_handler); | 546 core_->LaunchProcess(event_handler); |
| 554 } | 547 } |
| 555 | 548 |
| 556 void WtsSessionProcessDelegate::Send(IPC::Message* message) { | 549 void WtsSessionProcessDelegate::Send(IPC::Message* message) { |
| 557 core_->Send(message); | 550 core_->Send(message); |
| 558 } | 551 } |
| 559 | 552 |
| 560 void WtsSessionProcessDelegate::CloseChannel() { | 553 void WtsSessionProcessDelegate::CloseChannel() { |
| 561 core_->CloseChannel(); | 554 core_->CloseChannel(); |
| 562 } | 555 } |
| 563 | 556 |
| 564 void WtsSessionProcessDelegate::KillProcess() { | 557 void WtsSessionProcessDelegate::KillProcess() { |
| 565 core_->KillProcess(); | 558 core_->KillProcess(); |
| 566 } | 559 } |
| 567 | 560 |
| 568 } // namespace remoting | 561 } // namespace remoting |
| OLD | NEW |