| 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" | |
| 15 #include "base/timer.h" | |
| 16 #include "base/win/scoped_handle.h" | 12 #include "base/win/scoped_handle.h" |
| 17 #include "base/win/object_watcher.h" | |
| 18 #include "ipc/ipc_channel.h" | |
| 19 #include "remoting/base/stoppable.h" | |
| 20 | 13 |
| 21 namespace base { | 14 namespace base { |
| 22 class SingleThreadTaskRunner; | 15 class SingleThreadTaskRunner; |
| 23 } // namespace base | 16 } // namespace base |
| 24 | 17 |
| 25 namespace IPC { | 18 namespace IPC { |
| 26 class ChannelProxy; | |
| 27 class Message; | 19 class Message; |
| 28 } // namespace IPC | 20 } // namespace IPC |
| 29 | 21 |
| 30 namespace remoting { | 22 namespace remoting { |
| 31 | 23 |
| 32 class WorkerProcessIpcDelegate; | 24 class WorkerProcessIpcDelegate; |
| 33 | 25 |
| 34 // Launches a worker process that is controlled via an IPC channel. All | 26 // 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() | 27 // interaction with the spawned process is through WorkerProcessIpcDelegate and |
| 36 // method. In case of error the channel is closed and the worker process is | 28 // Send() method. In case of error the channel is closed and the worker process |
| 37 // terminated. | 29 // is terminated. |
| 38 // | 30 class WorkerProcessLauncher { |
| 39 // WorkerProcessLauncher object is good for one process launch attempt only. | |
| 40 class WorkerProcessLauncher | |
| 41 : public Stoppable, | |
| 42 public base::win::ObjectWatcher::Delegate, | |
| 43 public IPC::Listener { | |
| 44 public: | 31 public: |
| 45 class Delegate { | 32 class Delegate { |
| 46 public: | 33 public: |
| 47 virtual ~Delegate(); | 34 virtual ~Delegate(); |
| 48 | 35 |
| 36 // Returns the exit code of the worker process. |
| 37 virtual DWORD GetExitCode() = 0; |
| 38 |
| 39 // Terminates the worker process with the given exit code. |
| 40 virtual void KillProcess(DWORD exit_code) = 0; |
| 41 |
| 49 // Starts the worker process and passes |channel_name| to it. | 42 // Starts the worker process and passes |channel_name| to it. |
| 50 // |process_exit_event_out| receives a handle that becomes signalled once | 43 // |process_exit_event_out| receives a handle that becomes signalled once |
| 51 // the launched process has been terminated. | 44 // the launched process has been terminated. |
| 52 virtual bool DoLaunchProcess( | 45 virtual bool LaunchProcess( |
| 53 const std::string& channel_name, | 46 const std::string& channel_name, |
| 54 base::win::ScopedHandle* process_exit_event_out) = 0; | 47 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 }; | 48 }; |
| 59 | 49 |
| 60 // Creates the launcher that will use |launcher_delegate| to manage the worker | 50 // Creates the launcher that will use |launcher_delegate| to manage the worker |
| 61 // process and |worker_delegate| to handle IPCs. | 51 // process and |worker_delegate| to handle IPCs. The caller must ensure that |
| 52 // |worker_delegate| remains valid until Stoppable::Stop() method has been |
| 53 // called. |
| 62 // | 54 // |
| 63 // |stopped_callback| and |main_task_runner| are passed to the underlying | 55 // The caller should call all the methods on this class on |
| 64 // |Stoppable| implementation. The caller should call all the methods on this | 56 // the |caller_task_runner| thread. Methods of both delegate interfaces are |
| 65 // class on the |main_task_runner| thread. |ipc_task_runner| is used to | 57 // called on the |caller_task_runner| thread as well. |io_task_runner| is used |
| 66 // perform background IPC I/O. | 58 // to perform background IPC I/O. |
| 67 WorkerProcessLauncher( | 59 WorkerProcessLauncher( |
| 68 Delegate* launcher_delegate, | 60 scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner, |
| 61 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner, |
| 62 scoped_ptr<Delegate> launcher_delegate, |
| 69 WorkerProcessIpcDelegate* worker_delegate, | 63 WorkerProcessIpcDelegate* worker_delegate, |
| 70 const base::Closure& stopped_callback, | 64 const std::string& pipe_security_descriptor); |
| 71 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner, | 65 ~WorkerProcessLauncher(); |
| 72 scoped_refptr<base::SingleThreadTaskRunner> ipc_task_runner); | |
| 73 virtual ~WorkerProcessLauncher(); | |
| 74 | |
| 75 // Starts the worker process. | |
| 76 void Start(const std::string& pipe_sddl); | |
| 77 | 66 |
| 78 // Sends an IPC message to the worker process. The message will be silently | 67 // 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 | 68 // dropped if Send() is called before Start() or after stutdown has been |
| 80 // initiated. | 69 // initiated. |
| 81 void Send(IPC::Message* message); | 70 void Send(IPC::Message* message); |
| 82 | 71 |
| 83 // base::win::ObjectWatcher::Delegate implementation. | |
| 84 virtual void OnObjectSignaled(HANDLE object) OVERRIDE; | |
| 85 | |
| 86 // IPC::Listener implementation. | |
| 87 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; | |
| 88 virtual void OnChannelConnected(int32 peer_pid) OVERRIDE; | |
| 89 virtual void OnChannelError() OVERRIDE; | |
| 90 | |
| 91 protected: | |
| 92 // Stoppable implementation. | |
| 93 virtual void DoStop() OVERRIDE; | |
| 94 | |
| 95 private: | 72 private: |
| 96 // Creates the server end of the Chromoting IPC channel. | 73 // The actual implementation resides in WorkerProcessLauncher::Core class. |
| 97 bool CreatePipeForIpcChannel(const std::string& channel_name, | 74 class Core; |
| 98 const std::string& pipe_sddl, | 75 scoped_refptr<Core> core_; |
| 99 base::win::ScopedHandle* pipe_out); | |
| 100 | |
| 101 // Generates random channel ID. | |
| 102 std::string GenerateRandomChannelId(); | |
| 103 | |
| 104 Delegate* launcher_delegate_; | |
| 105 WorkerProcessIpcDelegate* worker_delegate_; | |
| 106 | |
| 107 // The main service message loop. | |
| 108 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_; | |
| 109 | |
| 110 // Message loop used by the IPC channel. | |
| 111 scoped_refptr<base::SingleThreadTaskRunner> ipc_task_runner_; | |
| 112 | |
| 113 // Used to determine when the launched process terminates. | |
| 114 base::win::ObjectWatcher process_watcher_; | |
| 115 | |
| 116 // A waiting handle that becomes signalled once the launched process has | |
| 117 // been terminated. | |
| 118 base::win::ScopedHandle process_exit_event_; | |
| 119 | |
| 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. | |
| 128 base::win::ScopedHandle pipe_; | |
| 129 | 76 |
| 130 DISALLOW_COPY_AND_ASSIGN(WorkerProcessLauncher); | 77 DISALLOW_COPY_AND_ASSIGN(WorkerProcessLauncher); |
| 131 }; | 78 }; |
| 132 | 79 |
| 133 } // namespace remoting | 80 } // namespace remoting |
| 134 | 81 |
| 135 #endif // REMOTING_HOST_WIN_WORKER_PROCESS_LAUNCHER_H_ | 82 #endif // REMOTING_HOST_WIN_WORKER_PROCESS_LAUNCHER_H_ |
| OLD | NEW |