Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2082)

Unified Diff: android_webview/java/src/org/chromium/android_webview/PlatformServiceBridge.java

Issue 2815403002: WebView: Refactor PlatformServiceBridge.getInstance() (Closed)
Patch Set: Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: android_webview/java/src/org/chromium/android_webview/PlatformServiceBridge.java
diff --git a/android_webview/java/src/org/chromium/android_webview/PlatformServiceBridge.java b/android_webview/java/src/org/chromium/android_webview/PlatformServiceBridge.java
index 7957e96e4f5a496dfce8b0cd7d105bdc0858867a..1356aa428068ed33a4b642a25bfa354e68ce5b38 100644
--- a/android_webview/java/src/org/chromium/android_webview/PlatformServiceBridge.java
+++ b/android_webview/java/src/org/chromium/android_webview/PlatformServiceBridge.java
@@ -4,10 +4,8 @@
package org.chromium.android_webview;
-import android.content.Context;
import android.webkit.ValueCallback;
-import org.chromium.base.ContextUtils;
import org.chromium.base.Log;
import org.chromium.base.ThreadUtils;
@@ -22,53 +20,44 @@ public class PlatformServiceBridge {
private static final String PLATFORM_SERVICE_BRIDGE =
"com.android.webview.chromium.PlatformServiceBridgeGoogle";
- // Only written by getOrCreateInstance on the UI thread (aside from injectInstance, for
- // testing), but read by getInstance on arbitrary threads.
- private static volatile PlatformServiceBridge sInstance;
+ private static PlatformServiceBridge sInstance;
+ private static final Object sInstanceLock = new Object();
protected PlatformServiceBridge() {}
- public static PlatformServiceBridge getOrCreateInstance() {
- // Just to avoid race conditions on sInstance - nothing special about the UI thread.
- ThreadUtils.assertOnUiThread();
-
- if (sInstance != null) return sInstance;
-
- // Try to get a specialized service bridge.
- try {
- Class<?> cls = Class.forName(PLATFORM_SERVICE_BRIDGE);
- sInstance = (PlatformServiceBridge) cls.getDeclaredConstructor(Context.class)
- .newInstance(ContextUtils.getApplicationContext());
+ public static PlatformServiceBridge getInstance() {
+ synchronized (sInstanceLock) {
+ if (sInstance != null) return sInstance;
+
+ // Try to get a specialized service bridge.
+ try {
+ Class<?> cls = Class.forName(PLATFORM_SERVICE_BRIDGE);
+ sInstance = (PlatformServiceBridge) cls.getDeclaredConstructor().newInstance();
+ return sInstance;
+ } catch (ClassNotFoundException e) {
+ // This is not an error; it just means this device doesn't have specialized
+ // services.
+ } catch (IllegalAccessException | IllegalArgumentException | InstantiationException
+ | NoSuchMethodException e) {
+ Log.e(TAG, "Failed to get " + PLATFORM_SERVICE_BRIDGE + ": " + e);
+ } catch (InvocationTargetException e) {
+ Log.e(TAG, "Failed invocation to get " + PLATFORM_SERVICE_BRIDGE + ": ",
+ e.getCause());
+ }
+
+ // Otherwise, get the generic service bridge.
+ sInstance = new PlatformServiceBridge();
return sInstance;
- } catch (ClassNotFoundException e) {
- // This is not an error; it just means this device doesn't have specialized
- // services.
- } catch (IllegalAccessException | IllegalArgumentException | InstantiationException
- | NoSuchMethodException e) {
- Log.e(TAG, "Failed to get " + PLATFORM_SERVICE_BRIDGE + ": " + e);
- } catch (InvocationTargetException e) {
- Log.e(TAG, "Failed invocation to get " + PLATFORM_SERVICE_BRIDGE + ":", e.getCause());
}
-
- // Otherwise, get the generic service bridge.
- sInstance = new PlatformServiceBridge();
-
- return sInstance;
- }
-
- public static PlatformServiceBridge getInstance() {
- if (sInstance == null) throw new IllegalStateException("PlatformServiceBridge not created");
- return sInstance;
}
// Provide a mocked PlatformServiceBridge for testing.
public static void injectInstance(PlatformServiceBridge testBridge) {
- sInstance = testBridge;
- }
-
- // TODO(paulmiller): Remove after changing downstream users.
- public static PlatformServiceBridge getInstance(Context context) {
- return getInstance();
+ // Although reference assignments are atomic, we still wouldn't want to assign it in the
+ // middle of getInstance().
+ synchronized (sInstanceLock) {
+ sInstance = testBridge;
+ }
}
// Can WebView use Google Play Services (a.k.a. GMS)?

Powered by Google App Engine
This is Rietveld 408576698