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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
content / shell / android / shell_apk / src / org / chromium / content_shell_apk / TestChildProcessService.java [blame]
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package org.chromium.content_shell_apk;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Looper;
import android.os.RemoteException;
import android.util.SparseArray;
import org.chromium.base.CommandLine;
import org.chromium.base.Log;
import org.chromium.base.library_loader.LibraryLoader;
import org.chromium.base.library_loader.LibraryProcessType;
import org.chromium.base.process_launcher.ChildProcessService;
import org.chromium.base.process_launcher.ChildProcessServiceDelegate;
import java.util.List;
import javax.annotation.concurrent.GuardedBy;
/** Child service started by ChildProcessLauncherTest. */
public class TestChildProcessService extends Service {
private static final String TAG = "TestProcessService";
private static final long MAIN_BLOCKING_DURATION_MS = 5000;
private static class TestChildProcessServiceDelegate implements ChildProcessServiceDelegate {
private final Object mConnectionSetupLock = new Object();
@GuardedBy("mConnectionSetupLock")
private boolean mConnectionSetup;
private boolean mServiceCreated;
private Bundle mServiceBundle;
private String[] mCommandLine;
private IChildProcessTest mIChildProcessTest;
@Override
public void onServiceCreated() {
mServiceCreated = true;
}
@Override
public void onServiceBound(Intent intent) {
mServiceBundle = intent.getExtras();
LibraryLoader.getInstance().setLibraryProcessType(LibraryProcessType.PROCESS_CHILD);
}
@Override
public void onConnectionSetup(
Bundle connectionBundle, List<IBinder> clientInterfaces, IBinder binderBox) {
if (clientInterfaces != null && !clientInterfaces.isEmpty()) {
mIChildProcessTest = IChildProcessTest.Stub.asInterface(clientInterfaces.get(0));
}
if (mIChildProcessTest != null) {
try {
mIChildProcessTest.onConnectionSetup(
mServiceCreated, mServiceBundle, connectionBundle);
} catch (RemoteException re) {
Log.e(TAG, "Failed to call IChildProcessTest.onConnectionSetup.", re);
}
}
synchronized (mConnectionSetupLock) {
mConnectionSetup = true;
mConnectionSetupLock.notifyAll();
}
}
@Override
public void preloadNativeLibrary(String packageName) {
LibraryLoader.getInstance().preloadNow();
}
@Override
public void loadNativeLibrary(Context hostContext) {
// Store the command line before loading the library to avoid an assert in CommandLine.
mCommandLine = CommandLine.getJavaSwitchesForTesting();
LibraryLoader.getInstance().loadNow();
LibraryLoader.getInstance().ensureInitialized();
// Loading the library happen on the main thread and onConnectionSetup is called from
// the client. Wait for onConnectionSetup so mIChildProcessTest is set.
synchronized (mConnectionSetupLock) {
while (!mConnectionSetup) {
try {
mConnectionSetupLock.wait();
} catch (InterruptedException e) {
// Ignore.
}
}
}
if (mIChildProcessTest != null) {
try {
mIChildProcessTest.onLoadNativeLibrary(true);
} catch (RemoteException re) {
Log.e(TAG, "Failed to call IChildProcessTest.onLoadNativeLibrary.", re);
}
}
}
@Override
public SparseArray<String> getFileDescriptorsIdsToKeys() {
return null;
}
@Override
public void onBeforeMain() {
if (mIChildProcessTest == null) return;
try {
mIChildProcessTest.onBeforeMain(mCommandLine);
} catch (RemoteException re) {
Log.e(TAG, "Failed to call IChildProcessTest.onBeforeMain.", re);
}
}
@Override
public void runMain() {
if (mIChildProcessTest != null) {
try {
mIChildProcessTest.onRunMain();
} catch (RemoteException re) {
Log.e(TAG, "Failed to call IChildProcessTest.onRunMain.", re);
}
}
// Run a message loop to keep the service from exiting.
Looper.prepare();
Looper.loop();
}
@Override
public void consumeRelroBundle(Bundle bundle) {}
}
private ChildProcessService mService;
public TestChildProcessService() {}
@Override
public void onCreate() {
super.onCreate();
mService =
new ChildProcessService(
new TestChildProcessServiceDelegate(), this, getApplicationContext());
mService.onCreate();
}
@Override
public void onDestroy() {
super.onDestroy();
mService.onDestroy();
mService = null;
}
@Override
public IBinder onBind(Intent intent) {
return mService.onBind(intent);
}
}