From fa2aaa48b1fb35f09a1c30035d0f7ba742e00956 Mon Sep 17 00:00:00 2001 From: Zeke Sikelianos Date: Tue, 20 Sep 2016 12:01:59 -0700 Subject: [PATCH 1/5] allow GOOGLE_API_KEY to be set in environment --- atom/browser/atom_access_token_store.cc | 13 +++++-------- atom/browser/atom_access_token_store.h | 1 + atom/common/google_api_key.h | 5 +++++ 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/atom/browser/atom_access_token_store.cc b/atom/browser/atom_access_token_store.cc index 7c04113ff502..0ad637d7afa3 100644 --- a/atom/browser/atom_access_token_store.cc +++ b/atom/browser/atom_access_token_store.cc @@ -8,6 +8,7 @@ #include "atom/browser/atom_browser_context.h" #include "atom/common/google_api_key.h" +#include "base/environment.h" #include "content/public/browser/browser_thread.h" #include "content/public/browser/geolocation_provider.h" @@ -17,13 +18,6 @@ namespace atom { namespace { -// Notice that we just combined the api key with the url together here, because -// if we use the standard {url: key} format Chromium would override our key with -// the predefined one in common.gypi of libchromiumcontent, which is empty. -const char* kGeolocationProviderURL = - "https://www.googleapis.com/geolocation/v1/geolocate?key=" - GOOGLEAPIS_API_KEY; - // Loads access tokens and other necessary data on the UI thread, and // calls back to the originator on the originating thread. class TokenLoadingJob : public base::RefCountedThreadSafe { @@ -57,7 +51,7 @@ class TokenLoadingJob : public base::RefCountedThreadSafe { // of std::map on Linux, this can work around it. content::AccessTokenStore::AccessTokenMap access_token_map; std::pair token_pair; - token_pair.first = GURL(kGeolocationProviderURL); + token_pair.first = GURL(GOOGLEAPIS_ENDPOINT + api_key_); access_token_map.insert(token_pair); callback_.Run(access_token_map, request_context_getter_); @@ -71,6 +65,9 @@ class TokenLoadingJob : public base::RefCountedThreadSafe { AtomAccessTokenStore::AtomAccessTokenStore() { content::GeolocationProvider::GetInstance()->UserDidOptIntoLocationServices(); + std::unique_ptr env(base::Environment::Create()); + if (!env->GetVar("GOOGLE_API_KEY", &api_key_)) + api_key_ = GOOGLEAPIS_API_KEY; } AtomAccessTokenStore::~AtomAccessTokenStore() { diff --git a/atom/browser/atom_access_token_store.h b/atom/browser/atom_access_token_store.h index d70d44a0cd3e..dccbe3f5c4d0 100644 --- a/atom/browser/atom_access_token_store.h +++ b/atom/browser/atom_access_token_store.h @@ -21,6 +21,7 @@ class AtomAccessTokenStore : public content::AccessTokenStore { const base::string16& access_token) override; private: + std::string api_key_; DISALLOW_COPY_AND_ASSIGN(AtomAccessTokenStore); }; diff --git a/atom/common/google_api_key.h b/atom/common/google_api_key.h index dc38272ec611..e7a3209906d5 100644 --- a/atom/common/google_api_key.h +++ b/atom/common/google_api_key.h @@ -5,6 +5,11 @@ #ifndef ATOM_COMMON_GOOGLE_API_KEY_H_ #define ATOM_COMMON_GOOGLE_API_KEY_H_ +#ifndef GOOGLEAPIS_ENDPOINT +#define GOOGLEAPIS_ENDPOINT \ + "https://www.googleapis.com/geolocation/v1/geolocate?key=" +#endif + #ifndef GOOGLEAPIS_API_KEY #define GOOGLEAPIS_API_KEY "AIzaSyAQfxPJiounkhOjODEO5ZieffeBv6yft2Q" #endif From ea1927f428dafb951e18f9c71454a3ea3274c63a Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Tue, 20 Sep 2016 13:24:45 -0700 Subject: [PATCH 2/5] Store api_key_ on TokenLoadingJob --- atom/browser/atom_access_token_store.cc | 10 ++++++---- atom/browser/atom_access_token_store.h | 1 - 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/atom/browser/atom_access_token_store.cc b/atom/browser/atom_access_token_store.cc index 0ad637d7afa3..b4e19086d4c2 100644 --- a/atom/browser/atom_access_token_store.cc +++ b/atom/browser/atom_access_token_store.cc @@ -4,6 +4,7 @@ #include "atom/browser/atom_access_token_store.h" +#include #include #include "atom/browser/atom_browser_context.h" @@ -43,10 +44,13 @@ class TokenLoadingJob : public base::RefCountedThreadSafe { DCHECK_CURRENTLY_ON(BrowserThread::UI); auto browser_context = AtomBrowserContext::From("", false); request_context_getter_ = browser_context->GetRequestContext(); + std::unique_ptr env(base::Environment::Create()); + if (!env->GetVar("GOOGLE_API_KEY", &api_key_)) + api_key_ = GOOGLEAPIS_API_KEY; } void RespondOnOriginatingThread() { - // Equivelent to access_token_map[kGeolocationProviderURL]. + // Equivalent to access_token_map[kGeolocationProviderURL]. // Somehow base::string16 is causing compilation errors when used in a pair // of std::map on Linux, this can work around it. content::AccessTokenStore::AccessTokenMap access_token_map; @@ -59,15 +63,13 @@ class TokenLoadingJob : public base::RefCountedThreadSafe { content::AccessTokenStore::LoadAccessTokensCallback callback_; net::URLRequestContextGetter* request_context_getter_; + std::string api_key_; }; } // namespace AtomAccessTokenStore::AtomAccessTokenStore() { content::GeolocationProvider::GetInstance()->UserDidOptIntoLocationServices(); - std::unique_ptr env(base::Environment::Create()); - if (!env->GetVar("GOOGLE_API_KEY", &api_key_)) - api_key_ = GOOGLEAPIS_API_KEY; } AtomAccessTokenStore::~AtomAccessTokenStore() { diff --git a/atom/browser/atom_access_token_store.h b/atom/browser/atom_access_token_store.h index dccbe3f5c4d0..d70d44a0cd3e 100644 --- a/atom/browser/atom_access_token_store.h +++ b/atom/browser/atom_access_token_store.h @@ -21,7 +21,6 @@ class AtomAccessTokenStore : public content::AccessTokenStore { const base::string16& access_token) override; private: - std::string api_key_; DISALLOW_COPY_AND_ASSIGN(AtomAccessTokenStore); }; From 7fcba6ba0f18de252ee633ba232e60a82d0dfbe7 Mon Sep 17 00:00:00 2001 From: Zeke Sikelianos Date: Tue, 20 Sep 2016 13:58:39 -0700 Subject: [PATCH 3/5] document the GOOGLE_API_KEY env var --- docs/api/environment-variables.md | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/docs/api/environment-variables.md b/docs/api/environment-variables.md index a4df27504b57..5b11c2d78c91 100644 --- a/docs/api/environment-variables.md +++ b/docs/api/environment-variables.md @@ -19,6 +19,32 @@ Windows console example: > electron ``` +## Production Variables + +The following environment variables are intended primarily for use at runtime +in packaged Electron applications. + +### `GOOGLE_API_KEY` + +Electron includes a hardcoded API key for making requests to Google's geocoding +webservice. Because this API key is included in every version of Electron, it +often exceeds its usage quota. To work around this, you can supply your own +Google API key in the environment. Place the following code in your main process +file, before opening any browser windows that will make geocoding requests: + +```javascript +process.env.GOOGLE_API_KEY='YOUR_KEY_HERE' +``` + +By default, a newly generated Google API key may not be allowed to make +geocoding requests. To enable geocoding requests, visit this page: +https://console.developers.google.com/apis/api/geolocation/overview + +## Development Variables + +The following environment variables are intended primarily for development and +debugging purposes. + ### `ELECTRON_RUN_AS_NODE` Starts the process as a normal Node.js process. From d7934e7525d2a6344ed16c9506c6a0a52c58a794 Mon Sep 17 00:00:00 2001 From: Zeke Sikelianos Date: Tue, 20 Sep 2016 14:13:30 -0700 Subject: [PATCH 4/5] add link to chromium API keys doc --- docs/api/environment-variables.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/docs/api/environment-variables.md b/docs/api/environment-variables.md index 5b11c2d78c91..9db7ce2aafdb 100644 --- a/docs/api/environment-variables.md +++ b/docs/api/environment-variables.md @@ -36,6 +36,9 @@ file, before opening any browser windows that will make geocoding requests: process.env.GOOGLE_API_KEY='YOUR_KEY_HERE' ``` +For instructions on how to acquire a Google API key, see +https://www.chromium.org/developers/how-tos/api-keys + By default, a newly generated Google API key may not be allowed to make geocoding requests. To enable geocoding requests, visit this page: https://console.developers.google.com/apis/api/geolocation/overview From f4a8fb9eb475d7665370f38d0c949a40ee115692 Mon Sep 17 00:00:00 2001 From: Zeke Sikelianos Date: Tue, 20 Sep 2016 14:55:45 -0700 Subject: [PATCH 5/5] :art: standard --- docs/api/environment-variables.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/api/environment-variables.md b/docs/api/environment-variables.md index 9db7ce2aafdb..aca4f31cbd22 100644 --- a/docs/api/environment-variables.md +++ b/docs/api/environment-variables.md @@ -33,7 +33,7 @@ Google API key in the environment. Place the following code in your main process file, before opening any browser windows that will make geocoding requests: ```javascript -process.env.GOOGLE_API_KEY='YOUR_KEY_HERE' +process.env.GOOGLE_API_KEY = 'YOUR_KEY_HERE' ``` For instructions on how to acquire a Google API key, see