File: google_api_keys.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (194 lines) | stat: -rw-r--r-- 7,598 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef GOOGLE_APIS_GOOGLE_API_KEYS_H_
#define GOOGLE_APIS_GOOGLE_API_KEYS_H_

#include <string>

#include "base/component_export.h"
#include "base/functional/callback_helpers.h"
#include "build/build_config.h"
#include "google_apis/buildflags.h"

namespace version_info {
enum class Channel;
}

// These functions enable you to retrieve keys to use for Google APIs
// such as Translate and Safe Browsing.
//
// You can retrieve either an "API key" (sometimes called a developer
// key) which identifies you (or the company you work for) as a
// developer, or you can retrieve the "client ID" and "client secret"
// used by you (or the company you work for) to generate OAuth2
// requests.
//
// Each developer (or group of developers working together for a
// single company) must request a Google API key and the client ID and
// client secret for OAuth2.  See
// https://developers.google.com/console/help/ and
// https://developers.google.com/console/.
//
// The keys must either be provided using preprocessor variables (set via e.g.
// GN args). Alternatively, they can be overridden at runtime using one of the
// following methods (in priority order):
// - Command line parameters (only for GOOGLE_CLIENT_ID_MAIN and
//   GOOGLE_CLIENT_SECRET_MAIN values). The command-line parameters are
//   --oauth2-client-id and --oauth2-client-secret.
// - Config file entry of the same name. Path to a config file is set via the
//   --gaia-config command line parameter. See google_apis/gaia/gaia_config.h
//   for syntax reference.
// - Environment variables of the same name. Environment variable overrides will
//   be ignored for official Google Chrome builds.
//
// The names of the preprocessor variables (or environment variables
// to override them at runtime in Chromium builds) are as follows:
// - GOOGLE_API_KEY: The API key, a.k.a. developer key.
// - GOOGLE_DEFAULT_CLIENT_ID: If set, this is used as the default for
//   all client IDs not otherwise set.  This is intended only for
//   development.
// - GOOGLE_DEFAULT_CLIENT_SECRET: If set, this is used as the default
//   for all client secrets.  This is intended only for development.
// - GOOGLE_CLIENT_ID_[client name]
// - GOOGLE_CLIENT_SECRET_[client name]
//   (e.g. GOOGLE_CLIENT_SECRET_REMOTING, i.e. one for each item in
//   the OAuth2Client enumeration below)
//
// If some of the parameters mentioned above are not provided,
// Chromium will still build and run, but services that require them
// may fail to work without warning.  They should do so gracefully,
// similar to what would happen when a network connection is
// unavailable.

namespace google_apis {

class ApiKeyCache;

COMPONENT_EXPORT(GOOGLE_APIS) extern const char kAPIKeysDevelopersHowToURL[];

// Returns true if no dummy API key is set.
COMPONENT_EXPORT(GOOGLE_APIS) bool HasAPIKeyConfigured();

// Retrieves the API key, a.k.a. developer key, or a dummy string
// if not set.
//
// Note that the key should be escaped for the context you use it in,
// e.g. URL-escaped if you use it in a URL.
//
// If you want to attach the key to a network request, consider using
// `AddDefaultAPIKeyToRequest()` rather than calling this method and manually
// adding the key.
COMPONENT_EXPORT(GOOGLE_APIS)
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) const std::string& GetAPIKey();

// Retrieves the Chrome Remote Desktop API key.
COMPONENT_EXPORT(GOOGLE_APIS) const std::string& GetRemotingAPIKey();

// Retrieves the Speech On-Device API (SODA) API Key.
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) const std::string& GetHatsAPIKey();
#endif

#if BUILDFLAG(IS_CHROMEOS)
// Retrieves the Sharing API Key.
COMPONENT_EXPORT(GOOGLE_APIS) const std::string& GetSharingAPIKey();

// Retrieves the ReadAloud API Key.
COMPONENT_EXPORT(GOOGLE_APIS) const std::string& GetReadAloudAPIKey();

// Retrieves the Fresnel API Key.
COMPONENT_EXPORT(GOOGLE_APIS) const std::string& GetFresnelAPIKey();

// Retrieves the Boca API Key.
COMPONENT_EXPORT(GOOGLE_APIS) const std::string& GetBocaAPIKey();

// Retrieves the ChromeOS-specific Geolocation API Key used by the System
// Services.
COMPONENT_EXPORT(GOOGLE_APIS) const std::string& GetCrosSystemGeoAPIKey();
// Retrieves the ChromeOS-specific Geolocation API Key used by the Chrome
// browser.
COMPONENT_EXPORT(GOOGLE_APIS) const std::string& GetCrosChromeGeoAPIKey();

#endif

// Retrieves the key used to sign metrics (UMA/UKM) uploads.
COMPONENT_EXPORT(GOOGLE_APIS) const std::string& GetMetricsKey();

// Represents the different sets of client IDs and secrets in use.
enum OAuth2Client {
  CLIENT_MAIN,  // Several different features use this.
  CLIENT_REMOTING,
  CLIENT_REMOTING_HOST,

  CLIENT_NUM_ITEMS  // Must be last item.
};

// Returns true if no dummy OAuth2 client ID and secret are set.
COMPONENT_EXPORT(GOOGLE_APIS) bool HasOAuthClientConfigured();

// Retrieves the OAuth2 client ID for the specified client, or the
// empty string if not set.
//
// 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)
const std::string& GetOAuth2ClientID(OAuth2Client client);

// Retrieves the OAuth2 client secret for the specified client, or the
// empty string if not set.
//
// 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)
const std::string& GetOAuth2ClientSecret(OAuth2Client client);

// Returns if the API key using in the current build is the one for official
// Google Chrome.
COMPONENT_EXPORT(GOOGLE_APIS) bool IsGoogleChromeAPIKeyUsed();

#if BUILDFLAG(SUPPORT_EXTERNAL_GOOGLE_API_KEY)
// Initializes the API keys with default values and overrides the main APIKey
// with `api_key`.
//
// Note: Following this call, `GetApiKey()` and `GetAPIKey(channel)` will
// return `api_key`. This function does not override the other API keys
// or client IDS & client secrets.
COMPONENT_EXPORT(GOOGLE_APIS)
void InitializeAndOverrideAPIKey(const std::string& api_key);

// Initializes the API keys with default values and overrides the `APIKey` with
// `api_key`, all the client IDs with `client_id` and all client secrets with
// `client_secret`.
//
// Note: Following this call, `GetApiKey()` and `GetAPIKey(channel)` will
// return `api_key`, `GetOAuth2ClientID(*)` will return 'client_id' and
// `GetOAuth2ClientSecret(*)` will return `client_secret`. This function does
// not override the other API keys.
COMPONENT_EXPORT(GOOGLE_APIS)
void InitializeAndOverrideAPIKeyAndOAuthClient(
    const std::string& api_key,
    const std::string& client_id,
    const std::string& client_secret);
#endif

// Sets a testing global instance of `ApiKeyCache` and returns a scoped object
// that will restore the previous value once destroyed.
COMPONENT_EXPORT(GOOGLE_APIS)
base::ScopedClosureRunner
    SetScopedApiKeyCacheForTesting(ApiKeyCache* api_key_cache);

}  // namespace google_apis

#endif  // GOOGLE_APIS_GOOGLE_API_KEYS_H_