OSDN Git Service

Use less static synchronized
authorJohn Reck <jreck@google.com>
Thu, 20 Sep 2012 20:18:59 +0000 (13:18 -0700)
committerJohn Reck <jreck@google.com>
Thu, 20 Sep 2012 22:25:16 +0000 (15:25 -0700)
 Bug: 6482144

Change-Id: I86161e3298101c10a112add406615001561e649e

api/current.txt
core/java/android/webkit/WebViewDatabase.java
core/java/android/webkit/WebViewDatabaseClassic.java
core/java/android/webkit/WebViewFactory.java

index 0d09aa4..9dd67dd 100644 (file)
@@ -27467,7 +27467,7 @@ package android.webkit {
     method public void clearFormData();
     method public void clearHttpAuthUsernamePassword();
     method public void clearUsernamePassword();
-    method public static synchronized android.webkit.WebViewDatabase getInstance(android.content.Context);
+    method public static android.webkit.WebViewDatabase getInstance(android.content.Context);
     method public boolean hasFormData();
     method public boolean hasHttpAuthUsernamePassword();
     method public boolean hasUsernamePassword();
index 62ec0d5..5597259 100644 (file)
@@ -40,7 +40,7 @@ public class WebViewDatabase {
     protected WebViewDatabase() {
     }
 
-    public static synchronized WebViewDatabase getInstance(Context context) {
+    public static WebViewDatabase getInstance(Context context) {
         return WebViewFactory.getProvider().getWebViewDatabase(context);
     }
 
index c804b90..be01028 100644 (file)
@@ -52,6 +52,7 @@ final class WebViewDatabaseClassic extends WebViewDatabase {
     //          implemented for b/5265606.
 
     private static WebViewDatabaseClassic sInstance = null;
+    private static final Object sInstanceLock = new Object();
 
     private static SQLiteDatabase sDatabase = null;
 
@@ -99,7 +100,7 @@ final class WebViewDatabaseClassic extends WebViewDatabase {
     // Initially true until the background thread completes.
     private boolean mInitialized = false;
 
-    WebViewDatabaseClassic(final Context context) {
+    private WebViewDatabaseClassic(final Context context) {
         JniUtil.setContext(context);
         new Thread() {
             @Override
@@ -111,11 +112,13 @@ final class WebViewDatabaseClassic extends WebViewDatabase {
         // Singleton only, use getInstance()
     }
 
-    public static synchronized WebViewDatabaseClassic getInstance(Context context) {
-        if (sInstance == null) {
-            sInstance = new WebViewDatabaseClassic(context);
+    public static WebViewDatabaseClassic getInstance(Context context) {
+        synchronized (sInstanceLock) {
+            if (sInstance == null) {
+                sInstance = new WebViewDatabaseClassic(context);
+            }
+            return sInstance;
         }
-        return sInstance;
     }
 
     private synchronized void init(Context context) {
index 2fc9b39..b833a01 100644 (file)
@@ -41,36 +41,39 @@ class WebViewFactory {
     // Cache the factory both for efficiency, and ensure any one process gets all webviews from the
     // same provider.
     private static WebViewFactoryProvider sProviderInstance;
+    private static final Object sProviderLock = new Object();
 
-    static synchronized WebViewFactoryProvider getProvider() {
-        // For now the main purpose of this function (and the factory abstraction) is to keep
-        // us honest and minimize usage of WebViewClassic internals when binding the proxy.
-        if (sProviderInstance != null) return sProviderInstance;
+    static WebViewFactoryProvider getProvider() {
+        synchronized (sProviderLock) {
+            // For now the main purpose of this function (and the factory abstraction) is to keep
+            // us honest and minimize usage of WebViewClassic internals when binding the proxy.
+            if (sProviderInstance != null) return sProviderInstance;
 
-        // For debug builds, we allow a system property to specify that we should use the
-        // Chromium powered WebView. This enables us to switch between implementations
-        // at runtime. For user (release) builds, don't allow this.
-        if (Build.IS_DEBUGGABLE && SystemProperties.getBoolean("webview.use_chromium", false)) {
-            StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskReads();
-            try {
-                sProviderInstance = loadChromiumProvider();
-                if (DEBUG) Log.v(LOGTAG, "Loaded Chromium provider: " + sProviderInstance);
-            } finally {
-                StrictMode.setThreadPolicy(oldPolicy);
+            // For debug builds, we allow a system property to specify that we should use the
+            // Chromium powered WebView. This enables us to switch between implementations
+            // at runtime. For user (release) builds, don't allow this.
+            if (Build.IS_DEBUGGABLE && SystemProperties.getBoolean("webview.use_chromium", false)) {
+                StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskReads();
+                try {
+                    sProviderInstance = loadChromiumProvider();
+                    if (DEBUG) Log.v(LOGTAG, "Loaded Chromium provider: " + sProviderInstance);
+                } finally {
+                    StrictMode.setThreadPolicy(oldPolicy);
+                }
             }
-        }
 
-        if (sProviderInstance == null) {
-            if (DEBUG) Log.v(LOGTAG, "Falling back to default provider: "
-                    + DEFAULT_WEBVIEW_FACTORY);
-            sProviderInstance = getFactoryByName(DEFAULT_WEBVIEW_FACTORY,
-                    WebViewFactory.class.getClassLoader());
             if (sProviderInstance == null) {
-                if (DEBUG) Log.v(LOGTAG, "Falling back to explicit linkage");
-                sProviderInstance = new WebViewClassic.Factory();
+                if (DEBUG) Log.v(LOGTAG, "Falling back to default provider: "
+                        + DEFAULT_WEBVIEW_FACTORY);
+                sProviderInstance = getFactoryByName(DEFAULT_WEBVIEW_FACTORY,
+                        WebViewFactory.class.getClassLoader());
+                if (sProviderInstance == null) {
+                    if (DEBUG) Log.v(LOGTAG, "Falling back to explicit linkage");
+                    sProviderInstance = new WebViewClassic.Factory();
+                }
             }
+            return sProviderInstance;
         }
-        return sProviderInstance;
     }
 
     // TODO: This allows us to have the legacy and Chromium WebView coexist for development