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

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

Issue 2919343007: Check |errorCode| of translate.js and notify to Browser (Closed)
Patch Set: 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 4c8d98ffff6c2e825c97062cbdb5bba4b1dd6288..3462ac2e902b631020962ee14220542db7e9b697 100644
--- a/components/translate/content/renderer/translate_helper.cc
+++ b/components/translate/content/renderer/translate_helper.cc
@@ -64,6 +64,29 @@ const char kContentSecurityPolicy[] = "script-src 'self' 'unsafe-eval'";
namespace translate {
+static TranslateErrors::Type ErrorCodeToTranslateErrorType(int64_t error_code) {
Takashi Toyoshima 2017/06/13 11:58:29 can be in anonymous namespace above. But just usi
Gaja 2017/06/14 03:11:45 Done.
+ switch (error_code) {
+ case 0:
+ return TranslateErrors::NONE;
+ case 2:
+ return TranslateErrors::INITIALIZATION_ERROR;
+ case 4:
+ return TranslateErrors::UNSUPPORTED_LANGUAGE;
+ case 6:
+ return TranslateErrors::TRANSLATION_ERROR;
+ case 7:
+ return TranslateErrors::TRANSLATION_TIMEOUT;
+ case 8:
+ return TranslateErrors::UNEXPECTED_SCRIPT_ERROR;
+ case 9:
+ return TranslateErrors::BAD_ORIGIN;
+ case 10:
+ return TranslateErrors::SCRIPT_LOAD_ERROR;
+ default:
+ return TranslateErrors::TRANSLATION_ERROR;
+ }
+}
+
////////////////////////////////////////////////////////////////////////////////
// TranslateHelper, public:
TranslateHelper::TranslateHelper(content::RenderFrame* render_frame,
@@ -251,6 +274,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,
@@ -324,8 +366,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));
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