OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 package org.chromium.base; | |
6 | |
7 import android.app.Service; | |
8 import android.content.Intent; | |
9 import android.os.Handler; | |
10 import android.os.IBinder; | |
11 import android.os.Process; | |
12 | |
13 import org.chromium.base.annotations.CalledByNative; | |
14 import org.chromium.base.annotations.JNINamespace; | |
15 import org.chromium.base.annotations.SuppressFBWarnings; | |
16 import org.chromium.base.library_loader.NativeLibraries; | |
17 | |
18 /** | |
19 * The service implementation used to host all multiprocess test client code. | |
20 */ | |
21 @JNINamespace("base::android") | |
22 public class MultiprocessTestClientService extends Service { | |
23 private static final String TAG = "TestClient"; | |
nyquist
2016/12/13 20:40:31
Nit: cr_ prefix these days I think.
Jay Civelli
2016/12/14 02:20:11
OK, I'll do it...
| |
24 | |
25 private final Handler mHandler = new Handler(); | |
26 | |
27 private final Object mResultLock = new Object(); | |
28 private MainReturnCodeResult mResult; | |
nyquist
2016/12/13 20:40:31
@GuardedBy("mResultLock") ?
Jay Civelli
2016/12/14 02:20:11
Done.
| |
29 | |
30 private final ITestClient.Stub mBinder = new ITestClient.Stub() { | |
31 @Override | |
32 public int launch(final String[] commandLine, FileDescriptorInfo[] fdsTo Map) { | |
33 final int[] fdIds = new int[fdsToMap.length]; | |
34 final int[] fdFds = new int[fdsToMap.length]; | |
35 for (int i = 0; i < fdsToMap.length; i++) { | |
36 fdIds[i] = fdsToMap[i].mId; | |
37 fdFds[i] = fdsToMap[i].mFd.detachFd(); | |
38 } | |
39 // Don't run main directly, it would block and the response would no t be returned. | |
40 // We post to the main thread as this thread does not have a Looper. | |
41 mHandler.post(new Runnable() { | |
42 @Override | |
43 public void run() { | |
44 nativeRunMain(commandLine, fdIds, fdFds); | |
45 } | |
46 }); | |
47 return Process.myPid(); | |
48 } | |
49 | |
50 @SuppressFBWarnings("RCN_REDUNDANT_NULLCHECK_OF_NULL_VALUE") | |
51 @Override | |
52 public MainReturnCodeResult waitForMainToReturn(int timeoutMs) { | |
53 synchronized (mResultLock) { | |
54 if (mResult != null) return mResult; | |
55 try { | |
56 mResultLock.wait(timeoutMs); | |
nyquist
2016/12/13 20:40:31
This doesn't handle spurious wake-ups. Do you want
Jay Civelli
2016/12/14 02:20:11
Good call.
| |
57 } catch (InterruptedException ie) { | |
58 Log.e(TAG, "Failed to wait for main return value."); | |
59 return new MainReturnCodeResult(0, true /* timed-out */); | |
60 } | |
61 assert mResult != null; | |
62 return mResult; | |
63 } | |
64 } | |
65 | |
66 @SuppressFBWarnings("DM_EXIT") | |
67 @Override | |
68 public boolean forceStopSynchronous(int exitCode) { | |
69 System.exit(exitCode); | |
70 return true; | |
71 } | |
72 | |
73 @SuppressFBWarnings("DM_EXIT") | |
74 @Override | |
75 public void forceStop(int exitCode) { | |
76 System.exit(exitCode); | |
77 } | |
78 }; | |
79 | |
80 static boolean sAlreadyInitialized = false; | |
nyquist
2016/12/13 20:40:31
Nit: Could this static be at the top instead?
May
Jay Civelli
2016/12/14 02:20:11
Done.
| |
81 | |
82 @SuppressFBWarnings({"ST_WRITE_TO_STATIC_FROM_INSTANCE_METHOD", "DM_EXIT"}) | |
nyquist
2016/12/13 20:40:31
Do you really need to suppress the write-to-static
Jay Civelli
2016/12/14 02:20:11
Done.
| |
83 @Override | |
84 public void onCreate() { | |
85 super.onCreate(); | |
86 | |
87 if (sAlreadyInitialized) { | |
88 // The framework controls how services are reused and even though no thing is bound to a | |
89 // service it might be kept around. Since we really want to fork a n ew process when we | |
90 // bind, we'll kill the process early when a service is reused, forc ing the framework to | |
91 // recreate the service in a new process. | |
92 // This is not ideal, but there are no clear alternatives at this po int. | |
93 Log.e(TAG, "Service being reused, forcing stoppage."); | |
94 System.exit(0); | |
nyquist
2016/12/13 20:40:31
I think we usually add an extra return-call after
Jay Civelli
2016/12/14 02:20:11
Done.
| |
95 } | |
96 sAlreadyInitialized = true; | |
97 | |
98 ContextUtils.initApplicationContext(getApplicationContext()); | |
99 | |
100 PathUtils.setPrivateDataDirectorySuffix("chrome_multiprocess_test_client _service"); | |
101 | |
102 loadLibraries(); | |
103 | |
104 Log.e(TAG, "onCreate"); | |
nyquist
2016/12/13 20:40:31
Is this necessary as an error? (same below with on
Jay Civelli
2016/12/14 02:20:11
Yeah, removed, debug stuff that ends up polluting
| |
105 } | |
106 | |
107 @Override | |
108 public void onDestroy() { | |
109 Log.e(TAG, "onDestroy"); | |
nyquist
2016/12/13 20:40:31
If the log is removed, this just calls super, so I
Jay Civelli
2016/12/14 02:20:11
Done.
| |
110 super.onDestroy(); | |
111 } | |
112 | |
113 @Override | |
114 public IBinder onBind(Intent intent) { | |
115 // Only bind the first incoming request. | |
116 stopSelf(); | |
nyquist
2016/12/13 20:40:31
From what I remember from services, they can be st
Jay Civelli
2016/12/14 02:20:11
You are right, I don't think this is needed.
| |
117 | |
118 return mBinder; | |
119 } | |
120 | |
121 private void loadLibraries() { | |
nyquist
2016/12/13 20:40:31
What's the reason for this not using the LibraryLo
Jay Civelli
2016/12/14 02:20:11
None, now doing so.
| |
122 for (String library : NativeLibraries.LIBRARIES) { | |
123 Log.i(TAG, "loading: %s", library); | |
nyquist
2016/12/13 20:40:31
Do we need this? If so, uppercase L?
Jay Civelli
2016/12/14 02:20:11
This is now gone.
| |
124 System.loadLibrary(library); | |
125 Log.i(TAG, "loaded: %s", library); | |
126 } | |
127 ContextUtils.initApplicationContextForNative(); | |
128 } | |
129 | |
130 @CalledByNative | |
131 private void setMainReturnValue(int result) { | |
132 synchronized (mResultLock) { | |
133 mResult = new MainReturnCodeResult(result, false /* timed-out */); | |
134 mResultLock.notifyAll(); | |
135 } | |
136 } | |
137 | |
138 private native void nativeRunMain(String[] commandLine, int[] fdsToMapIds, i nt[] fdsToMapFds); | |
139 } | |
OLD | NEW |