| 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 #include "remoting/host/win/worker_process_launcher.h" | 5 #include "remoting/host/win/worker_process_launcher.h" |
| 6 | 6 |
| 7 #include <windows.h> | 7 #include <windows.h> |
| 8 #include <sddl.h> | 8 #include <sddl.h> |
| 9 #include <limits> | 9 #include <limits> |
| 10 | 10 |
| 11 #include "base/base_switches.h" | 11 #include "base/base_switches.h" |
| 12 #include "base/bind.h" | 12 #include "base/bind.h" |
| 13 #include "base/bind_helpers.h" | 13 #include "base/bind_helpers.h" |
| 14 #include "base/logging.h" | 14 #include "base/logging.h" |
| 15 #include "base/single_thread_task_runner.h" | 15 #include "base/single_thread_task_runner.h" |
| 16 #include "base/process_util.h" | 16 #include "base/process_util.h" |
| 17 #include "base/rand_util.h" | 17 #include "base/rand_util.h" |
| 18 #include "base/stringprintf.h" | 18 #include "base/stringprintf.h" |
| 19 #include "base/utf_string_conversions.h" | 19 #include "base/utf_string_conversions.h" |
| 20 #include "base/win/scoped_handle.h" | 20 #include "base/win/scoped_handle.h" |
| 21 #include "ipc/ipc_channel_proxy.h" | 21 #include "ipc/ipc_channel_proxy.h" |
| 22 #include "ipc/ipc_message.h" | 22 #include "ipc/ipc_message.h" |
| 23 #include "remoting/host/worker_process_ipc_delegate.h" |
| 23 | 24 |
| 24 using base::win::ScopedHandle; | 25 using base::win::ScopedHandle; |
| 25 | 26 |
| 26 namespace { | 27 namespace { |
| 27 | 28 |
| 28 // Match the pipe name prefix used by Chrome IPC channels so that the client | 29 // Match the pipe name prefix used by Chrome IPC channels so that the client |
| 29 // could use Chrome IPC APIs instead of connecting manually. | 30 // could use Chrome IPC APIs instead of connecting manually. |
| 30 const char kChromePipeNamePrefix[] = "\\\\.\\pipe\\chrome."; | 31 const char kChromePipeNamePrefix[] = "\\\\.\\pipe\\chrome."; |
| 31 | 32 |
| 32 } // namespace | 33 } // namespace |
| 33 | 34 |
| 34 namespace remoting { | 35 namespace remoting { |
| 35 | 36 |
| 36 WorkerProcessLauncher::Delegate::~Delegate() { | 37 WorkerProcessLauncher::Delegate::~Delegate() { |
| 37 } | 38 } |
| 38 | 39 |
| 39 WorkerProcessLauncher::WorkerProcessLauncher( | 40 WorkerProcessLauncher::WorkerProcessLauncher( |
| 40 Delegate* delegate, | 41 Delegate* launcher_delegate, |
| 42 WorkerProcessIpcDelegate* worker_delegate, |
| 41 const base::Closure& stopped_callback, | 43 const base::Closure& stopped_callback, |
| 42 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner, | 44 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner, |
| 43 scoped_refptr<base::SingleThreadTaskRunner> ipc_task_runner) | 45 scoped_refptr<base::SingleThreadTaskRunner> ipc_task_runner) |
| 44 : Stoppable(main_task_runner, stopped_callback), | 46 : Stoppable(main_task_runner, stopped_callback), |
| 45 delegate_(delegate), | 47 launcher_delegate_(launcher_delegate), |
| 48 worker_delegate_(worker_delegate), |
| 46 main_task_runner_(main_task_runner), | 49 main_task_runner_(main_task_runner), |
| 47 ipc_task_runner_(ipc_task_runner) { | 50 ipc_task_runner_(ipc_task_runner) { |
| 48 } | 51 } |
| 49 | 52 |
| 50 WorkerProcessLauncher::~WorkerProcessLauncher() { | 53 WorkerProcessLauncher::~WorkerProcessLauncher() { |
| 51 DCHECK(main_task_runner_->BelongsToCurrentThread()); | 54 DCHECK(main_task_runner_->BelongsToCurrentThread()); |
| 52 } | 55 } |
| 53 | 56 |
| 54 void WorkerProcessLauncher::Start(const std::string& pipe_sddl) { | 57 void WorkerProcessLauncher::Start(const std::string& pipe_sddl) { |
| 55 DCHECK(main_task_runner_->BelongsToCurrentThread()); | 58 DCHECK(main_task_runner_->BelongsToCurrentThread()); |
| 56 DCHECK(ipc_channel_.get() == NULL); | 59 DCHECK(ipc_channel_.get() == NULL); |
| 57 DCHECK(!pipe_.IsValid()); | 60 DCHECK(!pipe_.IsValid()); |
| 58 DCHECK(!process_exit_event_.IsValid()); | 61 DCHECK(!process_exit_event_.IsValid()); |
| 59 DCHECK(process_watcher_.GetWatchedObject() == NULL); | 62 DCHECK(process_watcher_.GetWatchedObject() == NULL); |
| 60 | 63 |
| 61 std::string channel_name = GenerateRandomChannelId(); | 64 std::string channel_name = GenerateRandomChannelId(); |
| 62 if (CreatePipeForIpcChannel(channel_name, pipe_sddl, &pipe_)) { | 65 if (CreatePipeForIpcChannel(channel_name, pipe_sddl, &pipe_)) { |
| 63 // Wrap the pipe into an IPC channel. | 66 // Wrap the pipe into an IPC channel. |
| 64 ipc_channel_.reset(new IPC::ChannelProxy( | 67 ipc_channel_.reset(new IPC::ChannelProxy( |
| 65 IPC::ChannelHandle(pipe_), | 68 IPC::ChannelHandle(pipe_), |
| 66 IPC::Channel::MODE_SERVER, | 69 IPC::Channel::MODE_SERVER, |
| 67 this, | 70 this, |
| 68 ipc_task_runner_)); | 71 ipc_task_runner_)); |
| 69 | 72 |
| 70 // Launch the process and attach an object watcher to the returned process | 73 // Launch the process and attach an object watcher to the returned process |
| 71 // handle so that we get notified if the process terminates. | 74 // handle so that we get notified if the process terminates. |
| 72 if (delegate_->DoLaunchProcess(channel_name, &process_exit_event_)) { | 75 if (launcher_delegate_->DoLaunchProcess(channel_name, |
| 76 &process_exit_event_)) { |
| 73 if (process_watcher_.StartWatching(process_exit_event_, this)) { | 77 if (process_watcher_.StartWatching(process_exit_event_, this)) { |
| 74 return; | 78 return; |
| 75 } | 79 } |
| 76 | 80 |
| 77 delegate_->DoKillProcess(CONTROL_C_EXIT); | 81 launcher_delegate_->DoKillProcess(CONTROL_C_EXIT); |
| 78 } | 82 } |
| 79 } | 83 } |
| 80 | 84 |
| 81 Stop(); | 85 Stop(); |
| 82 } | 86 } |
| 83 | 87 |
| 84 void WorkerProcessLauncher::Send(IPC::Message* message) { | 88 void WorkerProcessLauncher::Send(IPC::Message* message) { |
| 85 DCHECK(main_task_runner_->BelongsToCurrentThread()); | 89 DCHECK(main_task_runner_->BelongsToCurrentThread()); |
| 86 | 90 |
| 87 ipc_channel_->Send(message); | 91 if (ipc_channel_.get()) { |
| 92 ipc_channel_->Send(message); |
| 93 } else { |
| 94 delete message; |
| 95 } |
| 88 } | 96 } |
| 89 | 97 |
| 90 void WorkerProcessLauncher::OnObjectSignaled(HANDLE object) { | 98 void WorkerProcessLauncher::OnObjectSignaled(HANDLE object) { |
| 91 DCHECK(main_task_runner_->BelongsToCurrentThread()); | 99 DCHECK(main_task_runner_->BelongsToCurrentThread()); |
| 92 DCHECK(process_watcher_.GetWatchedObject() == NULL); | 100 DCHECK(process_watcher_.GetWatchedObject() == NULL); |
| 93 | 101 |
| 94 Stop(); | 102 Stop(); |
| 95 } | 103 } |
| 96 | 104 |
| 97 bool WorkerProcessLauncher::OnMessageReceived(const IPC::Message& message) { | 105 bool WorkerProcessLauncher::OnMessageReceived(const IPC::Message& message) { |
| 98 DCHECK(main_task_runner_->BelongsToCurrentThread()); | 106 DCHECK(main_task_runner_->BelongsToCurrentThread()); |
| 99 DCHECK(ipc_channel_.get() != NULL); | 107 DCHECK(ipc_channel_.get() != NULL); |
| 100 DCHECK(pipe_.IsValid()); | 108 DCHECK(pipe_.IsValid()); |
| 101 DCHECK(process_exit_event_.IsValid()); | 109 DCHECK(process_exit_event_.IsValid()); |
| 102 | 110 |
| 103 return delegate_->OnMessageReceived(message); | 111 return worker_delegate_->OnMessageReceived(message); |
| 104 } | 112 } |
| 105 | 113 |
| 106 void WorkerProcessLauncher::OnChannelConnected(int32 peer_pid) { | 114 void WorkerProcessLauncher::OnChannelConnected(int32 peer_pid) { |
| 107 DCHECK(main_task_runner_->BelongsToCurrentThread()); | 115 DCHECK(main_task_runner_->BelongsToCurrentThread()); |
| 108 DCHECK(ipc_channel_.get() != NULL); | 116 DCHECK(ipc_channel_.get() != NULL); |
| 109 DCHECK(pipe_.IsValid()); | 117 DCHECK(pipe_.IsValid()); |
| 110 DCHECK(process_exit_event_.IsValid()); | 118 DCHECK(process_exit_event_.IsValid()); |
| 111 | 119 |
| 112 // |peer_pid| is send by the client and cannot be trusted. | 120 // |peer_pid| is send by the client and cannot be trusted. |
| 113 // GetNamedPipeClientProcessId() is not available on XP. The pipe's security | 121 // GetNamedPipeClientProcessId() is not available on XP. The pipe's security |
| 114 // descriptor is the only protection we currently have against malicious | 122 // descriptor is the only protection we currently have against malicious |
| 115 // clients. | 123 // clients. |
| 116 // | 124 // |
| 117 // If we'd like to be able to launch low-privileged workers and let them | 125 // If we'd like to be able to launch low-privileged workers and let them |
| 118 // connect back, the pipe handle should be passed to the worker instead of | 126 // connect back, the pipe handle should be passed to the worker instead of |
| 119 // the pipe name. | 127 // the pipe name. |
| 120 delegate_->OnChannelConnected(); | 128 worker_delegate_->OnChannelConnected(); |
| 121 } | 129 } |
| 122 | 130 |
| 123 void WorkerProcessLauncher::OnChannelError() { | 131 void WorkerProcessLauncher::OnChannelError() { |
| 124 DCHECK(main_task_runner_->BelongsToCurrentThread()); | 132 DCHECK(main_task_runner_->BelongsToCurrentThread()); |
| 125 DCHECK(ipc_channel_.get() != NULL); | 133 DCHECK(ipc_channel_.get() != NULL); |
| 126 DCHECK(pipe_.IsValid()); | 134 DCHECK(pipe_.IsValid()); |
| 127 DCHECK(process_exit_event_.IsValid()); | 135 DCHECK(process_exit_event_.IsValid()); |
| 128 | 136 |
| 129 Stop(); | 137 Stop(); |
| 130 } | 138 } |
| 131 | 139 |
| 132 void WorkerProcessLauncher::DoStop() { | 140 void WorkerProcessLauncher::DoStop() { |
| 133 DCHECK(main_task_runner_->BelongsToCurrentThread()); | 141 DCHECK(main_task_runner_->BelongsToCurrentThread()); |
| 134 | 142 |
| 135 ipc_channel_.reset(); | 143 ipc_channel_.reset(); |
| 136 pipe_.Close(); | 144 pipe_.Close(); |
| 137 | 145 |
| 138 // Kill the process if it has been started already. | 146 // Kill the process if it has been started already. |
| 139 if (process_watcher_.GetWatchedObject() != NULL) { | 147 if (process_watcher_.GetWatchedObject() != NULL) { |
| 140 delegate_->DoKillProcess(CONTROL_C_EXIT); | 148 launcher_delegate_->DoKillProcess(CONTROL_C_EXIT); |
| 141 return; | 149 return; |
| 142 } | 150 } |
| 143 | 151 |
| 144 DCHECK(ipc_channel_.get() == NULL); | 152 DCHECK(ipc_channel_.get() == NULL); |
| 145 DCHECK(!pipe_.IsValid()); | 153 DCHECK(!pipe_.IsValid()); |
| 146 DCHECK(process_watcher_.GetWatchedObject() == NULL); | 154 DCHECK(process_watcher_.GetWatchedObject() == NULL); |
| 147 | 155 |
| 148 process_exit_event_.Close(); | 156 process_exit_event_.Close(); |
| 149 CompleteStopping(); | 157 CompleteStopping(); |
| 150 } | 158 } |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 201 } | 209 } |
| 202 | 210 |
| 203 // N.B. Copied from src/content/common/child_process_host_impl.cc | 211 // N.B. Copied from src/content/common/child_process_host_impl.cc |
| 204 std::string WorkerProcessLauncher::GenerateRandomChannelId() { | 212 std::string WorkerProcessLauncher::GenerateRandomChannelId() { |
| 205 return base::StringPrintf("%d.%p.%d", | 213 return base::StringPrintf("%d.%p.%d", |
| 206 base::GetCurrentProcId(), this, | 214 base::GetCurrentProcId(), this, |
| 207 base::RandInt(0, std::numeric_limits<int>::max())); | 215 base::RandInt(0, std::numeric_limits<int>::max())); |
| 208 } | 216 } |
| 209 | 217 |
| 210 } // namespace remoting | 218 } // namespace remoting |
| OLD | NEW |