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..ebd97ab5b4e631fe46e9d80da1987079b895b5c1 100644 |
--- a/components/translate/content/renderer/translate_helper.cc |
+++ b/components/translate/content/renderer/translate_helper.cc |
@@ -166,6 +166,12 @@ bool TranslateHelper::HasTranslationFailed() { |
return ExecuteScriptAndGetBoolResult("cr.googleTranslate.error", true); |
} |
+int64_t TranslateHelper::GetErrorCode() { |
+ return ExecuteScriptAndGetIntegerResult( |
+ "cr.googleTranslate.errorCode", |
+ static_cast<int>(TranslateErrors::TRANSLATION_ERROR)); |
+} |
+ |
bool TranslateHelper::StartTranslation() { |
std::string script = "cr.googleTranslate.translate('" + |
source_lang_ + |
@@ -252,6 +258,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 +350,8 @@ 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>(GetErrorCode())); |
napper
2017/06/16 12:41:51
Consider checking that GetErrorCode() < TRANSLATE_
Takashi Toyoshima
2017/06/16 16:14:17
maybe DCHECK inside GetErrorCode impl?
Gaja
2017/06/17 06:41:46
Acknowledged.
Gaja
2017/06/17 13:25:48
Done.
|
return; // There was an error. |
} |
@@ -339,14 +364,16 @@ void TranslateHelper::CheckTranslateStatus() { |
if (actual_source_lang.empty()) { |
NotifyBrowserTranslationFailed(TranslateErrors::UNKNOWN_LANGUAGE); |
return; |
- } else if (actual_source_lang == target_lang_) { |
- NotifyBrowserTranslationFailed(TranslateErrors::IDENTICAL_LANGUAGES); |
- return; |
} |
} else { |
actual_source_lang = source_lang_; |
} |
+ if (actual_source_lang == target_lang_) { |
Takashi Toyoshima
2017/06/16 04:51:29
What is the motivation to move here?
|actual_sourc
Gaja
2017/06/17 06:41:46
Please correct me if I am wrong.
As per my analysi
Takashi Toyoshima
2017/06/20 11:49:54
Here, |source_lang| is the language Chrome's CLD d
Gaja
2017/06/22 05:22:23
Thanks for the detailed explanation.
Now I unders
|
+ NotifyBrowserTranslationFailed(TranslateErrors::IDENTICAL_LANGUAGES); |
+ return; |
+ } |
+ |
if (!translate_callback_pending_) { |
NOTREACHED(); |
return; |
@@ -372,10 +399,18 @@ void TranslateHelper::CheckTranslateStatus() { |
void TranslateHelper::TranslatePageImpl(int count) { |
DCHECK_LT(count, kMaxTranslateInitCheckAttempts); |
if (!IsTranslateLibReady()) { |
+ // There was an error during initialization of library. |
Takashi Toyoshima
2017/06/16 04:51:28
I don't think errorCode is ready here, because IsT
Gaja
2017/06/17 06:41:46
I added this check considering the way the errorCo
Takashi Toyoshima
2017/06/20 11:49:54
I see, that makes sense.
So, could you change the
Gaja
2017/06/22 05:22:23
I made the change suggested by you and all the tes
|
+ TranslateErrors::Type error = |
+ static_cast<translate::TranslateErrors::Type>(GetErrorCode()); |
+ if (error == TranslateErrors::INITIALIZATION_ERROR) { |
+ 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); |
Takashi Toyoshima
2017/06/16 04:51:28
We use the timeout error only when actual translat
Gaja
2017/06/17 06:41:46
I added this check considering the way the errorCo
Takashi Toyoshima
2017/06/20 11:49:54
Oops, I was wrong. We already used it for library
Gaja
2017/06/22 05:22:23
Done.
|
return; |
} |
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
@@ -393,7 +428,7 @@ void TranslateHelper::TranslatePageImpl(int count) { |
ExecuteScriptAndGetDoubleResult("cr.googleTranslate.loadTime")); |
if (!StartTranslation()) { |
- NotifyBrowserTranslationFailed(TranslateErrors::TRANSLATION_ERROR); |
+ CheckTranslateStatus(); |
return; |
} |
// Check the status of the translation. |