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

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

Issue 11040065: [Chromoting] Reimplemented the worker process launcher to take into account the encountered issues: (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 2 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 <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>,
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.
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 // Stops the worker process asynchronously. The caller can drop the reference
76 void Start(const std::string& pipe_sddl); 75 // to |this| as soon as Stop() returns.
76 void Stop();
77 77
78 // 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
79 // 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
80 // initiated. 80 // initiated.
81 void Send(IPC::Message* message); 81 void Send(IPC::Message* message);
82 82
83 // base::win::ObjectWatcher::Delegate implementation. 83 // base::win::ObjectWatcher::Delegate implementation.
84 virtual void OnObjectSignaled(HANDLE object) OVERRIDE; 84 virtual void OnObjectSignaled(HANDLE object) OVERRIDE;
85 85
86 // IPC::Listener implementation. 86 // IPC::Listener implementation.
87 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; 87 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
88 virtual void OnChannelConnected(int32 peer_pid) OVERRIDE; 88 virtual void OnChannelConnected(int32 peer_pid) OVERRIDE;
89 virtual void OnChannelError() OVERRIDE; 89 virtual void OnChannelError() OVERRIDE;
90 90
91 protected: 91 private:
92 // Stoppable implementation. 92 friend class base::RefCountedThreadSafe<WorkerProcessLauncher>;
93 virtual void DoStop() OVERRIDE; 93 virtual ~WorkerProcessLauncher();
94 94
95 private:
96 // Creates the server end of the Chromoting IPC channel. 95 // Creates the server end of the Chromoting IPC channel.
97 bool CreatePipeForIpcChannel(const std::string& channel_name, 96 bool CreatePipeForIpcChannel(const std::string& channel_name,
98 const std::string& pipe_sddl, 97 const std::string& pipe_security_descriptor,
99 base::win::ScopedHandle* pipe_out); 98 base::win::ScopedHandle* pipe_out);
100 99
101 // Generates random channel ID. 100 // Generates random channel ID.
102 std::string GenerateRandomChannelId(); 101 std::string GenerateRandomChannelId();
103 102
104 Delegate* launcher_delegate_; 103 // Attempts to launch the worker process. Schedules next launch attempt if
104 // creation of the process fails.
105 void LaunchWorker();
106
107 // Stops the worker process asynchronously and schedules next launch attempt
108 // unless Stop() has been called already.
109 void StopWorker();
110
111 // All public methods are called on the |main_task_runner| thread.
112 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_;
113
114 // The task runner is used perform background IPC I/O.
115 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_;
116
117 // Implements specifics of launching a worker process.
118 scoped_ptr<Delegate> launcher_delegate_;
119
120 // Handles IPC messages sent by the worker process.
105 WorkerProcessIpcDelegate* worker_delegate_; 121 WorkerProcessIpcDelegate* worker_delegate_;
106 122
107 // The main service message loop. 123 // The IPC channel connecting to the launched process.
108 scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_; 124 scoped_ptr<IPC::ChannelProxy> ipc_channel_;
109 125
110 // Message loop used by the IPC channel. 126 // The timer used to delay termination of the process in the case of an IPC
111 scoped_refptr<base::SingleThreadTaskRunner> ipc_task_runner_; 127 // error.
128 scoped_ptr<base::OneShotTimer<WorkerProcessLauncher> > ipc_error_timer_;
129
130 // Time of the last launch attempt.
131 base::Time launch_time_;
132
133 // Current backoff delay.
134 base::TimeDelta launch_backoff_;
135
136 // Timer used to schedule the next attempt to launch the process.
137 scoped_ptr<base::OneShotTimer<WorkerProcessLauncher> > launch_timer_;
112 138
113 // Used to determine when the launched process terminates. 139 // Used to determine when the launched process terminates.
114 base::win::ObjectWatcher process_watcher_; 140 base::win::ObjectWatcher process_watcher_;
115 141
116 // A waiting handle that becomes signalled once the launched process has 142 // A waiting handle that becomes signalled once the launched process has
117 // been terminated. 143 // been terminated.
118 base::win::ScopedHandle process_exit_event_; 144 base::win::ScopedHandle process_exit_event_;
119 145
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. 146 // The server end of the pipe.
128 base::win::ScopedHandle pipe_; 147 base::win::ScopedHandle pipe_;
129 148
149 // The security descriptor (as SDDL) of the server end of the pipe.
150 std::string pipe_security_descriptor_;
151
152 // Self reference to keep the object alive white the worker process is being
simonmorris 2012/10/05 20:19:45 s/white/while/
alexeypa (please no reviews) 2012/10/05 21:18:34 Done.
153 // terminated.
154 scoped_refptr<WorkerProcessLauncher> self_;
155
156 // True when Stop() has been called.
157 bool stopping_;
158
130 DISALLOW_COPY_AND_ASSIGN(WorkerProcessLauncher); 159 DISALLOW_COPY_AND_ASSIGN(WorkerProcessLauncher);
131 }; 160 };
132 161
133 } // namespace remoting 162 } // namespace remoting
134 163
135 #endif // REMOTING_HOST_WIN_WORKER_PROCESS_LAUNCHER_H_ 164 #endif // REMOTING_HOST_WIN_WORKER_PROCESS_LAUNCHER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698