Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(487)

Side by Side Diff: remoting/host/win/worker_process_launcher.h

Issue 15077010: [Chromoting] Refactored worker process launching code and speeded up the desktop process launch. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebased Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 are delivered once
91 // OnFatalError() has been called.
92 void OnFatalError();
93
94 // Mirrors methods of IPC::Listener to be invoked by |Delegate|. |Delegate|
95 // has to validate |peer_pid| if necessary.
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 used to watch for
94 void ResetLaunchSuccessTimeoutForTest(); 104 // the worker process exiting.
105 virtual void OnObjectSignaled(HANDLE object) OVERRIDE;
106
107 // Returns true when the object is being destroyed.
108 bool stopping() const { return ipc_handler_ == NULL; }
109
110 // Attempts to launch the worker process. Schedules next launch attempt if
111 // creation of the process fails.
112 void LaunchWorker();
113
114 // Called to record outcome of a launch attempt: success or failure.
115 void RecordLaunchResult();
116
117 // Called by the test to record a successful launch attempt.
118 void RecordSuccessfulLaunchForTest();
119
120 // Set the desired timeout for |kill_process_timer_|.
95 void SetKillProcessTimeoutForTest(const base::TimeDelta& timeout); 121 void SetKillProcessTimeoutForTest(const base::TimeDelta& timeout);
96 122
97 // The actual implementation resides in WorkerProcessLauncher::Core class. 123 // Stops the worker process and schedules next launch attempt unless the
98 class Core; 124 // object is being destroyed already.
99 scoped_refptr<Core> core_; 125 void StopWorker();
126
127 // Handles IPC messages sent by the worker process.
128 WorkerProcessIpcDelegate* ipc_handler_;
129
130 // Implements specifics of launching a worker process.
131 scoped_ptr<WorkerProcessLauncher::Delegate> launcher_delegate_;
132
133 // Keeps the exit code of the worker process after it was closed. The exit
134 // code is used to determine whether the process has to be restarted.
135 DWORD exit_code_;
136
137 // True if IPC messages should be passed to |ipc_handler_|.
138 bool ipc_enabled_;
139
140 // The timer used to delay termination of the worker process when an IPC error
141 // occured or when Crash() request is pending
142 base::OneShotTimer<WorkerProcessLauncher> kill_process_timer_;
143
144 // The default timeout for |kill_process_timer_|.
145 base::TimeDelta kill_process_timeout_;
146
147 // State used to backoff worker launch attempts on failure.
148 net::BackoffEntry launch_backoff_;
149
150 // Timer used to schedule the next attempt to launch the process.
151 base::OneShotTimer<WorkerProcessLauncher> launch_timer_;
152
153 // Monitors |worker_process_| to detect when the launched process
154 // terminates.
155 base::win::ObjectWatcher process_watcher_;
156
157 // Timer used to detect whether a launch attempt was successful or not, and to
158 // cancel the launch attempt if it is taking too long.
159 base::OneShotTimer<WorkerProcessLauncher> launch_result_timer_;
160
161 // The handle of the worker process, if launched.
162 base::win::ScopedHandle worker_process_;
100 163
101 DISALLOW_COPY_AND_ASSIGN(WorkerProcessLauncher); 164 DISALLOW_COPY_AND_ASSIGN(WorkerProcessLauncher);
102 }; 165 };
103 166
104 } // namespace remoting 167 } // namespace remoting
105 168
106 #endif // REMOTING_HOST_WIN_WORKER_PROCESS_LAUNCHER_H_ 169 #endif // REMOTING_HOST_WIN_WORKER_PROCESS_LAUNCHER_H_
OLDNEW
« no previous file with comments | « remoting/host/win/unprivileged_process_delegate.cc ('k') | remoting/host/win/worker_process_launcher.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698