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

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

Issue 2919343007: Check |errorCode| of translate.js and notify to Browser (Closed)
Patch Set: Addressed review comments. 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
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..c2e1989e08202c8a5461b485bf6d4e27654ec60e 100644
--- a/components/translate/content/renderer/translate_helper.cc
+++ b/components/translate/content/renderer/translate_helper.cc
@@ -166,6 +166,14 @@ bool TranslateHelper::HasTranslationFailed() {
return ExecuteScriptAndGetBoolResult("cr.googleTranslate.error", true);
}
+int64_t TranslateHelper::GetErrorCodeWithFallback(
+ TranslateErrors::Type fallback) {
+ int64_t error_code = ExecuteScriptAndGetIntegerResult(
+ "cr.googleTranslate.errorCode", static_cast<int>(fallback));
+ DCHECK_LT(error_code, static_cast<int>(TranslateErrors::TRANSLATE_ERROR_MAX));
+ return error_code;
+}
+
bool TranslateHelper::StartTranslation() {
std::string script = "cr.googleTranslate.translate('" +
source_lang_ +
@@ -252,6 +260,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();
Takashi Toyoshima 2017/06/27 12:04:55 If this actually happens as you said, this NOTREAC
Gaja 2017/06/28 03:43:31 It reaches this point for test cases in TrnaslateH
Gaja 2017/06/28 04:16:00 Sorry, I made a mistake here.
Takashi Toyoshima 2017/06/28 07:25:32 So, we should still have this NOTREACHED() check a
Gaja 2017/06/28 11:34:45 Okay.
+ return fallback;
+ }
+
+ return results[0]->IntegerValue();
+}
+
// mojom::Page implementations.
void TranslateHelper::Translate(const std::string& translate_script,
const std::string& source_lang,
@@ -325,8 +352,9 @@ 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);
+ NotifyBrowserTranslationFailed(
+ static_cast<translate::TranslateErrors::Type>(
+ GetErrorCodeWithFallback(TranslateErrors::TRANSLATION_ERROR)));
return; // There was an error.
}
@@ -372,10 +400,18 @@ void TranslateHelper::CheckTranslateStatus() {
void TranslateHelper::TranslatePageImpl(int count) {
DCHECK_LT(count, kMaxTranslateInitCheckAttempts);
if (!IsTranslateLibReady()) {
+ // There was an error during initialization of library.
+ TranslateErrors::Type error = static_cast<translate::TranslateErrors::Type>(
+ GetErrorCodeWithFallback(TranslateErrors::NONE));
+ if (error != TranslateErrors::NONE) {
+ NotifyBrowserTranslationFailed(error);
+ return;
+ }
+
// The library is not ready, try again later, unless we have tried several
// times unsuccessfully already.
if (++count >= kMaxTranslateInitCheckAttempts) {
- NotifyBrowserTranslationFailed(TranslateErrors::INITIALIZATION_ERROR);
+ NotifyBrowserTranslationFailed(TranslateErrors::TRANSLATION_TIMEOUT);
return;
}
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
@@ -393,7 +429,7 @@ void TranslateHelper::TranslatePageImpl(int count) {
ExecuteScriptAndGetDoubleResult("cr.googleTranslate.loadTime"));
if (!StartTranslation()) {
- NotifyBrowserTranslationFailed(TranslateErrors::TRANSLATION_ERROR);
+ CheckTranslateStatus();
return;
}
// Check the status of the translation.

Powered by Google App Engine
This is Rietveld 408576698