0
Files
android_webview
apps
ash
base
build
build_overrides
buildtools
cc
chrome
chromecast
chromeos
clank
codelabs
components
content
courgette
crypto
dbus
device
docs
accessibility
autofill
design
enterprise
experiments
fuchsia
gpu
images
infra
intl
ios
lacros
linux
login
mac
media
memory
memory-infra
patterns
privacy
privacy_budget
process
security
speed
speed_metrics
standards
telemetry_extension
testing
transcripts
ui
updater
webapps
website
workflow
DIR_METADATA
OWNERS
README.md
accessibility.md
ad_tagging.md
adding_to_third_party.md
android_accessing_cpp_enums_in_java.md
android_accessing_cpp_features_in_java.md
android_accessing_cpp_switches_in_java.md
android_build_instructions.md
android_cast_build_instructions.md
android_debugging_instructions.md
android_dynamic_feature_modules.md
android_emulator.md
android_isolated_splits.md
android_jni_ownership_best_practices.md
android_logging.md
android_native_libraries.md
android_studio.md
angle_in_chromium.md
api_keys.md
asan.md
atom.md
bitmap_pipeline.md
branch_gardener.md
building_old_revisions.md
callback.md
ccache_mac.md
chrome_os_logging.md
chrome_settings.md
chrome_untrusted.md
chromedriver_status.md
chromeos_build_instructions.md
chromeos_glossary.md
chromium_browser_vs_google_chrome.md
cipd_and_3pp.md
cl_respect.md
cl_tips.md
clang.md
clang_code_coverage_wrapper.md
clang_format.md
clang_gardening.md
clang_sheriffing.md
clang_static_analyzer.md
clang_tidy.md
clang_tool_refactoring.md
clangd.md
clion.md
closure_compilation.md
cocoa_tips_and_tricks.md
code_review_owners.md
code_reviews.md
commit_checklist.md
component_build.md
configuration.md
contributing.md
cq_fault_attribution.md
cr_respect.md
cr_user_manual.md
cross_platform_ui.md
cygwin_dll_remapping_failure.md
dangling_ptr.md
dangling_ptr_guide.md
dbus_mojo_connection_service.md
debugging_with_crash_keys.md
dependencies.md
deterministic_builds.md
disassemble_code.md
documentation_best_practices.md
documentation_guidelines.md
early-hints.md
eclipse.md
emacs.md
erc_irc.md
flag_expiry.md
flag_guarding_guidelines.md
flag_ownership.md
frame_trees.md
gardener.md
gdbinit.md
get_the_code.md
git_cookbook.md
git_submodules.md
git_tips.md
google_chrome_branded_builds.md
google_play_services.md
graphical_debugging_aid_chromium_views.md
gwp_asan.md
history_manipulation_intervention.md
how_cc_works.md
how_to_add_your_feature_flag.md
how_to_extend_web_test_framework.md
idn.md
initialize_blink_features.md
inlined_stack_traces.md
installation_at_vmware.md
ios_build_instructions.md
ios_infra.md
ios_voiceover.md
kiosk_mode.md
lacros.md
life_of_a_frame.md
lldbinit.md
mac_arm64.md
mac_build_instructions.md
mac_lld.md
modifying_session_history_serialization.md
mojo_and_services.md
mojo_ipc_conversion.md
mojo_testing.md
native_relocations.md
navbar.md
navigation-request-navigation-state.gv
navigation-request-navigation-state.png
navigation.md
navigation_concepts.md
network_traffic_annotations.md
no_sources_assignment_filter.md
optimizing_web_uis.md
origin_trials_integration.md
ozone_overview.md
parsing_test_results.md
pgo.md
piranha_plant.md
process_model_and_site_isolation.md
profiling.md
profiling_content_shell_on_android.md
proxy_auto_config.md
qtcreator.md
release_branch_guidance.md
render-frame-host-lifecycle-state.gv
render-frame-host-lifecycle-state.png
render_document.md
rust.md
seccomp_sandbox_crash_dumping.md
servicification.md
session_history.md
sheriff.md
shutdown.md
special_case_urls.md
static_initializers.md
sublime_ide.md
system_hardening_features.md
tab_helpers.md
threading_and_tasks.md
threading_and_tasks_faq.md
threading_and_tasks_testing.md
toolchain_support.md
tour_of_luci_ui.md
tpm_quick_ref.md
translation_screenshots.md
trusted_types_on_webui.md
updating_clang.md
updating_clang_format_binaries.md
use_counter_wiki.md
useful_urls.md
user_data_dir.md
user_data_storage.md
user_handle_mapping.md
vanilla_msysgit_workflow.md
vscode.md
vscode_python.md
webui_build_configuration.md
webui_explainer.md
webui_in_chrome.md
webui_in_components.md
webview_policies.md
win_cross.md
win_order_files.md
windows_build_instructions.md
windows_native_window_occlusion_tracking.md
windows_pwa_integration.md
windows_shortcut_and_taskbar_handling.md
windows_split_dll.md
windows_virtual_desktop_handling.md
wmax_tokens.md
working_remotely_with_android.md
writing_clang_plugins.md
extensions
fuchsia_web
gin
google_apis
google_update
gpu
headless
infra
internal
ios
ios_internal
ipc
media
mojo
native_client
native_client_sdk
net
pdf
ppapi
printing
remoting
rlz
sandbox
services
signing_keys
skia
sql
storage
styleguide
testing
third_party
tools
ui
url
v8
webkit
.clang-format
.clang-tidy
.eslintrc.js
.git-blame-ignore-revs
.gitattributes
.gitignore
.gitmodules
.gn
.mailmap
.rustfmt.toml
.vpython3
.yapfignore
ATL_OWNERS
AUTHORS
BUILD.gn
CODE_OF_CONDUCT.md
DEPS
DIR_METADATA
LICENSE
LICENSE.chromium_os
OWNERS
PRESUBMIT.py
PRESUBMIT_test.py
PRESUBMIT_test_mocks.py
README.md
WATCHLISTS
codereview.settings
src/docs/android_accessing_cpp_features_in_java.md
Henrique Nakashima 1623df1518 [Android] Use FeatureMap to generalize ContentFeatureList
This reduces code duplication in ContentFeatureList.java and
content_feature_list.cc, reusing the code in base
FeatureMap.java and feature_map.cc.

ContentFeatureList is kept for convenience static methods to
access ContentFeatureMap, but we can inline and update callers in
a follow-up if desired.

Also add a TODO to ContentFeatureList since I noticed the constants
there with feature names largely overlap with the constants in the
generated ContentFeatures.

Using this opportunity to update documentation
(since ContentFeatureList is used as an example) with the new
FeatureMap API.

Change-Id: I2360830bfbceff20714fc65db4dce5ef743ab95a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4544343
Reviewed-by: Mark Schillaci <mschillaci@google.com>
Reviewed-by: Jinsuk Kim <jinsukkim@chromium.org>
Commit-Queue: Henrique Nakashima <hnakashima@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1151372}
2023-05-31 18:23:35 +00:00

6.4 KiB

Accessing C++ Features In Java

[TOC]

Checking if a Feature is enabled

In C++, add your base::Feature to an existing base::android::FeatureMap in the appropriate layer/component. Then, you can check the enabled state like so:

// FooFeatureMap can check FooFeatures.MY_FEATURE as long as foo_feature_map.cc
// adds `kMyFeature` to its `base::android::FeatureMap`.
if (FooFeatureMap.getInstance().isEnabled(FooFeatures.MY_FEATURE)) {
    // ...
}

If the components or layer does not have a FeatureMap, create a new one:

  1. In C++, create a new foo_feature_map.cc (ex. content_feature_map) with:
    • kFeaturesExposedToJava array with a pointer to your base::Feature.
    • GetFeatureMap with a static base::android::FeatureMap initialized with kFeaturesExposedToJava.
    • JNI_FooFeatureList_GetNativeMap simply calling GetFeatureMap.
  2. In Java, create a FooFeatureMap.java class extending FeatureMap.java (ex. ContentFeatureMap) with:
    • A getInstance() that returns the singleton instance.
    • A single long getNativeMap() as @NativeMethods.
    • An @Override for the abstract getNativeMap() simply calling FooFeatureMapJni.get().getNativeMap().
  3. Still in Java, FooFeatures.java with the String constants with the feature names needs to be generated or created.
    • Auto-generate it by writing a FooFeatures.java.tmpl. See instructions below.
    • If FooFeatures cannot be auto-generated, keep the list of String constants with the feature names in a FooFeatures or FooFeatureList separate from the pure boilerplate FooFeatureMap.

Auto-generating FooFeatureList.java

Accessing C++ base::Features in Java is implemented via a Python script which analyzes the *_features.cc file and generates the corresponding Java class, based on a template file. The template file must be specified in the GN target. This outputs Java String constants which represent the name of the base::Feature.

Usage

  1. Create a template file (ex. FooFeatures.java.tmpl). Change "Copyright 2020" to be whatever the year is at the time of writing (as you would for any other file).

     // Copyright 2020 The Chromium Authors
     // Use of this source code is governed by a BSD-style license that can be
     // found in the LICENSE file.
    
     package org.chromium.foo;
    
     // Be sure to escape any curly braces in your template by doubling as
     // follows.
     /**
      * Contains features that are specific to the foo project.
      */
     public final class FooFeatures {{
    
     {NATIVE_FEATURES}
    
         // Prevents instantiation.
         private FooFeatures() {{}}
     }}
    
  2. Add a new build target and add it to the srcjar_deps of an android_library target:

    if (is_android) {
      import("//build/config/android/rules.gni")
    }
    
    if (is_android) {
      java_cpp_features("java_features_srcjar") {
        # External code should depend on ":foo_java" instead.
        visibility = [ ":*" ]
        sources = [
          "//base/android/foo_features.cc",
        ]
        template = "//base/android/java_templates/FooFeatures.java.tmpl"
      }
    
      # If there's already an android_library target, you can add
      # java_features_srcjar to that target's srcjar_deps. Otherwise, the best
      # practice is to create a new android_library just for this target.
      android_library("foo_java") {
        srcjar_deps = [ ":java_features_srcjar" ]
      }
    }
    
  3. Add a deps entry to "common_java" in "//android_webview/BUILD.gn" if creating a new android_library in the previous step:

    android_library("common_java") {
      ...
    
      deps = [
        ...
        "//path/to:foo_java",
        ...
      ]
    }
    
  4. The generated file out/Default/gen/.../org/chromium/foo/FooFeatures.java would contain:

    // Copyright $YEAR The Chromium Authors
    // Use of this source code is governed by a BSD-style license that can be
    // found in the LICENSE file.
    
    package org.chromium.foo;
    
    // Be sure to escape any curly braces in your template by doubling as
    // follows.
    /**
     * Contains features that are specific to the foo project.
     */
    public final class FooFeatures {
    
        // This following string constants were inserted by
        //     java_cpp_features.py
        // From
        //     ../../base/android/foo_features.cc
        // Into
        //     ../../base/android/java_templates/FooFeatures.java.tmpl
    
        // Documentation for the C++ Feature is copied here.
        public static final String SOME_FEATURE = "SomeFeature";
    
        // ...snip...
    
        // Prevents instantiation.
        private FooFeatures() {}
    }
    

Troubleshooting

The script only supports limited syntaxes for declaring C++ base::Features. You may see an error like the following during compilation:

...
org/chromium/foo/FooFeatures.java:41: error: duplicate declaration of field: MY_FEATURE
    public static final String MY_FEATURE = "MyFeature";

This can happen if you've re-declared a feature for mutually-exclusive build configs (ex. the feature is enabled-by-default for one config, but disabled-by-default for another). Example:

#if defined(...)
BASE_FEATURE(kMyFeature, "MyFeature", base::FEATURE_ENABLED_BY_DEFAULT);
#else
BASE_FEATURE(kMyFeature, "MyFeature", base::FEATURE_DISABLED_BY_DEFAULT);
#endif

The java_cpp_features rule doesn't know how to evaluate C++ preprocessor directives, so it generates two identical Java fields (which is what the compilation error is complaining about). Fortunately, the workaround is fairly simple. Rewrite the definition to only use directives around the enabled state:

BASE_FEATURE(kMyFeature,
             "MyFeature",
#if defined(...)
             base::FEATURE_ENABLED_BY_DEFAULT
#else
             base::FEATURE_DISABLED_BY_DEFAULT
#endif
};

See also

Code