Commit a4c20235 authored by Eric Stevenson's avatar Eric Stevenson Committed by Commit Bot

android_webview: Move ServiceInit logic into WebViewApkApplication

This doesn't fix any bugs, but is a simplification to store all
initialization logic in one spot.

Change-Id: I9b1fdf309993851531c8de07493ccb40dc5fbc10
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1626347
Commit-Queue: Andrew Grieve <agrieve@chromium.org>
Reviewed-by: default avatarRichard Coles <torne@chromium.org>
Reviewed-by: default avatarPaul Miller <paulmiller@chromium.org>
Cr-Commit-Position: refs/heads/master@{#671287}
parent 5e6f9dc0
......@@ -1069,7 +1069,6 @@ android_library("android_webview_services_java") {
"java/src/org/chromium/android_webview/services/AwMinidumpUploaderDelegate.java",
"java/src/org/chromium/android_webview/services/AwVariationsSeedFetcher.java",
"java/src/org/chromium/android_webview/services/CrashReceiverService.java",
"java/src/org/chromium/android_webview/services/ServiceInit.java",
"java/src/org/chromium/android_webview/services/VariationsSeedHolder.java",
"java/src/org/chromium/android_webview/services/VariationsSeedServer.java",
]
......
......@@ -31,6 +31,7 @@ android_library("apk_java") {
"java/src/com/android/webview/chromium/WebViewApkApplication.java",
]
deps = [
"//android_webview:android_webview_commandline_java",
"//base:base_java",
"//components/embedder_support/android:application_java",
]
......
......@@ -7,7 +7,9 @@ package com.android.webview.chromium;
import android.app.Application;
import android.content.Context;
import org.chromium.android_webview.command_line.CommandLineUtil;
import org.chromium.base.ContextUtils;
import org.chromium.base.PathUtils;
import org.chromium.base.annotations.JNINamespace;
import org.chromium.base.library_loader.LibraryLoader;
import org.chromium.components.embedder_support.application.FontPreloadingWorkaround;
......@@ -15,7 +17,9 @@ import org.chromium.components.embedder_support.application.FontPreloadingWorkar
/**
* Application subclass for SystemWebView and Trichrome.
*
* Application subclass is only used in renderer processes and in the WebView APK's own services.
* Application subclass is used by renderer processes, services, and content providers that run
* under the WebView APK's package.
*
* None of this code runs in an application which simply uses WebView.
*/
@JNINamespace("android_webview")
......@@ -26,6 +30,8 @@ public class WebViewApkApplication extends Application {
protected void attachBaseContext(Context context) {
super.attachBaseContext(context);
ContextUtils.initApplicationContext(this);
PathUtils.setPrivateDataDirectorySuffix("webview");
initCommandLine();
}
@Override
......@@ -34,6 +40,11 @@ public class WebViewApkApplication extends Application {
FontPreloadingWorkaround.maybeInstallWorkaround(this);
}
// Overridden by webview shell to point to a different flags file.
protected void initCommandLine() {
CommandLineUtil.initCommandLine();
}
/**
* Performs minimal native library initialization required when running as a stand-alone APK.
* @return True if the library was loaded, false if running as webview stub.
......
......@@ -14,13 +14,6 @@ import org.chromium.components.minidump_uploader.MinidumpUploaderImpl;
*/
// OBS: This class needs to be public to be started from android.app.ActivityThread.
public class AwMinidumpUploadJobService extends MinidumpUploadJobService {
@Override
@SuppressWarnings("NoContextGetApplicationContext")
public void onCreate() {
super.onCreate();
ServiceInit.init(getApplicationContext());
}
@Override
protected MinidumpUploader createMinidumpUploader(PersistableBundle unusedExtras) {
return new MinidumpUploaderImpl(new AwMinidumpUploaderDelegate());
......
......@@ -154,10 +154,8 @@ public class AwVariationsSeedFetcher extends JobService {
}
@Override
@SuppressWarnings("NoContextGetApplicationContext")
public void onCreate() {
super.onCreate();
ServiceInit.init(getApplicationContext());
mSeedHolder = VariationsSeedHolder.getInstance();
}
......
......@@ -35,13 +35,6 @@ public class CrashReceiverService extends Service {
private final Object mCopyingLock = new Object();
private boolean mIsCopying;
@Override
@SuppressWarnings("NoContextGetApplicationContext")
public void onCreate() {
super.onCreate();
ServiceInit.init(getApplicationContext());
}
private final ICrashReceiverService.Stub mBinder = new ICrashReceiverService.Stub() {
@Override
public void transmitCrashes(ParcelFileDescriptor[] fileDescriptors, List crashInfo) {
......
// 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.
package org.chromium.android_webview.services;
import android.content.Context;
import org.chromium.android_webview.command_line.CommandLineUtil;
import org.chromium.base.ContextUtils;
import org.chromium.base.PathUtils;
import org.chromium.base.ThreadUtils;
import org.chromium.base.VisibleForTesting;
/**
* Does initialization common to all WebView services.
*/
@VisibleForTesting
public class ServiceInit {
private static boolean sInitDone;
@VisibleForTesting
public static void setPrivateDataDirectorySuffix() {
// This is unrelated to the PathUtils directory set in WebView proper, because this code
// runs only in the service process.
PathUtils.setPrivateDataDirectorySuffix("webview");
}
public static void init(Context appContext) {
ThreadUtils.assertOnUiThread();
if (sInitDone) return;
ContextUtils.initApplicationContext(appContext);
// In Monochrome, ChromeApplication.attachBaseContext() will set Chrome's command line.
// initCommandLine() overwrites this with WebView's command line.
CommandLineUtil.initCommandLine();
setPrivateDataDirectorySuffix();
sInitDone = true;
}
}
......@@ -30,10 +30,8 @@ public class VariationsSeedServer extends Service {
}
@Override
@SuppressWarnings("NoContextGetApplicationContext")
public void onCreate() {
super.onCreate();
ServiceInit.init(getApplicationContext());
mSeedHolder = VariationsSeedHolder.getInstance();
}
}
......@@ -23,13 +23,11 @@ import org.junit.runner.RunWith;
import org.chromium.android_webview.VariationsUtils;
import org.chromium.android_webview.services.AwVariationsSeedFetcher;
import org.chromium.android_webview.services.ServiceInit;
import org.chromium.android_webview.test.util.VariationsTestUtils;
import org.chromium.base.ContextUtils;
import org.chromium.base.test.util.CallbackHelper;
import org.chromium.components.background_task_scheduler.TaskIds;
import org.chromium.components.variations.firstrun.VariationsSeedFetcher;
import org.chromium.components.variations.firstrun.VariationsSeedFetcher.SeedInfo;
import java.io.File;
import java.io.IOException;
......@@ -124,7 +122,6 @@ public class AwVariationsSeedFetcherTest {
@Before
public void setUp() throws IOException {
ServiceInit.setPrivateDataDirectorySuffix();
AwVariationsSeedFetcher.setMocks(mScheduler, mDownloader);
VariationsTestUtils.deleteSeeds();
}
......
......@@ -4,7 +4,6 @@
package org.chromium.android_webview.test;
import android.content.Context;
import android.os.Looper;
import android.support.test.InstrumentationRegistry;
import android.support.test.filters.MediumTest;
......@@ -23,7 +22,6 @@ import org.chromium.android_webview.AwCookieManager;
import org.chromium.android_webview.AwWebResourceResponse;
import org.chromium.android_webview.test.util.CommonResources;
import org.chromium.android_webview.test.util.CookieUtils;
import org.chromium.base.ContextUtils;
import org.chromium.base.ThreadUtils;
import org.chromium.base.test.util.Feature;
import org.chromium.net.test.util.TestWebServer;
......@@ -56,10 +54,6 @@ public class CookieManagerStartupTest {
// CookieManager assumes that native is loaded, but webview browser should not be loaded for
// these tests as webview is not necessarily loaded when CookieManager is called.
Context appContext = InstrumentationRegistry.getInstrumentation()
.getTargetContext()
.getApplicationContext();
ContextUtils.initApplicationContext(appContext);
AwBrowserProcess.loadLibrary(null);
}
......
......@@ -16,7 +16,6 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.chromium.android_webview.VariationsUtils;
import org.chromium.android_webview.services.ServiceInit;
import org.chromium.android_webview.services.VariationsSeedHolder;
import org.chromium.android_webview.test.util.VariationsTestUtils;
import org.chromium.base.test.util.CallbackHelper;
......@@ -83,7 +82,6 @@ public class VariationsSeedHolderTest {
@Before
public void setUp() throws IOException {
ServiceInit.setPrivateDataDirectorySuffix();
VariationsTestUtils.deleteSeeds();
}
......
......@@ -9,7 +9,6 @@ import static org.chromium.android_webview.test.OnlyRunIn.ProcessMode.SINGLE_PRO
import android.content.Intent;
import android.os.Handler;
import android.os.Looper;
import android.support.test.InstrumentationRegistry;
import android.support.test.filters.MediumTest;
import org.junit.After;
......@@ -20,7 +19,6 @@ import org.junit.runner.RunWith;
import org.chromium.android_webview.VariationsSeedLoader;
import org.chromium.android_webview.VariationsUtils;
import org.chromium.android_webview.services.ServiceInit;
import org.chromium.android_webview.test.services.MockVariationsSeedServer;
import org.chromium.android_webview.test.util.VariationsTestUtils;
import org.chromium.base.ContextUtils;
......@@ -108,10 +106,6 @@ public class VariationsSeedLoaderTest {
@Before
public void setUp() throws IOException {
mMainHandler = new Handler(Looper.getMainLooper());
ContextUtils.initApplicationContextForTests(
InstrumentationRegistry.getInstrumentation()
.getTargetContext().getApplicationContext());
ServiceInit.setPrivateDataDirectorySuffix();
RecordHistogram.setDisabledForTests(true);
VariationsTestUtils.deleteSeeds();
}
......
......@@ -7,18 +7,15 @@ package org.chromium.android_webview.test.services;
import static org.chromium.android_webview.test.OnlyRunIn.ProcessMode.SINGLE_PROCESS;
import android.os.ParcelFileDescriptor;
import android.support.test.InstrumentationRegistry;
import android.support.test.filters.MediumTest;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.chromium.android_webview.services.CrashReceiverService;
import org.chromium.android_webview.test.AwJUnit4ClassRunner;
import org.chromium.android_webview.test.OnlyRunIn;
import org.chromium.base.ContextUtils;
import java.io.File;
import java.io.IOException;
......@@ -29,13 +26,6 @@ import java.io.IOException;
@RunWith(AwJUnit4ClassRunner.class)
@OnlyRunIn(SINGLE_PROCESS)
public class CrashReceiverServiceTest {
@Before
public void setUp() throws Exception {
ContextUtils.initApplicationContextForTests(InstrumentationRegistry.getInstrumentation()
.getTargetContext()
.getApplicationContext());
}
/**
* Ensure that the minidump copying doesn't trigger when we pass it invalid file descriptors.
*/
......
......@@ -14,7 +14,6 @@ import android.os.ConditionVariable;
import android.os.IBinder;
import android.os.ParcelFileDescriptor;
import android.os.RemoteException;
import android.support.test.InstrumentationRegistry;
import android.support.test.filters.MediumTest;
import org.junit.After;
......@@ -45,9 +44,6 @@ public class VariationsSeedServerTest {
@Before
public void setUp() throws IOException {
ContextUtils.initApplicationContextForTests(
InstrumentationRegistry.getInstrumentation().getTargetContext()
.getApplicationContext());
mTempFile = File.createTempFile("test_variations_seed", null);
}
......
......@@ -48,6 +48,7 @@ android_apk("webview_instrumentation_apk") {
"//android_webview:android_webview_java",
"//android_webview:locale_pak_assets",
"//android_webview:platform_service_bridge_upstream_implementation_java",
"//android_webview/apk:apk_java",
"//base:base_java",
"//base:base_java_test_support",
"//components/heap_profiling:heap_profiling_java_test_support",
......
......@@ -36,7 +36,6 @@ import org.chromium.android_webview.JsResultReceiver;
import org.chromium.android_webview.test.AwTestContainerView;
import org.chromium.android_webview.test.NullContentsClient;
import org.chromium.base.CommandLine;
import org.chromium.base.ContextUtils;
import org.chromium.base.Log;
import org.chromium.base.TraceEvent;
import org.chromium.content_public.browser.NavigationController;
......@@ -70,9 +69,6 @@ public class AwShellActivity extends Activity {
AwShellResourceProvider.registerResources(this);
((AwShellApplication) getApplication()).initCommandLine();
ContextUtils.initApplicationContext(getApplicationContext());
AwBrowserProcess.loadLibrary(null);
if (CommandLine.getInstance().hasSwitch(AwShellSwitches.ENABLE_ATRACE)) {
......
......@@ -4,17 +4,16 @@
package org.chromium.android_webview.shell;
import android.app.Application;
import com.android.webview.chromium.WebViewApkApplication;
import org.chromium.base.CommandLine;
/**
* The android_webview shell Application subclass.
*/
public class AwShellApplication extends Application {
public void initCommandLine() {
if (!CommandLine.isInitialized()) {
public class AwShellApplication extends WebViewApkApplication {
@Override
protected void initCommandLine() {
CommandLine.initFromFile("/data/local/tmp/android-webview-command-line");
}
}
}
......@@ -13,7 +13,6 @@ import android.widget.LinearLayout;
import org.chromium.android_webview.AwBrowserProcess;
import org.chromium.android_webview.shell.AwShellResourceProvider;
import org.chromium.base.ContextUtils;
import org.chromium.base.StrictModeContext;
/**
......@@ -30,7 +29,6 @@ public class AwTestRunnerActivity extends Activity {
super.onCreate(savedInstanceState);
AwShellResourceProvider.registerResources(this);
ContextUtils.initApplicationContext(getApplicationContext());
try (StrictModeContext ctx = StrictModeContext.allowDiskReads()) {
AwBrowserProcess.loadLibrary(null);
}
......
......@@ -14,8 +14,6 @@ import android.os.Process;
import org.chromium.android_webview.AwBrowserProcess;
import org.chromium.android_webview.AwResource;
import org.chromium.android_webview.shell.R;
import org.chromium.base.CommandLine;
import org.chromium.base.ContextUtils;
/**
* This is a service for imitating a second browser process in the application.
......@@ -52,10 +50,8 @@ public class SecondBrowserProcess extends Service {
}
private void startBrowserProcess() throws Exception {
CommandLine.initFromFile("/data/local/tmp/android-webview-command-line");
AwResource.setResources(this.getResources());
AwResource.setConfigKeySystemUuidMapping(R.array.config_key_system_uuid_mapping);
ContextUtils.initApplicationContext(getApplicationContext());
AwBrowserProcess.loadLibrary(null);
AwBrowserProcess.start();
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment