1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef GOOGLE_APIS_GOOGLE_API_KEYS_H_
6 #define GOOGLE_APIS_GOOGLE_API_KEYS_H_
8 // If you add more includes to this file, you also need to add them to
9 // google_api_keys_unittest.cc.
12 #include "base/component_export.h"
13 #include "build/build_config.h"
14 #include "build/chromeos_buildflags.h"
15 #include "google_apis/buildflags.h"
17 // These functions enable you to retrieve keys to use for Google APIs
18 // such as Translate and Safe Browsing.
20 // You can retrieve either an "API key" (sometimes called a developer
21 // key) which identifies you (or the company you work for) as a
22 // developer, or you can retrieve the "client ID" and "client secret"
23 // used by you (or the company you work for) to generate OAuth2
26 // Each developer (or group of developers working together for a
27 // single company) must request a Google API key and the client ID and
28 // client secret for OAuth2. See
29 // https://developers.google.com/console/help/ and
30 // https://developers.google.com/console/.
32 // The keys must either be provided using preprocessor variables (set via e.g.
33 // GN args). Alternatively, they can be overridden at runtime using one of the
34 // following methods (in priority order):
35 // - Command line parameters (only for GOOGLE_CLIENT_ID_MAIN and
36 // GOOGLE_CLIENT_SECRET_MAIN values). The command-line parameters are
37 // --oauth2-client-id and --oauth2-client-secret.
38 // - Config file entry of the same name. Path to a config file is set via the
39 // --gaia-config command line parameter. See google_apis/gaia/gaia_config.h
40 // for syntax reference.
41 // - Environment variables of the same name. Environment variable overrides will
42 // be ignored for official Google Chrome builds.
44 // The names of the preprocessor variables (or environment variables
45 // to override them at runtime in Chromium builds) are as follows:
46 // - GOOGLE_API_KEY: The API key, a.k.a. developer key.
47 // - GOOGLE_DEFAULT_CLIENT_ID: If set, this is used as the default for
48 // all client IDs not otherwise set. This is intended only for
50 // - GOOGLE_DEFAULT_CLIENT_SECRET: If set, this is used as the default
51 // for all client secrets. This is intended only for development.
52 // - GOOGLE_CLIENT_ID_[client name]
53 // - GOOGLE_CLIENT_SECRET_[client name]
54 // (e.g. GOOGLE_CLIENT_SECRET_REMOTING, i.e. one for each item in
55 // the OAuth2Client enumeration below)
57 // If some of the parameters mentioned above are not provided,
58 // Chromium will still build and run, but services that require them
59 // may fail to work without warning. They should do so gracefully,
60 // similar to what would happen when a network connection is
63 namespace google_apis {
65 COMPONENT_EXPORT(GOOGLE_APIS) extern const char kAPIKeysDevelopersHowToURL[];
67 // Returns true if no dummy API key is set.
68 COMPONENT_EXPORT(GOOGLE_APIS) bool HasAPIKeyConfigured();
70 // Retrieves the API key, a.k.a. developer key, or a dummy string
73 // Note that the key should be escaped for the context you use it in,
74 // e.g. URL-escaped if you use it in a URL.
75 COMPONENT_EXPORT(GOOGLE_APIS) std::string GetAPIKey();
77 // Non-stable channels may have a different Google API key.
78 COMPONENT_EXPORT(GOOGLE_APIS) std::string GetNonStableAPIKey();
80 // Retrieves the Chrome Remote Desktop API key.
81 COMPONENT_EXPORT(GOOGLE_APIS) std::string GetRemotingAPIKey();
83 // Retrieves the Speech On-Device API (SODA) API Key.
84 COMPONENT_EXPORT(GOOGLE_APIS) std::string GetSodaAPIKey();
86 #if !BUILDFLAG(IS_ANDROID)
87 // Retrieves the HaTS API Key. This key is only used for desktop HaTS
88 // and the internal API Key is only defined in non-Android builds.
89 COMPONENT_EXPORT(GOOGLE_APIS) std::string GetHatsAPIKey();
92 #if BUILDFLAG(IS_CHROMEOS_ASH)
93 // Retrieves the Sharing API Key.
94 COMPONENT_EXPORT(GOOGLE_APIS) std::string GetSharingAPIKey();
96 // Retrieves the ReadAloud API Key.
97 COMPONENT_EXPORT(GOOGLE_APIS) std::string GetReadAloudAPIKey();
99 // Retrieves the Fresnel API Key.
100 COMPONENT_EXPORT(GOOGLE_APIS) std::string GetFresnelAPIKey();
103 #if BUILDFLAG(SUPPORT_EXTERNAL_GOOGLE_API_KEY)
104 // Sets the API key. This should be called as early as possible before this
105 // API key is even accessed. It must be called before GetAPIKey.
106 // TODO(https://crbug.com/1166007): Enforce this is called before GetAPIKey.
107 COMPONENT_EXPORT(GOOGLE_APIS) void SetAPIKey(const std::string& api_key);
110 // Retrieves the key used to sign metrics (UMA/UKM) uploads.
111 COMPONENT_EXPORT(GOOGLE_APIS) std::string GetMetricsKey();
113 // Represents the different sets of client IDs and secrets in use.
115 CLIENT_MAIN, // Several different features use this.
117 CLIENT_REMOTING_HOST,
119 CLIENT_NUM_ITEMS // Must be last item.
122 // Returns true if no dummy OAuth2 client ID and secret are set.
123 COMPONENT_EXPORT(GOOGLE_APIS) bool HasOAuthClientConfigured();
125 // Retrieves the OAuth2 client ID for the specified client, or the
126 // empty string if not set.
128 // Note that the ID should be escaped for the context you use it in,
129 // e.g. URL-escaped if you use it in a URL.
130 COMPONENT_EXPORT(GOOGLE_APIS)
131 std::string GetOAuth2ClientID(OAuth2Client client);
133 // Retrieves the OAuth2 client secret for the specified client, or the
134 // empty string if not set.
136 // Note that the secret should be escaped for the context you use it
137 // in, e.g. URL-escaped if you use it in a URL.
138 COMPONENT_EXPORT(GOOGLE_APIS)
139 std::string GetOAuth2ClientSecret(OAuth2Client client);
141 #if BUILDFLAG(IS_IOS)
142 // Sets the client id for the specified client. Should be called as early as
143 // possible before these ids are accessed.
144 COMPONENT_EXPORT(GOOGLE_APIS)
145 void SetOAuth2ClientID(OAuth2Client client, const std::string& client_id);
147 // Sets the client secret for the specified client. Should be called as early as
148 // possible before these secrets are accessed.
149 COMPONENT_EXPORT(GOOGLE_APIS)
150 void SetOAuth2ClientSecret(OAuth2Client client,
151 const std::string& client_secret);
154 // Returns if the API key using in the current build is the one for official
156 COMPONENT_EXPORT(GOOGLE_APIS) bool IsGoogleChromeAPIKeyUsed();
158 } // namespace google_apis
160 #endif // GOOGLE_APIS_GOOGLE_API_KEYS_H_