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 |worker_delegate| to handle IPCs. The caller must ensure that |
| 67 // |worker_delegate| remains valid until Stoppable::Stop() method has been | 66 // |worker_delegate| must outlive this object. |
| 68 // called. | |
| 69 // | |
| 70 // The caller should call all the methods on this class on | |
| 71 // the |caller_task_runner| thread. Methods of both delegate interfaces are | |
| 72 // called on the |caller_task_runner| thread as well. | |
| 73 WorkerProcessLauncher( | 67 WorkerProcessLauncher( |
| 74 scoped_refptr<base::SingleThreadTaskRunner> caller_task_runner, | |
| 75 scoped_ptr<Delegate> launcher_delegate, | 68 scoped_ptr<Delegate> launcher_delegate, |
| 76 WorkerProcessIpcDelegate* worker_delegate); | 69 WorkerProcessIpcDelegate* worker_delegate); |
| 77 ~WorkerProcessLauncher(); | 70 virtual ~WorkerProcessLauncher(); |
| 78 | 71 |
| 79 // Asks the worker process to crash and generate a dump, and closes the IPC | 72 // 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 | 73 // 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 | 74 // the dump. Restarts the worker process forcefully, if it does |
| 82 // not exit on its own. | 75 // not exit on its own. |
| 83 void Crash(const tracked_objects::Location& location); | 76 void Crash(const tracked_objects::Location& location); |
| 84 | 77 |
| 85 // Sends an IPC message to the worker process. The message will be silently | 78 // 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 | 79 // dropped if Send() is called before Start() or after stutdown has been |
| 87 // initiated. | 80 // initiated. |
| 88 void Send(IPC::Message* message); | 81 void Send(IPC::Message* message); |
| 89 | 82 |
| 83 // Notification methods invoked by |Delegate|. | |
| 84 | |
| 85 // Invoked to pass a handle of the launched process back to the caller of | |
| 86 // Delegate::LaunchProcess(). The implementation has to make sure that this | |
|
Wez
2013/05/14 03:45:22
nit: The WorkerProcessLauncher implementation, or
alexeypa (please no reviews)
2013/05/14 18:31:10
It is the delegate. I updated the comment.
| |
| 87 // method is called before OnChannelConnected(). | |
| 88 void OnProcessLaunched(base::win::ScopedHandle worker_process); | |
| 89 | |
| 90 // Called when a fatal error occurs (i.e. a failed process launch). | |
| 91 // The implementation must guarantee that no other notifications is | |
| 92 // delivered once OnFatalError() has been called. | |
|
Wez
2013/05/14 03:45:22
Same here.
alexeypa (please no reviews)
2013/05/14 18:31:10
Done.
| |
| 93 void OnFatalError(); | |
| 94 | |
| 95 // Mirrors methods of IPC::Listener. | |
|
Wez
2013/05/14 03:45:22
nit: Clarify why these need to be mirrored here.
alexeypa (please no reviews)
2013/05/14 18:31:10
Done.
| |
| 96 bool OnMessageReceived(const IPC::Message& message); | |
| 97 void OnChannelConnected(int32 peer_pid); | |
| 98 void OnChannelError(); | |
| 99 | |
| 90 private: | 100 private: |
| 91 friend class WorkerProcessLauncherTest; | 101 friend class WorkerProcessLauncherTest; |
| 92 | 102 |
| 93 // Hooks that allow test code to call the corresponding methods of |Core|. | 103 // base::win::ObjectWatcher::Delegate implementation. |
|
Wez
2013/05/14 03:45:22
nit: "... used to watch for the worker process exi
alexeypa (please no reviews)
2013/05/14 18:31:10
Done.
| |
| 94 void ResetLaunchSuccessTimeoutForTest(); | 104 virtual void OnObjectSignaled(HANDLE object) OVERRIDE; |
| 105 | |
| 106 // Attempts to launch the worker process. Schedules next launch attempt if | |
| 107 // creation of the process fails. | |
| 108 void LaunchWorker(); | |
| 109 | |
| 110 // Called to record a successfull launch attempt. | |
| 111 void RecordSuccessfulLaunch(); | |
| 112 | |
| 113 // Used to emulate |start_process_timer_| expiration. | |
|
Wez
2013/05/14 03:45:22
nit: Clarify what this method actually does that's
alexeypa (please no reviews)
2013/05/14 18:31:10
Done.
| |
| 114 void RecordSuccessfulLaunchForTest(); | |
| 115 | |
| 116 // Set the desired timeout for |kill_process_timer_|. | |
| 95 void SetKillProcessTimeoutForTest(const base::TimeDelta& timeout); | 117 void SetKillProcessTimeoutForTest(const base::TimeDelta& timeout); |
| 96 | 118 |
| 97 // The actual implementation resides in WorkerProcessLauncher::Core class. | 119 // Stops the worker process and schedules next launch attempt unless the |
| 98 class Core; | 120 // object is being destroyed already. |
| 99 scoped_refptr<Core> core_; | 121 void StopWorker(); |
| 122 | |
| 123 // Implements specifics of launching a worker process. | |
| 124 scoped_ptr<WorkerProcessLauncher::Delegate> launcher_delegate_; | |
| 125 | |
| 126 // Handles IPC messages sent by the worker process. | |
| 127 WorkerProcessIpcDelegate* worker_delegate_; | |
|
Wez
2013/05/14 03:45:22
nit: ipc_handler_?
alexeypa (please no reviews)
2013/05/14 18:31:10
Done.
| |
| 128 | |
| 129 DWORD exit_code_; | |
|
Wez
2013/05/14 03:45:22
nit: Add a comment to explain why we need to store
alexeypa (please no reviews)
2013/05/14 18:31:10
Done.
| |
| 130 | |
| 131 // True if IPC messages should be passed to |worker_delegate_|. | |
| 132 bool ipc_enabled_; | |
| 133 | |
| 134 // The timer used to delay termination of the worker process when an IPC error | |
| 135 // occured or when Crash() request is pending | |
| 136 base::OneShotTimer<WorkerProcessLauncher> kill_process_timer_; | |
| 137 | |
| 138 // The default timeout for |kill_process_timer_|. | |
|
Wez
2013/05/14 03:45:22
nit: Do you really mean "default", or "initial"?
alexeypa (please no reviews)
2013/05/14 18:31:10
I really meant "default".
| |
| 139 base::TimeDelta kill_process_timeout_; | |
| 140 | |
| 141 // Launch backoff state. | |
|
Wez
2013/05/14 03:45:22
nit: Suggest "State used to backoff worker launch
alexeypa (please no reviews)
2013/05/14 18:31:10
Done.
| |
| 142 net::BackoffEntry launch_backoff_; | |
| 143 | |
| 144 // Timer used to schedule the next attempt to launch the process. | |
| 145 base::OneShotTimer<WorkerProcessLauncher> launch_timer_; | |
| 146 | |
| 147 // Monitors |process_exit_event_| to detect when the launched process | |
|
Wez
2013/05/14 03:45:22
nit: There is no |process_exit_event| - do you mea
alexeypa (please no reviews)
2013/05/14 18:31:10
Done.
| |
| 148 // terminates. | |
| 149 base::win::ObjectWatcher process_watcher_; | |
| 150 | |
| 151 // Timer used to cancel a launch attempt if it is taking too long. | |
| 152 base::OneShotTimer<WorkerProcessLauncher> start_process_timer_; | |
|
Wez
2013/05/14 03:45:22
nit: This is confusingly named wrt |launch_timer_|
alexeypa (please no reviews)
2013/05/14 18:31:10
Done.
| |
| 153 | |
| 154 // The handle of the worker process, if launched. | |
| 155 base::win::ScopedHandle worker_process_; | |
| 156 | |
| 157 // True when the object is being destroyed. | |
| 158 bool stopping_; | |
| 100 | 159 |
| 101 DISALLOW_COPY_AND_ASSIGN(WorkerProcessLauncher); | 160 DISALLOW_COPY_AND_ASSIGN(WorkerProcessLauncher); |
| 102 }; | 161 }; |
| 103 | 162 |
| 104 } // namespace remoting | 163 } // namespace remoting |
| 105 | 164 |
| 106 #endif // REMOTING_HOST_WIN_WORKER_PROCESS_LAUNCHER_H_ | 165 #endif // REMOTING_HOST_WIN_WORKER_PROCESS_LAUNCHER_H_ |
| OLD | NEW |