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 "base/basictypes.h" | 8 #include "base/basictypes.h" |
| 9 #include "base/callback.h" | |
| 9 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
| 10 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
| 11 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/threading/non_thread_safe.h" | |
| 14 #include "base/timer.h" | |
| 15 #include "base/win/object_watcher.h" | |
| 12 #include "base/win/scoped_handle.h" | 16 #include "base/win/scoped_handle.h" |
| 13 #include "ipc/ipc_sender.h" | 17 #include "net/base/backoff_entry.h" |
| 14 | 18 |
| 15 namespace base { | 19 namespace base { |
| 16 class SingleThreadTaskRunner; | 20 class SingleThreadTaskRunner; |
| 17 class TimeDelta; | 21 class TimeDelta; |
| 18 } // namespace base | 22 } // namespace base |
| 19 | 23 |
| 20 namespace IPC { | 24 namespace IPC { |
| 21 class Listener; | |
| 22 class Message; | 25 class Message; |
| 23 } // namespace IPC | 26 } // namespace IPC |
| 24 | 27 |
| 25 namespace tracked_objects { | 28 namespace tracked_objects { |
| 26 class Location; | 29 class Location; |
| 27 } // namespace tracked_objects | 30 } // namespace tracked_objects |
| 28 | 31 |
| 29 namespace remoting { | 32 namespace remoting { |
| 30 | 33 |
| 31 class WorkerProcessIpcDelegate; | 34 class WorkerProcessIpcDelegate; |
| 32 | 35 |
| 33 // Launches a worker process that is controlled via an IPC channel. All | 36 // Launches a worker process that is controlled via an IPC channel. All |
| 34 // interaction with the spawned process is through WorkerProcessIpcDelegate and | 37 // interaction with the spawned process is through WorkerProcessIpcDelegate and |
| 35 // Send() method. In case of error the channel is closed and the worker process | 38 // Send() method. In case of error the channel is closed and the worker process |
| 36 // is terminated. | 39 // is terminated. |
| 37 class WorkerProcessLauncher { | 40 class WorkerProcessLauncher |
| 41 : public base::NonThreadSafe, | |
| 42 public base::win::ObjectWatcher::Delegate { | |
| 38 public: | 43 public: |
| 39 class Delegate : public IPC::Sender { | 44 class Delegate { |
| 40 public: | 45 public: |
| 41 virtual ~Delegate(); | 46 virtual ~Delegate(); |
| 42 | 47 |
| 48 // Asynchronously starts the worker process and creates an IPC channel it | |
| 49 // can connect to. |event_handler| must remain valid until KillProcess() has | |
| 50 // been called. | |
| 51 virtual void LaunchProcess(WorkerProcessLauncher* event_handler) = 0; | |
| 52 | |
| 53 // Sends an IPC message to the worker process. The message will be silently | |
| 54 // dropped if the channel is closed. | |
| 55 virtual void Send(IPC::Message* message) = 0; | |
| 56 | |
| 43 // Closes the IPC channel. | 57 // Closes the IPC channel. |
| 44 virtual void CloseChannel() = 0; | 58 virtual void CloseChannel() = 0; |
| 45 | 59 |
| 46 // Returns PID of the worker process or 0 if it is not available. | 60 // Terminates the worker process and closes the IPC channel. |
| 47 virtual DWORD GetProcessId() const = 0; | 61 virtual void KillProcess() = 0; |
| 48 | |
| 49 // Returns true if the worker process should not be restarted any more. | |
| 50 virtual bool IsPermanentError(int failure_count) const = 0; | |
| 51 | |
| 52 // Terminates the worker process with the given exit code. Destroys the IPC | |
| 53 // channel created by LaunchProcess(). | |
| 54 virtual void KillProcess(DWORD exit_code) = 0; | |
| 55 | |
| 56 // Starts the worker process and creates an IPC channel it can connect to. | |
| 57 // |delegate| specifies the object that will receive notifications from | |
| 58 // the IPC channel. |process_exit_event_out| receives a handle that becomes | |
| 59 // signalled once the launched process has been terminated. | |
| 60 virtual bool LaunchProcess( | |
| 61 IPC::Listener* delegate, | |
| 62 base::win::ScopedHandle* process_exit_event_out) = 0; | |
| 63 }; | 62 }; |
| 64 | 63 |
| 65 // Creates the launcher that will use |launcher_delegate| to manage the worker | 64 // Creates the launcher that will use |launcher_delegate| to manage the worker |
| 66 // process and |worker_delegate| to handle IPCs. The caller must ensure that | 65 // process and |ipc_handler| to handle IPCs. The caller must ensure that |
| 67 // |worker_delegate| remains valid until Stoppable::Stop() method has been | 66 // |ipc_handler| must outlive this object. |
| 68 // called. | 67 WorkerProcessLauncher(scoped_ptr<Delegate> launcher_delegate, |
| 69 // | 68 WorkerProcessIpcDelegate* ipc_handler); |
| 70 // The caller should call all the methods on this class on | 69 virtual ~WorkerProcessLauncher(); |
| 71 // the |caller_task_runner| thread. Methods of both delegate interfaces are | |
| 72 // called on the |caller_task_runner| thread as well. | |
| 73 WorkerProcessLauncher( | |
| 74 scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner, | |
| 75 scoped_ptr<Delegate> launcher_delegate, | |
| 76 WorkerProcessIpcDelegate* worker_delegate); | |
| 77 ~WorkerProcessLauncher(); | |
| 78 | 70 |
| 79 // Asks the worker process to crash and generate a dump, and closes the IPC | 71 // Asks the worker process to crash and generate a dump, and closes the IPC |
| 80 // channel. |location| is passed to the worker so that it is on the stack in | 72 // channel. |location| is passed to the worker so that it is on the stack in |
| 81 // the dump. Restarts the worker process forcefully, if it does | 73 // the dump. Restarts the worker process forcefully, if it does |
| 82 // not exit on its own. | 74 // not exit on its own. |
| 83 void Crash(const tracked_objects::Location& location); | 75 void Crash(const tracked_objects::Location& location); |
| 84 | 76 |
| 85 // Sends an IPC message to the worker process. The message will be silently | 77 // Sends an IPC message to the worker process. The message will be silently |
| 86 // dropped if Send() is called before Start() or after stutdown has been | 78 // dropped if Send() is called before Start() or after stutdown has been |
| 87 // initiated. | 79 // initiated. |
| 88 void Send(IPC::Message* message); | 80 void Send(IPC::Message* message); |
| 89 | 81 |
| 82 // Notification methods invoked by |Delegate|. | |
| 83 | |
| 84 // Invoked to pass a handle of the launched process back to the caller of | |
| 85 // Delegate::LaunchProcess(). The delegate has to make sure that this method | |
| 86 // is called before OnChannelConnected(). | |
| 87 void OnProcessLaunched(base::win::ScopedHandle worker_process); | |
| 88 | |
| 89 // Called when a fatal error occurs (i.e. a failed process launch). | |
| 90 // The delegate must guarantee that no other notifications is delivered once | |
|
garykac
2013/05/15 23:24:30
s/is/are/
alexeypa (please no reviews)
2013/05/16 17:50:04
Done.
| |
| 91 // OnFatalError() has been called. | |
| 92 void OnFatalError(); | |
| 93 | |
| 94 // Mirrors methods of IPC::Listener to be invoked by |Delegate|. | |
| 95 bool OnMessageReceived(const IPC::Message& message); | |
| 96 void OnChannelConnected(int32 peer_pid); | |
| 97 void OnChannelError(); | |
| 98 | |
| 90 private: | 99 private: |
| 91 friend class WorkerProcessLauncherTest; | 100 friend class WorkerProcessLauncherTest; |
| 92 | 101 |
| 93 // Hooks that allow test code to call the corresponding methods of |Core|. | 102 // base::win::ObjectWatcher::Delegate implementation used to watch for |
| 94 void ResetLaunchSuccessTimeoutForTest(); | 103 // the worker process exiting. |
| 104 virtual void OnObjectSignaled(HANDLE object) OVERRIDE; | |
| 105 | |
| 106 // Returns true when the object is being destroyed. | |
| 107 bool stopping() const { return ipc_handler_ == NULL; } | |
| 108 | |
| 109 // Attempts to launch the worker process. Schedules next launch attempt if | |
| 110 // creation of the process fails. | |
| 111 void LaunchWorker(); | |
| 112 | |
| 113 // Called to record outcome of a launch attempt: success or failure. | |
| 114 void RecordLaunchResult(); | |
| 115 | |
| 116 // Called by the test to record a successful launch attempt. | |
| 117 void RecordSuccessfulLaunchForTest(); | |
| 118 | |
| 119 // Set the desired timeout for |kill_process_timer_|. | |
| 95 void SetKillProcessTimeoutForTest(const base::TimeDelta& timeout); | 120 void SetKillProcessTimeoutForTest(const base::TimeDelta& timeout); |
| 96 | 121 |
| 97 // The actual implementation resides in WorkerProcessLauncher::Core class. | 122 // Stops the worker process and schedules next launch attempt unless the |
| 98 class Core; | 123 // object is being destroyed already. |
| 99 scoped_refptr<Core> core_; | 124 void StopWorker(); |
| 125 | |
| 126 // Handles IPC messages sent by the worker process. | |
| 127 WorkerProcessIpcDelegate* ipc_handler_; | |
| 128 | |
| 129 // Implements specifics of launching a worker process. | |
| 130 scoped_ptr<WorkerProcessLauncher::Delegate> launcher_delegate_; | |
| 131 | |
| 132 // Keeps the exit code of the worker process after it was closed. The exit | |
| 133 // code is used to determine whether the process has to be restarted. | |
| 134 DWORD exit_code_; | |
| 135 | |
| 136 // True if IPC messages should be passed to |ipc_handler_|. | |
| 137 bool ipc_enabled_; | |
| 138 | |
| 139 // The timer used to delay termination of the worker process when an IPC error | |
| 140 // occured or when Crash() request is pending | |
| 141 base::OneShotTimer<WorkerProcessLauncher> kill_process_timer_; | |
| 142 | |
| 143 // The default timeout for |kill_process_timer_|. | |
| 144 base::TimeDelta kill_process_timeout_; | |
| 145 | |
| 146 // State used to backoff worker launch attempts on failure. | |
| 147 net::BackoffEntry launch_backoff_; | |
| 148 | |
| 149 // Timer used to schedule the next attempt to launch the process. | |
| 150 base::OneShotTimer<WorkerProcessLauncher> launch_timer_; | |
| 151 | |
| 152 // Monitors |worker_process_| to detect when the launched process | |
| 153 // terminates. | |
| 154 base::win::ObjectWatcher process_watcher_; | |
| 155 | |
| 156 // Timer used to detect whether a launch attempt was successful or not, and to | |
| 157 // cancel the launch attempt if it is taking too long. | |
| 158 base::OneShotTimer<WorkerProcessLauncher> launch_result_timer_; | |
| 159 | |
| 160 // The handle of the worker process, if launched. | |
| 161 base::win::ScopedHandle worker_process_; | |
| 100 | 162 |
| 101 DISALLOW_COPY_AND_ASSIGN(WorkerProcessLauncher); | 163 DISALLOW_COPY_AND_ASSIGN(WorkerProcessLauncher); |
| 102 }; | 164 }; |
| 103 | 165 |
| 104 } // namespace remoting | 166 } // namespace remoting |
| 105 | 167 |
| 106 #endif // REMOTING_HOST_WIN_WORKER_PROCESS_LAUNCHER_H_ | 168 #endif // REMOTING_HOST_WIN_WORKER_PROCESS_LAUNCHER_H_ |
| OLD | NEW |