Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(97)

Unified Diff: components/translate/content/renderer/translate_helper.cc

Issue 2919343007: Check |errorCode| of translate.js and notify to Browser (Closed)
Patch Set: Addressing review comments : Use static_cast for enum conversion instead of switch case. Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « components/translate/content/renderer/translate_helper.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: components/translate/content/renderer/translate_helper.cc
diff --git a/components/translate/content/renderer/translate_helper.cc b/components/translate/content/renderer/translate_helper.cc
index f1b8ce439604062e46d72094d4f05731725861dc..d66f1bc4f2d9b1b2d186ad5de6cc0b09959eb5d9 100644
--- a/components/translate/content/renderer/translate_helper.cc
+++ b/components/translate/content/renderer/translate_helper.cc
@@ -60,6 +60,13 @@ const char kAutoDetectionLanguage[] = "auto";
// Isolated world sets following content-security-policy.
const char kContentSecurityPolicy[] = "script-src 'self' 'unsafe-eval'";
+// Converts the error value returned by 'cr.googleTranslate.errorCode' to
+// TranslateErrors::Type.
+translate::TranslateErrors::Type ErrorCodeToTranslateErrorType(
+ int64_t error_code) {
+ return static_cast<translate::TranslateErrors::Type>(error_code);
+}
+
} // namespace
namespace translate {
@@ -252,6 +259,25 @@ double TranslateHelper::ExecuteScriptAndGetDoubleResult(
return results[0]->NumberValue();
}
+int64_t TranslateHelper::ExecuteScriptAndGetIntegerResult(
+ const std::string& script,
+ int64_t fallback) {
+ WebLocalFrame* main_frame = render_frame()->GetWebFrame();
+ if (!main_frame)
+ return fallback;
+
+ v8::HandleScope handle_scope(v8::Isolate::GetCurrent());
+ WebVector<v8::Local<v8::Value>> results;
+ WebScriptSource source = WebScriptSource(WebString::FromASCII(script));
+ main_frame->ExecuteScriptInIsolatedWorld(world_id_, &source, 1, &results);
+ if (results.size() != 1 || results[0].IsEmpty() || !results[0]->IsNumber()) {
+ NOTREACHED();
+ return fallback;
+ }
+
+ return results[0]->IntegerValue();
+}
+
// mojom::Page implementations.
void TranslateHelper::Translate(const std::string& translate_script,
const std::string& source_lang,
@@ -325,8 +351,10 @@ void TranslateHelper::RevertTranslation() {
void TranslateHelper::CheckTranslateStatus() {
// First check if there was an error.
if (HasTranslationFailed()) {
- // TODO(toyoshim): Check |errorCode| of translate.js and notify it here.
- NotifyBrowserTranslationFailed(TranslateErrors::TRANSLATION_ERROR);
+ int64_t error_code = ExecuteScriptAndGetIntegerResult(
+ "cr.googleTranslate.errorCode",
+ static_cast<int>(TranslateErrors::TRANSLATION_ERROR));
+ NotifyBrowserTranslationFailed(ErrorCodeToTranslateErrorType(error_code));
Takashi Toyoshima 2017/06/14 04:00:02 Probably it's consistent to use static_cast<Transl
Gaja 2017/06/14 06:53:11 Acknowledged. Ok, this seems better.
return; // There was an error.
}
« no previous file with comments | « components/translate/content/renderer/translate_helper.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698