forked from Pissandshittium/pissandshittium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathipc_test_util.h
151 lines (115 loc) · 5.13 KB
/
ipc_test_util.h
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
139
140
141
142
143
144
145
146
147
148
149
150
151
// Copyright 2018 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 CHROME_CHROME_CLEANER_IPC_IPC_TEST_UTIL_H_
#define CHROME_CHROME_CLEANER_IPC_IPC_TEST_UTIL_H_
#include <memory>
#include <string>
#include "base/callback_forward.h"
#include "base/command_line.h"
#include "base/memory/ref_counted.h"
#include "base/process/launch.h"
#include "base/process/process.h"
#include "base/time/time.h"
#include "chrome/chrome_cleaner/ipc/chrome_prompt_ipc.h"
#include "chrome/chrome_cleaner/ipc/mojo_task_runner.h"
#include "chrome/chrome_cleaner/test/child_process_logger.h"
#include "mojo/public/cpp/platform/platform_channel.h"
#include "mojo/public/cpp/system/invitation.h"
#include "mojo/public/cpp/system/message_pipe.h"
namespace chrome_cleaner {
typedef base::RepeatingCallback<void(mojo::ScopedMessagePipeHandle mojo_pipe)>
CreateImplCallback;
class ParentProcess : public base::RefCountedThreadSafe<ParentProcess> {
public:
explicit ParentProcess(scoped_refptr<MojoTaskRunner> mojo_task_runner);
bool LaunchConnectedChildProcess(const std::string& child_main_function,
int32_t* exit_code);
bool LaunchConnectedChildProcess(const std::string& child_main_function,
base::TimeDelta timeout,
int32_t* exit_code);
void AppendSwitch(const std::string& switch_string);
void AppendSwitch(const std::string& switch_string, const std::string& value);
void AppendSwitchNative(const std::string& switch_string,
const std::wstring& value);
void AppendSwitchPath(const std::string& switch_string,
const base::FilePath& value);
void AppendSwitchHandleToShare(const std::string& switch_string,
HANDLE handle);
// The following methods are called during the launch sequence. They are
// public so they can be called from helper classes.
void CreateImplOnIPCThread(mojo::ScopedMessagePipeHandle mojo_pipe);
void DestroyImplOnIPCThread();
void CreateMojoPipe(base::CommandLine* command_line,
base::HandlesToInheritVector* handles_to_inherit);
void ConnectMojoPipe(base::Process child_process);
base::HandlesToInheritVector extra_handles_to_inherit() const {
return extra_handles_to_inherit_;
}
const ChildProcessLogger& child_process_logger() const {
return child_process_logger_;
}
protected:
friend base::RefCountedThreadSafe<ParentProcess>;
virtual ~ParentProcess();
// This is called on the IPC thread.
virtual void CreateImpl(mojo::ScopedMessagePipeHandle mojo_pipe) = 0;
virtual void DestroyImpl() = 0;
// Subclasses can override this to launch the child in different ways, such
// as in the sandbox. Subclasses should call CreateMojoPipe before the
// subprocess is spawned and ConnectMojoPipe afterward.
virtual bool PrepareAndLaunchTestChildProcess(
const std::string& child_main_function);
scoped_refptr<MojoTaskRunner> mojo_task_runner();
base::CommandLine command_line_;
base::HandlesToInheritVector extra_handles_to_inherit_;
ChildProcessLogger child_process_logger_;
private:
scoped_refptr<MojoTaskRunner> mojo_task_runner_;
mojo::OutgoingInvitation outgoing_invitation_;
mojo::ScopedMessagePipeHandle mojo_pipe_;
mojo::PlatformChannel mojo_channel_;
base::Process child_process_;
};
class SandboxedParentProcess : public ParentProcess {
public:
explicit SandboxedParentProcess(
scoped_refptr<MojoTaskRunner> mojo_task_runner);
protected:
friend base::RefCountedThreadSafe<SandboxedParentProcess>;
~SandboxedParentProcess() override;
bool PrepareAndLaunchTestChildProcess(
const std::string& child_main_function) override;
};
class ChildProcess : public base::RefCountedThreadSafe<ChildProcess> {
public:
explicit ChildProcess(scoped_refptr<MojoTaskRunner> mojo_task_runner);
mojo::ScopedMessagePipeHandle CreateMessagePipeFromCommandLine();
std::string mojo_pipe_token() const;
const base::CommandLine& command_line() const { return *command_line_; }
// This will drop all privileges if the child process is running in a
// sandbox. If not, it will do nothing.
void LowerToken() const;
protected:
friend base::RefCountedThreadSafe<ChildProcess>;
virtual ~ChildProcess();
scoped_refptr<MojoTaskRunner> mojo_task_runner_;
private:
base::CommandLine* command_line_;
// This will be true iff the process is running in a sandbox and
// TargetServices was initialized successfully.
bool target_services_initialized_ = false;
};
class ChromePromptIPCTestErrorHandler : public ChromePromptIPC::ErrorHandler {
public:
ChromePromptIPCTestErrorHandler(base::OnceClosure on_closed,
base::OnceClosure on_closed_after_done);
~ChromePromptIPCTestErrorHandler() override;
void OnConnectionClosed() override;
void OnConnectionClosedAfterDone() override;
private:
base::OnceClosure on_closed_;
base::OnceClosure on_closed_after_done_;
};
} // namespace chrome_cleaner
#endif // CHROME_CHROME_CLEANER_IPC_IPC_TEST_UTIL_H_