Skip to content

Commit

Permalink
Merge pull request #20244 from brave/issues/33141
Browse files Browse the repository at this point in the history
Download Griffin seed on first launch on Android.
  • Loading branch information
goodov authored Sep 22, 2023
2 parents 8045111 + b7b8c3e commit bcaeae3
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 25 deletions.
3 changes: 3 additions & 0 deletions android/java/apk_for_test.flags
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,9 @@
}

-keep class org.chromium.components.variations.firstrun.VariationsSeedFetcher {
*** sLock;
*** DEFAULT_VARIATIONS_SERVER_URL;
*** DEFAULT_FAST_VARIATIONS_SERVER_URL;
*** get(...);
}

Expand Down
12 changes: 11 additions & 1 deletion android/javatests/org/chromium/chrome/browser/BytecodeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions build/android/bytecode/bytecode_rewriter.gni
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
9 changes: 1 addition & 8 deletions components/variations/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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\"" ]
Expand Down
15 changes: 12 additions & 3 deletions components/variations/android/BUILD.gn
Original file line number Diff line number Diff line change
@@ -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") {
Expand All @@ -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\"" ]
}
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}
}
12 changes: 12 additions & 0 deletions components/variations/buildflags.gni
Original file line number Diff line number Diff line change
@@ -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 != "")
}
1 change: 1 addition & 0 deletions test/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit bcaeae3

Please sign in to comment.