Commit 30674fb5 authored by toyoshim@chromium.org's avatar toyoshim@chromium.org

Translate: cleanup on how to pass api key to the script

Now that translate.js runs in an isolated world, it is safe to expose
API key as an isolated world global variable.
Of course, reusing this key outside Chrome is not permitted if it's possible:)

BUG=none
TEST=browser_tests --gtest_filter='Translate*.*'

Review URL: https://chromiumcodereview.appspot.com/22645015

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@217777 0039d316-1c4b-4281-b951-d872f2087c98
parent 3c6a7825
...@@ -9,7 +9,11 @@ ...@@ -9,7 +9,11 @@
var cr = {}; var cr = {};
cr.googleTranslate = (function(key) { /**
* An object to provide functions to interact with the Translate library.
* @type {object}
*/
cr.googleTranslate = (function() {
/** /**
* The Translate Element library's instance. * The Translate Element library's instance.
* @type {object} * @type {object}
...@@ -259,11 +263,14 @@ cr.googleTranslate = (function(key) { ...@@ -259,11 +263,14 @@ cr.googleTranslate = (function(key) {
loadedTime = performance.now(); loadedTime = performance.now();
try { try {
lib = google.translate.TranslateService({ lib = google.translate.TranslateService({
'key': key, // translateApiKey is predefined by translate_script.cc.
'key': translateApiKey,
'useSecureConnection': true 'useSecureConnection': true
}); });
translateApiKey = undefined;
} catch (err) { } catch (err) {
error = true; error = true;
translateApiKey = undefined;
return; return;
} }
// The TranslateService is not available immediately as it needs to start // The TranslateService is not available immediately as it needs to start
...@@ -271,4 +278,4 @@ cr.googleTranslate = (function(key) { ...@@ -271,4 +278,4 @@ cr.googleTranslate = (function(key) {
checkLibReady(); checkLibReady();
} }
}; };
})/* Calling code '(|key|);' will be appended by TranslateHelper in C++ here. */ })();
...@@ -8,7 +8,9 @@ ...@@ -8,7 +8,9 @@
#include "base/command_line.h" #include "base/command_line.h"
#include "base/logging.h" #include "base/logging.h"
#include "base/message_loop/message_loop.h" #include "base/message_loop/message_loop.h"
#include "base/strings/string_piece.h"
#include "base/strings/string_util.h" #include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "chrome/browser/translate/translate_url_fetcher.h" #include "chrome/browser/translate/translate_url_fetcher.h"
#include "chrome/browser/translate/translate_url_util.h" #include "chrome/browser/translate/translate_url_util.h"
#include "chrome/common/chrome_switches.h" #include "chrome/common/chrome_switches.h"
...@@ -96,15 +98,13 @@ void TranslateScript::OnScriptFetchComplete( ...@@ -96,15 +98,13 @@ void TranslateScript::OnScriptFetchComplete(
scoped_ptr<const TranslateURLFetcher> delete_ptr(fetcher_.release()); scoped_ptr<const TranslateURLFetcher> delete_ptr(fetcher_.release());
if (success) { if (success) {
DCHECK(data_.empty());
data_ = base::StringPrintf("var translateApiKey = '%s';\n",
google_apis::GetAPIKey().c_str());
base::StringPiece str = ResourceBundle::GetSharedInstance(). base::StringPiece str = ResourceBundle::GetSharedInstance().
GetRawDataResource(IDR_TRANSLATE_JS); GetRawDataResource(IDR_TRANSLATE_JS);
DCHECK(data_.empty()); str.AppendToString(&data_);
str.CopyToString(&data_); data_ += data;
std::string argument = "('";
std::string api_key = google_apis::GetAPIKey();
argument += net::EscapeQueryParamValue(api_key, true);
argument += "');\n";
data_ += argument + data;
// We'll expire the cached script after some time, to make sure long // We'll expire the cached script after some time, to make sure long
// running browsers still get fixes that might get pushed with newer // running browsers still get fixes that might get pushed with newer
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment