Skip to content

Commit

Permalink
Android: Simplify singleton getters involving AppHooks
Browse files Browse the repository at this point in the history
I'd guess that these getters used to be lazy because they
relied on Application context to be created. However, now
that we've moved to AppHooks, having the context is no
longer necessary, so the singletons can be created during
class initialization.

As far as I can tell, none of these classes are useful without
their singleton, so there's no need to make them lazy.

Change-Id: I5e9c4b9025761a2c6da2daa9d076b9f70b85b002
Reviewed-on: https://chromium-review.googlesource.com/766515
Reviewed-by: Tommy Nyquist <nyquist@chromium.org>
Commit-Queue: agrieve <agrieve@chromium.org>
Cr-Commit-Position: refs/heads/master@{#516845}
  • Loading branch information
agrieve authored and Commit Bot committed Nov 15, 2017
1 parent 26855ed commit 003eedb
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;

/**
* Implementation of the ICustomTabsConnectionService interface.
Expand Down Expand Up @@ -137,7 +136,8 @@ public class CustomTabsConnection {
@VisibleForTesting
static final String REDIRECT_ENDPOINT_KEY = "android.support.customtabs.REDIRECT_ENDPOINT";

private static AtomicReference<CustomTabsConnection> sInstance = new AtomicReference<>();
private static final CustomTabsConnection sInstance =
AppHooks.get().createCustomTabsConnection();

/** Holds the parameters for the current speculation. */
@VisibleForTesting
Expand Down Expand Up @@ -228,10 +228,7 @@ public CustomTabsConnection() {
* @return The unique instance of ChromeCustomTabsConnection.
*/
public static CustomTabsConnection getInstance() {
if (sInstance.get() == null) {
sInstance.compareAndSet(null, AppHooks.get().createCustomTabsConnection());
}
return sInstance.get();
return sInstance;
}

/**
Expand Down Expand Up @@ -362,7 +359,7 @@ public boolean warmup(long flags) {
* @return Whether {@link CustomTabsConnection#warmup(long)} has been called.
*/
public static boolean hasWarmUpBeenFinished() {
return getInstance().mWarmupHasBeenFinished.get();
return sInstance.mWarmupHasBeenFinished.get();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import org.chromium.chrome.browser.AppHooks;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;

/**
* Utility class for external authentication tools.
Expand All @@ -41,9 +40,8 @@ public class ExternalAuthUtils {
public static final int FLAG_SHOULD_BE_SYSTEM = 1 << 1;
private static final String TAG = "ExternalAuthUtils";

// Use an AtomicReference since getInstance() can be called from multiple threads.
private static AtomicReference<ExternalAuthUtils> sInstance =
new AtomicReference<ExternalAuthUtils>();
private static final ExternalAuthUtils sInstance = AppHooks.get().createExternalAuthUtils();

private final SparseHistogramSample mConnectionResultHistogramSample =
new SparseHistogramSample("GooglePlayServices.ConnectionResult");
private final TimesHistogramSample mRegistrationTimeHistogramSample = new TimesHistogramSample(
Expand All @@ -53,10 +51,7 @@ public class ExternalAuthUtils {
* Returns the singleton instance of ExternalAuthUtils, creating it if needed.
*/
public static ExternalAuthUtils getInstance() {
if (sInstance.get() == null) {
sInstance.compareAndSet(null, AppHooks.get().createExternalAuthUtils());
}
return sInstance.get();
return sInstance;
}

/**
Expand Down Expand Up @@ -217,7 +212,7 @@ public void run() {
* @return true if and only if Google Play Services can be used
*/
public static boolean canUseGooglePlayServices() {
return getInstance().canUseGooglePlayServices(new UserRecoverableErrorHandler.Silent());
return sInstance.canUseGooglePlayServices(new UserRecoverableErrorHandler.Silent());
}

/**
Expand Down Expand Up @@ -248,7 +243,7 @@ public boolean canUseFirstPartyGooglePlayServices(
* @return true if and only if first-party Google Play Services can be used
*/
public static boolean canUseFirstPartyGooglePlayServices() {
return getInstance().canUseFirstPartyGooglePlayServices(
return sInstance.canUseFirstPartyGooglePlayServices(
new UserRecoverableErrorHandler.Silent());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@

import java.lang.ref.WeakReference;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

import javax.annotation.Nullable;

Expand All @@ -37,7 +36,8 @@
* Thread-safe: This class may be accessed from any thread.
*/
public class MultiWindowUtils implements ActivityStateListener {
private static AtomicReference<MultiWindowUtils> sInstance = new AtomicReference<>();
// getInstance() is called early in start-up, so there is not point in lazily initializing it.
private static final MultiWindowUtils sInstance = AppHooks.get().createMultiWindowUtils();

// Used to keep track of whether ChromeTabbedActivity2 is running. A tri-state Boolean is
// used in case both activities die in the background and MultiWindowUtils is recreated.
Expand All @@ -46,13 +46,10 @@ public class MultiWindowUtils implements ActivityStateListener {
private boolean mIsInMultiWindowModeForTesting;

/**
* Returns the singleton instance of MultiWindowUtils, creating it if needed.
* Returns the singleton instance of MultiWindowUtils.
*/
public static MultiWindowUtils getInstance() {
if (sInstance.get() == null) {
sInstance.compareAndSet(null, AppHooks.get().createMultiWindowUtils());
}
return sInstance.get();
return sInstance;
}

/**
Expand Down Expand Up @@ -90,11 +87,11 @@ public Class<? extends Activity> getOpenInOtherWindowActivity(Activity current)
// If a second ChromeTabbedActivity is created, MultiWindowUtils needs to listen for
// activity state changes to facilitate determining which ChromeTabbedActivity should
// be used for intents.
ApplicationStatus.registerStateListenerForAllActivities(sInstance.get());
ApplicationStatus.registerStateListenerForAllActivities(sInstance);
return ChromeTabbedActivity.class;
} else if (current instanceof ChromeTabbedActivity) {
mTabbedActivity2TaskRunning = true;
ApplicationStatus.registerStateListenerForAllActivities(sInstance.get());
ApplicationStatus.registerStateListenerForAllActivities(sInstance);
return ChromeTabbedActivity2.class;
} else {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,20 @@
import org.chromium.chrome.browser.AppHooks;
import org.chromium.chrome.browser.tab.Tab;

import java.util.concurrent.atomic.AtomicReference;

/**
* Utility class for managing revenue sharing information.
*/
@JNINamespace("chrome::android")
public class RevenueStats {
private static final String PREF_RLZ_NOTIFIED = "rlz_first_search_notified";

// Use an AtomicReference since getInstance() can be called from multiple threads.
private static AtomicReference<RevenueStats> sInstance = new AtomicReference<RevenueStats>();
private static final RevenueStats sInstance = AppHooks.get().createRevenueStatsInstance();

/**
* Returns the singleton instance of ExternalAuthUtils, creating it if needed.
*/
public static RevenueStats getInstance() {
if (sInstance.get() == null) {
sInstance.compareAndSet(null, AppHooks.get().createRevenueStatsInstance());
}
return sInstance.get();
return sInstance;
}

/**
Expand Down

0 comments on commit 003eedb

Please sign in to comment.