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

Side by Side 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 unified diff | Download patch
« no previous file with comments | « components/translate/content/renderer/translate_helper.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
53 // The delay we wait in milliseconds before checking whether the translation has 53 // The delay we wait in milliseconds before checking whether the translation has
54 // finished. 54 // finished.
55 const int kTranslateStatusCheckDelayMs = 400; 55 const int kTranslateStatusCheckDelayMs = 400;
56 56
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 // Converts the error value returned by 'cr.googleTranslate.errorCode' to
64 // TranslateErrors::Type.
65 translate::TranslateErrors::Type ErrorCodeToTranslateErrorType(
66 int64_t error_code) {
67 return static_cast<translate::TranslateErrors::Type>(error_code);
68 }
69
63 } // namespace 70 } // namespace
64 71
65 namespace translate { 72 namespace translate {
66 73
67 //////////////////////////////////////////////////////////////////////////////// 74 ////////////////////////////////////////////////////////////////////////////////
68 // TranslateHelper, public: 75 // TranslateHelper, public:
69 TranslateHelper::TranslateHelper(content::RenderFrame* render_frame, 76 TranslateHelper::TranslateHelper(content::RenderFrame* render_frame,
70 int world_id, 77 int world_id,
71 const std::string& extension_scheme) 78 const std::string& extension_scheme)
72 : content::RenderFrameObserver(render_frame), 79 : content::RenderFrameObserver(render_frame),
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after
245 WebScriptSource source = WebScriptSource(WebString::FromASCII(script)); 252 WebScriptSource source = WebScriptSource(WebString::FromASCII(script));
246 main_frame->ExecuteScriptInIsolatedWorld(world_id_, &source, 1, &results); 253 main_frame->ExecuteScriptInIsolatedWorld(world_id_, &source, 1, &results);
247 if (results.size() != 1 || results[0].IsEmpty() || !results[0]->IsNumber()) { 254 if (results.size() != 1 || results[0].IsEmpty() || !results[0]->IsNumber()) {
248 NOTREACHED(); 255 NOTREACHED();
249 return 0.0; 256 return 0.0;
250 } 257 }
251 258
252 return results[0]->NumberValue(); 259 return results[0]->NumberValue();
253 } 260 }
254 261
262 int64_t TranslateHelper::ExecuteScriptAndGetIntegerResult(
263 const std::string& script,
264 int64_t fallback) {
265 WebLocalFrame* main_frame = render_frame()->GetWebFrame();
266 if (!main_frame)
267 return fallback;
268
269 v8::HandleScope handle_scope(v8::Isolate::GetCurrent());
270 WebVector<v8::Local<v8::Value>> results;
271 WebScriptSource source = WebScriptSource(WebString::FromASCII(script));
272 main_frame->ExecuteScriptInIsolatedWorld(world_id_, &source, 1, &results);
273 if (results.size() != 1 || results[0].IsEmpty() || !results[0]->IsNumber()) {
274 NOTREACHED();
275 return fallback;
276 }
277
278 return results[0]->IntegerValue();
279 }
280
255 // mojom::Page implementations. 281 // mojom::Page implementations.
256 void TranslateHelper::Translate(const std::string& translate_script, 282 void TranslateHelper::Translate(const std::string& translate_script,
257 const std::string& source_lang, 283 const std::string& source_lang,
258 const std::string& target_lang, 284 const std::string& target_lang,
259 TranslateCallback callback) { 285 TranslateCallback callback) {
260 WebLocalFrame* main_frame = render_frame()->GetWebFrame(); 286 WebLocalFrame* main_frame = render_frame()->GetWebFrame();
261 if (!main_frame) { 287 if (!main_frame) {
262 // Cancelled. 288 // Cancelled.
263 std::move(callback).Run(true, source_lang, target_lang, 289 std::move(callback).Run(true, source_lang, target_lang,
264 TranslateErrors::NONE); 290 TranslateErrors::NONE);
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
318 CancelPendingTranslation(); 344 CancelPendingTranslation();
319 345
320 ExecuteScript("cr.googleTranslate.revert()"); 346 ExecuteScript("cr.googleTranslate.revert()");
321 } 347 }
322 348
323 //////////////////////////////////////////////////////////////////////////////// 349 ////////////////////////////////////////////////////////////////////////////////
324 // TranslateHelper, private: 350 // TranslateHelper, private:
325 void TranslateHelper::CheckTranslateStatus() { 351 void TranslateHelper::CheckTranslateStatus() {
326 // First check if there was an error. 352 // First check if there was an error.
327 if (HasTranslationFailed()) { 353 if (HasTranslationFailed()) {
328 // TODO(toyoshim): Check |errorCode| of translate.js and notify it here. 354 int64_t error_code = ExecuteScriptAndGetIntegerResult(
329 NotifyBrowserTranslationFailed(TranslateErrors::TRANSLATION_ERROR); 355 "cr.googleTranslate.errorCode",
356 static_cast<int>(TranslateErrors::TRANSLATION_ERROR));
357 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.
330 return; // There was an error. 358 return; // There was an error.
331 } 359 }
332 360
333 if (HasTranslationFinished()) { 361 if (HasTranslationFinished()) {
334 std::string actual_source_lang; 362 std::string actual_source_lang;
335 // Translation was successfull, if it was auto, retrieve the source 363 // Translation was successfull, if it was auto, retrieve the source
336 // language the Translate Element detected. 364 // language the Translate Element detected.
337 if (source_lang_ == kAutoDetectionLanguage) { 365 if (source_lang_ == kAutoDetectionLanguage) {
338 actual_source_lang = GetOriginalPageLanguage(); 366 actual_source_lang = GetOriginalPageLanguage();
339 if (actual_source_lang.empty()) { 367 if (actual_source_lang.empty()) {
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
424 binding_.Close(); 452 binding_.Close();
425 translate_callback_pending_.Reset(); 453 translate_callback_pending_.Reset();
426 CancelPendingTranslation(); 454 CancelPendingTranslation();
427 } 455 }
428 456
429 void TranslateHelper::OnDestruct() { 457 void TranslateHelper::OnDestruct() {
430 delete this; 458 delete this;
431 } 459 }
432 460
433 } // namespace translate 461 } // namespace translate
OLDNEW
« 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