From b7b8c3e71d4c61ae3b72ffb14d2714437ab07ee0 Mon Sep 17 00:00:00 2001 From: Aleksey Khoroshilov Date: Thu, 21 Sep 2023 23:35:52 +0700 Subject: [PATCH] Download Griffin seed on first launch. --- android/java/apk_for_test.flags | 3 ++ .../chromium/chrome/browser/BytecodeTest.java | 12 ++++- build/android/bytecode/bytecode_rewriter.gni | 2 + ...raveVariationsSeedFetcherClassAdapter.java | 7 +++ components/variations/BUILD.gn | 9 +--- components/variations/android/BUILD.gn | 15 ++++-- .../firstrun/BraveVariationsConfig.template | 13 ++++++ .../firstrun/BraveVariationsSeedFetcher.java | 46 +++++++++++++------ components/variations/buildflags.gni | 12 +++++ test/BUILD.gn | 1 + 10 files changed, 95 insertions(+), 25 deletions(-) create mode 100644 components/variations/android/java/src/org/chromium/components/variations/firstrun/BraveVariationsConfig.template create mode 100644 components/variations/buildflags.gni diff --git a/android/java/apk_for_test.flags b/android/java/apk_for_test.flags index 1ab121f15d38..64d6070d0ac2 100644 --- a/android/java/apk_for_test.flags +++ b/android/java/apk_for_test.flags @@ -634,6 +634,9 @@ } -keep class org.chromium.components.variations.firstrun.VariationsSeedFetcher { + *** sLock; + *** DEFAULT_VARIATIONS_SERVER_URL; + *** DEFAULT_FAST_VARIATIONS_SERVER_URL; *** get(...); } diff --git a/android/javatests/org/chromium/chrome/browser/BytecodeTest.java b/android/javatests/org/chromium/chrome/browser/BytecodeTest.java index 0a6540bd0b75..dc6664a2c353 100644 --- a/android/javatests/org/chromium/chrome/browser/BytecodeTest.java +++ b/android/javatests/org/chromium/chrome/browser/BytecodeTest.java @@ -1017,6 +1017,15 @@ public void testFieldsExist() throws Exception { Assert.assertTrue(fieldExists( "org/chromium/components/browser_ui/site_settings/SingleWebsiteSettings", "mSite", true, Website.class)); + Assert.assertTrue( + fieldExists("org/chromium/components/variations/firstrun/VariationsSeedFetcher", + "sLock", true, Object.class)); + Assert.assertTrue( + fieldExists("org/chromium/components/variations/firstrun/VariationsSeedFetcher", + "DEFAULT_VARIATIONS_SERVER_URL", true, String.class)); + Assert.assertTrue( + fieldExists("org/chromium/components/variations/firstrun/VariationsSeedFetcher", + "DEFAULT_FAST_VARIATIONS_SERVER_URL", true, String.class)); Assert.assertTrue( fieldExists("org/chromium/chrome/browser/omnibox/suggestions/AutocompleteMediator", "mNativeInitialized", true, boolean.class)); @@ -1220,8 +1229,9 @@ private boolean fieldExists( if (f.getName().equals(fieldName)) { if (checkTypes) { if (fieldType != null && f.getType().equals(fieldType)) return true; - } else + } else { return true; + } } } return false; diff --git a/build/android/bytecode/bytecode_rewriter.gni b/build/android/bytecode/bytecode_rewriter.gni index 13351445c4eb..c4413bd40106 100644 --- a/build/android/bytecode/bytecode_rewriter.gni +++ b/build/android/bytecode/bytecode_rewriter.gni @@ -14,6 +14,7 @@ brave_bytecode_jars = [ "obj/brave/browser/ui/android/omnibox/java.javac.jar", "obj/brave/browser/ui/android/theme/java.javac.jar", "obj/brave/components/browser_ui/site_settings/android/java.javac.jar", + "obj/brave/components/variations/android/java.javac.jar", "obj/chrome/android/base_module_java.javac.jar", "obj/chrome/android/chrome_java.javac.jar", "obj/chrome/android/features/tab_ui/java.javac.jar", @@ -39,6 +40,7 @@ brave_bytecode_jars = [ "obj/components/external_intents/android/java.javac.jar", "obj/components/permissions/android/java.javac.jar", "obj/components/sync/android/sync_java.javac.jar", + "obj/components/variations/android/variations_java.javac.jar", ] template("brave_bytecode_rewriter") { diff --git a/build/android/bytecode/java/org/brave/bytecode/BraveVariationsSeedFetcherClassAdapter.java b/build/android/bytecode/java/org/brave/bytecode/BraveVariationsSeedFetcherClassAdapter.java index 6cc893248a19..b5dc6d527966 100644 --- a/build/android/bytecode/java/org/brave/bytecode/BraveVariationsSeedFetcherClassAdapter.java +++ b/build/android/bytecode/java/org/brave/bytecode/BraveVariationsSeedFetcherClassAdapter.java @@ -18,5 +18,12 @@ public BraveVariationsSeedFetcherClassAdapter(ClassVisitor visitor) { changeMethodOwner( sVariationsSeedFetcherClassName, "get", sBraveVariationsSeedFetcherClassName); + + deleteField(sBraveVariationsSeedFetcherClassName, "sLock"); + makeProtectedField(sVariationsSeedFetcherClassName, "sLock"); + deleteField(sBraveVariationsSeedFetcherClassName, "DEFAULT_VARIATIONS_SERVER_URL"); + makeProtectedField(sVariationsSeedFetcherClassName, "DEFAULT_VARIATIONS_SERVER_URL"); + deleteField(sBraveVariationsSeedFetcherClassName, "DEFAULT_FAST_VARIATIONS_SERVER_URL"); + makeProtectedField(sVariationsSeedFetcherClassName, "DEFAULT_FAST_VARIATIONS_SERVER_URL"); } } diff --git a/components/variations/BUILD.gn b/components/variations/BUILD.gn index 03dc73c18415..d624aaa14430 100644 --- a/components/variations/BUILD.gn +++ b/components/variations/BUILD.gn @@ -3,16 +3,9 @@ # License, v. 2.0. If a copy of the MPL was not distributed with this file, # You can obtain one at http://mozilla.org/MPL/2.0/. +import("//brave/components/variations/buildflags.gni") import("//build/buildflag_header.gni") -declare_args() { - brave_variations_server_url = "" -} - -if (is_official_build) { - assert(brave_variations_server_url != "") -} - buildflag_header("buildflags") { header = "buildflags.h" flags = [ "BRAVE_VARIATIONS_SERVER_URL=\"$brave_variations_server_url\"" ] diff --git a/components/variations/android/BUILD.gn b/components/variations/android/BUILD.gn index a7f7fc7c514f..ed4023e80a83 100644 --- a/components/variations/android/BUILD.gn +++ b/components/variations/android/BUILD.gn @@ -1,7 +1,9 @@ -# Copyright 2022 The Brave Authors. All rights reserved. -# Use of this source code is governed by a BSD-style license that can be -# found in the LICENSE file. +# Copyright (c) 2022 The Brave Authors. All rights reserved. +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this file, +# You can obtain one at https://mozilla.org/MPL/2.0/. +import("//brave/components/variations/buildflags.gni") import("//build/config/android/rules.gni") android_library("java") { @@ -12,4 +14,11 @@ android_library("java") { "//components/variations/android:variations_java", "//third_party/androidx:androidx_annotation_annotation_java", ] + srcjar_deps = [ ":brave_variations_config_java" ] +} + +java_cpp_template("brave_variations_config_java") { + sources = [ "java/src/org/chromium/components/variations/firstrun/BraveVariationsConfig.template" ] + defines = + [ "BRAVE_ANDROID_VARIATIONS_SERVER_URL=\"$brave_variations_server_url\"" ] } diff --git a/components/variations/android/java/src/org/chromium/components/variations/firstrun/BraveVariationsConfig.template b/components/variations/android/java/src/org/chromium/components/variations/firstrun/BraveVariationsConfig.template new file mode 100644 index 000000000000..9f4c33f9bbaf --- /dev/null +++ b/components/variations/android/java/src/org/chromium/components/variations/firstrun/BraveVariationsConfig.template @@ -0,0 +1,13 @@ +/* Copyright (c) 2023 The Brave Authors. All rights reserved. + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at https://mozilla.org/MPL/2.0/. */ + +package org.chromium.components.variations.firstrun; + +/** + * Brave Variations configuration. + */ +public class BraveVariationsConfig { + public static final String VARIATIONS_SERVER_URL = BRAVE_ANDROID_VARIATIONS_SERVER_URL; +} diff --git a/components/variations/android/java/src/org/chromium/components/variations/firstrun/BraveVariationsSeedFetcher.java b/components/variations/android/java/src/org/chromium/components/variations/firstrun/BraveVariationsSeedFetcher.java index 27ddc78b2b79..d26ec755cbb9 100644 --- a/components/variations/android/java/src/org/chromium/components/variations/firstrun/BraveVariationsSeedFetcher.java +++ b/components/variations/android/java/src/org/chromium/components/variations/firstrun/BraveVariationsSeedFetcher.java @@ -8,16 +8,25 @@ import androidx.annotation.VisibleForTesting; import org.chromium.base.CommandLine; +import org.chromium.base.Log; import org.chromium.components.variations.VariationsSwitches; public class BraveVariationsSeedFetcher extends VariationsSeedFetcher { + // To delete in bytecode. Variables from the parent class will be used instead. + private static Object sLock; + private static String DEFAULT_VARIATIONS_SERVER_URL; + private static String DEFAULT_FAST_VARIATIONS_SERVER_URL; + private static VariationsSeedFetcher sInstance; + private static final String TAG = "BraveVariations"; public static VariationsSeedFetcher get() { - if (sInstance == null) { - sInstance = new BraveVariationsSeedFetcher(); + synchronized (sLock) { + if (sInstance == null) { + sInstance = new BraveVariationsSeedFetcher(); + } + return sInstance; } - return sInstance; } @VisibleForTesting @@ -28,18 +37,29 @@ public BraveVariationsSeedFetcher() { @VisibleForTesting @Override protected String getConnectionString(SeedFetchParameters params) { - if (!CommandLine.getInstance().hasSwitch(VariationsSwitches.VARIATIONS_SERVER_URL)) { - // If there is no alternative variations server specified, we still don't want to - // connect to Google's server. - return ""; + String urlString = super.getConnectionString(params); + + // Fix channel name. + urlString = urlString.replaceAll("=canary", "=nightly"); + + // Return as is if URL was passed manually. + if (CommandLine.getInstance().hasSwitch(VariationsSwitches.VARIATIONS_SERVER_URL)) { + return urlString; } - String urlString = super.getConnectionString(params); - // Relacing Google's variations server with ours, but keep parameters. - int paramsPosition = urlString.indexOf("?", 0); - urlString = - CommandLine.getInstance().getSwitchValue(VariationsSwitches.VARIATIONS_SERVER_URL) - + (paramsPosition > 0 ? urlString.substring(paramsPosition) : ""); + // Replace Chromium URL with Brave URL. + if (urlString.indexOf(DEFAULT_VARIATIONS_SERVER_URL) != -1) { + urlString = urlString.replaceFirst( + DEFAULT_VARIATIONS_SERVER_URL, BraveVariationsConfig.VARIATIONS_SERVER_URL); + } else if (urlString.indexOf(DEFAULT_FAST_VARIATIONS_SERVER_URL) != -1) { + urlString = urlString.replaceFirst(DEFAULT_FAST_VARIATIONS_SERVER_URL, + BraveVariationsConfig.VARIATIONS_SERVER_URL); + } else { + Log.e(TAG, "Cannot replace seed URL to fetch variations: %s", urlString); + urlString = ""; + } + + Log.i(TAG, "Fetching variations from: %s", urlString); return urlString; } } diff --git a/components/variations/buildflags.gni b/components/variations/buildflags.gni new file mode 100644 index 000000000000..fb7f9006cd99 --- /dev/null +++ b/components/variations/buildflags.gni @@ -0,0 +1,12 @@ +# Copyright (c) 2023 The Brave Authors. All rights reserved. +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this file, +# You can obtain one at https://mozilla.org/MPL/2.0/. + +declare_args() { + brave_variations_server_url = "" +} + +if (is_official_build) { + assert(brave_variations_server_url != "") +} diff --git a/test/BUILD.gn b/test/BUILD.gn index cf1a63713ce1..c02e0cd67d17 100644 --- a/test/BUILD.gn +++ b/test/BUILD.gn @@ -1279,6 +1279,7 @@ if (is_android) { "//components/permissions/android:java", "//components/search_engines/android:java", "//components/sync/android:sync_java", + "//components/variations/android:variations_java", "//content/public/android:content_full_java", "//third_party/androidx:androidx_appcompat_appcompat_java", "//third_party/androidx:androidx_preference_preference_java",