| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "google_apis/google_api_keys.h" | 5 #include "google_apis/google_api_keys.h" |
| 6 | 6 |
| 7 // If you add more includes to this list, you also need to add them to | 7 // If you add more includes to this list, you also need to add them to |
| 8 // google_api_keys_unittest.cc. | 8 // google_api_keys_unittest.cc. |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/environment.h" | 10 #include "base/environment.h" |
| 11 #include "base/lazy_instance.h" | 11 #include "base/lazy_instance.h" |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/strings/stringize_macros.h" | 14 #include "base/strings/stringize_macros.h" |
| 15 #include "google_apis/gaia/gaia_switches.h" | 15 #include "google_apis/gaia/gaia_switches.h" |
| 16 | 16 |
| 17 #if defined(GOOGLE_CHROME_BUILD) || defined(USE_OFFICIAL_GOOGLE_API_KEYS) | 17 #if defined(GOOGLE_CHROME_BUILD) || defined(USE_OFFICIAL_GOOGLE_API_KEYS) |
| 18 #include "google_apis/internal/google_chrome_api_keys.h" | 18 #include "google_apis/internal/google_chrome_api_keys.h" |
| 19 #endif | 19 #endif |
| 20 | 20 |
| 21 // Used to indicate an unset key/id/secret. This works better with | 21 // Used to indicate an unset key/id/secret. This works better with |
| 22 // various unit tests than leaving the token empty. | 22 // various unit tests than leaving the token empty. |
| 23 #define DUMMY_API_TOKEN "dummytoken" | 23 #define DUMMY_API_TOKEN "dummytoken" |
| 24 | 24 |
| 25 #if !defined(GOOGLE_API_KEY) | 25 #if !defined(GOOGLE_API_KEY) |
| 26 #define GOOGLE_API_KEY DUMMY_API_TOKEN | 26 #define GOOGLE_API_KEY DUMMY_API_TOKEN |
| 27 #endif | 27 #endif |
| 28 | 28 |
| 29 #if !defined(GOOGLE_API_KEY_SAFESITES) |
| 30 #define GOOGLE_API_KEY_SAFESITES DUMMY_API_TOKEN |
| 31 #endif |
| 32 |
| 29 #if !defined(GOOGLE_CLIENT_ID_MAIN) | 33 #if !defined(GOOGLE_CLIENT_ID_MAIN) |
| 30 #define GOOGLE_CLIENT_ID_MAIN DUMMY_API_TOKEN | 34 #define GOOGLE_CLIENT_ID_MAIN DUMMY_API_TOKEN |
| 31 #endif | 35 #endif |
| 32 | 36 |
| 33 #if !defined(GOOGLE_CLIENT_SECRET_MAIN) | 37 #if !defined(GOOGLE_CLIENT_SECRET_MAIN) |
| 34 #define GOOGLE_CLIENT_SECRET_MAIN DUMMY_API_TOKEN | 38 #define GOOGLE_CLIENT_SECRET_MAIN DUMMY_API_TOKEN |
| 35 #endif | 39 #endif |
| 36 | 40 |
| 37 #if !defined(GOOGLE_CLIENT_ID_CLOUD_PRINT) | 41 #if !defined(GOOGLE_CLIENT_ID_CLOUD_PRINT) |
| 38 #define GOOGLE_CLIENT_ID_CLOUD_PRINT DUMMY_API_TOKEN | 42 #define GOOGLE_CLIENT_ID_CLOUD_PRINT DUMMY_API_TOKEN |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 79 scoped_ptr<base::Environment> environment(base::Environment::Create()); | 83 scoped_ptr<base::Environment> environment(base::Environment::Create()); |
| 80 CommandLine* command_line = CommandLine::ForCurrentProcess(); | 84 CommandLine* command_line = CommandLine::ForCurrentProcess(); |
| 81 | 85 |
| 82 api_key_ = CalculateKeyValue(GOOGLE_API_KEY, | 86 api_key_ = CalculateKeyValue(GOOGLE_API_KEY, |
| 83 STRINGIZE_NO_EXPANSION(GOOGLE_API_KEY), | 87 STRINGIZE_NO_EXPANSION(GOOGLE_API_KEY), |
| 84 NULL, | 88 NULL, |
| 85 std::string(), | 89 std::string(), |
| 86 environment.get(), | 90 environment.get(), |
| 87 command_line); | 91 command_line); |
| 88 | 92 |
| 93 api_key_safesites_ = |
| 94 CalculateKeyValue(GOOGLE_API_KEY_SAFESITES, |
| 95 STRINGIZE_NO_EXPANSION(GOOGLE_API_KEY_SAFESITES), |
| 96 NULL, |
| 97 std::string(), |
| 98 environment.get(), |
| 99 command_line); |
| 100 |
| 89 std::string default_client_id = | 101 std::string default_client_id = |
| 90 CalculateKeyValue(GOOGLE_DEFAULT_CLIENT_ID, | 102 CalculateKeyValue(GOOGLE_DEFAULT_CLIENT_ID, |
| 91 STRINGIZE_NO_EXPANSION(GOOGLE_DEFAULT_CLIENT_ID), | 103 STRINGIZE_NO_EXPANSION(GOOGLE_DEFAULT_CLIENT_ID), |
| 92 NULL, | 104 NULL, |
| 93 std::string(), | 105 std::string(), |
| 94 environment.get(), | 106 environment.get(), |
| 95 command_line); | 107 command_line); |
| 96 std::string default_client_secret = | 108 std::string default_client_secret = |
| 97 CalculateKeyValue(GOOGLE_DEFAULT_CLIENT_SECRET, | 109 CalculateKeyValue(GOOGLE_DEFAULT_CLIENT_SECRET, |
| 98 STRINGIZE_NO_EXPANSION(GOOGLE_DEFAULT_CLIENT_SECRET), | 110 STRINGIZE_NO_EXPANSION(GOOGLE_DEFAULT_CLIENT_SECRET), |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 162 client_secrets_[CLIENT_REMOTING_HOST] = CalculateKeyValue( | 174 client_secrets_[CLIENT_REMOTING_HOST] = CalculateKeyValue( |
| 163 GOOGLE_CLIENT_SECRET_REMOTING_HOST, | 175 GOOGLE_CLIENT_SECRET_REMOTING_HOST, |
| 164 STRINGIZE_NO_EXPANSION(GOOGLE_CLIENT_SECRET_REMOTING_HOST), | 176 STRINGIZE_NO_EXPANSION(GOOGLE_CLIENT_SECRET_REMOTING_HOST), |
| 165 NULL, | 177 NULL, |
| 166 default_client_secret, | 178 default_client_secret, |
| 167 environment.get(), | 179 environment.get(), |
| 168 command_line); | 180 command_line); |
| 169 } | 181 } |
| 170 | 182 |
| 171 std::string api_key() const { return api_key_; } | 183 std::string api_key() const { return api_key_; } |
| 184 std::string api_key_safesites() const { return api_key_safesites_; } |
| 172 | 185 |
| 173 std::string GetClientID(OAuth2Client client) const { | 186 std::string GetClientID(OAuth2Client client) const { |
| 174 DCHECK_LT(client, CLIENT_NUM_ITEMS); | 187 DCHECK_LT(client, CLIENT_NUM_ITEMS); |
| 175 return client_ids_[client]; | 188 return client_ids_[client]; |
| 176 } | 189 } |
| 177 | 190 |
| 178 std::string GetClientSecret(OAuth2Client client) const { | 191 std::string GetClientSecret(OAuth2Client client) const { |
| 179 DCHECK_LT(client, CLIENT_NUM_ITEMS); | 192 DCHECK_LT(client, CLIENT_NUM_ITEMS); |
| 180 return client_secrets_[client]; | 193 return client_secrets_[client]; |
| 181 } | 194 } |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 227 } | 240 } |
| 228 } | 241 } |
| 229 | 242 |
| 230 // This should remain a debug-only log. | 243 // This should remain a debug-only log. |
| 231 DVLOG(1) << "API key " << environment_variable_name << "=" << key_value; | 244 DVLOG(1) << "API key " << environment_variable_name << "=" << key_value; |
| 232 | 245 |
| 233 return key_value; | 246 return key_value; |
| 234 } | 247 } |
| 235 | 248 |
| 236 std::string api_key_; | 249 std::string api_key_; |
| 250 std::string api_key_safesites_; |
| 237 std::string client_ids_[CLIENT_NUM_ITEMS]; | 251 std::string client_ids_[CLIENT_NUM_ITEMS]; |
| 238 std::string client_secrets_[CLIENT_NUM_ITEMS]; | 252 std::string client_secrets_[CLIENT_NUM_ITEMS]; |
| 239 }; | 253 }; |
| 240 | 254 |
| 241 static base::LazyInstance<APIKeyCache> g_api_key_cache = | 255 static base::LazyInstance<APIKeyCache> g_api_key_cache = |
| 242 LAZY_INSTANCE_INITIALIZER; | 256 LAZY_INSTANCE_INITIALIZER; |
| 243 | 257 |
| 244 bool HasKeysConfigured() { | 258 bool HasKeysConfigured() { |
| 245 if (GetAPIKey() == DUMMY_API_TOKEN) | 259 if (GetAPIKey() == DUMMY_API_TOKEN) |
| 246 return false; | 260 return false; |
| 247 | 261 |
| 248 for (size_t client_id = 0; client_id < CLIENT_NUM_ITEMS; ++client_id) { | 262 for (size_t client_id = 0; client_id < CLIENT_NUM_ITEMS; ++client_id) { |
| 249 OAuth2Client client = static_cast<OAuth2Client>(client_id); | 263 OAuth2Client client = static_cast<OAuth2Client>(client_id); |
| 250 if (GetOAuth2ClientID(client) == DUMMY_API_TOKEN || | 264 if (GetOAuth2ClientID(client) == DUMMY_API_TOKEN || |
| 251 GetOAuth2ClientSecret(client) == DUMMY_API_TOKEN) { | 265 GetOAuth2ClientSecret(client) == DUMMY_API_TOKEN) { |
| 252 return false; | 266 return false; |
| 253 } | 267 } |
| 254 } | 268 } |
| 255 | 269 |
| 256 return true; | 270 return true; |
| 257 } | 271 } |
| 258 | 272 |
| 259 std::string GetAPIKey() { | 273 std::string GetAPIKey() { |
| 260 return g_api_key_cache.Get().api_key(); | 274 return g_api_key_cache.Get().api_key(); |
| 261 } | 275 } |
| 262 | 276 |
| 277 std::string GetSafeSitesAPIKey() { |
| 278 return g_api_key_cache.Get().api_key_safesites(); |
| 279 } |
| 280 |
| 263 std::string GetOAuth2ClientID(OAuth2Client client) { | 281 std::string GetOAuth2ClientID(OAuth2Client client) { |
| 264 return g_api_key_cache.Get().GetClientID(client); | 282 return g_api_key_cache.Get().GetClientID(client); |
| 265 } | 283 } |
| 266 | 284 |
| 267 std::string GetOAuth2ClientSecret(OAuth2Client client) { | 285 std::string GetOAuth2ClientSecret(OAuth2Client client) { |
| 268 return g_api_key_cache.Get().GetClientSecret(client); | 286 return g_api_key_cache.Get().GetClientSecret(client); |
| 269 } | 287 } |
| 270 | 288 |
| 271 std::string GetSpdyProxyAuthValue() { | 289 std::string GetSpdyProxyAuthValue() { |
| 272 return g_api_key_cache.Get().GetSpdyProxyAuthValue(); | 290 return g_api_key_cache.Get().GetSpdyProxyAuthValue(); |
| 273 } | 291 } |
| 274 | 292 |
| 275 bool IsGoogleChromeAPIKeyUsed() { | 293 bool IsGoogleChromeAPIKeyUsed() { |
| 276 #if defined(GOOGLE_CHROME_BUILD) || defined(USE_OFFICIAL_GOOGLE_API_KEYS) | 294 #if defined(GOOGLE_CHROME_BUILD) || defined(USE_OFFICIAL_GOOGLE_API_KEYS) |
| 277 return true; | 295 return true; |
| 278 #else | 296 #else |
| 279 return false; | 297 return false; |
| 280 #endif | 298 #endif |
| 281 } | 299 } |
| 282 | 300 |
| 283 } // namespace google_apis | 301 } // namespace google_apis |
| OLD | NEW |