chromium/components/keyed_service/core/BUILD.gn

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

import("//build/buildflag_header.gni")
import("//build/config/features.gni")

component("core") {
  output_name = "keyed_service_core"
  sources = [
    "dependency_graph.cc",
    "dependency_graph.h",
    "dependency_manager.cc",
    "dependency_manager.h",
    "dependency_node.h",
    "keyed_service.h",
    "keyed_service_base_factory.cc",
    "keyed_service_base_factory.h",
    "keyed_service_export.h",
    "keyed_service_factory.h",
    "keyed_service_shutdown_notifier.cc",
    "keyed_service_shutdown_notifier.h",
    "keyed_service_templated_factory.cc",
    "keyed_service_templated_factory.h",
    "refcounted_keyed_service.cc",
    "refcounted_keyed_service.h",
    "refcounted_keyed_service_factory.h",
    "service_access_type.h",
    "simple_dependency_manager.cc",
    "simple_dependency_manager.h",
    "simple_factory_key.cc",
    "simple_factory_key.h",
    "simple_key_map.cc",
    "simple_key_map.h",
    "simple_keyed_service_factory.cc",
    "simple_keyed_service_factory.h",
  ]

  defines = [ "KEYED_SERVICE_IMPLEMENTATION" ]

  public_deps = [
    ":features",
    "//base",
  ]

  deps = [
    "//components/pref_registry",
    "//components/prefs",
    "//services/tracing/public/cpp:cpp",
  ]
}

buildflag_header("features") {
  # If this flag is set to `true`, then the `DependencyManager` will assert
  # that no factories are registered after the creation of the first context
  # object and the `KeyedServiceTemplatedFactory` will be allowed to depend
  # on that assumption.
  #
  # TODO(crbug.com/40158018): remove this flag when set to `true` for all
  # embedders. This will requires fixing the registration of all factories
  # to happen before the creation of the first context (i.e. by listing all
  # factories in the `Ensure.*KeyedServiceFactoriesBuilt` free functions).
  has_tight_factory_registration = !use_blink

  # If this flag is set to `true`, then `KeyedServiceTemplatedFactory` will
  # be allowed to rely on the fact that the lifetime of the context and the
  # service will follow the order described by `MappingStage`. In addition,
  # it will enforce that no instance of a service is created before the call
  # to `CreateContextServices` (i.e. when the context is marked as fully
  # initialized).
  #
  # TODO(crbug.com/342112724): remove this flag when set to `true` for all
  # embedders. This will requires correctly calling the method `MarkContextLive`,
  # `CreateContextServices`, but also `ContextShutdown` and `ContextDestroyed`
  # in the correct order. Additionally, this requires changing the factories of
  # services created before CreateContextServices to override the method
  # `ServiceIsRequiredForContextInitializatio`n to return true.
  has_tight_context_initialization = !use_blink

  # If this flag is set to `true`, then `KeyedServiceTemplatedFactory` will
  # expose the deprecated `Associate`/`Disassociate`API. Those API can break
  # the invariants of `KeyedServiceTemplatedFactory` as they allow to ignore
  # the lifetime cycle, also allow to destroy a service instance without going
  # through the two phase shutdown and allow to destroy a service that is still
  # in use.
  #
  # TODO(crbug.com/351120207): remove this flag when set to `false` for all
  # embedders. This will requires remove all explicit calls to `Associate`
  # and `Disassociate` methods.
  has_deprecated_associate_api = use_blink

  # has_tight_context_initialization requires has_tight_factory_registration
  # to be true and has_deprecated_associate_api to be false. Assert this.
  assert(
      !has_tight_context_initialization ||
          (has_tight_factory_registration && !has_deprecated_associate_api),
      "has_tight_context_initialization only supported if " +
          "has_tight_factory_registration and not has_deprecated_associate_api")

  header = "features_buildflags.h"
  flags = [
    "KEYED_SERVICE_HAS_TIGHT_REGISTRATION=$has_tight_factory_registration",
    "KEYED_SERVICE_HAS_TIGHT_INITIALIZATION=$has_tight_context_initialization",
    "KEYED_SERVICE_HAS_DEPRECATED_ASSOCIATE_API=$has_deprecated_associate_api",
  ]
}

source_set("unit_tests") {
  testonly = true
  sources = [ "dependency_graph_unittest.cc" ]
  deps = [
    ":core",
    "//base",
    "//testing/gtest",
    "//third_party/re2",
  ]
}