0

[google_apis] Add BUILDFLAG for whether to define SetAPIKey()

Replace iOS and Fuchsia conditions with a new
SUPPORT_EXTERNAL_GOOGLE_API_KEY BUILDFLAG so the logic is only defined
in one location and can be referenced outside //google_apis.

No build logic is changed.
`#if BUILDFLAG(IS_IOS) || BUILDFLAG(IS_FUCHSIA)` is replaced with
`#if BUILDFLAG(SUPPORT_EXTERNAL_GOOGLE_API_KEY)`, which is defined as
`is_ios || is_fuchsia`.

Bug: 1294915
Change-Id: I9a57727d7df86bd22f04c32946af4b49b1b69f29
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3756795
Auto-Submit: David Dorwin <ddorwin@chromium.org>
Reviewed-by: Sylvain Defresne <sdefresne@chromium.org>
Commit-Queue: David Dorwin <ddorwin@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1045946}
This commit is contained in:
David Dorwin
2022-09-12 19:11:48 +00:00
committed by Chromium LUCI CQ
parent 56657e989e
commit c5f373c3a2
4 changed files with 23 additions and 3 deletions

@ -22,6 +22,7 @@
#include "fuchsia_web/webengine/common/web_engine_content_client.h"
#include "fuchsia_web/webengine/renderer/web_engine_content_renderer_client.h"
#include "fuchsia_web/webengine/switches.h"
#include "google_apis/buildflags.h"
#include "google_apis/google_api_keys.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/base/ui_base_paths.h"
@ -80,8 +81,12 @@ absl::optional<int> WebEngineMainDelegate::BasicStartupComplete() {
}
if (command_line->HasSwitch(switches::kGoogleApiKey)) {
#if BUILDFLAG(SUPPORT_EXTERNAL_GOOGLE_API_KEY)
google_apis::SetAPIKey(
command_line->GetSwitchValueASCII(switches::kGoogleApiKey));
#else
LOG(WARNING) << "Ignored " << switches::kGoogleApiKey;
#endif
}
SetCorsExemptHeaders(base::SplitString(

@ -2,6 +2,7 @@
# 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/features.gni")
import("//extensions/buildflags/buildflags.gni")
@ -59,6 +60,16 @@ if (use_official_google_api_keys == "") {
use_official_google_api_keys = check_internal_result == 1
}
declare_args() {
# Whether the SetAPIKey() method is defined.
support_external_google_api_key = is_ios || is_fuchsia
}
buildflag_header("buildflags") {
header = "buildflags.h"
flags = [ "SUPPORT_EXTERNAL_GOOGLE_API_KEY=$support_external_google_api_key" ]
}
config("key_defines") {
defines = []
@ -132,6 +143,8 @@ template("google_apis_tmpl") {
configs += [ ":key_defines" ]
public_deps = [ ":buildflags" ]
deps = [
":oauth2_mint_token_consent_result_proto",
"//base",

@ -18,6 +18,7 @@
#include "base/logging.h"
#include "base/strings/stringize_macros.h"
#include "build/branding_buildflags.h"
#include "google_apis/buildflags.h"
#include "google_apis/gaia/gaia_config.h"
#include "google_apis/gaia/gaia_switches.h"
@ -205,7 +206,7 @@ class APIKeyCache {
}
std::string api_key() const { return api_key_; }
#if BUILDFLAG(IS_IOS) || BUILDFLAG(IS_FUCHSIA)
#if BUILDFLAG(SUPPORT_EXTERNAL_GOOGLE_API_KEY)
void set_api_key(const std::string& api_key) { api_key_ = api_key; }
#endif
std::string api_key_non_stable() const { return api_key_non_stable_; }
@ -369,7 +370,7 @@ std::string GetFresnelAPIKey() {
return g_api_key_cache.Get().api_key_fresnel();
}
#if BUILDFLAG(IS_IOS) || BUILDFLAG(IS_FUCHSIA)
#if BUILDFLAG(SUPPORT_EXTERNAL_GOOGLE_API_KEY)
void SetAPIKey(const std::string& api_key) {
g_api_key_cache.Get().set_api_key(api_key);
}

@ -10,6 +10,7 @@
#include <string>
#include "build/build_config.h"
#include "google_apis/buildflags.h"
// These functions enable you to retrieve keys to use for Google APIs
// such as Translate and Safe Browsing.
@ -89,7 +90,7 @@ std::string GetReadAloudAPIKey();
// Retrieves the Fresnel API Key.
std::string GetFresnelAPIKey();
#if BUILDFLAG(IS_IOS) || BUILDFLAG(IS_FUCHSIA)
#if BUILDFLAG(SUPPORT_EXTERNAL_GOOGLE_API_KEY)
// Sets the API key. This should be called as early as possible before this
// API key is even accessed. It must be called before GetAPIKey.
// TODO(https://crbug.com/1166007): Enforce this is called before GetAPIKey.