Skip to content

Commit

Permalink
CoAT changes to support Kimono build
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.

b/372559266
  • Loading branch information
johnxwork committed Jan 3, 2025
1 parent 8e80e03 commit 5de8542
Show file tree
Hide file tree
Showing 12 changed files with 716 additions and 45 deletions.
52 changes: 52 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,48 @@ ABSL_CONST_INIT thread_local void* stack_frame_pointer = nullptr;

bool g_fatal_exception_occurred = false;

#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 +271,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_
80 changes: 74 additions & 6 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,6 +37,74 @@ generate_jni("jni_headers") {
sources = [ "apk/app/src/main/java/dev/cobalt/coat/StarboardBridge.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

#srcjar_deps = [ ":content_javatests_aidl" ]

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",

#":content_shell_manifest",
"//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" ]
}

# TODO(cobalt): Re-enable or remove disabled java files.
android_library("cobalt_apk_java") {
testonly = true
Expand All @@ -44,6 +113,8 @@ android_library("cobalt_apk_java") {

deps = [
":cobalt_java_resources",
":cobalt_shell_java",
":content_shell_apk_java",
":jni_headers",
"//base:base_java",
"//base:jni_java",
Expand All @@ -52,9 +123,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 Down Expand Up @@ -143,15 +211,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,14 +236,14 @@ shared_library("libcobalt_content_shell_content_view") {
# TODO(b/375655377): remove testonly
testonly = true
deps = [
":content_shell_jni_headers",
"//cobalt/user_agent",

# TODO: what can be removed in the dependencies?
"//components/crash/content/browser",
"//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 @@ -32,10 +32,8 @@
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.app.CobaltApplication;
import dev.cobalt.coat.javabridge.CobaltJavaScriptAndroidObject;
import dev.cobalt.coat.javabridge.CobaltJavaScriptInterface;
import dev.cobalt.coat.javabridge.H5vccPlatformService;
Expand Down Expand Up @@ -79,6 +77,7 @@ public abstract class CobaltActivity extends Activity {

private static final String ACTIVE_SHELL_URL_KEY = "activeUrl";
public static final String COMMAND_LINE_ARGS_KEY = "commandLineArgs";
public static final String COMMAND_LINE_FILE = "/data/local/tmp/content-shell-command-line";

private static final Pattern URL_PARAM_PATTERN = Pattern.compile("^[a-zA-Z0-9_=]*$");

Expand All @@ -97,12 +96,13 @@ public abstract class CobaltActivity extends Activity {
private Intent mLastSentIntent;
private String mStartupUrl;
private IntentRequestTracker mIntentRequestTracker;
protected Boolean shouldSetJNIPrefix = false;

// Initially copied from ContentShellActiviy.java
protected void createContent(final Bundle savedInstanceState) {
// Initializing the command line must occur before loading the library.
if (!CommandLine.isInitialized()) {
((CobaltApplication) getApplication()).initCommandLine();
CommandLine.initFromFile(COMMAND_LINE_FILE);

// Note that appendSwitchesAndArguments excludes cobaltCommandLineParams[0]
// as the program name, and all other arguments SHOULD start with '--'.
Expand All @@ -127,6 +127,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 @@ -237,8 +245,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 @@ -317,10 +323,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
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>

<!--
Copyright 2012 The Chromium Authors
Use of this source code is governed by a BSD-style license that can be
found in the LICENSE file.
-->

<merge xmlns:android="http://schemas.android.com/apk/res/android">
<cobalt.org.chromium.content_shell.ShellManager
android:id="@+id/shell_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</merge>
Loading

0 comments on commit 5de8542

Please sign in to comment.