blob: 1d4702276a80af6981adcc4b85910fde838e5e41 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef REMOTING_HOST_WIN_WORKER_PROCESS_LAUNCHER_H_
#define REMOTING_HOST_WIN_WORKER_PROCESS_LAUNCHER_H_
#include <windows.h>
#include "base/basictypes.h"
#include "base/compiler_specific.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/process.h"
#include "base/timer.h"
#include "base/win/scoped_handle.h"
#include "base/win/object_watcher.h"
#include "ipc/ipc_channel.h"
#include "remoting/base/stoppable.h"
namespace base {
class SingleThreadTaskRunner;
} // namespace base
namespace IPC {
class ChannelProxy;
class Message;
} // namespace IPC
namespace remoting {
// Launches a worker process that is controlled via an IPC channel. All
// interaction with the spawned process is through the IPC::Listener and Send()
// method. In case of error the channel is closed and the worker process is
// terminated.
//
// WorkerProcessLauncher object is good for one process launch attempt only.
class WorkerProcessLauncher
: public Stoppable,
public base::win::ObjectWatcher::Delegate,
public IPC::Listener {
public:
class Delegate {
public:
virtual ~Delegate();
// Starts the worker process and passes |channel_name| to it.
// |process_exit_event_out| receives a handle that becomes signalled once
// the launched process has been terminated.
virtual bool DoLaunchProcess(
const std::string& channel_name,
base::win::ScopedHandle* process_exit_event_out) = 0;
// Terminates the worker process with the given exit code.
virtual void DoKillProcess(DWORD exit_code) = 0;
// Notifies that a client has been connected to the channel.
virtual void OnChannelConnected() = 0;
// Processes messages sent by the client.
virtual bool OnMessageReceived(const IPC::Message& message) = 0;
};
// Creates the launcher.
// |delegate| will be able to receive messages sent over the channel once
// the worker has been started and until it is stopped by Stop() or an error
// occurs.
//
// |stopped_callback| and |main_task_runner| are passed to the underlying
// |Stoppable| implementation. The caller should call all the methods on this
// class on the |main_task_runner| thread. |ipc_task_runner| is used to
// perform background IPC I/O.
WorkerProcessLauncher(
Delegate* delegate,
const base::Closure& stopped_callback,
scoped_refptr<base::SingleThreadTaskRunner> main_task_runner,
scoped_refptr<base::SingleThreadTaskRunner> ipc_task_runner);
virtual ~WorkerProcessLauncher();
// Starts the worker process.
void Start(const std::string& pipe_sddl);
// Sends an IPC message to the worker process. This method can be called only
// after successful Start() and until Stop() is called or an error occurred.
void Send(IPC::Message* message);
// base::win::ObjectWatcher::Delegate implementation.
virtual void OnObjectSignaled(HANDLE object) OVERRIDE;
// IPC::Listener implementation.
virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
virtual void OnChannelConnected(int32 peer_pid) OVERRIDE;
virtual void OnChannelError() OVERRIDE;
protected:
// Stoppable implementation.
virtual void DoStop() OVERRIDE;
private:
// Creates the server end of the Chromoting IPC channel.
bool CreatePipeForIpcChannel(const std::string& channel_name,
const std::string& pipe_sddl,
base::win::ScopedHandle* pipe_out);
// Generates random channel ID.
std::string GenerateRandomChannelId();
Delegate* delegate_;
// The main service message loop.
scoped_refptr<base::SingleThreadTaskRunner> main_task_runner_;
// Message loop used by the IPC channel.
scoped_refptr<base::SingleThreadTaskRunner> ipc_task_runner_;
// Used to determine when the launched process terminates.
base::win::ObjectWatcher process_watcher_;
// A waiting handle that becomes signalled once the launched process has
// been terminated.
base::win::ScopedHandle process_exit_event_;
// The IPC channel connecting to the launched process.
scoped_ptr<IPC::ChannelProxy> ipc_channel_;
// The timer used to delay termination of the process in the case of an IPC
// error.
base::OneShotTimer<WorkerProcessLauncher> ipc_error_timer_;
// The server end of the pipe.
base::win::ScopedHandle pipe_;
DISALLOW_COPY_AND_ASSIGN(WorkerProcessLauncher);
};
} // namespace remoting
#endif // REMOTING_HOST_WIN_WORKER_PROCESS_LAUNCHER_H_
|