[Crow] Gate crow by language and locale.

Bug: 1353872
Change-Id: I5d9dca3abdb0de636afb6d68fcbf8bc9aab5a256
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3877577
Commit-Queue: Sophey Dong <sophey@chromium.org>
Reviewed-by: Travis Skare <skare@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1044652}
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/share/crow/CrowButtonDelegateImpl.java b/chrome/android/java/src/org/chromium/chrome/browser/share/crow/CrowButtonDelegateImpl.java
index 970638d..0d391ba 100644
--- a/chrome/android/java/src/org/chromium/chrome/browser/share/crow/CrowButtonDelegateImpl.java
+++ b/chrome/android/java/src/org/chromium/chrome/browser/share/crow/CrowButtonDelegateImpl.java
@@ -11,8 +11,11 @@
 import androidx.browser.customtabs.CustomTabsIntent;
 
 import org.chromium.base.Callback;
+import org.chromium.base.LocaleUtils;
+import org.chromium.chrome.browser.ChromeActivitySessionTracker;
 import org.chromium.chrome.browser.customtabs.CustomTabActivity;
 import org.chromium.chrome.browser.flags.ChromeFeatureList;
+import org.chromium.chrome.browser.language.AppLocaleUtils;
 import org.chromium.chrome.browser.optimization_guide.OptimizationGuideBridgeFactory;
 import org.chromium.chrome.browser.privacy.settings.PrivacyPreferencesManagerImpl;
 import org.chromium.chrome.browser.profiles.Profile;
@@ -33,6 +36,8 @@
 public class CrowButtonDelegateImpl implements CrowButtonDelegate {
     /** Domain to ID map, populated on first read. */
     private HashMap<String, String> mDomainIdMap;
+    // Tracker used to get the latest country of the user.
+    private final ChromeActivitySessionTracker mChromeActivitySessionTracker;
 
     private static final String APP_MENU_BUTTON_TEXT_PARAM = "AppMenuButtonText";
     private static final String DEBUG_SERVER_URL_PARAM = "DebugServerURL";
@@ -44,7 +49,9 @@
     private static final String TAG = "CrowButton";
 
     /** Constructs a new {@link CrowButtonDelegateImpl}. */
-    public CrowButtonDelegateImpl() {}
+    public CrowButtonDelegateImpl() {
+        mChromeActivitySessionTracker = ChromeActivitySessionTracker.getInstance();
+    }
 
     // Lazy initialization of OptimizationGuideBridgeFactory
     private static class OptimizationGuideBridgeFactoryHolder {
@@ -118,8 +125,23 @@
         }
     }
 
+    /**
+     * Returns true if the user is in the US using en-US, and false otherwise.
+     *
+     * <p>This should match how Finch gates features by locale and country. See
+     * VariationsService::GetLatestCountry() and language::GetApplicationLocale().
+     */
+    private boolean isEnabledForLocaleAndCountry() {
+        String country = mChromeActivitySessionTracker.getVariationsLatestCountry();
+        String locale = AppLocaleUtils.getAppLanguagePref();
+        if (locale == null) {
+            locale = LocaleUtils.getDefaultLocaleString();
+        }
+        return country != null && country.equals("us") && locale.equals("en-US");
+    }
+
     public boolean isCrowEnabled() {
-        return ChromeFeatureList.isInitialized()
+        return isEnabledForLocaleAndCountry() && ChromeFeatureList.isInitialized()
                 && ChromeFeatureList.isEnabled(ChromeFeatureList.SHARE_CROW_BUTTON);
     }
 
diff --git a/chrome/android/javatests/src/org/chromium/chrome/browser/share/crow/CrowButtonDelegateImplTest.java b/chrome/android/javatests/src/org/chromium/chrome/browser/share/crow/CrowButtonDelegateImplTest.java
index a9147d0..e267c14 100644
--- a/chrome/android/javatests/src/org/chromium/chrome/browser/share/crow/CrowButtonDelegateImplTest.java
+++ b/chrome/android/javatests/src/org/chromium/chrome/browser/share/crow/CrowButtonDelegateImplTest.java
@@ -8,32 +8,40 @@
 
 import androidx.test.filters.SmallTest;
 
+import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 
 import org.chromium.base.test.BaseJUnit4ClassRunner;
+import org.chromium.base.test.util.Batch;
 import org.chromium.chrome.test.ChromeBrowserTestRule;
+import org.chromium.content_public.browser.test.util.TestThreadUtils;
 import org.chromium.url.GURL;
 
 import java.util.HashMap;
+import java.util.concurrent.ExecutionException;
 
 /**
  * Tests of the CrowButtonDelegateImpl.
  * Requires native for GURL and canonicalization tests.
  */
 @RunWith(BaseJUnit4ClassRunner.class)
+@Batch(Batch.PER_CLASS)
 public class CrowButtonDelegateImplTest {
     @Rule
     public final ChromeBrowserTestRule mBrowserTestRule = new ChromeBrowserTestRule();
 
-    private final CrowButtonDelegateImpl mCrowButtonDelegate = new CrowButtonDelegateImpl();
+    private CrowButtonDelegateImpl mCrowButtonDelegate;
+
+    @Before
+    public void setUp() throws ExecutionException {
+        mCrowButtonDelegate = TestThreadUtils.runOnUiThreadBlocking(CrowButtonDelegateImpl::new);
+    }
 
     @Test
     @SmallTest
     public void testBuildServerUrl() {
-        CrowButtonDelegateImpl delegate = new CrowButtonDelegateImpl();
-
         final GURL serverUrl = new GURL("https://www.foo.com/v1/api?q=hi");
         // No query string, http rather than https.
         final GURL serverUrlWithoutQueryString = new GURL("http://www.foo.com/v1/api");
@@ -43,39 +51,39 @@
         boolean isFollowing = false;
 
         assertEquals("",
-                delegate.buildServerUrlInternal(
+                mCrowButtonDelegate.buildServerUrlInternal(
                         GURL.emptyGURL(), shareUrl1, shareUrl1, "", allowMetrics, isFollowing));
 
         // Baseline/common case.
         assertEquals(
                 "https://www.foo.com/v1/api?q=hi&pageUrl=https%3A%2F%2Ftestsitewearesharing.com%2Fblog%2Fentry&entry=menu&relCanonUrl=https%3A%2F%2Ftestsitewearesharing.com%2Fblog%2Fentry&publicationId=pubId1&metrics=true",
-                delegate.buildServerUrlInternal(
+                mCrowButtonDelegate.buildServerUrlInternal(
                         serverUrl, shareUrl1, shareUrl1, "pubId1", allowMetrics, isFollowing));
 
         // Sending a URL with urlparams of its own.
         assertEquals(
                 "https://www.foo.com/v1/api?q=hi&pageUrl=https%3A%2F%2Ftestsitewearesharing.com%2F%3Fblog%3D1%26entry%3D2&entry=menu&relCanonUrl=https%3A%2F%2Ftestsitewearesharing.com%2F%3Fblog%3D1%26entry%3D2&publicationId=pubId2&metrics=true",
-                delegate.buildServerUrlInternal(
+                mCrowButtonDelegate.buildServerUrlInternal(
                         serverUrl, shareUrl2, shareUrl2, "pubId2", allowMetrics, isFollowing));
 
         // Empty canonical URL is ok, passes as empty param.
         assertEquals(
                 "https://www.foo.com/v1/api?q=hi&pageUrl=https%3A%2F%2Ftestsitewearesharing.com%2Fblog%2Fentry&entry=menu&relCanonUrl=&publicationId=pubId1&metrics=true",
-                delegate.buildServerUrlInternal(serverUrl, shareUrl1, GURL.emptyGURL(), "pubId1",
-                        allowMetrics, isFollowing));
+                mCrowButtonDelegate.buildServerUrlInternal(serverUrl, shareUrl1, GURL.emptyGURL(),
+                        "pubId1", allowMetrics, isFollowing));
 
         // Experimental URL can be passed with an empty set of params.
         assertEquals(
                 "http://www.foo.com/v1/api?pageUrl=https%3A%2F%2Ftestsitewearesharing.com%2Fblog%2Fentry&entry=menu&relCanonUrl=https%3A%2F%2Ftestsitewearesharing.com%2Fblog%2Fentry&publicationId=pubId1&metrics=true",
-                delegate.buildServerUrlInternal(serverUrlWithoutQueryString, shareUrl1, shareUrl1,
-                        "pubId1", allowMetrics, isFollowing));
+                mCrowButtonDelegate.buildServerUrlInternal(serverUrlWithoutQueryString, shareUrl1,
+                        shareUrl1, "pubId1", allowMetrics, isFollowing));
 
         // Metrics off and already following should be reflected.
         allowMetrics = false;
         isFollowing = true;
         assertEquals(
                 "https://www.foo.com/v1/api?q=hi&pageUrl=https%3A%2F%2Ftestsitewearesharing.com%2Fblog%2Fentry&entry=menu&relCanonUrl=https%3A%2F%2Ftestsitewearesharing.com%2Fblog%2Fentry&publicationId=pubId1&metrics=false&following=true",
-                delegate.buildServerUrlInternal(
+                mCrowButtonDelegate.buildServerUrlInternal(
                         serverUrl, shareUrl1, shareUrl1, "pubId1", allowMetrics, isFollowing));
     }