Skip to content

Commit

Permalink
CoAT changes to support Kimono build (#4506)
Browse files Browse the repository at this point in the history
1. Removed most java dependencies on content/shell/android/ resources;
2. Support dynamically adding "cobalt." java package prefix when
invoking JNI;
3. Avoid using hash names in JNI templates.

go/kimono-chrobalt-build
b/372559266
  • Loading branch information
johnxwork authored Jan 14, 2025
1 parent aba5a7a commit 5e6a0d9
Show file tree
Hide file tree
Showing 14 changed files with 738 additions and 49 deletions.
56 changes: 56 additions & 0 deletions base/android/jni_android.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "base/android/jni_android.h"

#include <cstring>
#include <stddef.h>
#include <sys/prctl.h>

Expand All @@ -13,6 +14,8 @@
#include "base/base_jni_headers/PiiElider_jni.h"
#include "base/debug/debugging_buildflags.h"
#include "base/logging.h"
#include "base/base_switches.h"
#include "base/command_line.h"
#include "build/build_config.h"
#include "third_party/abseil-cpp/absl/base/attributes.h"

Expand All @@ -30,9 +33,52 @@ ABSL_CONST_INIT thread_local void* stack_frame_pointer = nullptr;

bool g_fatal_exception_occurred = false;

/* Cobalt specific hack to move Java classes to a custom namespace.
For every class org.chromium.foo moves them to cobalt.org.chromium.foo
This works around link-time conflicts when building the final
package against other Chromium release artifacts. */
#if BUILDFLAG(IS_COBALT)
const char* COBALT_ORG_CHROMIUM = "cobalt/org/chromium";
const char* ORG_CHROMIUM = "org/chromium";

bool g_add_cobalt_prefix = false;
std::atomic<bool> g_checked_command_line(false);

std::string getRepackagedName(const char* signature) {
std::string holder(signature);
size_t pos = 0;
while ((pos = holder.find(ORG_CHROMIUM, pos)) != std::string::npos) {
holder.replace(pos, strlen(ORG_CHROMIUM), COBALT_ORG_CHROMIUM);
pos += strlen(COBALT_ORG_CHROMIUM);
}
return holder;
}

bool shouldAddCobaltPrefix() {
if (!g_checked_command_line && base::CommandLine::InitializedForCurrentProcess()) {
g_add_cobalt_prefix = base::CommandLine::ForCurrentProcess()->HasSwitch(switches::kCobaltJniPrefix);
g_checked_command_line = true;
}
return g_add_cobalt_prefix;
}
#endif

ScopedJavaLocalRef<jclass> GetClassInternal(JNIEnv* env,
#if BUILDFLAG(IS_COBALT)
const char* original_class_name,
jobject class_loader) {
const char* class_name;
std::string holder;
if (shouldAddCobaltPrefix()) {
holder = getRepackagedName(original_class_name);
class_name = holder.c_str();
} else {
class_name = original_class_name;
}
#else
const char* class_name,
jobject class_loader) {
#endif
jclass clazz;
if (class_loader != nullptr) {
// ClassLoader.loadClass expects a classname with components separated by
Expand Down Expand Up @@ -229,7 +275,17 @@ jmethodID MethodID::LazyGet(JNIEnv* env,
const jmethodID value = atomic_method_id->load(std::memory_order_acquire);
if (value)
return value;
#if BUILDFLAG(IS_COBALT)
jmethodID id;
if (shouldAddCobaltPrefix()) {
std::string holder = getRepackagedName(jni_signature);
id = MethodID::Get<type>(env, clazz, method_name, holder.c_str());
} else {
id = MethodID::Get<type>(env, clazz, method_name, jni_signature);
}
#else
jmethodID id = MethodID::Get<type>(env, clazz, method_name, jni_signature);
#endif
atomic_method_id->store(id, std::memory_order_release);
return id;
}
Expand Down
4 changes: 4 additions & 0 deletions base/base_switches.cc
Original file line number Diff line number Diff line change
Expand Up @@ -183,4 +183,8 @@ extern const char kEnableCrashpad[] = "enable-crashpad";
const char kSchedulerBoostUrgent[] = "scheduler-boost-urgent";
#endif

#if BUILDFLAG(IS_COBALT)
const char kCobaltJniPrefix[] = "cobalt-jni-prefix";
#endif

} // namespace switches
4 changes: 4 additions & 0 deletions base/base_switches.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ extern const char kEnableCrashpad[];
extern const char kSchedulerBoostUrgent[];
#endif

#if BUILDFLAG(IS_COBALT)
extern const char kCobaltJniPrefix[];
#endif

} // namespace switches

#endif // BASE_BASE_SWITCHES_H_
96 changes: 84 additions & 12 deletions cobalt/android/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ android_resources("cobalt_java_resources") {
"apk/app/src/app/res/mipmap-xxhdpi/ic_app.png",
"apk/app/src/app/res/values/strings.xml",
"apk/app/src/main/res/layout/coat_error_dialog.xml",
"apk/app/src/main/res/layout/content_shell_activity.xml",
"apk/app/src/main/res/values/colors.xml",
"apk/app/src/main/res/values/ids.xml",
"apk/app/src/main/res/values/overlayable.xml",
Expand All @@ -36,14 +37,77 @@ generate_jni("jni_headers") {
sources = [ "apk/app/src/main/java/dev/cobalt/coat/StarboardBridge.java" ]
}

# TODO(cobalt): Re-enable or remove disabled java files.
android_library("cobalt_apk_java") {
generate_jni("content_shell_jni_headers") {
sources = [
"apk/app/src/org/chromium/content_shell/Shell.java",
"apk/app/src/org/chromium/content_shell/ShellManager.java",
]
}

android_library("content_shell_apk_java") {
testonly = true
resources_package = "dev.cobalt.coat"

resources_package = "org.chromium.content_shell_apk"
deps = [
":cobalt_shell_java",
"//base:base_java",
"//base:process_launcher_java",
"//build/android:build_java",
"//components/embedder_support/android:view_java",
"//content/public/android:content_java",
"//content/shell/android:content_shell_apk_resources",
"//content/shell/android:content_shell_manifest",
"//media/capture/video/android:capture_java",
"//net/android:net_java",
"//third_party/android_deps:com_google_code_findbugs_jsr305_java",
"//ui/android:ui_java",
"//url:gurl_java",
]

sources = [
"//content/shell/android/shell_apk/src/org/chromium/content_shell_apk/ContentShellActivity.java",
"//content/shell/android/shell_apk/src/org/chromium/content_shell_apk/ContentShellApplication.java",
]
}

android_library("cobalt_shell_java") {
testonly = true
resources_package = "org.chromium.content_shell"
deps = [
":cobalt_java_resources",
"//base:base_java",
"//base:jni_java",
"//build/android:build_java",
"//components/download/internal/common:internal_java",
"//components/embedder_support/android:content_view_java",
"//components/embedder_support/android:view_java",
"//components/viz/service:service_java",
"//content/public/android:content_java",
"//content/shell/android:content_shell_java_resources",
"//media/base/android:media_java",
"//media/capture/video/android:capture_java",
"//mojo/public/java:system_java",
"//net/android:net_java",
"//ui/android:ui_java",
"//ui/base/cursor/mojom:cursor_type_java",
"//url:gurl_java",
]
sources = [
"apk/app/src/org/chromium/content_shell/Shell.java",
"apk/app/src/org/chromium/content_shell/ShellManager.java",
"apk/app/src/org/chromium/content_shell/ShellViewAndroidDelegate.java",
]

annotation_processor_deps = [ "//base/android/jni_generator:jni_processor" ]
}

android_library("cobalt_main_java") {
testonly = true
resources_package = "dev.cobalt.coat"
deps = [
":cobalt_java_resources",
":cobalt_shell_java",
":content_shell_apk_java",
":jni_headers",
"//base:base_java",
"//base:jni_java",
Expand All @@ -52,9 +116,6 @@ android_library("cobalt_apk_java") {
"//components/embedder_support/android:view_java",
"//components/version_info/android:version_constants_java",
"//content/public/android:content_java",
"//content/shell/android:content_shell_apk_java",
"//content/shell/android:content_shell_apk_resources",
"//content/shell/android:content_shell_java",
"//content/shell/android:content_shell_manifest",
"//media/capture/video/android:capture_java",
"//net/android:net_java",
Expand All @@ -65,10 +126,7 @@ android_library("cobalt_apk_java") {
"//ui/android:ui_no_recycler_view_java",
"//url:gurl_java",
]

sources = [
"apk/app/src/app/java/dev/cobalt/app/CobaltApplication.java",
"apk/app/src/app/java/dev/cobalt/app/MainActivity.java",
"apk/app/src/main/java/dev/cobalt/coat/ArtworkDownloader.java",
"apk/app/src/main/java/dev/cobalt/coat/ArtworkDownloaderDefault.java",
"apk/app/src/main/java/dev/cobalt/coat/ArtworkLoader.java",
Expand Down Expand Up @@ -120,6 +178,20 @@ android_library("cobalt_apk_java") {
"apk/app/src/main/java/dev/cobalt/util/SystemPropertiesHelper.java",
"apk/app/src/main/java/dev/cobalt/util/UsedByNative.java",
]

annotation_processor_deps = [ "//base/android/jni_generator:jni_processor" ]
}

android_library("cobalt_apk_java") {
testonly = true
resources_package = "dev.cobalt.coat"

deps = [ ":cobalt_main_java" ]

sources = [
"apk/app/src/app/java/dev/cobalt/app/CobaltApplication.java",
"apk/app/src/app/java/dev/cobalt/app/MainActivity.java",
]
}

android_assets("cobalt_apk_assets") {
Expand All @@ -143,15 +215,15 @@ template("content_shell_apk_tmpl") {
deps = []
}
deps += [
":cobalt_shell_java",
":content_shell_apk_java",
"//base:base_java_test_support",
"//components/crash/android:java",
"//components/crash/core/app:chrome_crashpad_handler_named_as_so",
"//components/metrics:metrics_java",
"//content/public/android:content_java",
"//content/public/test/android:android_test_message_pump_support_java",
"//content/shell/android:content_shell_apk_java",
"//content/shell/android:content_shell_assets",
"//content/shell/android:content_shell_java",
"//media/capture/video/android:capture_java",
"//net/android:net_java",
"//services/shape_detection:shape_detection_java",
Expand All @@ -168,6 +240,7 @@ shared_library("libcobalt_content_shell_content_view") {
# TODO(b/375655377): remove testonly
testonly = true
deps = [
":content_shell_jni_headers",
"//cobalt/renderer:renderer",
"//cobalt/user_agent",

Expand All @@ -176,7 +249,6 @@ shared_library("libcobalt_content_shell_content_view") {
"//content/shell:content_shell_app",
"//content/shell:content_shell_lib",
"//content/shell:pak",
"//content/shell/android:content_shell_jni_headers",
"//media",
"//skia",
"//starboard/android/shared:starboard_jni_state",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,17 @@
import android.app.Application;
import android.content.Context;
import dev.cobalt.coat.StarboardBridge;

import org.chromium.content_shell_apk.ContentShellApplication;
import org.chromium.base.ApplicationStatus;
import org.chromium.base.ContextUtils;
import org.chromium.base.PathUtils;
import org.chromium.base.library_loader.LibraryLoader;
import org.chromium.base.library_loader.LibraryProcessType;
import org.chromium.ui.base.ResourceBundle;

/** Android Application hosting the Starboard application. */
public class CobaltApplication extends ContentShellApplication implements StarboardBridge.HostApplication {
public class CobaltApplication extends Application implements StarboardBridge.HostApplication {
private static final String PRIVATE_DATA_DIRECTORY_SUFFIX = "content_shell";

StarboardBridge starboardBridge;

@Override
Expand All @@ -33,4 +39,19 @@ public void setStarboardBridge(StarboardBridge starboardBridge) {
public StarboardBridge getStarboardBridge() {
return starboardBridge;
}

@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
boolean isBrowserProcess = !ContextUtils.getProcessName().contains(":");
ContextUtils.initApplicationContext(this);
ResourceBundle.setNoAvailableLocalePaks();
LibraryLoader.getInstance().setLibraryProcessType(isBrowserProcess
? LibraryProcessType.PROCESS_BROWSER
: LibraryProcessType.PROCESS_CHILD);
if (isBrowserProcess) {
PathUtils.setPrivateDataDirectorySuffix(PRIVATE_DATA_DIRECTORY_SUFFIX);
ApplicationStatus.initialize(this);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import android.app.Activity;
import android.app.Service;
import android.os.Bundle;
import dev.cobalt.coat.ArtworkDownloaderDefault;
import dev.cobalt.coat.CobaltActivity;
import dev.cobalt.coat.CobaltService;
Expand Down Expand Up @@ -50,4 +51,10 @@ protected StarboardBridge createStarboardBridge(String[] args, String startDeepL

return bridge;
}

@Override
public void onCreate(Bundle savedInstanceState) {
this.shouldSetJNIPrefix = false;
super.onCreate(savedInstanceState);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import android.view.ViewGroup.LayoutParams;
import android.view.ViewParent;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.Toast;
import androidx.annotation.Nullable;
import dev.cobalt.coat.javabridge.CobaltJavaScriptAndroidObject;
Expand Down Expand Up @@ -96,6 +95,7 @@ public abstract class CobaltActivity extends Activity {
private Intent mLastSentIntent;
private String mStartupUrl;
private IntentRequestTracker mIntentRequestTracker;
protected Boolean shouldSetJNIPrefix = true;

// Initially copied from ContentShellActiviy.java
protected void createContent(final Bundle savedInstanceState) {
Expand Down Expand Up @@ -123,6 +123,14 @@ protected void createContent(final Bundle savedInstanceState) {
"--force-device-scale-factor=1",
};
CommandLine.getInstance().appendSwitchesAndArguments(cobaltCommandLineParams);
if (shouldSetJNIPrefix) {
CommandLine.getInstance().appendSwitchesAndArguments(
new String[] {
// Helps Kimono build avoid package name conflict with cronet.
"--cobalt-jni-prefix",
}
);
}

if (!VersionInfo.isOfficialBuild()) {
String[] debugCommandLineParams =
Expand Down Expand Up @@ -233,8 +241,6 @@ private void finishInitialization(Bundle savedInstanceState) {
// Load the `url` with the same shell we created above.
Log.i(TAG, "shellManager load url:" + shellUrl);
mShellManager.getActiveShell().loadUrl(shellUrl);

toggleFullscreenMode(true);
}

// Initially copied from ContentShellActiviy.java
Expand Down Expand Up @@ -313,10 +319,6 @@ protected void shellHandleIntent(Intent intent) {
}
}

protected void toggleFullscreenMode(boolean enterFullscreen) {
LinearLayout toolBar = (LinearLayout) findViewById(R.id.toolbar);
toolBar.setVisibility(enterFullscreen ? View.GONE : View.VISIBLE);
}

// Initially copied from ContentShellActiviy.java
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,9 @@ public void requestSuspend() {
}
}

// TODO(cobalt): remove when Kimono fully switches to Chrobalt.
public void requestStop(int errorLevel) {}

public boolean onSearchRequested() {
// TODO(cobalt): re-enable native search request if needed.
// if (applicationReady) {
Expand Down
Loading

0 comments on commit 5e6a0d9

Please sign in to comment.