chromium/components/optimization_guide/core/BUILD.gn

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

import("//mojo/public/tools/bindings/mojom.gni")

if (is_android) {
  import("//build/config/android/rules.gni")
}
import("//build/buildflag_header.gni")
import("//components/optimization_guide/features.gni")
import("//testing/libfuzzer/fuzzer_test.gni")
import("//third_party/mediapipe/features.gni")
import("//third_party/tflite/features.gni")

static_library("bloomfilter") {
  sources = [
    "bloom_filter.cc",
    "bloom_filter.h",
  ]
  deps = [
    "//base",
    "//third_party/smhasher:murmurhash3",
  ]
  configs += [ "//build/config/compiler:wexit_time_destructors" ]
}

static_library("model_executor") {
  sources = [
    "execution_status.cc",
    "execution_status.h",
    "model_enums.h",
    "model_executor.h",
    "model_info.cc",
    "model_info.h",
    "model_util.cc",
    "model_util.h",
  ]
  if (build_with_tflite_lib) {
    sources += [
      "base_model_executor.h",
      "base_model_executor_helpers.h",
      "bert_model_executor.cc",
      "bert_model_executor.h",
      "model_execution_timeout_watchdog.cc",
      "model_execution_timeout_watchdog.h",
      "page_visibility_model_executor.cc",
      "page_visibility_model_executor.h",
      "tflite_model_executor.h",
    ]
  }

  if (build_with_mediapipe_lib) {
    assert(build_with_tflite_lib)

    sources += [
      "mediapipe_text_model_executor.cc",
      "mediapipe_text_model_executor.h",
    ]
  }

  public_deps = [
    "//components/optimization_guide:machine_learning_tflite_buildflags",
    "//components/optimization_guide/proto:optimization_guide_proto",
    "//third_party/re2",
  ]

  deps = [
    ":features",
    "//base",
    "//net",
    "//url",
  ]

  configs += [ "//build/config/compiler:wexit_time_destructors" ]

  if (build_with_tflite_lib) {
    public_deps += [
      "//components/optimization_guide/core:machine_learning",
      "//third_party/abseil-cpp:absl",
      "//third_party/tflite",
      "//third_party/tflite:tflite_public_headers",
      "//third_party/tflite_support",
      "//third_party/tflite_support:tflite_support_proto",
    ]
    deps += [ ":machine_learning" ]
  }

  if (build_with_mediapipe_lib) {
    assert(build_with_tflite_lib)

    public_deps += [ "//third_party/mediapipe" ]
  }
}

mojom("interfaces") {
  sources = [ "optimization_guide_common.mojom" ]
  webui_module_path = "/"
  public_deps = [ "//mojo/public/mojom/base" ]
}

if (build_with_tflite_lib) {
  static_library("machine_learning") {
    sources = [
      "page_visibility_op_resolver.cc",
      "page_visibility_op_resolver.h",
      "tflite_op_resolver.cc",
      "tflite_op_resolver.h",
    ]
    deps = [
      ":features",
      "//components/optimization_guide:machine_learning_tflite_buildflags",
      "//third_party/tensorflow_models:tflite_custom_ops",
      "//third_party/tflite",
      "//third_party/tflite:buildflags",
      "//third_party/tflite:tflite_public_headers",
    ]
    configs += [ "//build/config/compiler:wexit_time_destructors" ]
  }
}

component("features") {
  sources = [
    "feature_registry/enterprise_policy_registry.cc",
    "feature_registry/enterprise_policy_registry.h",
    "feature_registry/feature_registration.cc",
    "feature_registry/feature_registration.h",
    "feature_registry/mqls_feature_registry.cc",
    "feature_registry/mqls_feature_registry.h",
    "feature_registry/settings_ui_registry.cc",
    "feature_registry/settings_ui_registry.h",
    "insertion_ordered_set.h",
    "model_execution/feature_keys.h",
    "model_execution/model_execution_prefs.cc",
    "model_execution/model_execution_prefs.h",
    "model_quality/feature_type_map.h",
    "optimization_guide_constants.cc",
    "optimization_guide_constants.h",
    "optimization_guide_enums.h",
    "optimization_guide_features.cc",
    "optimization_guide_features.h",
    "optimization_guide_prefs.cc",
    "optimization_guide_prefs.h",
    "optimization_guide_switches.cc",
    "optimization_guide_switches.h",
  ]
  public_deps = [ "//base" ]
  deps = [
    "//components/optimization_guide:machine_learning_tflite_buildflags",
    "//components/optimization_guide/proto:optimization_guide_proto",
    "//components/prefs",
    "//components/variations",
    "//google_apis",
    "//net",
    "//third_party/metrics_proto:metrics_proto",
    "//ui/base",
  ]
  configs += [ "//build/config/compiler:wexit_time_destructors" ]
  defines = [ "IS_OPTIMIZATION_GUIDE_FEATURES_IMPL" ]
}

output_proto_descriptors_cc_file = "$root_gen_dir/components/optimization_guide/core/model_execution/on_device_model_execution_proto_descriptors.cc"

static_library("core") {
  sources = [
    "access_token_helper.cc",
    "access_token_helper.h",
    "command_line_top_host_provider.cc",
    "command_line_top_host_provider.h",
    "hint_cache.cc",
    "hint_cache.h",
    "hints_component_info.h",
    "hints_component_util.cc",
    "hints_component_util.h",
    "hints_fetcher.cc",
    "hints_fetcher.h",
    "hints_fetcher_factory.cc",
    "hints_fetcher_factory.h",
    "hints_manager.cc",
    "hints_manager.h",
    "hints_processing_util.cc",
    "hints_processing_util.h",
    "memory_hint.cc",
    "memory_hint.h",
    "model_execution/optimization_guide_model_execution_error.cc",
    "model_execution/optimization_guide_model_execution_error.h",
    "model_handler.h",
    "model_quality/model_quality_log_entry.cc",
    "model_quality/model_quality_log_entry.h",
    "model_quality/model_quality_logs_uploader_service.cc",
    "model_quality/model_quality_logs_uploader_service.h",
    "model_quality/model_quality_util.cc",
    "model_quality/model_quality_util.h",
    "noisy_metrics_recorder.cc",
    "noisy_metrics_recorder.h",
    "optimization_filter.cc",
    "optimization_filter.h",
    "optimization_guide_decider.h",
    "optimization_guide_decision.h",
    "optimization_guide_logger.cc",
    "optimization_guide_logger.h",
    "optimization_guide_model_executor.cc",
    "optimization_guide_model_executor.h",
    "optimization_guide_model_provider.h",
    "optimization_guide_navigation_data.cc",
    "optimization_guide_navigation_data.h",
    "optimization_guide_permissions_util.cc",
    "optimization_guide_permissions_util.h",
    "optimization_guide_proto_util.cc",
    "optimization_guide_proto_util.h",
    "optimization_guide_store.cc",
    "optimization_guide_store.h",
    "optimization_guide_util.cc",
    "optimization_guide_util.h",
    "optimization_hints_component_observer.h",
    "optimization_hints_component_update_listener.cc",
    "optimization_hints_component_update_listener.h",
    "optimization_metadata.cc",
    "optimization_metadata.h",
    "optimization_target_model_observer.h",
    "prediction_model_fetcher.h",
    "prediction_model_fetcher_impl.cc",
    "prediction_model_fetcher_impl.h",
    "push_notification_manager.cc",
    "push_notification_manager.h",
    "store_update_data.cc",
    "store_update_data.h",
    "tab_url_provider.h",
    "top_host_provider.h",
    "url_pattern_with_wildcards.cc",
    "url_pattern_with_wildcards.h",
  ]
  if (!is_fuchsia || !optimize_for_size) {
    sources += [
      "model_execution/aqa_response_parser.cc",
      "model_execution/aqa_response_parser.h",
      "model_execution/json_response_parser.cc",
      "model_execution/json_response_parser.h",
      "model_execution/model_execution_features.cc",
      "model_execution/model_execution_features.h",
      "model_execution/model_execution_features_controller.cc",
      "model_execution/model_execution_features_controller.h",
      "model_execution/model_execution_fetcher.cc",
      "model_execution/model_execution_fetcher.h",
      "model_execution/model_execution_manager.cc",
      "model_execution/model_execution_manager.h",
      "model_execution/model_execution_util.cc",
      "model_execution/model_execution_util.h",
      "model_execution/on_device_model_access_controller.cc",
      "model_execution/on_device_model_access_controller.h",
      "model_execution/on_device_model_adaptation_controller.cc",
      "model_execution/on_device_model_adaptation_controller.h",
      "model_execution/on_device_model_adaptation_loader.cc",
      "model_execution/on_device_model_adaptation_loader.h",
      "model_execution/on_device_model_component.cc",
      "model_execution/on_device_model_component.h",
      "model_execution/on_device_model_execution_proto_descriptors.h",
      "model_execution/on_device_model_execution_proto_value_utils.cc",
      "model_execution/on_device_model_execution_proto_value_utils.h",
      "model_execution/on_device_model_feature_adapter.cc",
      "model_execution/on_device_model_feature_adapter.h",
      "model_execution/on_device_model_metadata.cc",
      "model_execution/on_device_model_metadata.h",
      "model_execution/on_device_model_service_controller.cc",
      "model_execution/on_device_model_service_controller.h",
      "model_execution/on_device_model_validator.cc",
      "model_execution/on_device_model_validator.h",
      "model_execution/redactor.cc",
      "model_execution/redactor.h",
      "model_execution/repetition_checker.cc",
      "model_execution/repetition_checker.h",
      "model_execution/response_parser.cc",
      "model_execution/response_parser.h",
      "model_execution/response_parser_registry.cc",
      "model_execution/response_parser_registry.h",
      "model_execution/safety_config.cc",
      "model_execution/safety_config.h",
      "model_execution/safety_model_info.cc",
      "model_execution/safety_model_info.h",
      "model_execution/session_impl.cc",
      "model_execution/session_impl.h",
      "model_execution/settings_enabled_observer.cc",
      "model_execution/settings_enabled_observer.h",
      "model_execution/simple_response_parser.cc",
      "model_execution/simple_response_parser.h",
      "model_execution/substitution.cc",
      "model_execution/substitution.h",
      output_proto_descriptors_cc_file,
    ]
  }
  if (build_with_tflite_lib) {
    sources += [
      "bert_model_handler.cc",
      "bert_model_handler.h",
      "model_validator.cc",
      "model_validator.h",
    ]
  }

  public_deps = [
    ":features",
    ":model_executor",
    "//components/autofill/core/common",
    "//components/leveldb_proto",
    "//components/optimization_guide:machine_learning_tflite_buildflags",
    "//components/optimization_guide/core:interfaces",
    "//components/optimization_guide/proto:optimization_guide_proto",
    "//third_party/re2",
  ]
  if (build_with_tflite_lib) {
    public_deps += [
      "//components/optimization_guide/core:machine_learning",
      "//third_party/abseil-cpp:absl",
      "//third_party/tflite",
      "//third_party/tflite:tflite_public_headers",
      "//third_party/tflite_support",
      "//third_party/tflite_support:tflite_support_proto",
      "//third_party/zlib/google:compression_utils",
    ]
  }

  deps = [
    ":bloomfilter",
    "//base",
    "//components/prefs",
    "//components/signin/public/identity_manager:identity_manager",
    "//components/unified_consent",
    "//components/variations",
    "//components/variations/net",
    "//google_apis/common:request_util",
    "//services/metrics/public/cpp:metrics_cpp",
    "//services/metrics/public/cpp:ukm_builders",
    "//services/network/public/cpp",
    "//services/on_device_model/public/mojom",
    "//third_party/tflite:buildflags",
    "//url:url",
  ]

  if (!is_fuchsia || !optimize_for_size) {
    deps += [
      ":on_device_model_execution_proto_generator",
      "//components/component_updater",
      "//components/version_info",
      "//services/data_decoder/public/cpp",
      "//third_party/re2",
    ]
  }

  configs += [ "//build/config/compiler:wexit_time_destructors" ]
}

static_library("prediction") {
  sources = [
    "model_store_metadata_entry.cc",
    "model_store_metadata_entry.h",
    "prediction_manager.cc",
    "prediction_manager.h",
    "prediction_model_download_manager.cc",
    "prediction_model_download_manager.h",
    "prediction_model_download_observer.h",
    "prediction_model_fetch_timer.cc",
    "prediction_model_fetch_timer.h",
    "prediction_model_override.cc",
    "prediction_model_override.h",
    "prediction_model_store.cc",
    "prediction_model_store.h",
  ]
  deps = [
    "//components/crx_file",
    "//components/download/public/background_service:public",
    "//components/optimization_guide/core",
    "//components/optimization_guide/optimization_guide_internals/webui:mojo_bindings",
    "//components/optimization_guide/proto:optimization_guide_proto",
    "//components/prefs",
    "//components/services/unzip/public/cpp",
    "//crypto",
    "//google_apis/common:request_util",
    "//net/traffic_annotation",
    "//services/network/public/cpp",
  ]
  configs += [ "//build/config/compiler:wexit_time_destructors" ]
  if (is_ios) {
    deps += [ "//components/services/unzip:in_process" ]
  } else {
    deps += [ "//components/services/unzip/content" ]
  }
}

static_library("test_support") {
  testonly = true
  sources = [
    "mock_optimization_guide_model_executor.cc",
    "mock_optimization_guide_model_executor.h",
    "mock_push_notification_manager.cc",
    "mock_push_notification_manager.h",
    "model_quality/test_model_quality_logs_uploader_service.cc",
    "model_quality/test_model_quality_logs_uploader_service.h",
    "optimization_guide_test_util.cc",
    "optimization_guide_test_util.h",
    "proto_database_provider_test_base.cc",
    "proto_database_provider_test_base.h",
    "test_hints_component_creator.cc",
    "test_hints_component_creator.h",
    "test_model_executor.cc",
    "test_model_executor.h",
    "test_model_handler.h",
    "test_model_info_builder.cc",
    "test_model_info_builder.h",
    "test_optimization_guide_decider.cc",
    "test_optimization_guide_decider.h",
    "test_optimization_guide_model_provider.cc",
    "test_optimization_guide_model_provider.h",
  ]
  if (build_with_tflite_lib) {
    sources += [
      "test_tflite_model_executor.cc",
      "test_tflite_model_executor.h",
      "test_tflite_model_handler.h",
    ]
  }

  deps = [
    ":bloomfilter",
    ":core",
    "//base",
    "//components/leveldb_proto",
    "//components/optimization_guide/proto:optimization_guide_proto",
    "//services/network/public/cpp",
    "//testing/gmock",
    "//testing/gtest",
    "//url",
  ]
  if (build_with_tflite_lib) {
    deps += [ "//third_party/abseil-cpp:absl" ]
  }
}

if (is_ios) {
  source_set("eg_test_support+eg2") {
    testonly = true
    sources = [
      "optimization_guide_constants.cc",
      "optimization_guide_constants.h",
      "optimization_guide_enums.h",
      "optimization_guide_switches.cc",
      "optimization_guide_switches.h",
      "optimization_guide_test_util.cc",
      "optimization_guide_test_util.h",
    ]
    deps = [
      "//base",
      "//components/optimization_guide/proto:optimization_guide_proto",
      "//google_apis",
      "//url",
    ]
  }
}

source_set("unit_tests") {
  testonly = true
  sources = [
    "bloom_filter_unittest.cc",
    "command_line_top_host_provider_unittest.cc",
    "feature_registry/enterprise_policy_registry_unittest.cc",
    "feature_registry/mqls_feature_registry_unittest.cc",
    "feature_registry/settings_ui_registry_unittest.cc",
    "hint_cache_unittest.cc",
    "hints_component_util_unittest.cc",
    "hints_fetcher_unittest.cc",
    "hints_manager_unittest.cc",
    "hints_processing_util_unittest.cc",
    "insertion_ordered_set_unittest.cc",
    "model_handler_unittest.cc",
    "model_quality/model_quality_log_entry_unittest.cc",
    "model_quality/model_quality_logs_uploader_service_unittest.cc",
    "model_quality/model_quality_util_unittest.cc",
    "model_store_metadata_entry_unittest.cc",
    "model_util_unittest.cc",
    "noisy_metrics_recorder_unittest.cc",
    "optimization_filter_unittest.cc",
    "optimization_guide_features_unittest.cc",
    "optimization_guide_navigation_data_unittest.cc",
    "optimization_guide_permissions_util_unittest.cc",
    "optimization_guide_proto_util_unittest.cc",
    "optimization_guide_store_unittest.cc",
    "optimization_guide_switches_unittest.cc",
    "optimization_guide_util_unittest.cc",
    "optimization_hints_component_update_listener_unittest.cc",
    "optimization_metadata_unittest.cc",
    "prediction_manager_unittest.cc",
    "prediction_model_download_manager_unittest.cc",
    "prediction_model_fetch_timer_unittest.cc",
    "prediction_model_fetcher_unittest.cc",
    "prediction_model_store_unittest.cc",
    "push_notification_manager_unittest.cc",
    "store_update_data_unittest.cc",
    "url_pattern_with_wildcards_unittest.cc",
  ]

  if (!is_fuchsia || !optimize_for_size) {
    sources += [
      "model_execution/aqa_response_parser_unittest.cc",
      "model_execution/json_response_parser_unittest.cc",
      "model_execution/model_execution_features_controller_unittest.cc",
      "model_execution/model_execution_features_unittest.cc",
      "model_execution/model_execution_fetcher_unittest.cc",
      "model_execution/model_execution_manager_unittest.cc",
      "model_execution/on_device_model_adaptation_loader_unittest.cc",
      "model_execution/on_device_model_component_unittest.cc",
      "model_execution/on_device_model_execution_proto_value_utils_unittest.cc",
      "model_execution/on_device_model_feature_adapter_unittest.cc",
      "model_execution/on_device_model_metadata_unittest.cc",
      "model_execution/on_device_model_service_controller_unittest.cc",
      "model_execution/on_device_model_validator_unittest.cc",
      "model_execution/optimization_guide_model_execution_error_unittest.cc",
      "model_execution/redactor_unittest.cc",
      "model_execution/repetition_checker_unittest.cc",
      "model_execution/simple_response_parser_unittest.cc",
      "model_execution/substitution_unittest.cc",
      "model_execution/test/fake_model_assets.cc",
      "model_execution/test/fake_model_assets.h",
      "model_execution/test/fake_on_device_model_service_controller.cc",
      "model_execution/test/fake_on_device_model_service_controller.h",
      "model_execution/test/feature_config_builder.cc",
      "model_execution/test/feature_config_builder.h",
      "model_execution/test/request_builder.cc",
      "model_execution/test/request_builder.h",
      "model_execution/test/response_holder.cc",
      "model_execution/test/response_holder.h",
      "model_execution/test/test_on_device_model_component_state_manager.cc",
      "model_execution/test/test_on_device_model_component_state_manager.h",
    ]
  }
  if (build_with_tflite_lib) {
    sources += [
      "bert_model_executor_unittest.cc",
      "model_validator_unittest.cc",
      "tflite_model_executor_unittest.cc",
    ]
  }

  if (build_with_mediapipe_lib) {
    assert(build_with_tflite_lib)

    sources += [ "mediapipe_text_model_executor_unittest.cc" ]
  }

  deps = [
    ":bloomfilter",
    ":core",
    ":features",
    ":prediction",
    ":test_support",
    ":unit_tests_bundle_data",
    "//base",
    "//base/test:test_proto",
    "//base/test:test_support",
    "//build:chromeos_buildflags",
    "//components/download/public/background_service/test:test_support",
    "//components/leveldb_proto:test_support",
    "//components/optimization_guide/proto:optimization_guide_proto",
    "//components/prefs:test_support",
    "//components/services/unzip:in_process",
    "//components/signin/public/identity_manager:test_support",
    "//components/sync_preferences:test_support",
    "//components/ukm:test_support",
    "//components/unified_consent",
    "//components/variations:test_support",
    "//net:test_support",
    "//services/data_decoder/public/cpp:test_support",
    "//services/network:network_service",
    "//services/network:test_support",
    "//services/on_device_model/public/cpp/test_support",
    "//testing/gmock",
    "//testing/gtest",
    "//third_party/tflite:buildflags",
    "//third_party/zlib/google:zip",
    "//ui/base",
    "//url:url",
  ]
  if (!is_ios) {
    deps += [ "//components/services/unzip/content" ]
  }

  if (!is_fuchsia || !optimize_for_size) {
    deps += [ "//components/component_updater" ]
  }

  if (build_with_tflite_lib) {
    deps += [
      "//third_party/abseil-cpp:absl",
      "//third_party/tflite",
      "//third_party/tflite:tflite_public_headers",
      "//third_party/tflite_support",
      "//third_party/tflite_support:tflite_support_proto",
    ]
  }

  if (build_with_mediapipe_lib) {
    assert(build_with_tflite_lib)

    deps += [ "//third_party/mediapipe" ]
  }
}

bundle_data("unit_tests_bundle_data") {
  visibility = [ ":unit_tests" ]
  testonly = true
  sources = [
    "//components/test/data/optimization_guide/bert_page_topics_model.tflite",
    "//components/test/data/optimization_guide/invalid_model.crx3",
    "//components/test/data/optimization_guide/simple_test.tflite",
  ]
  outputs = [ "{{bundle_resources_dir}}/" +
              "{{source_root_relative_dir}}/{{source_file_part}}" ]
}

if (is_android) {
  java_cpp_enum("optimization_guide_generated_enums") {
    sources = [ "optimization_guide_decision.h" ]
    visibility = [ "//chrome/browser/optimization_guide/android:*" ]
  }
}

if (is_mac && build_with_internal_optimization_guide) {
  # We need to copy the optimization guide shared library so that the
  # bundle_data dependencies have a "copy" target type.Otherwise for
  # "shared_library" target types it will try to link things into
  # Chromium Framework when we want to keep it separate instead.
  copy("optimization_guide_internal_library_copy") {
    sources = [ "$root_out_dir/liboptimization_guide_internal.dylib" ]
    outputs = [ "$root_out_dir/og_intermediates/{{source_file_part}}" ]
    deps = [
      "//components/optimization_guide/internal:optimization_guide_internal",
    ]
  }
}

if (build_with_tflite_lib) {
  fuzzer_test("optimization_guide_page_visibility_model_fuzzer") {
    sources = [ "page_visibility_model_fuzzer.cc" ]
    deps = [
      ":core",
      ":features",
      ":machine_learning",
      ":model_executor",
      "//base:base",
      "//base/test:test_support",
      "//components/optimization_guide/proto:optimization_guide_proto",
    ]
  }
}

action("on_device_model_execution_proto_generator") {
  script =
      "//components/optimization_guide/tools/gen_on_device_proto_descriptors.py"

  inputs = [
    "$root_gen_dir/components/optimization_guide/proto/compose.descriptor",
    "$root_gen_dir/components/optimization_guide/proto/string_value.descriptor",
    "$root_gen_dir/components/optimization_guide/proto/tab_organization.descriptor",
    "$root_gen_dir/components/optimization_guide/proto/history_answer.descriptor",
    "$root_gen_dir/components/optimization_guide/proto/summarize.descriptor",
  ]
  outputs = [ output_proto_descriptors_cc_file ]

  args = [
    "--output_cc=" +
        rebase_path(output_proto_descriptors_cc_file, root_build_dir),
    "--include=\"components/optimization_guide/proto/features/compose.pb.h\"",
    "--include=\"components/optimization_guide/proto/string_value.pb.h\"",
    "--include=\"components/optimization_guide/proto/features/tab_organization.pb.h\"",
    "--include=\"components/optimization_guide/proto/features/history_answer.pb.h\"",
    "--include=\"components/optimization_guide/proto/features/summarize.pb.h\"",
    "--request=.optimization_guide.proto.ComposeRequest",
    "--request=.optimization_guide.proto.TabOrganizationRequest",
    "--request=.optimization_guide.proto.HistoryAnswerRequest",
    "--request=.optimization_guide.proto.StringValue",
    "--request=.optimization_guide.proto.SummarizeRequest",
    "--response=.optimization_guide.proto.ComposeResponse",
    "--response=.optimization_guide.proto.TabOrganizationResponse",
    "--response=.optimization_guide.proto.HistoryAnswerResponse",
    "--response=.optimization_guide.proto.StringValue",
  ]
  foreach(file, inputs) {
    args += [
      "--input_file",
      rebase_path(file, root_build_dir),
    ]
  }

  deps = [
    "//components/optimization_guide/proto:compose_proto_descriptor",
    "//components/optimization_guide/proto:history_answer_proto_descriptor",
    "//components/optimization_guide/proto:string_value_proto_descriptor",
    "//components/optimization_guide/proto:summarize_descriptor",
    "//components/optimization_guide/proto:tab_organization_proto_descriptor",
    "//third_party/protobuf:py_proto_runtime",
  ]
}