Avi Drissman | 8ce0344d | 2022-09-13 20:40:56 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
[email protected] | e5e894ff | 2012-09-06 02:18:47 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 6386cf5 | 2012-09-07 04:26:37 | [diff] [blame] | 5 | #ifndef GOOGLE_APIS_GOOGLE_API_KEYS_H_ |
| 6 | #define GOOGLE_APIS_GOOGLE_API_KEYS_H_ |
[email protected] | e5e894ff | 2012-09-06 02:18:47 | [diff] [blame] | 7 | |
[email protected] | e03604b | 2013-01-10 23:38:22 | [diff] [blame] | 8 | // If you add more includes to this file, you also need to add them to |
| 9 | // google_api_keys_unittest.cc. |
[email protected] | e5e894ff | 2012-09-06 02:18:47 | [diff] [blame] | 10 | #include <string> |
| 11 | |
Sharon Yang | 41d26ed | 2021-01-14 21:23:52 | [diff] [blame] | 12 | #include "build/build_config.h" |
David Dorwin | 581738b | 2022-10-28 10:40:50 | [diff] [blame] | 13 | #include "build/chromeos_buildflags.h" |
David Dorwin | c5f373c | 2022-09-12 19:11:48 | [diff] [blame] | 14 | #include "google_apis/buildflags.h" |
Sharon Yang | 41d26ed | 2021-01-14 21:23:52 | [diff] [blame] | 15 | |
[email protected] | e5e894ff | 2012-09-06 02:18:47 | [diff] [blame] | 16 | // These functions enable you to retrieve keys to use for Google APIs |
| 17 | // such as Translate and Safe Browsing. |
| 18 | // |
| 19 | // You can retrieve either an "API key" (sometimes called a developer |
| 20 | // key) which identifies you (or the company you work for) as a |
| 21 | // developer, or you can retrieve the "client ID" and "client secret" |
| 22 | // used by you (or the company you work for) to generate OAuth2 |
| 23 | // requests. |
| 24 | // |
| 25 | // Each developer (or group of developers working together for a |
| 26 | // single company) must request a Google API key and the client ID and |
| 27 | // client secret for OAuth2. See |
| 28 | // https://developers.google.com/console/help/ and |
| 29 | // https://developers.google.com/console/. |
| 30 | // |
Alex Ilin | 9ac2a9c | 2020-07-06 14:28:55 | [diff] [blame] | 31 | // The keys must either be provided using preprocessor variables (set via e.g. |
| 32 | // GN args). Alternatively, they can be overridden at runtime using one of the |
| 33 | // following methods (in priority order): |
| 34 | // - Command line parameters (only for GOOGLE_CLIENT_ID_MAIN and |
| 35 | // GOOGLE_CLIENT_SECRET_MAIN values). The command-line parameters are |
| 36 | // --oauth2-client-id and --oauth2-client-secret. |
| 37 | // - Config file entry of the same name. Path to a config file is set via the |
| 38 | // --gaia-config command line parameter. See google_apis/gaia/gaia_config.h |
| 39 | // for syntax reference. |
| 40 | // - Environment variables of the same name. Environment variable overrides will |
| 41 | // be ignored for official Google Chrome builds. |
[email protected] | e5e894ff | 2012-09-06 02:18:47 | [diff] [blame] | 42 | // |
| 43 | // The names of the preprocessor variables (or environment variables |
robertshield | 8d06e9e | 2017-06-23 14:09:18 | [diff] [blame] | 44 | // to override them at runtime in Chromium builds) are as follows: |
[email protected] | e5e894ff | 2012-09-06 02:18:47 | [diff] [blame] | 45 | // - GOOGLE_API_KEY: The API key, a.k.a. developer key. |
| 46 | // - GOOGLE_DEFAULT_CLIENT_ID: If set, this is used as the default for |
| 47 | // all client IDs not otherwise set. This is intended only for |
| 48 | // development. |
| 49 | // - GOOGLE_DEFAULT_CLIENT_SECRET: If set, this is used as the default |
| 50 | // for all client secrets. This is intended only for development. |
| 51 | // - GOOGLE_CLIENT_ID_[client name] |
[email protected] | e5e894ff | 2012-09-06 02:18:47 | [diff] [blame] | 52 | // - GOOGLE_CLIENT_SECRET_[client name] |
Lei Zhang | 2c1a1e54 | 2022-01-21 19:41:43 | [diff] [blame] | 53 | // (e.g. GOOGLE_CLIENT_SECRET_REMOTING, i.e. one for each item in |
[email protected] | e5e894ff | 2012-09-06 02:18:47 | [diff] [blame] | 54 | // the OAuth2Client enumeration below) |
| 55 | // |
[email protected] | e5e894ff | 2012-09-06 02:18:47 | [diff] [blame] | 56 | // If some of the parameters mentioned above are not provided, |
| 57 | // Chromium will still build and run, but services that require them |
| 58 | // may fail to work without warning. They should do so gracefully, |
| 59 | // similar to what would happen when a network connection is |
| 60 | // unavailable. |
| 61 | |
| 62 | namespace google_apis { |
| 63 | |
dzhioev | bd066141 | 2015-10-20 23:00:07 | [diff] [blame] | 64 | extern const char kAPIKeysDevelopersHowToURL[]; |
| 65 | |
Mihai Sardarescu | 806c3540 | 2018-06-20 17:01:06 | [diff] [blame] | 66 | // Returns true if no dummy API key is set. |
| 67 | bool HasAPIKeyConfigured(); |
[email protected] | e7dd789 | 2013-05-16 19:10:40 | [diff] [blame] | 68 | |
| 69 | // Retrieves the API key, a.k.a. developer key, or a dummy string |
[email protected] | e5e894ff | 2012-09-06 02:18:47 | [diff] [blame] | 70 | // if not set. |
| 71 | // |
| 72 | // Note that the key should be escaped for the context you use it in, |
| 73 | // e.g. URL-escaped if you use it in a URL. |
| 74 | std::string GetAPIKey(); |
| 75 | |
jkrcal | f7baf81 | 2016-03-22 10:23:42 | [diff] [blame] | 76 | // Non-stable channels may have a different Google API key. |
| 77 | std::string GetNonStableAPIKey(); |
| 78 | |
Yuwei Huang | 701d2093 | 2019-05-10 22:19:02 | [diff] [blame] | 79 | // Retrieves the Chrome Remote Desktop API key. |
| 80 | std::string GetRemotingAPIKey(); |
Yuwei Huang | 48b145e7 | 2019-02-13 19:07:26 | [diff] [blame] | 81 | |
David Dorwin | 97f1a03 | 2022-11-02 21:44:03 | [diff] [blame^] | 82 | // Retrieves the Speech On-Device API (SODA) API Key. |
| 83 | std::string GetSodaAPIKey(); |
| 84 | |
David Dorwin | 581738b | 2022-10-28 10:40:50 | [diff] [blame] | 85 | #if BUILDFLAG(IS_CHROMEOS_ASH) |
Himanshu Jaju | 6d02188 | 2020-01-23 10:32:48 | [diff] [blame] | 86 | // Retrieves the Sharing API Key. |
| 87 | std::string GetSharingAPIKey(); |
evliu | dcab713 | 2020-07-23 09:17:21 | [diff] [blame] | 88 | |
Joel Riley | 43ccd76 | 2021-06-04 20:25:57 | [diff] [blame] | 89 | // Retrieves the ReadAloud API Key. |
| 90 | std::string GetReadAloudAPIKey(); |
| 91 | |
Hirthanan Subenderan | 658abca4 | 2021-12-02 19:22:17 | [diff] [blame] | 92 | // Retrieves the Fresnel API Key. |
| 93 | std::string GetFresnelAPIKey(); |
David Dorwin | 97f1a03 | 2022-11-02 21:44:03 | [diff] [blame^] | 94 | #endif |
Hirthanan Subenderan | 658abca4 | 2021-12-02 19:22:17 | [diff] [blame] | 95 | |
David Dorwin | c5f373c | 2022-09-12 19:11:48 | [diff] [blame] | 96 | #if BUILDFLAG(SUPPORT_EXTERNAL_GOOGLE_API_KEY) |
jzw | 2d292082 | 2017-04-20 09:19:29 | [diff] [blame] | 97 | // Sets the API key. This should be called as early as possible before this |
Sharon Yang | 41d26ed | 2021-01-14 21:23:52 | [diff] [blame] | 98 | // API key is even accessed. It must be called before GetAPIKey. |
| 99 | // TODO(https://crbug.com/1166007): Enforce this is called before GetAPIKey. |
jzw | 2d292082 | 2017-04-20 09:19:29 | [diff] [blame] | 100 | void SetAPIKey(const std::string& api_key); |
| 101 | #endif |
| 102 | |
Jesse Doherty | bc473b4 | 2019-02-14 16:05:34 | [diff] [blame] | 103 | // Retrieves the key used to sign metrics (UMA/UKM) uploads. |
| 104 | std::string GetMetricsKey(); |
| 105 | |
[email protected] | e5e894ff | 2012-09-06 02:18:47 | [diff] [blame] | 106 | // Represents the different sets of client IDs and secrets in use. |
| 107 | enum OAuth2Client { |
Lei Zhang | 2c1a1e54 | 2022-01-21 19:41:43 | [diff] [blame] | 108 | CLIENT_MAIN, // Several different features use this. |
[email protected] | e5e894ff | 2012-09-06 02:18:47 | [diff] [blame] | 109 | CLIENT_REMOTING, |
[email protected] | d66d8174 | 2013-08-16 05:18:38 | [diff] [blame] | 110 | CLIENT_REMOTING_HOST, |
[email protected] | e5e894ff | 2012-09-06 02:18:47 | [diff] [blame] | 111 | |
Lei Zhang | 2c1a1e54 | 2022-01-21 19:41:43 | [diff] [blame] | 112 | CLIENT_NUM_ITEMS // Must be last item. |
[email protected] | e5e894ff | 2012-09-06 02:18:47 | [diff] [blame] | 113 | }; |
| 114 | |
Mihai Sardarescu | 806c3540 | 2018-06-20 17:01:06 | [diff] [blame] | 115 | // Returns true if no dummy OAuth2 client ID and secret are set. |
| 116 | bool HasOAuthClientConfigured(); |
| 117 | |
[email protected] | e5e894ff | 2012-09-06 02:18:47 | [diff] [blame] | 118 | // Retrieves the OAuth2 client ID for the specified client, or the |
| 119 | // empty string if not set. |
| 120 | // |
| 121 | // Note that the ID should be escaped for the context you use it in, |
| 122 | // e.g. URL-escaped if you use it in a URL. |
| 123 | std::string GetOAuth2ClientID(OAuth2Client client); |
| 124 | |
| 125 | // Retrieves the OAuth2 client secret for the specified client, or the |
| 126 | // empty string if not set. |
| 127 | // |
| 128 | // Note that the secret should be escaped for the context you use it |
| 129 | // in, e.g. URL-escaped if you use it in a URL. |
| 130 | std::string GetOAuth2ClientSecret(OAuth2Client client); |
| 131 | |
Xiaohan Wang | 83bdf14 | 2022-01-21 19:23:11 | [diff] [blame] | 132 | #if BUILDFLAG(IS_IOS) |
jzw | 2d292082 | 2017-04-20 09:19:29 | [diff] [blame] | 133 | // Sets the client id for the specified client. Should be called as early as |
| 134 | // possible before these ids are accessed. |
| 135 | void SetOAuth2ClientID(OAuth2Client client, const std::string& client_id); |
| 136 | |
| 137 | // Sets the client secret for the specified client. Should be called as early as |
| 138 | // possible before these secrets are accessed. |
| 139 | void SetOAuth2ClientSecret(OAuth2Client client, |
| 140 | const std::string& client_secret); |
| 141 | #endif |
kundaji | 033fb6ec | 2014-10-15 19:11:08 | [diff] [blame] | 142 | |
[email protected] | e4b54e3 | 2014-01-17 13:55:20 | [diff] [blame] | 143 | // Returns if the API key using in the current build is the one for official |
| 144 | // Google Chrome. |
| 145 | bool IsGoogleChromeAPIKeyUsed(); |
| 146 | |
[email protected] | e5e894ff | 2012-09-06 02:18:47 | [diff] [blame] | 147 | } // namespace google_apis |
| 148 | |
[email protected] | 6386cf5 | 2012-09-07 04:26:37 | [diff] [blame] | 149 | #endif // GOOGLE_APIS_GOOGLE_API_KEYS_H_ |