OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/content/renderer/translate_helper.h" | 5 #include "components/translate/content/renderer/translate_helper.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
57 // Language name passed to the Translate element for it to detect the language. | 57 // Language name passed to the Translate element for it to detect the language. |
58 const char kAutoDetectionLanguage[] = "auto"; | 58 const char kAutoDetectionLanguage[] = "auto"; |
59 | 59 |
60 // Isolated world sets following content-security-policy. | 60 // Isolated world sets following content-security-policy. |
61 const char kContentSecurityPolicy[] = "script-src 'self' 'unsafe-eval'"; | 61 const char kContentSecurityPolicy[] = "script-src 'self' 'unsafe-eval'"; |
62 | 62 |
63 } // namespace | 63 } // namespace |
64 | 64 |
65 namespace translate { | 65 namespace translate { |
66 | 66 |
67 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.
| |
68 switch (error_code) { | |
69 case 0: | |
70 return TranslateErrors::NONE; | |
71 case 2: | |
72 return TranslateErrors::INITIALIZATION_ERROR; | |
73 case 4: | |
74 return TranslateErrors::UNSUPPORTED_LANGUAGE; | |
75 case 6: | |
76 return TranslateErrors::TRANSLATION_ERROR; | |
77 case 7: | |
78 return TranslateErrors::TRANSLATION_TIMEOUT; | |
79 case 8: | |
80 return TranslateErrors::UNEXPECTED_SCRIPT_ERROR; | |
81 case 9: | |
82 return TranslateErrors::BAD_ORIGIN; | |
83 case 10: | |
84 return TranslateErrors::SCRIPT_LOAD_ERROR; | |
85 default: | |
86 return TranslateErrors::TRANSLATION_ERROR; | |
87 } | |
88 } | |
89 | |
67 //////////////////////////////////////////////////////////////////////////////// | 90 //////////////////////////////////////////////////////////////////////////////// |
68 // TranslateHelper, public: | 91 // TranslateHelper, public: |
69 TranslateHelper::TranslateHelper(content::RenderFrame* render_frame, | 92 TranslateHelper::TranslateHelper(content::RenderFrame* render_frame, |
70 int world_id, | 93 int world_id, |
71 const std::string& extension_scheme) | 94 const std::string& extension_scheme) |
72 : content::RenderFrameObserver(render_frame), | 95 : content::RenderFrameObserver(render_frame), |
73 world_id_(world_id), | 96 world_id_(world_id), |
74 extension_scheme_(extension_scheme), | 97 extension_scheme_(extension_scheme), |
75 binding_(this), | 98 binding_(this), |
76 weak_method_factory_(this) {} | 99 weak_method_factory_(this) {} |
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
244 WebScriptSource source = WebScriptSource(WebString::FromASCII(script)); | 267 WebScriptSource source = WebScriptSource(WebString::FromASCII(script)); |
245 main_frame->ExecuteScriptInIsolatedWorld(world_id_, &source, 1, &results); | 268 main_frame->ExecuteScriptInIsolatedWorld(world_id_, &source, 1, &results); |
246 if (results.size() != 1 || results[0].IsEmpty() || !results[0]->IsNumber()) { | 269 if (results.size() != 1 || results[0].IsEmpty() || !results[0]->IsNumber()) { |
247 NOTREACHED(); | 270 NOTREACHED(); |
248 return 0.0; | 271 return 0.0; |
249 } | 272 } |
250 | 273 |
251 return results[0]->NumberValue(); | 274 return results[0]->NumberValue(); |
252 } | 275 } |
253 | 276 |
277 int64_t TranslateHelper::ExecuteScriptAndGetIntegerResult( | |
278 const std::string& script, | |
279 int64_t fallback) { | |
280 WebLocalFrame* main_frame = render_frame()->GetWebFrame(); | |
281 if (!main_frame) | |
282 return fallback; | |
283 | |
284 v8::HandleScope handle_scope(v8::Isolate::GetCurrent()); | |
285 WebVector<v8::Local<v8::Value>> results; | |
286 WebScriptSource source = WebScriptSource(WebString::FromASCII(script)); | |
287 main_frame->ExecuteScriptInIsolatedWorld(world_id_, &source, 1, &results); | |
288 if (results.size() != 1 || results[0].IsEmpty() || !results[0]->IsNumber()) { | |
289 NOTREACHED(); | |
290 return fallback; | |
291 } | |
292 | |
293 return results[0]->IntegerValue(); | |
294 } | |
295 | |
254 // mojom::Page implementations. | 296 // mojom::Page implementations. |
255 void TranslateHelper::Translate(const std::string& translate_script, | 297 void TranslateHelper::Translate(const std::string& translate_script, |
256 const std::string& source_lang, | 298 const std::string& source_lang, |
257 const std::string& target_lang, | 299 const std::string& target_lang, |
258 TranslateCallback callback) { | 300 TranslateCallback callback) { |
259 WebLocalFrame* main_frame = render_frame()->GetWebFrame(); | 301 WebLocalFrame* main_frame = render_frame()->GetWebFrame(); |
260 if (!main_frame) { | 302 if (!main_frame) { |
261 // Cancelled. | 303 // Cancelled. |
262 std::move(callback).Run(true, source_lang, target_lang, | 304 std::move(callback).Run(true, source_lang, target_lang, |
263 TranslateErrors::NONE); | 305 TranslateErrors::NONE); |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
317 CancelPendingTranslation(); | 359 CancelPendingTranslation(); |
318 | 360 |
319 ExecuteScript("cr.googleTranslate.revert()"); | 361 ExecuteScript("cr.googleTranslate.revert()"); |
320 } | 362 } |
321 | 363 |
322 //////////////////////////////////////////////////////////////////////////////// | 364 //////////////////////////////////////////////////////////////////////////////// |
323 // TranslateHelper, private: | 365 // TranslateHelper, private: |
324 void TranslateHelper::CheckTranslateStatus() { | 366 void TranslateHelper::CheckTranslateStatus() { |
325 // First check if there was an error. | 367 // First check if there was an error. |
326 if (HasTranslationFailed()) { | 368 if (HasTranslationFailed()) { |
327 // TODO(toyoshim): Check |errorCode| of translate.js and notify it here. | 369 int64_t error_code = ExecuteScriptAndGetIntegerResult( |
328 NotifyBrowserTranslationFailed(TranslateErrors::TRANSLATION_ERROR); | 370 "cr.googleTranslate.errorCode", |
371 static_cast<int>(TranslateErrors::TRANSLATION_ERROR)); | |
372 NotifyBrowserTranslationFailed(ErrorCodeToTranslateErrorType(error_code)); | |
329 return; // There was an error. | 373 return; // There was an error. |
330 } | 374 } |
331 | 375 |
332 if (HasTranslationFinished()) { | 376 if (HasTranslationFinished()) { |
333 std::string actual_source_lang; | 377 std::string actual_source_lang; |
334 // Translation was successfull, if it was auto, retrieve the source | 378 // Translation was successfull, if it was auto, retrieve the source |
335 // language the Translate Element detected. | 379 // language the Translate Element detected. |
336 if (source_lang_ == kAutoDetectionLanguage) { | 380 if (source_lang_ == kAutoDetectionLanguage) { |
337 actual_source_lang = GetOriginalPageLanguage(); | 381 actual_source_lang = GetOriginalPageLanguage(); |
338 if (actual_source_lang.empty()) { | 382 if (actual_source_lang.empty()) { |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
423 binding_.Close(); | 467 binding_.Close(); |
424 translate_callback_pending_.Reset(); | 468 translate_callback_pending_.Reset(); |
425 CancelPendingTranslation(); | 469 CancelPendingTranslation(); |
426 } | 470 } |
427 | 471 |
428 void TranslateHelper::OnDestruct() { | 472 void TranslateHelper::OnDestruct() { |
429 delete this; | 473 delete this; |
430 } | 474 } |
431 | 475 |
432 } // namespace translate | 476 } // namespace translate |
OLD | NEW |