chromium/google_apis/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/chrome_build.gni")
import("//build/config/chromeos/ui_mode.gni")
import("//build/config/features.gni")
import("//extensions/buildflags/buildflags.gni")
import("//testing/test.gni")
import("//third_party/protobuf/proto_library.gni")

declare_args() {
  # Set the variable 'use_official_google_api_keys' to true
  # to use the Google-internal file containing official API keys
  # even in a developer build.  Setting this variable explicitly to
  # true will cause the build to fail if the internal file is missing.
  #
  # Set the variable to false to not use the internal file, even for
  # Chrome-branded builds or when it exists in your checkout.
  #
  # Leave it set to "" to have the variable implicitly set to true for
  # Chrome-branded builds or if
  # //src/google_apis/internal/google_chrome_api_keys.h is present and false
  # otherwise.
  # This does not apply to iOS builds, which use different mechanisms and always
  # evaluate to use_official_google_api_keys=false.
  # See https://crbug.com/1183709.
  use_official_google_api_keys = ""

  # Set these to bake the specified API keys and OAuth client
  # IDs/secrets into your build.
  #
  # If you create a build without values baked in, you can instead
  # set environment variables to provide the keys at runtime (see
  # src/google_apis/google_api_keys.h for details).  Features that
  # require server-side APIs may fail to work if no keys are
  # provided.
  #
  # Note that if `use_official_google_api_keys` has been set to true
  # (explicitly or implicitly), these values will be ignored and the official
  # keys will be used instead.
  google_api_key = ""

  # See google_api_key.
  google_default_client_id = ""

  # See google_api_key.
  google_default_client_secret = ""
}

if (use_official_google_api_keys == "") {
  # No override set. Determine the default behavior.
  if (is_chrome_branded) {
    # Chrome-branded builds default to behaving as if
    #`use_official_google_api_keys` was explicitly set to true and will fail to
    # build if the file is missing.
    use_official_google_api_keys = true
  } else {
    # Check if the key file exists.
    check_internal_result =
        exec_script("build/check_internal.py",
                    [ rebase_path("internal/google_chrome_api_keys.h",
                                  root_build_dir) ],
                    "value")
    use_official_google_api_keys = check_internal_result == 1
  }
}

# Official API keys should always be used for Chrome-branded builds except on
# iOS (see https://crbug.com/1183709) and Fuchsia (see the description of
# https://crbug.com/1171510 for background).
assert(
    use_official_google_api_keys || !is_chrome_branded || is_ios || is_fuchsia)

declare_args() {
  # Whether the SetAPIKey() method is defined.
  # iOS and Fuchsia builds that do not use the built-in key use this.
  support_external_google_api_key =
      is_ios || (is_fuchsia && !use_official_google_api_keys)
}

# internal:generate_metrics_key_header must be a deps when
# USE_OFFICIAL_GOOGLE_API_KEYS` is defined. Use this variable for both.
# On iOS, Chrome branding had been overriding use_official_google_api_keys.
# TODO(crbug.com/40864074): Fix this logic on iOS. Can this variable be replaced
# with just use_official_google_api_keys?
_use_official_google_keys_and_generate_metrics_key_header =
    use_official_google_api_keys || (is_ios && is_chrome_branded)

buildflag_header("buildflags") {
  header = "buildflags.h"
  flags = [ "SUPPORT_EXTERNAL_GOOGLE_API_KEY=$support_external_google_api_key" ]
}

config("key_defines") {
  defines = []

  if (_use_official_google_keys_and_generate_metrics_key_header) {
    defines += [ "USE_OFFICIAL_GOOGLE_API_KEYS=1" ]
  }
  if (google_api_key != "") {
    defines += [ "GOOGLE_API_KEY=\"$google_api_key\"" ]
  }
  if (google_default_client_id != "") {
    defines += [ "GOOGLE_DEFAULT_CLIENT_ID=\"$google_default_client_id\"" ]
  }
  if (google_default_client_secret != "") {
    defines +=
        [ "GOOGLE_DEFAULT_CLIENT_SECRET=\"$google_default_client_secret\"" ]
  }
}

component("google_apis") {
  sources = [
    "credentials_mode.cc",
    "credentials_mode.h",
    "gaia/core_account_id.cc",
    "gaia/core_account_id.h",
    "gaia/gaia_access_token_fetcher.cc",
    "gaia/gaia_access_token_fetcher.h",
    "gaia/gaia_auth_consumer.cc",
    "gaia/gaia_auth_consumer.h",
    "gaia/gaia_auth_fetcher.cc",
    "gaia/gaia_auth_fetcher.h",
    "gaia/gaia_auth_util.cc",
    "gaia/gaia_auth_util.h",
    "gaia/gaia_config.cc",
    "gaia/gaia_config.h",
    "gaia/gaia_constants.cc",
    "gaia/gaia_constants.h",
    "gaia/gaia_oauth_client.cc",
    "gaia/gaia_oauth_client.h",
    "gaia/gaia_switches.cc",
    "gaia/gaia_switches.h",
    "gaia/gaia_urls.cc",
    "gaia/gaia_urls.h",
    "gaia/google_service_auth_error.cc",
    "gaia/google_service_auth_error.h",
    "gaia/oauth2_access_token_consumer.cc",
    "gaia/oauth2_access_token_consumer.h",
    "gaia/oauth2_access_token_fetcher.cc",
    "gaia/oauth2_access_token_fetcher.h",
    "gaia/oauth2_access_token_fetcher_immediate_error.cc",
    "gaia/oauth2_access_token_fetcher_immediate_error.h",
    "gaia/oauth2_access_token_fetcher_impl.cc",
    "gaia/oauth2_access_token_fetcher_impl.h",
    "gaia/oauth2_access_token_manager.cc",
    "gaia/oauth2_access_token_manager.h",
    "gaia/oauth2_api_call_flow.cc",
    "gaia/oauth2_api_call_flow.h",
    "gaia/oauth2_id_token_decoder.cc",
    "gaia/oauth2_id_token_decoder.h",
    "gaia/oauth2_mint_access_token_fetcher_adapter.cc",
    "gaia/oauth2_mint_access_token_fetcher_adapter.h",
    "gaia/oauth2_mint_token_flow.cc",
    "gaia/oauth2_mint_token_flow.h",
    "gaia/oauth_multilogin_result.cc",
    "gaia/oauth_multilogin_result.h",
    "gaia/oauth_request_signer.cc",
    "gaia/oauth_request_signer.h",
    "google_api_keys-inc.cc",
    "google_api_keys.h",
    "google_api_keys_utils.cc",
    "google_api_keys_utils.h",
  ]

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

  public_deps = [
    ":buildflags",
    "//base",
    "//build:chromeos_buildflags",
    "//services/network/public/cpp",
  ]

  deps = [
    ":proto",
    "//base/version_info:channel",
    "//build:branding_buildflags",
    "//build:chromeos_buildflags",
    "//crypto",
    "//mojo/public/cpp/bindings:struct_traits",
    "//net",
    "//services/network/public/mojom",
  ]

  if (_use_official_google_keys_and_generate_metrics_key_header) {
    deps += [ "internal:generate_metrics_key_header" ]
  }

  if (is_apple) {
    sources += [
      "google_api_keys_mac.h",
      "google_api_keys_mac.mm",
    ]

    frameworks = [ "Foundation.framework" ]
  }

  defines = [ "IS_GOOGLE_APIS_IMPL" ]
}

proto_library("proto") {
  sources = [
    "gaia/bound_oauth_token.proto",
    "gaia/oauth2_mint_token_consent_result.proto",
  ]
}

static_library("test_support") {
  testonly = true
  sources = [
    "gaia/fake_gaia.cc",
    "gaia/fake_gaia.h",
    "gaia/fake_oauth2_access_token_manager.cc",
    "gaia/fake_oauth2_access_token_manager.h",
    "gaia/gaia_auth_test_util.cc",
    "gaia/gaia_auth_test_util.h",
    "gaia/gaia_urls_overrider_for_testing.cc",
    "gaia/gaia_urls_overrider_for_testing.h",
    "gaia/oauth2_access_token_manager_test_util.cc",
    "gaia/oauth2_access_token_manager_test_util.h",
  ]

  public_deps = [
    ":google_apis",
    ":proto",
    "//base",
    "//base/test:test_support",
    "//net:test_support",
    "//services/network:network_service",
    "//services/network:test_support",
    "//services/network/public/cpp",
  ]
}

source_set("run_all_unittests") {
  testonly = true
  sources = [ "run_all_unittests.cc" ]

  deps = [
    "//base/test:test_support",
    "//mojo/core/embedder",
  ]
}

test("google_apis_unittests") {
  sources = [
    "gaia/gaia_auth_fetcher_unittest.cc",
    "gaia/gaia_auth_util_unittest.cc",
    "gaia/gaia_config_unittest.cc",
    "gaia/gaia_oauth_client_unittest.cc",
    "gaia/gaia_urls_unittest.cc",
    "gaia/google_service_auth_error_unittest.cc",
    "gaia/oauth2_access_token_fetcher_impl_unittest.cc",
    "gaia/oauth2_access_token_manager_unittest.cc",
    "gaia/oauth2_api_call_flow_unittest.cc",
    "gaia/oauth2_id_token_decoder_unittest.cc",
    "gaia/oauth2_mint_access_token_fetcher_adapter_unittest.cc",
    "gaia/oauth2_mint_token_flow_unittest.cc",
    "gaia/oauth_multilogin_result_unittest.cc",
    "gaia/oauth_request_signer_unittest.cc",
    "google_api_keys_unittest.cc",
    "google_api_keys_unittest.h",
  ]

  configs += [ ":key_defines" ]

  deps = [
    ":google_apis",
    ":proto",
    ":run_all_unittests",
    ":test_support",
    "//base",
    "//base/test:test_support",
    "//base/version_info:channel",
    "//build:branding_buildflags",
    "//build:chromeos_buildflags",
    "//google_apis/calendar:calendar_unittests",
    "//google_apis/common:common_unittests",
    "//testing/gmock",
    "//testing/gtest",
  ]

  if (is_ios) {
    deps += [ ":google_apis_unittest_bundle_data" ]
  } else {
    data = [ "test/" ]
  }

  if (is_android) {
    deps += [ "//net/android:net_java" ]
  }

  if (is_fuchsia) {
    additional_manifest_fragments =
        [ "//build/config/fuchsia/test/network.shard.test-cml" ]
  }

  if (enable_extensions) {
    deps += [ "//google_apis/drive:drive_unittests" ]
  }

  if (is_apple) {
    sources += [ "google_api_keys_mac_unittest.mm" ]

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

  if (is_chromeos) {
    deps += [
      "//google_apis/classroom:classroom_unittests",
      "//google_apis/tasks:tasks_unittests",
    ]
  }
}

bundle_data("google_apis_unittest_bundle_data") {
  testonly = true
  sources = [
    "test/data/calendar/calendar_list.json",
    "test/data/calendar/event_self_response_statuses.json",
    "test/data/calendar/event_statuses.json",
    "test/data/calendar/event_with_attachments.json",
    "test/data/calendar/event_with_invalid_attachments.json",
    "test/data/calendar/event_with_invalid_conference_data_uri.json",
    "test/data/calendar/event_with_missing_entry_point_type.json",
    "test/data/calendar/events.json",
    "test/data/calendar/invalid_events.json",
    "test/data/gaia/all_base_urls.json",
    "test/data/gaia/all_urls.json",
    "test/data/gaia/api_keys.json",
    "test/data/gaia/bad_url.json",
    "test/data/gaia/bad_url_key.json",
    "test/data/gaia/bad_urls_key.json",
    "test/data/gaia/not_a_json.txt",
    "test/data/gaia/one_base_url.json",
    "test/data/gaia/one_url.json",
    "test/data/tasks/task.json",
    "test/data/tasks/task_lists.json",
    "test/data/tasks/tasks.json",
  ]
  outputs = [ "{{bundle_resources_dir}}/" +
              "{{source_root_relative_dir}}/{{source_file_part}}" ]
}