Chromium Code Reviews| 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 #ifndef REMOTING_HOST_WIN_WORKER_PROCESS_LAUNCHER_H_ | 5 #ifndef REMOTING_HOST_WIN_WORKER_PROCESS_LAUNCHER_H_ |
| 6 #define REMOTING_HOST_WIN_WORKER_PROCESS_LAUNCHER_H_ | 6 #define REMOTING_HOST_WIN_WORKER_PROCESS_LAUNCHER_H_ |
| 7 | 7 |
| 8 #include <windows.h> | |
| 9 | |
| 10 #include "base/basictypes.h" | 8 #include "base/basictypes.h" |
| 11 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
| 12 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
| 13 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/process.h" | 12 #include "base/time.h" |
| 15 #include "base/timer.h" | 13 #include "base/timer.h" |
| 16 #include "base/win/scoped_handle.h" | 14 #include "base/win/scoped_handle.h" |
| 17 #include "base/win/object_watcher.h" | 15 #include "base/win/object_watcher.h" |
| 18 #include "ipc/ipc_channel.h" | 16 #include "ipc/ipc_channel.h" |
| 19 #include "remoting/base/stoppable.h" | |
| 20 | 17 |
| 21 namespace base { | 18 namespace base { |
| 22 class SingleThreadTaskRunner; | 19 class SingleThreadTaskRunner; |
| 23 } // namespace base | 20 } // namespace base |
| 24 | 21 |
| 25 namespace IPC { | 22 namespace IPC { |
| 26 class ChannelProxy; | 23 class ChannelProxy; |
| 27 class Message; | 24 class Message; |
| 28 } // namespace IPC | 25 } // namespace IPC |
| 29 | 26 |
| 30 namespace remoting { | 27 namespace remoting { |
| 31 | 28 |
| 32 class WorkerProcessIpcDelegate; | 29 class WorkerProcessIpcDelegate; |
| 33 | 30 |
| 34 // Launches a worker process that is controlled via an IPC channel. All | 31 // Launches a worker process that is controlled via an IPC channel. All |
| 35 // interaction with the spawned process is through the IPC::Listener and Send() | 32 // interaction with the spawned process is through the IPC::Listener and Send() |
| 36 // method. In case of error the channel is closed and the worker process is | 33 // method. In case of error the channel is closed and the worker process is |
| 37 // terminated. | 34 // terminated. |
| 38 // | |
| 39 // WorkerProcessLauncher object is good for one process launch attempt only. | |
| 40 class WorkerProcessLauncher | 35 class WorkerProcessLauncher |
| 41 : public Stoppable, | 36 : public base::RefCountedThreadSafe<WorkerProcessLauncher>, |
|
Wez
2012/10/09 03:40:39
Ouch. Could this be a caller-owned class with a r
alexeypa (please no reviews)
2012/10/09 19:42:04
Done.
| |
| 42 public base::win::ObjectWatcher::Delegate, | 37 public base::win::ObjectWatcher::Delegate, |
| 43 public IPC::Listener { | 38 public IPC::Listener { |
| 44 public: | 39 public: |
| 45 class Delegate { | 40 class Delegate { |
| 46 public: | 41 public: |
| 47 virtual ~Delegate(); | 42 virtual ~Delegate(); |
| 48 | 43 |
| 44 // Returns the exit code of the worker process. | |
| 45 virtual DWORD GetExitCode() = 0; | |
| 46 | |
| 47 // Terminates the worker process with the given exit code. | |
| 48 virtual void KillProcess(DWORD exit_code) = 0; | |
| 49 | |
| 49 // Starts the worker process and passes |channel_name| to it. | 50 // Starts the worker process and passes |channel_name| to it. |
| 50 // |process_exit_event_out| receives a handle that becomes signalled once | 51 // |process_exit_event_out| receives a handle that becomes signalled once |
| 51 // the launched process has been terminated. | 52 // the launched process has been terminated. |
| 52 virtual bool DoLaunchProcess( | 53 virtual bool LaunchProcess( |
| 53 const std::string& channel_name, | 54 const std::string& channel_name, |
| 54 base::win::ScopedHandle* process_exit_event_out) = 0; | 55 base::win::ScopedHandle* process_exit_event_out) = 0; |
| 55 | |
| 56 // Terminates the worker process with the given exit code. | |
| 57 virtual void DoKillProcess(DWORD exit_code) = 0; | |
| 58 }; | 56 }; |
| 59 | 57 |
| 60 // Creates the launcher that will use |launcher_delegate| to manage the worker | 58 // Creates the launcher that will use |launcher_delegate| to manage the worker |
| 61 // process and |worker_delegate| to handle IPCs. | 59 // process and |worker_delegate| to handle IPCs. The caller must ensure that |
| 60 // |worker_delegate| remains valid until Stoppable::Stop() method has been | |
| 61 // called. | |
| 62 // | 62 // |
| 63 // |stopped_callback| and |main_task_runner| are passed to the underlying | 63 // The caller should call all the methods on this class on |
| 64 // |Stoppable| implementation. The caller should call all the methods on this | 64 // the |main_task_runner| thread. Methods of both delegate interfaces are |
| 65 // class on the |main_task_runner| thread. |ipc_task_runner| is used to | 65 // called on the |main_task_runner| thread as well. |io_task_runner| is used |
| 66 // perform background IPC I/O. | 66 // to perform background IPC I/O. |
|
Wez
2012/10/09 03:40:39
Could you grab |main_thread_task_runner| implicitl
alexeypa (please no reviews)
2012/10/09 19:42:04
I don't think so. ThreadTaskRunnerHandle::Get() wi
| |
| 67 WorkerProcessLauncher( | 67 WorkerProcessLauncher( |
| 68 Delegate* launcher_delegate, | 68 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner, |
| 69 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner, | |
| 70 scoped_ptr<Delegate> launcher_delegate, | |
| 69 WorkerProcessIpcDelegate* worker_delegate, | 71 WorkerProcessIpcDelegate* worker_delegate, |
| 70 const base::Closure& stopped_callback, | 72 const std::string& pipe_security_descriptor); |
| 71 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner, | |
| 72 scoped_refptr<base::SingleThreadTaskRunner> ipc_task_runner); | |
| 73 virtual ~WorkerProcessLauncher(); | |
| 74 | 73 |
| 75 // Starts the worker process. | 74 // Launches the worker process. |
| 76 void Start(const std::string& pipe_sddl); | 75 void Start(); |
| 76 | |
| 77 // Stops the worker process asynchronously. The caller can drop the reference | |
| 78 // to |this| as soon as Stop() returns. | |
| 79 void Stop(); | |
|
Wez
2012/10/09 03:40:39
nit: The callers seem to Stop() and immediately dr
alexeypa (please no reviews)
2012/10/09 19:42:04
Done.
| |
| 77 | 80 |
| 78 // Sends an IPC message to the worker process. The message will be silently | 81 // Sends an IPC message to the worker process. The message will be silently |
| 79 // dropped if Send() is called before Start() or after stutdown has been | 82 // dropped if Send() is called before Start() or after stutdown has been |
| 80 // initiated. | 83 // initiated. |
| 81 void Send(IPC::Message* message); | 84 void Send(IPC::Message* message); |
| 82 | 85 |
| 83 // base::win::ObjectWatcher::Delegate implementation. | 86 // base::win::ObjectWatcher::Delegate implementation. |
| 84 virtual void OnObjectSignaled(HANDLE object) OVERRIDE; | 87 virtual void OnObjectSignaled(HANDLE object) OVERRIDE; |
| 85 | 88 |
| 86 // IPC::Listener implementation. | 89 // IPC::Listener implementation. |
| 87 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; | 90 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; |
| 88 virtual void OnChannelConnected(int32 peer_pid) OVERRIDE; | 91 virtual void OnChannelConnected(int32 peer_pid) OVERRIDE; |
| 89 virtual void OnChannelError() OVERRIDE; | 92 virtual void OnChannelError() OVERRIDE; |
| 90 | 93 |
| 91 protected: | 94 private: |
| 92 // Stoppable implementation. | 95 friend class base::RefCountedThreadSafe<WorkerProcessLauncher>; |
| 93 virtual void DoStop() OVERRIDE; | 96 virtual ~WorkerProcessLauncher(); |
| 94 | 97 |
| 95 private: | |
| 96 // Creates the server end of the Chromoting IPC channel. | 98 // Creates the server end of the Chromoting IPC channel. |
| 97 bool CreatePipeForIpcChannel(const std::string& channel_name, | 99 bool CreatePipeForIpcChannel(const std::string& channel_name, |
| 98 const std::string& pipe_sddl, | 100 const std::string& pipe_security_descriptor, |
| 99 base::win::ScopedHandle* pipe_out); | 101 base::win::ScopedHandle* pipe_out); |
| 100 | 102 |
| 101 // Generates random channel ID. | 103 // Generates random channel ID. |
| 102 std::string GenerateRandomChannelId(); | 104 std::string GenerateRandomChannelId(); |
| 103 | 105 |
| 104 Delegate* launcher_delegate_; | 106 // Attempts to launch the worker process. Schedules next launch attempt if |
| 107 // creation of the process fails. | |
| 108 void LaunchWorker(); | |
| 109 | |
| 110 // Stops the worker process asynchronously and schedules next launch attempt | |
| 111 // unless Stop() has been called already. | |
| 112 void StopWorker(); | |
| 113 | |
| 114 // All public methods are called on the |main_task_runner| thread. | |
| 115 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_; | |
| 116 | |
| 117 // The task runner is used perform background IPC I/O. | |
| 118 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_; | |
| 119 | |
| 120 // Implements specifics of launching a worker process. | |
| 121 scoped_ptr<Delegate> launcher_delegate_; | |
| 122 | |
| 123 // Handles IPC messages sent by the worker process. | |
| 105 WorkerProcessIpcDelegate* worker_delegate_; | 124 WorkerProcessIpcDelegate* worker_delegate_; |
| 106 | 125 |
| 107 // The main service message loop. | 126 // The IPC channel connecting to the launched process. |
| 108 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_; | 127 scoped_ptr<IPC::ChannelProxy> ipc_channel_; |
| 109 | 128 |
| 110 // Message loop used by the IPC channel. | 129 // The timer used to delay termination of the process in the case of an IPC |
| 111 scoped_refptr<base::SingleThreadTaskRunner> ipc_task_runner_; | 130 // error. |
| 131 scoped_ptr<base::OneShotTimer<WorkerProcessLauncher> > ipc_error_timer_; | |
| 132 | |
| 133 // Time of the last launch attempt. | |
| 134 base::Time launch_time_; | |
| 135 | |
| 136 // Current backoff delay. | |
| 137 base::TimeDelta launch_backoff_; | |
| 138 | |
| 139 // Timer used to schedule the next attempt to launch the process. | |
| 140 scoped_ptr<base::OneShotTimer<WorkerProcessLauncher> > launch_timer_; | |
| 112 | 141 |
| 113 // Used to determine when the launched process terminates. | 142 // Used to determine when the launched process terminates. |
| 114 base::win::ObjectWatcher process_watcher_; | 143 base::win::ObjectWatcher process_watcher_; |
| 115 | 144 |
| 116 // A waiting handle that becomes signalled once the launched process has | 145 // A waiting handle that becomes signalled once the launched process has |
| 117 // been terminated. | 146 // been terminated. |
| 118 base::win::ScopedHandle process_exit_event_; | 147 base::win::ScopedHandle process_exit_event_; |
| 119 | 148 |
| 120 // The IPC channel connecting to the launched process. | |
| 121 scoped_ptr<IPC::ChannelProxy> ipc_channel_; | |
| 122 | |
| 123 // The timer used to delay termination of the process in the case of an IPC | |
| 124 // error. | |
| 125 base::OneShotTimer<WorkerProcessLauncher> ipc_error_timer_; | |
| 126 | |
| 127 // The server end of the pipe. | 149 // The server end of the pipe. |
| 128 base::win::ScopedHandle pipe_; | 150 base::win::ScopedHandle pipe_; |
| 129 | 151 |
| 152 // The security descriptor (as SDDL) of the server end of the pipe. | |
| 153 std::string pipe_security_descriptor_; | |
| 154 | |
| 155 // Self reference to keep the object alive while the worker process is being | |
| 156 // terminated. | |
| 157 scoped_refptr<WorkerProcessLauncher> self_; | |
| 158 | |
| 159 // True when Stop() has been called. | |
| 160 bool stopping_; | |
| 161 | |
| 130 DISALLOW_COPY_AND_ASSIGN(WorkerProcessLauncher); | 162 DISALLOW_COPY_AND_ASSIGN(WorkerProcessLauncher); |
| 131 }; | 163 }; |
| 132 | 164 |
| 133 } // namespace remoting | 165 } // namespace remoting |
| 134 | 166 |
| 135 #endif // REMOTING_HOST_WIN_WORKER_PROCESS_LAUNCHER_H_ | 167 #endif // REMOTING_HOST_WIN_WORKER_PROCESS_LAUNCHER_H_ |
| OLD | NEW |