diff options
author | 2017-01-05 09:37:32 +0000 | |
---|---|---|
committer | 2017-01-05 09:37:32 +0000 | |
commit | 2c3818158aa31798b8df17330605d7da396d80cf (patch) | |
tree | 215d450c7ff34cde460d6203d4e857374d23cdf1 | |
parent | 7d149142399a82350c0030abbd2ad4fe9937caf1 (diff) | |
parent | c8111e07d0eb360f12b2c09bb558080d06d2dea4 (diff) |
Merge "Synchronize access to WebViewZygote." am: fd6f631de5
am: c8111e07d0
Change-Id: I8c389a9c06bd0b8d1964756323e6c7e010901f89
-rw-r--r-- | core/java/android/webkit/WebViewZygote.java | 117 |
1 files changed, 77 insertions, 40 deletions
diff --git a/core/java/android/webkit/WebViewZygote.java b/core/java/android/webkit/WebViewZygote.java index e0d589a8a8fb..2d6f44352ba7 100644 --- a/core/java/android/webkit/WebViewZygote.java +++ b/core/java/android/webkit/WebViewZygote.java @@ -24,6 +24,8 @@ import android.os.ZygoteProcess; import android.text.TextUtils; import android.util.Log; +import com.android.internal.annotations.GuardedBy; + import java.io.File; import java.io.IOException; import java.util.ArrayList; @@ -38,70 +40,104 @@ public class WebViewZygote { private static final String WEBVIEW_ZYGOTE_SERVICE_32 = "webview_zygote32"; private static final String WEBVIEW_ZYGOTE_SERVICE_64 = "webview_zygote64"; + /** + * Lock object that protects all other static members. + */ + private static final Object sLock = new Object(); + + /** + * Instance that maintains the socket connection to the zygote. This is null if the zygote + * is not running or is not connected. + */ + @GuardedBy("sLock") private static ZygoteProcess sZygote; + /** + * Information about the selected WebView package. This is set from #onWebViewProviderChanged(). + */ + @GuardedBy("sLock") private static PackageInfo sPackage; + /** + * Flag for whether multi-process WebView is enabled. If this is false, the zygote + * will not be started. + */ + @GuardedBy("sLock") private static boolean sMultiprocessEnabled = false; public static ZygoteProcess getProcess() { - connectToZygoteIfNeeded(); - return sZygote; + synchronized (sLock) { + connectToZygoteIfNeededLocked(); + return sZygote; + } } public static String getPackageName() { - return sPackage.packageName; + synchronized (sLock) { + return sPackage.packageName; + } } public static boolean isMultiprocessEnabled() { - return sMultiprocessEnabled && sPackage != null; + synchronized (sLock) { + return sMultiprocessEnabled && sPackage != null; + } } public static void setMultiprocessEnabled(boolean enabled) { - sMultiprocessEnabled = enabled; - - // When toggling between multi-process being on/off, start or stop the - // service. If it is enabled and the zygote is not yet started, bring up the service. - // Otherwise, bring down the service. The name may be null if the package - // information has not yet been resolved. - final String serviceName = getServiceName(); - if (serviceName == null) return; - - if (enabled && sZygote == null) { - SystemService.start(serviceName); - } else { - SystemService.stop(serviceName); - sZygote = null; + synchronized (sLock) { + sMultiprocessEnabled = enabled; + + // When toggling between multi-process being on/off, start or stop the + // service. If it is enabled and the zygote is not yet started, bring up the service. + // Otherwise, bring down the service. The name may be null if the package + // information has not yet been resolved. + final String serviceName = getServiceNameLocked(); + if (serviceName == null) return; + + if (enabled && sZygote == null) { + SystemService.start(serviceName); + } else { + SystemService.stop(serviceName); + sZygote = null; + } } } public static void onWebViewProviderChanged(PackageInfo packageInfo) { - sPackage = packageInfo; - - // If multi-process is not enabled, then do not start the zygote service. - if (!sMultiprocessEnabled) { - return; - } + String serviceName; + synchronized (sLock) { + sPackage = packageInfo; - final String serviceName = getServiceName(); + // If multi-process is not enabled, then do not start the zygote service. + if (!sMultiprocessEnabled) { + return; + } - if (SystemService.isStopped(serviceName)) { - SystemService.start(serviceName); - } else if (sZygote != null) { - SystemService.restart(serviceName); - } + serviceName = getServiceNameLocked(); + sZygote = null; - try { - SystemService.waitForState(serviceName, SystemService.State.RUNNING, 5000); - } catch (TimeoutException e) { - Log.e(LOGTAG, "Timed out waiting for " + serviceName); - return; + // The service may enter the RUNNING state before it opens the socket, + // so connectToZygoteIfNeededLocked() may still fail. + if (SystemService.isStopped(serviceName)) { + SystemService.start(serviceName); + } else { + SystemService.restart(serviceName); + } + + try { + SystemService.waitForState(serviceName, SystemService.State.RUNNING, 5000); + } catch (TimeoutException e) { + Log.e(LOGTAG, "Timed out waiting for " + serviceName); + return; + } + + connectToZygoteIfNeededLocked(); } - - connectToZygoteIfNeeded(); } - private static String getServiceName() { + @GuardedBy("sLock") + private static String getServiceNameLocked() { if (sPackage == null) return null; @@ -113,7 +149,8 @@ public class WebViewZygote { return WEBVIEW_ZYGOTE_SERVICE_32; } - private static void connectToZygoteIfNeeded() { + @GuardedBy("sLock") + private static void connectToZygoteIfNeededLocked() { if (sZygote != null) return; @@ -122,7 +159,7 @@ public class WebViewZygote { return; } - final String serviceName = getServiceName(); + final String serviceName = getServiceNameLocked(); if (!SystemService.isRunning(serviceName)) { Log.e(LOGTAG, serviceName + " is not running"); return; |