Browse Source

Merge pull request #7276 from electron/google-api-key-env2

allow GOOGLE_API_KEY to be set
Cheng Zhao 8 years ago
parent
commit
805ce0dee9

+ 8 - 9
atom/browser/atom_access_token_store.cc

@@ -4,10 +4,12 @@
 
 #include "atom/browser/atom_access_token_store.h"
 
+#include <string>
 #include <utility>
 
 #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 +19,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<TokenLoadingJob> {
@@ -49,15 +44,18 @@ class TokenLoadingJob : public base::RefCountedThreadSafe<TokenLoadingJob> {
     DCHECK_CURRENTLY_ON(BrowserThread::UI);
     auto browser_context = AtomBrowserContext::From("", false);
     request_context_getter_ = browser_context->GetRequestContext();
+    std::unique_ptr<base::Environment> 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;
     std::pair<GURL, base::string16> 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_);
@@ -65,6 +63,7 @@ class TokenLoadingJob : public base::RefCountedThreadSafe<TokenLoadingJob> {
 
   content::AccessTokenStore::LoadAccessTokensCallback callback_;
   net::URLRequestContextGetter* request_context_getter_;
+  std::string api_key_;
 };
 
 }  // namespace

+ 5 - 0
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

+ 29 - 0
docs/api/environment-variables.md

@@ -19,6 +19,35 @@ 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'
+```
+
+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
+
+## 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.