From cb98f483a972190366ba611e34563d44e5288676 Mon Sep 17 00:00:00 2001 From: jbudorick Date: Mon, 3 Aug 2015 16:25:05 -0700 Subject: [PATCH] [Android] Reland CommandLine reset listeners. This is a reland of https://codereview.chromium.org/1254973003, which I had reverted in https://codereview.chromium.org/1263433002 because I thought all calls to FeatureUtilities.isDocumentMode would now happen after CommandLineFlags.setUp was called. This was mistaken and has broken L bots. BUG=516421 TBR=yfriedman@chromium.org,dgn@chromium.org Review URL: https://codereview.chromium.org/1269083005 Cr-Commit-Position: refs/heads/master@{#341639} --- .../java/src/org/chromium/base/CommandLine.java | 25 ++++++++++++++++++++++ .../chrome/browser/util/FeatureUtilities.java | 22 +++++++++++++------ 2 files changed, 41 insertions(+), 6 deletions(-) diff --git a/base/android/java/src/org/chromium/base/CommandLine.java b/base/android/java/src/org/chromium/base/CommandLine.java index 9f5407921a9f..409329cfabf8 100644 --- a/base/android/java/src/org/chromium/base/CommandLine.java +++ b/base/android/java/src/org/chromium/base/CommandLine.java @@ -16,6 +16,7 @@ import java.io.Reader; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; +import java.util.List; import java.util.concurrent.atomic.AtomicReference; /** @@ -25,6 +26,15 @@ import java.util.concurrent.atomic.AtomicReference; * ContentShellApplication.COMMAND_LINE_FILE or ChromeShellApplication.COMMAND_LINE_FILE. **/ public abstract class CommandLine { + /** + * Allows classes who cache command line flags to be notified when those arguments are updated + * at runtime. This happens in tests. + */ + public interface ResetListener { + /** Called when the command line arguments are reset. */ + void onCommandLineReset(); + } + // Public abstract interface, implemented in derived classes. // All these methods reflect their native-side counterparts. /** @@ -87,6 +97,7 @@ public abstract class CommandLine { return false; } + private static final List sResetListeners = new ArrayList<>(); private static final AtomicReference sCommandLine = new AtomicReference(); @@ -132,6 +143,20 @@ public abstract class CommandLine { @VisibleForTesting public static void reset() { setInstance(null); + ThreadUtils.postOnUiThread(new Runnable() { + @Override + public void run() { + for (ResetListener listener : sResetListeners) listener.onCommandLineReset(); + } + }); + } + + public static void addResetListener(ResetListener listener) { + sResetListeners.add(listener); + } + + public static void removeResetListener(ResetListener listener) { + sResetListeners.remove(listener); } /** diff --git a/chrome/android/java/src/org/chromium/chrome/browser/util/FeatureUtilities.java b/chrome/android/java/src/org/chromium/chrome/browser/util/FeatureUtilities.java index 112ab3cd6ff8..b32c299cf10a 100644 --- a/chrome/android/java/src/org/chromium/chrome/browser/util/FeatureUtilities.java +++ b/chrome/android/java/src/org/chromium/chrome/browser/util/FeatureUtilities.java @@ -33,7 +33,8 @@ public class FeatureUtilities { private static Boolean sHasGoogleAccountAuthenticator; private static Boolean sHasRecognitionIntentHandler; private static Boolean sDocumentModeDisabled; - private static Boolean sCustomTabVisible; + /** Used to track if cached command line flags should be refreshed. */ + private static CommandLine.ResetListener sResetListener = null; /** * Determines whether or not the {@link RecognizerIntent#ACTION_WEB_SEARCH} {@link Intent} @@ -98,6 +99,7 @@ public class FeatureUtilities { */ public static boolean isDocumentMode(Context context) { if (sDocumentModeDisabled == null && CommandLine.isInitialized()) { + initResetListener(); sDocumentModeDisabled = CommandLine.getInstance().hasSwitch( ChromeSwitches.DISABLE_DOCUMENT_MODE); } @@ -130,7 +132,6 @@ public class FeatureUtilities { * @param visible Whether a custom tab is visible. */ public static void setCustomTabVisible(boolean visible) { - sCustomTabVisible = visible; nativeSetCustomTabVisible(visible); } @@ -138,10 +139,19 @@ public class FeatureUtilities { * @return Whether a custom tab is visible. */ public static boolean getCustomTabVisible() { - if (sCustomTabVisible == null) { - sCustomTabVisible = nativeGetCustomTabVisible(); - } - return sCustomTabVisible; + return nativeGetCustomTabVisible(); + } + + private static void initResetListener() { + if (sResetListener != null) return; + + sResetListener = new CommandLine.ResetListener() { + @Override + public void onCommandLineReset() { + sDocumentModeDisabled = null; + } + }; + CommandLine.addResetListener(sResetListener); } private static native void nativeSetDocumentModeEnabled(boolean enabled); -- 2.11.4.GIT