0

[google_apis] Use const-string references

This CL uses const-string references for API keys accessors. This is
safe as the keys are stored in global variables and are never released.

Note: As a side effect this no longer loads the API keys on non-browser
processes as GaiaUrls::oauth2_chrome_client_id_ is no longer loaded
in the GaiaUrls() constructor.

Bug: 364842312

Change-Id: I527f4f25b00a174592bb6e36257ff06d39322b58
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5837280
Reviewed-by: Alex Ilin <alexilin@chromium.org>
Commit-Queue: Mihai Sardarescu <msarda@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1353250}
This commit is contained in:
Mihai Sardarescu
2024-09-10 11:32:46 +00:00
committed by Chromium LUCI CQ
parent c483b4583f
commit 629b3b3e83
4 changed files with 35 additions and 43 deletions

@ -269,11 +269,11 @@ const GURL& GaiaUrls::account_capabilities_url() const {
}
const std::string& GaiaUrls::oauth2_chrome_client_id() const {
return oauth2_chrome_client_id_;
return google_apis::GetOAuth2ClientID(google_apis::CLIENT_MAIN);
}
const std::string& GaiaUrls::oauth2_chrome_client_secret() const {
return oauth2_chrome_client_secret_;
return google_apis::GetOAuth2ClientSecret(google_apis::CLIENT_MAIN);
}
const GURL& GaiaUrls::oauth2_token_url() const {
@ -373,11 +373,6 @@ void GaiaUrls::InitializeDefault() {
tasks_api_origin_url_ = GURL(kDefaultTasksApiBaseUrl);
}
oauth2_chrome_client_id_ =
google_apis::GetOAuth2ClientID(google_apis::CLIENT_MAIN);
oauth2_chrome_client_secret_ =
google_apis::GetOAuth2ClientSecret(google_apis::CLIENT_MAIN);
CHECK(!gaia_origin_.opaque());
const GURL gaia_url = gaia_origin_.GetURL();
CHECK(gaia_url.SchemeIsHTTPOrHTTPS());

@ -116,9 +116,6 @@ class COMPONENT_EXPORT(GOOGLE_APIS) GaiaUrls {
GURL account_capabilities_url_;
GURL get_check_connection_info_url_;
std::string oauth2_chrome_client_id_;
std::string oauth2_chrome_client_secret_;
GURL oauth2_token_url_;
GURL oauth2_issue_token_url_;
GURL oauth2_token_info_url_;

@ -231,7 +231,7 @@ class MetricsKeyCache {
}
~MetricsKeyCache() = default;
std::string metrics_key() const { return metrics_key_; }
const std::string& metrics_key() const { return metrics_key_; }
private:
std::string metrics_key_;
@ -266,11 +266,11 @@ class MainApiKeyCache {
~MainApiKeyCache() = default;
std::string api_key() const { return api_key_; }
const std::string& api_key() const { return api_key_; }
#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_; }
const std::string& api_key_non_stable() const { return api_key_non_stable_; }
private:
std::string api_key_;
@ -319,15 +319,15 @@ class PerFeatureApiKeysCache {
}
~PerFeatureApiKeysCache() = default;
std::string api_key_remoting() const { return api_key_remoting_; }
std::string api_key_soda() const { return api_key_soda_; }
const std::string& api_key_remoting() const { return api_key_remoting_; }
const std::string& api_key_soda() const { return api_key_soda_; }
#if !BUILDFLAG(IS_ANDROID)
std::string api_key_hats() const { return api_key_hats_; }
const std::string& api_key_hats() const { return api_key_hats_; }
#endif
#if BUILDFLAG(IS_CHROMEOS_ASH)
std::string api_key_sharing() const { return api_key_sharing_; }
std::string api_key_read_aloud() const { return api_key_read_aloud_; }
std::string api_key_fresnel() const { return api_key_fresnel_; }
const std::string& api_key_sharing() const { return api_key_sharing_; }
const std::string& api_key_read_aloud() const { return api_key_read_aloud_; }
const std::string& api_key_fresnel() const { return api_key_fresnel_; }
#endif
private:
@ -400,7 +400,7 @@ class OAuthClientInfoCache {
}
~OAuthClientInfoCache() = default;
std::string GetClientID(OAuth2Client client) const {
const std::string& GetClientID(OAuth2Client client) const {
DCHECK_LT(client, CLIENT_NUM_ITEMS);
return client_ids_[client];
}
@ -411,7 +411,7 @@ class OAuthClientInfoCache {
}
#endif
std::string GetClientSecret(OAuth2Client client) const {
const std::string& GetClientSecret(OAuth2Client client) const {
DCHECK_LT(client, CLIENT_NUM_ITEMS);
return client_secrets_[client];
}
@ -440,40 +440,40 @@ bool HasAPIKeyConfigured() {
return GetAPIKey() != DUMMY_API_TOKEN;
}
std::string GetAPIKey(::version_info::Channel channel) {
const std::string& GetAPIKey(::version_info::Channel channel) {
return channel == ::version_info::Channel::STABLE
? GetAPIKey()
: g_main_api_key_cache.Get().api_key_non_stable();
}
std::string GetAPIKey() {
const std::string& GetAPIKey() {
return g_main_api_key_cache.Get().api_key();
}
std::string GetRemotingAPIKey() {
const std::string& GetRemotingAPIKey() {
return g_per_feature_api_key_cache.Get().api_key_remoting();
}
std::string GetSodaAPIKey() {
const std::string& GetSodaAPIKey() {
return g_per_feature_api_key_cache.Get().api_key_soda();
}
#if !BUILDFLAG(IS_ANDROID)
std::string GetHatsAPIKey() {
const std::string& GetHatsAPIKey() {
return g_per_feature_api_key_cache.Get().api_key_hats();
}
#endif
#if BUILDFLAG(IS_CHROMEOS_ASH)
std::string GetSharingAPIKey() {
const std::string& GetSharingAPIKey() {
return g_per_feature_api_key_cache.Get().api_key_sharing();
}
std::string GetReadAloudAPIKey() {
const std::string& GetReadAloudAPIKey() {
return g_per_feature_api_key_cache.Get().api_key_read_aloud();
}
std::string GetFresnelAPIKey() {
const std::string& GetFresnelAPIKey() {
return g_per_feature_api_key_cache.Get().api_key_fresnel();
}
#endif
@ -488,7 +488,7 @@ void SetAPIKey(const std::string& api_key) {
}
#endif
std::string GetMetricsKey() {
const std::string& GetMetricsKey() {
return g_metrics_key_cache.Get().metrics_key();
}
@ -504,11 +504,11 @@ bool HasOAuthClientConfigured() {
return true;
}
std::string GetOAuth2ClientID(OAuth2Client client) {
const std::string& GetOAuth2ClientID(OAuth2Client client) {
return g_oauth_client_info_cache.Get().GetClientID(client);
}
std::string GetOAuth2ClientSecret(OAuth2Client client) {
const std::string& GetOAuth2ClientSecret(OAuth2Client client) {
return g_oauth_client_info_cache.Get().GetClientSecret(client);
}

@ -81,35 +81,35 @@ COMPONENT_EXPORT(GOOGLE_APIS) bool HasAPIKeyConfigured();
// `AddDefaultAPIKeyToRequest()` rather than calling this method and manually
// adding the key.
COMPONENT_EXPORT(GOOGLE_APIS)
std::string GetAPIKey(version_info::Channel channel);
const std::string& GetAPIKey(version_info::Channel channel);
// Retrieves the API key, for the stable channel.
//
// DEPRECATED: Use `GetAPIKey(channel)` to get the right key for your
// distribution channel instead of calling this function directly.
COMPONENT_EXPORT(GOOGLE_APIS) std::string GetAPIKey();
COMPONENT_EXPORT(GOOGLE_APIS) const std::string& GetAPIKey();
// Retrieves the Chrome Remote Desktop API key.
COMPONENT_EXPORT(GOOGLE_APIS) std::string GetRemotingAPIKey();
COMPONENT_EXPORT(GOOGLE_APIS) const std::string& GetRemotingAPIKey();
// Retrieves the Speech On-Device API (SODA) API Key.
COMPONENT_EXPORT(GOOGLE_APIS) std::string GetSodaAPIKey();
COMPONENT_EXPORT(GOOGLE_APIS) const std::string& GetSodaAPIKey();
#if !BUILDFLAG(IS_ANDROID)
// Retrieves the HaTS API Key. This key is only used for desktop HaTS
// and the internal API Key is only defined in non-Android builds.
COMPONENT_EXPORT(GOOGLE_APIS) std::string GetHatsAPIKey();
COMPONENT_EXPORT(GOOGLE_APIS) const std::string& GetHatsAPIKey();
#endif
#if BUILDFLAG(IS_CHROMEOS_ASH)
// Retrieves the Sharing API Key.
COMPONENT_EXPORT(GOOGLE_APIS) std::string GetSharingAPIKey();
COMPONENT_EXPORT(GOOGLE_APIS) const std::string& GetSharingAPIKey();
// Retrieves the ReadAloud API Key.
COMPONENT_EXPORT(GOOGLE_APIS) std::string GetReadAloudAPIKey();
COMPONENT_EXPORT(GOOGLE_APIS) const std::string& GetReadAloudAPIKey();
// Retrieves the Fresnel API Key.
COMPONENT_EXPORT(GOOGLE_APIS) std::string GetFresnelAPIKey();
COMPONENT_EXPORT(GOOGLE_APIS) const std::string& GetFresnelAPIKey();
#endif
#if BUILDFLAG(SUPPORT_EXTERNAL_GOOGLE_API_KEY)
@ -121,7 +121,7 @@ void SetAPIKey(const std::string& api_key);
#endif
// Retrieves the key used to sign metrics (UMA/UKM) uploads.
COMPONENT_EXPORT(GOOGLE_APIS) std::string GetMetricsKey();
COMPONENT_EXPORT(GOOGLE_APIS) const std::string& GetMetricsKey();
// Represents the different sets of client IDs and secrets in use.
enum OAuth2Client {
@ -141,7 +141,7 @@ COMPONENT_EXPORT(GOOGLE_APIS) bool HasOAuthClientConfigured();
// Note that the ID should be escaped for the context you use it in,
// e.g. URL-escaped if you use it in a URL.
COMPONENT_EXPORT(GOOGLE_APIS)
std::string GetOAuth2ClientID(OAuth2Client client);
const std::string& GetOAuth2ClientID(OAuth2Client client);
// Retrieves the OAuth2 client secret for the specified client, or the
// empty string if not set.
@ -149,7 +149,7 @@ std::string GetOAuth2ClientID(OAuth2Client client);
// Note that the secret should be escaped for the context you use it
// in, e.g. URL-escaped if you use it in a URL.
COMPONENT_EXPORT(GOOGLE_APIS)
std::string GetOAuth2ClientSecret(OAuth2Client client);
const std::string& GetOAuth2ClientSecret(OAuth2Client client);
#if BUILDFLAG(IS_IOS)
// Sets the client id for the specified client. Should be called as early as