Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "components/translate/core/browser/translate_script.h" | 5 #include "components/translate/core/browser/translate_script.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
| 9 #include "base/location.h" | 9 #include "base/location.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/profiler/scoped_tracker.h" | 11 #include "base/profiler/scoped_tracker.h" |
| 12 #include "base/single_thread_task_runner.h" | 12 #include "base/single_thread_task_runner.h" |
| 13 #include "base/strings/string_piece.h" | 13 #include "base/strings/string_piece.h" |
| 14 #include "base/strings/string_util.h" | 14 #include "base/strings/string_util.h" |
| 15 #include "base/strings/stringprintf.h" | 15 #include "base/strings/stringprintf.h" |
| 16 #include "base/threading/thread_task_runner_handle.h" | 16 #include "base/threading/thread_task_runner_handle.h" |
| 17 #include "build/build_config.h" | 17 #include "build/build_config.h" |
| 18 #include "components/translate/core/browser/translate_url_fetcher.h" | 18 #include "components/translate/core/browser/translate_url_fetcher.h" |
| 19 #include "components/translate/core/browser/translate_url_util.h" | 19 #include "components/translate/core/browser/translate_url_util.h" |
| 20 #include "components/translate/core/common/translate_switches.h" | 20 #include "components/translate/core/common/translate_switches.h" |
| 21 #include "components/translate/core/common/translate_util.h" | 21 #include "components/translate/core/common/translate_util.h" |
| 22 #include "components/variations/variations_associated_data.h" | |
| 22 #include "google_apis/google_api_keys.h" | 23 #include "google_apis/google_api_keys.h" |
| 23 #include "grit/components_resources.h" | 24 #include "grit/components_resources.h" |
| 24 #include "net/base/escape.h" | 25 #include "net/base/escape.h" |
| 25 #include "net/base/url_util.h" | 26 #include "net/base/url_util.h" |
| 26 #include "ui/base/resource/resource_bundle.h" | 27 #include "ui/base/resource/resource_bundle.h" |
| 27 | 28 |
| 28 namespace translate { | 29 namespace translate { |
| 29 | 30 |
| 30 namespace { | 31 namespace { |
| 31 | 32 |
| 32 const int kExpirationDelayDays = 1; | 33 const int kExpirationDelayDays = 1; |
| 33 | 34 |
| 34 } // namespace | 35 } // namespace |
| 35 | 36 |
| 36 const char TranslateScript::kScriptURL[] = | 37 const char TranslateScript::kScriptURL[] = |
| 37 "https://translate.googleapis.com/translate_a/element.js"; | 38 "https://translate.googleapis.com/translate_a/element.js"; |
| 38 const char TranslateScript::kRequestHeader[] = | 39 const char TranslateScript::kRequestHeader[] = |
| 39 "Google-Translate-Element-Mode: library"; | 40 "Google-Translate-Element-Mode: library"; |
| 40 const char TranslateScript::kAlwaysUseSslQueryName[] = "aus"; | 41 const char TranslateScript::kAlwaysUseSslQueryName[] = "aus"; |
| 41 const char TranslateScript::kAlwaysUseSslQueryValue[] = "true"; | 42 const char TranslateScript::kAlwaysUseSslQueryValue[] = "true"; |
| 42 const char TranslateScript::kCallbackQueryName[] = "cb"; | 43 const char TranslateScript::kCallbackQueryName[] = "cb"; |
| 43 const char TranslateScript::kCallbackQueryValue[] = | 44 const char TranslateScript::kCallbackQueryValue[] = |
| 44 "cr.googleTranslate.onTranslateElementLoad"; | 45 "cr.googleTranslate.onTranslateElementLoad"; |
| 45 const char TranslateScript::kCssLoaderCallbackQueryName[] = "clc"; | 46 const char TranslateScript::kCssLoaderCallbackQueryName[] = "clc"; |
| 46 const char TranslateScript::kCssLoaderCallbackQueryValue[] = | 47 const char TranslateScript::kCssLoaderCallbackQueryValue[] = |
| 47 "cr.googleTranslate.onLoadCSS"; | 48 "cr.googleTranslate.onLoadCSS"; |
| 48 const char TranslateScript::kJavascriptLoaderCallbackQueryName[] = "jlc"; | 49 const char TranslateScript::kJavascriptLoaderCallbackQueryName[] = "jlc"; |
| 49 const char TranslateScript::kJavascriptLoaderCallbackQueryValue[] = | 50 const char TranslateScript::kJavascriptLoaderCallbackQueryValue[] = |
| 50 "cr.googleTranslate.onLoadJavascript"; | 51 "cr.googleTranslate.onLoadJavascript"; |
| 52 const char kTranslateServerStudy[] = "TranslateServerStudy"; | |
| 53 const char kServerParams[] = "server_params"; | |
| 51 | 54 |
| 52 TranslateScript::TranslateScript() | 55 TranslateScript::TranslateScript() |
| 53 : expiration_delay_(base::TimeDelta::FromDays(kExpirationDelayDays)), | 56 : expiration_delay_(base::TimeDelta::FromDays(kExpirationDelayDays)), |
| 54 weak_method_factory_(this) { | 57 weak_method_factory_(this) { |
| 55 } | 58 } |
| 56 | 59 |
| 57 TranslateScript::~TranslateScript() { | 60 TranslateScript::~TranslateScript() { |
| 58 } | 61 } |
| 59 | 62 |
| 60 void TranslateScript::Request(const RequestCallback& callback) { | 63 void TranslateScript::Request(const RequestCallback& callback) { |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 125 DCHECK_EQ(kFetcherId, id); | 128 DCHECK_EQ(kFetcherId, id); |
| 126 | 129 |
| 127 std::unique_ptr<const TranslateURLFetcher> delete_ptr(fetcher_.release()); | 130 std::unique_ptr<const TranslateURLFetcher> delete_ptr(fetcher_.release()); |
| 128 | 131 |
| 129 if (success) { | 132 if (success) { |
| 130 DCHECK(data_.empty()); | 133 DCHECK(data_.empty()); |
| 131 // Insert variable definitions on API Key and security origin. | 134 // Insert variable definitions on API Key and security origin. |
| 132 data_ = base::StringPrintf("var translateApiKey = '%s';\n", | 135 data_ = base::StringPrintf("var translateApiKey = '%s';\n", |
| 133 google_apis::GetAPIKey().c_str()); | 136 google_apis::GetAPIKey().c_str()); |
| 134 | 137 |
| 138 // Insert server params to pass experimental params to google translate | |
| 139 // server. | |
| 140 std::string server_params; | |
| 141 std::map<std::string, std::string> params; | |
| 142 if (variations::GetVariationParams(kTranslateServerStudy, ¶ms)) { | |
| 143 server_params = params[kServerParams]; | |
| 144 } | |
| 145 base::StringAppendF(&data_, "var serverParams = '%s';\n", | |
|
groby-ooo-7-16
2016/08/04 01:31:36
Last question, and then I promise I let this go. D
| |
| 146 server_params.c_str()); | |
| 147 | |
| 135 GURL security_origin = translate::GetTranslateSecurityOrigin(); | 148 GURL security_origin = translate::GetTranslateSecurityOrigin(); |
| 136 base::StringAppendF( | 149 base::StringAppendF( |
| 137 &data_, "var securityOrigin = '%s';", security_origin.spec().c_str()); | 150 &data_, "var securityOrigin = '%s';", security_origin.spec().c_str()); |
| 138 | 151 |
| 139 // Append embedded translate.js and a remote element library. | 152 // Append embedded translate.js and a remote element library. |
| 140 base::StringPiece str = ResourceBundle::GetSharedInstance(). | 153 base::StringPiece str = ResourceBundle::GetSharedInstance(). |
| 141 GetRawDataResource(IDR_TRANSLATE_JS); | 154 GetRawDataResource(IDR_TRANSLATE_JS); |
| 142 str.AppendToString(&data_); | 155 str.AppendToString(&data_); |
| 143 data_ += data; | 156 data_ += data; |
| 144 | 157 |
| 145 // We'll expire the cached script after some time, to make sure long | 158 // We'll expire the cached script after some time, to make sure long |
| 146 // running browsers still get fixes that might get pushed with newer | 159 // running browsers still get fixes that might get pushed with newer |
| 147 // scripts. | 160 // scripts. |
| 148 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 161 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 149 FROM_HERE, | 162 FROM_HERE, |
| 150 base::Bind(&TranslateScript::Clear, weak_method_factory_.GetWeakPtr()), | 163 base::Bind(&TranslateScript::Clear, weak_method_factory_.GetWeakPtr()), |
| 151 expiration_delay_); | 164 expiration_delay_); |
| 152 } | 165 } |
| 153 | 166 |
| 154 for (RequestCallbackList::iterator it = callback_list_.begin(); | 167 for (RequestCallbackList::iterator it = callback_list_.begin(); |
| 155 it != callback_list_.end(); | 168 it != callback_list_.end(); |
| 156 ++it) { | 169 ++it) { |
| 157 it->Run(success, data); | 170 it->Run(success, data); |
| 158 } | 171 } |
| 159 callback_list_.clear(); | 172 callback_list_.clear(); |
| 160 } | 173 } |
| 161 | 174 |
| 162 } // namespace translate | 175 } // namespace translate |
| OLD | NEW |