Reenable NullOpenerRedirectForksProcess, as the offending patch has been reverted
[chromium-blink-merge.git] / google_apis / google_api_keys.cc
blob830a135d95931a2a5d6983c6d780773048c1114c
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "google_apis/google_api_keys.h"
7 #include "base/command_line.h"
8 #include "base/environment.h"
9 #include "base/lazy_instance.h"
10 #include "base/logging.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/stringize_macros.h"
14 #if defined(GOOGLE_CHROME_BUILD) || defined(USE_OFFICIAL_GOOGLE_API_KEYS)
15 #include "google_apis/internal/google_chrome_api_keys.h"
16 #endif
18 // TODO(joi): Can we enable this warning without having it treated as
19 // an error? We don't want to fail builds, just warn, but all warnings
20 // from the preprocessor are currently treated as errors, at least in
21 // Linux builds.
22 #if 0
23 #if !defined(GOOGLE_API_KEY) && ( \
24 (!defined(GOOGLE_DEFAULT_CLIENT_ID) && \
25 !defined(GOOGLE_DEFAULT_CLIENT_SECRET)) \
26 || \
27 (!defined(GOOGLE_CLIENT_ID_MAIN) && \
28 !defined(GOOGLE_CLIENT_SECRET_MAIN)))
29 #warning You have not specified API keys; some features may not work.
30 #warning See www.chromium.org/developers/how-tos/api-keys for details.
31 #endif // (API keys unset)
32 #endif // 0
34 // Used to indicate an unset key/id/secret. This works better with
35 // various unit tests than leaving the token empty.
36 #define DUMMY_API_TOKEN "dummytoken"
38 #if !defined(GOOGLE_API_KEY)
39 // TODO(joi): This is temporary; switch to DUMMY_API_TOKEN once people
40 // have had some time to install API keys per
41 // http://chromium.org/developers/how-tos/api-keys
42 #define GOOGLE_API_KEY "AIzaSyBHDrl33hwRp4rMQY0ziRbj8K9LPA6vUCY"
43 #endif
45 #if !defined(GOOGLE_CLIENT_ID_MAIN)
46 #define GOOGLE_CLIENT_ID_MAIN DUMMY_API_TOKEN
47 #endif
49 #if !defined(GOOGLE_CLIENT_SECRET_MAIN)
50 #define GOOGLE_CLIENT_SECRET_MAIN DUMMY_API_TOKEN
51 #endif
53 #if !defined(GOOGLE_CLIENT_ID_CLOUD_PRINT)
54 #define GOOGLE_CLIENT_ID_CLOUD_PRINT DUMMY_API_TOKEN
55 #endif
57 #if !defined(GOOGLE_CLIENT_SECRET_CLOUD_PRINT)
58 #define GOOGLE_CLIENT_SECRET_CLOUD_PRINT DUMMY_API_TOKEN
59 #endif
61 #if !defined(GOOGLE_CLIENT_ID_REMOTING)
62 #define GOOGLE_CLIENT_ID_REMOTING DUMMY_API_TOKEN
63 #endif
65 #if !defined(GOOGLE_CLIENT_SECRET_REMOTING)
66 #define GOOGLE_CLIENT_SECRET_REMOTING DUMMY_API_TOKEN
67 #endif
69 // These are used as shortcuts for developers and users providing
70 // OAuth credentials via preprocessor defines or environment
71 // variables. If set, they will be used to replace any of the client
72 // IDs and secrets above that have not been set (and only those; they
73 // will not override already-set values).
75 // TODO(joi): This is temporary; make both blank once people have had
76 // some time to install API keys per
77 // http://chromium.org/developers/how-tos/api-keys
78 #if !defined(GOOGLE_DEFAULT_CLIENT_ID)
79 #define GOOGLE_DEFAULT_CLIENT_ID "609716072145.apps.googleusercontent.com"
80 #endif
81 #if !defined(GOOGLE_DEFAULT_CLIENT_SECRET)
82 #define GOOGLE_DEFAULT_CLIENT_SECRET "WF4uG3gJzEH0KLpS7OuFBDux"
83 #endif
85 namespace switches {
87 // Specifies custom OAuth2 client id for testing purposes.
88 const char kOAuth2ClientID[] = "oauth2-client-id";
90 // Specifies custom OAuth2 client secret for testing purposes.
91 const char kOAuth2ClientSecret[] = "oauth2-client-secret";
93 } // namespace switches
95 namespace google_apis {
97 // This is used as a lazy instance to determine keys once and cache them.
98 class APIKeyCache {
99 public:
100 APIKeyCache() {
101 scoped_ptr<base::Environment> environment(base::Environment::Create());
102 CommandLine* command_line = CommandLine::ForCurrentProcess();
104 api_key_ = CalculateKeyValue(GOOGLE_API_KEY,
105 STRINGIZE_NO_EXPANSION(GOOGLE_API_KEY),
106 NULL, "",
107 environment.get(),
108 command_line);
110 std::string default_client_id = CalculateKeyValue(
111 GOOGLE_DEFAULT_CLIENT_ID,
112 STRINGIZE_NO_EXPANSION(GOOGLE_DEFAULT_CLIENT_ID),
113 NULL, "",
114 environment.get(),
115 command_line);
116 std::string default_client_secret = CalculateKeyValue(
117 GOOGLE_DEFAULT_CLIENT_SECRET,
118 STRINGIZE_NO_EXPANSION(GOOGLE_DEFAULT_CLIENT_SECRET),
119 NULL, "",
120 environment.get(),
121 command_line);
123 // We currently only allow overriding the baked-in values for the
124 // default OAuth2 client ID and secret using a command-line
125 // argument, since that is useful to enable testing against
126 // staging servers, and since that was what was possible and
127 // likely practiced by the QA team before this implementation was
128 // written.
129 client_ids_[CLIENT_MAIN] = CalculateKeyValue(
130 GOOGLE_CLIENT_ID_MAIN,
131 STRINGIZE_NO_EXPANSION(GOOGLE_CLIENT_ID_MAIN),
132 switches::kOAuth2ClientID,
133 default_client_id,
134 environment.get(),
135 command_line);
136 client_secrets_[CLIENT_MAIN] = CalculateKeyValue(
137 GOOGLE_CLIENT_SECRET_MAIN,
138 STRINGIZE_NO_EXPANSION(GOOGLE_CLIENT_SECRET_MAIN),
139 switches::kOAuth2ClientSecret,
140 default_client_secret,
141 environment.get(),
142 command_line);
144 client_ids_[CLIENT_CLOUD_PRINT] = CalculateKeyValue(
145 GOOGLE_CLIENT_ID_CLOUD_PRINT,
146 STRINGIZE_NO_EXPANSION(GOOGLE_CLIENT_ID_CLOUD_PRINT),
147 NULL,
148 default_client_id,
149 environment.get(),
150 command_line);
151 client_secrets_[CLIENT_CLOUD_PRINT] = CalculateKeyValue(
152 GOOGLE_CLIENT_SECRET_CLOUD_PRINT,
153 STRINGIZE_NO_EXPANSION(GOOGLE_CLIENT_SECRET_CLOUD_PRINT),
154 NULL,
155 default_client_secret,
156 environment.get(),
157 command_line);
159 client_ids_[CLIENT_REMOTING] = CalculateKeyValue(
160 GOOGLE_CLIENT_ID_REMOTING,
161 STRINGIZE_NO_EXPANSION(GOOGLE_CLIENT_ID_REMOTING),
162 NULL,
163 default_client_id,
164 environment.get(),
165 command_line);
166 client_secrets_[CLIENT_REMOTING] = CalculateKeyValue(
167 GOOGLE_CLIENT_SECRET_REMOTING,
168 STRINGIZE_NO_EXPANSION(GOOGLE_CLIENT_SECRET_REMOTING),
169 NULL,
170 default_client_secret,
171 environment.get(),
172 command_line);
175 std::string api_key() const { return api_key_; }
177 std::string GetClientID(OAuth2Client client) const {
178 DCHECK_LT(client, CLIENT_NUM_ITEMS);
179 return client_ids_[client];
182 std::string GetClientSecret(OAuth2Client client) const {
183 DCHECK_LT(client, CLIENT_NUM_ITEMS);
184 return client_secrets_[client];
187 private:
188 // Gets a value for a key. In priority order, this will be the value
189 // provided via a command-line switch, the value provided via an
190 // environment variable, or finally a value baked into the build.
191 // |command_line_switch| may be NULL.
192 static std::string CalculateKeyValue(const char* baked_in_value,
193 const char* environment_variable_name,
194 const char* command_line_switch,
195 const std::string& default_if_unset,
196 base::Environment* environment,
197 CommandLine* command_line) {
198 std::string key_value = baked_in_value;
199 std::string temp;
200 if (environment->GetVar(environment_variable_name, &temp)) {
201 key_value = temp;
202 LOG(INFO) << "Overriding API key " << environment_variable_name
203 << " with value " << key_value << " from environment variable.";
206 if (command_line_switch && command_line->HasSwitch(command_line_switch)) {
207 key_value = command_line->GetSwitchValueASCII(command_line_switch);
208 LOG(INFO) << "Overriding API key " << environment_variable_name
209 << " with value " << key_value << " from command-line switch.";
212 if (key_value == DUMMY_API_TOKEN) {
213 #if defined(GOOGLE_CHROME_BUILD)
214 // No key should be unset in an official build except the
215 // GOOGLE_DEFAULT_* keys. The default keys don't trigger this
216 // check as their "unset" value is not DUMMY_API_TOKEN.
217 CHECK(false);
218 #endif
219 if (default_if_unset.size() > 0) {
220 LOG(INFO) << "Using default value \"" << default_if_unset
221 << "\" for API key " << environment_variable_name;
222 key_value = default_if_unset;
226 // This should remain a debug-only log.
227 DVLOG(1) << "API key " << environment_variable_name << "=" << key_value;
229 return key_value;
232 std::string api_key_;
233 std::string client_ids_[CLIENT_NUM_ITEMS];
234 std::string client_secrets_[CLIENT_NUM_ITEMS];
237 static base::LazyInstance<APIKeyCache> g_api_key_cache =
238 LAZY_INSTANCE_INITIALIZER;
240 std::string GetAPIKey() {
241 return g_api_key_cache.Get().api_key();
244 std::string GetOAuth2ClientID(OAuth2Client client) {
245 return g_api_key_cache.Get().GetClientID(client);
248 std::string GetOAuth2ClientSecret(OAuth2Client client) {
249 return g_api_key_cache.Get().GetClientSecret(client);
252 } // namespace google_apis