Chromium Code Reviews| Index: android_webview/test/shell/src/org/chromium/android_webview/test/SeparateProcessWebViewService.java |
| diff --git a/android_webview/test/shell/src/org/chromium/android_webview/test/SeparateProcessWebViewService.java b/android_webview/test/shell/src/org/chromium/android_webview/test/SeparateProcessWebViewService.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..e3c5b47d8896781c5276810da20ea5deaff0771c |
| --- /dev/null |
| +++ b/android_webview/test/shell/src/org/chromium/android_webview/test/SeparateProcessWebViewService.java |
| @@ -0,0 +1,78 @@ |
| +// Copyright 2016 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. |
| + |
| +package org.chromium.android_webview.test; |
| + |
| +import android.annotation.SuppressLint; |
| +import android.app.Service; |
| +import android.content.Intent; |
| +import android.os.Bundle; |
| +import android.os.Handler; |
| +import android.os.IBinder; |
| +import android.os.Looper; |
| +import android.os.Message; |
| +import android.os.Messenger; |
| +import android.os.Process; |
| +import android.os.RemoteException; |
| +import android.util.AndroidRuntimeException; |
| + |
| +import java.io.Serializable; |
| + |
| +/** |
| + * This is a service for imitating a second browser process in the application. |
| + */ |
| +public class SeparateProcessWebViewService extends Service { |
| + private static final String TAG = "SeparateWebView"; |
| + public static final String TEST_CODE_BUNDLE_TAG = "test_code_bundle_tag"; |
|
boliu
2016/09/15 04:57:53
all caps
|
| + public static final int PROCESS_PID_MESSAGE_ID = -1; |
| + public static final String PROCESS_PID_BUNDLE_TAG = "process_pid_bundle_tag"; |
| + Messenger mTestMessenger; |
| + |
| + /** |
| + * Handler that receives a Messenger instance from the test so that we can communicate with the |
| + * test. |
| + */ |
| + // SeparateProcessWebViewService is a test-only Service that should be killed quickly after use, |
| + // thus we shouldn't worry much about leaking the Service Context from this Handler. |
| + @SuppressLint("HandlerLeak") |
| + class TestMessageReceiverHandler extends Handler { |
| + public TestMessageReceiverHandler() { |
| + // Always run this on the UI thread |
| + super(Looper.getMainLooper()); |
| + } |
| + |
| + @Override |
| + public void handleMessage(Message msg) { |
| + mTestMessenger = msg.replyTo; |
| + |
| + sendProcessPid(); |
| + |
| + Bundle bundle = msg.getData(); |
| + Serializable serializableRunner = bundle.getSerializable(TEST_CODE_BUNDLE_TAG); |
| + if (serializableRunner instanceof ServiceTestRunner) { |
| + ServiceTestRunner runner = (ServiceTestRunner) serializableRunner; |
| + runner.runTestOnMainThread(mTestMessenger, SeparateProcessWebViewService.this); |
| + } else { |
| + throw new AndroidRuntimeException("The object with tag " + TEST_CODE_BUNDLE_TAG |
| + + " is not an instance of " + ServiceTestRunner.class.getSimpleName()); |
| + } |
| + } |
| + } |
| + |
| + private void sendProcessPid() { |
| + try { |
| + Message message = Message.obtain(null, PROCESS_PID_MESSAGE_ID, Process.myPid(), 0); |
| + mTestMessenger.send(message); |
| + } catch (RemoteException e) { |
| + throw new AndroidRuntimeException("Couldn't send process pid back to test code"); |
| + } |
| + } |
| + |
| + Messenger mMessenger = new Messenger(new TestMessageReceiverHandler()); |
| + |
| + @Override |
| + public IBinder onBind(Intent intent) { |
| + return mMessenger.getBinder(); |
| + } |
| +} |