Chromium Code Reviews| 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 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 159 } | 159 } |
| 160 | 160 |
| 161 bool TranslateHelper::HasTranslationFinished() { | 161 bool TranslateHelper::HasTranslationFinished() { |
| 162 return ExecuteScriptAndGetBoolResult("cr.googleTranslate.finished", true); | 162 return ExecuteScriptAndGetBoolResult("cr.googleTranslate.finished", true); |
| 163 } | 163 } |
| 164 | 164 |
| 165 bool TranslateHelper::HasTranslationFailed() { | 165 bool TranslateHelper::HasTranslationFailed() { |
| 166 return ExecuteScriptAndGetBoolResult("cr.googleTranslate.error", true); | 166 return ExecuteScriptAndGetBoolResult("cr.googleTranslate.error", true); |
| 167 } | 167 } |
| 168 | 168 |
| 169 int64_t TranslateHelper::GetErrorCode() { | |
| 170 return ExecuteScriptAndGetIntegerResult( | |
| 171 "cr.googleTranslate.errorCode", | |
| 172 static_cast<int>(TranslateErrors::TRANSLATION_ERROR)); | |
| 173 } | |
| 174 | |
| 169 bool TranslateHelper::StartTranslation() { | 175 bool TranslateHelper::StartTranslation() { |
| 170 std::string script = "cr.googleTranslate.translate('" + | 176 std::string script = "cr.googleTranslate.translate('" + |
| 171 source_lang_ + | 177 source_lang_ + |
| 172 "','" + | 178 "','" + |
| 173 target_lang_ + | 179 target_lang_ + |
| 174 "')"; | 180 "')"; |
| 175 return ExecuteScriptAndGetBoolResult(script, false); | 181 return ExecuteScriptAndGetBoolResult(script, false); |
| 176 } | 182 } |
| 177 | 183 |
| 178 std::string TranslateHelper::GetOriginalPageLanguage() { | 184 std::string TranslateHelper::GetOriginalPageLanguage() { |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 245 WebScriptSource source = WebScriptSource(WebString::FromASCII(script)); | 251 WebScriptSource source = WebScriptSource(WebString::FromASCII(script)); |
| 246 main_frame->ExecuteScriptInIsolatedWorld(world_id_, &source, 1, &results); | 252 main_frame->ExecuteScriptInIsolatedWorld(world_id_, &source, 1, &results); |
| 247 if (results.size() != 1 || results[0].IsEmpty() || !results[0]->IsNumber()) { | 253 if (results.size() != 1 || results[0].IsEmpty() || !results[0]->IsNumber()) { |
| 248 NOTREACHED(); | 254 NOTREACHED(); |
| 249 return 0.0; | 255 return 0.0; |
| 250 } | 256 } |
| 251 | 257 |
| 252 return results[0]->NumberValue(); | 258 return results[0]->NumberValue(); |
| 253 } | 259 } |
| 254 | 260 |
| 261 int64_t TranslateHelper::ExecuteScriptAndGetIntegerResult( | |
| 262 const std::string& script, | |
| 263 int64_t fallback) { | |
| 264 WebLocalFrame* main_frame = render_frame()->GetWebFrame(); | |
| 265 if (!main_frame) | |
| 266 return fallback; | |
| 267 | |
| 268 v8::HandleScope handle_scope(v8::Isolate::GetCurrent()); | |
| 269 WebVector<v8::Local<v8::Value>> results; | |
| 270 WebScriptSource source = WebScriptSource(WebString::FromASCII(script)); | |
| 271 main_frame->ExecuteScriptInIsolatedWorld(world_id_, &source, 1, &results); | |
| 272 if (results.size() != 1 || results[0].IsEmpty() || !results[0]->IsNumber()) { | |
| 273 NOTREACHED(); | |
| 274 return fallback; | |
| 275 } | |
| 276 | |
| 277 return results[0]->IntegerValue(); | |
| 278 } | |
| 279 | |
| 255 // mojom::Page implementations. | 280 // mojom::Page implementations. |
| 256 void TranslateHelper::Translate(const std::string& translate_script, | 281 void TranslateHelper::Translate(const std::string& translate_script, |
| 257 const std::string& source_lang, | 282 const std::string& source_lang, |
| 258 const std::string& target_lang, | 283 const std::string& target_lang, |
| 259 TranslateCallback callback) { | 284 TranslateCallback callback) { |
| 260 WebLocalFrame* main_frame = render_frame()->GetWebFrame(); | 285 WebLocalFrame* main_frame = render_frame()->GetWebFrame(); |
| 261 if (!main_frame) { | 286 if (!main_frame) { |
| 262 // Cancelled. | 287 // Cancelled. |
| 263 std::move(callback).Run(true, source_lang, target_lang, | 288 std::move(callback).Run(true, source_lang, target_lang, |
| 264 TranslateErrors::NONE); | 289 TranslateErrors::NONE); |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 318 CancelPendingTranslation(); | 343 CancelPendingTranslation(); |
| 319 | 344 |
| 320 ExecuteScript("cr.googleTranslate.revert()"); | 345 ExecuteScript("cr.googleTranslate.revert()"); |
| 321 } | 346 } |
| 322 | 347 |
| 323 //////////////////////////////////////////////////////////////////////////////// | 348 //////////////////////////////////////////////////////////////////////////////// |
| 324 // TranslateHelper, private: | 349 // TranslateHelper, private: |
| 325 void TranslateHelper::CheckTranslateStatus() { | 350 void TranslateHelper::CheckTranslateStatus() { |
| 326 // First check if there was an error. | 351 // First check if there was an error. |
| 327 if (HasTranslationFailed()) { | 352 if (HasTranslationFailed()) { |
| 328 // TODO(toyoshim): Check |errorCode| of translate.js and notify it here. | 353 NotifyBrowserTranslationFailed( |
| 329 NotifyBrowserTranslationFailed(TranslateErrors::TRANSLATION_ERROR); | 354 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.
| |
| 330 return; // There was an error. | 355 return; // There was an error. |
| 331 } | 356 } |
| 332 | 357 |
| 333 if (HasTranslationFinished()) { | 358 if (HasTranslationFinished()) { |
| 334 std::string actual_source_lang; | 359 std::string actual_source_lang; |
| 335 // Translation was successfull, if it was auto, retrieve the source | 360 // Translation was successfull, if it was auto, retrieve the source |
| 336 // language the Translate Element detected. | 361 // language the Translate Element detected. |
| 337 if (source_lang_ == kAutoDetectionLanguage) { | 362 if (source_lang_ == kAutoDetectionLanguage) { |
| 338 actual_source_lang = GetOriginalPageLanguage(); | 363 actual_source_lang = GetOriginalPageLanguage(); |
| 339 if (actual_source_lang.empty()) { | 364 if (actual_source_lang.empty()) { |
| 340 NotifyBrowserTranslationFailed(TranslateErrors::UNKNOWN_LANGUAGE); | 365 NotifyBrowserTranslationFailed(TranslateErrors::UNKNOWN_LANGUAGE); |
| 341 return; | 366 return; |
| 342 } else if (actual_source_lang == target_lang_) { | |
| 343 NotifyBrowserTranslationFailed(TranslateErrors::IDENTICAL_LANGUAGES); | |
| 344 return; | |
| 345 } | 367 } |
| 346 } else { | 368 } else { |
| 347 actual_source_lang = source_lang_; | 369 actual_source_lang = source_lang_; |
| 348 } | 370 } |
| 349 | 371 |
| 372 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
| |
| 373 NotifyBrowserTranslationFailed(TranslateErrors::IDENTICAL_LANGUAGES); | |
| 374 return; | |
| 375 } | |
| 376 | |
| 350 if (!translate_callback_pending_) { | 377 if (!translate_callback_pending_) { |
| 351 NOTREACHED(); | 378 NOTREACHED(); |
| 352 return; | 379 return; |
| 353 } | 380 } |
| 354 | 381 |
| 355 // Check JavaScript performance counters for UMA reports. | 382 // Check JavaScript performance counters for UMA reports. |
| 356 ReportTimeToTranslate( | 383 ReportTimeToTranslate( |
| 357 ExecuteScriptAndGetDoubleResult("cr.googleTranslate.translationTime")); | 384 ExecuteScriptAndGetDoubleResult("cr.googleTranslate.translationTime")); |
| 358 | 385 |
| 359 // Notify the browser we are done. | 386 // Notify the browser we are done. |
| 360 std::move(translate_callback_pending_) | 387 std::move(translate_callback_pending_) |
| 361 .Run(false, actual_source_lang, target_lang_, TranslateErrors::NONE); | 388 .Run(false, actual_source_lang, target_lang_, TranslateErrors::NONE); |
| 362 return; | 389 return; |
| 363 } | 390 } |
| 364 | 391 |
| 365 // The translation is still pending, check again later. | 392 // The translation is still pending, check again later. |
| 366 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 393 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 367 FROM_HERE, base::Bind(&TranslateHelper::CheckTranslateStatus, | 394 FROM_HERE, base::Bind(&TranslateHelper::CheckTranslateStatus, |
| 368 weak_method_factory_.GetWeakPtr()), | 395 weak_method_factory_.GetWeakPtr()), |
| 369 AdjustDelay(kTranslateStatusCheckDelayMs)); | 396 AdjustDelay(kTranslateStatusCheckDelayMs)); |
| 370 } | 397 } |
| 371 | 398 |
| 372 void TranslateHelper::TranslatePageImpl(int count) { | 399 void TranslateHelper::TranslatePageImpl(int count) { |
| 373 DCHECK_LT(count, kMaxTranslateInitCheckAttempts); | 400 DCHECK_LT(count, kMaxTranslateInitCheckAttempts); |
| 374 if (!IsTranslateLibReady()) { | 401 if (!IsTranslateLibReady()) { |
| 402 // 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
| |
| 403 TranslateErrors::Type error = | |
| 404 static_cast<translate::TranslateErrors::Type>(GetErrorCode()); | |
| 405 if (error == TranslateErrors::INITIALIZATION_ERROR) { | |
| 406 NotifyBrowserTranslationFailed(error); | |
| 407 return; | |
| 408 } | |
| 409 | |
| 375 // The library is not ready, try again later, unless we have tried several | 410 // The library is not ready, try again later, unless we have tried several |
| 376 // times unsuccessfully already. | 411 // times unsuccessfully already. |
| 377 if (++count >= kMaxTranslateInitCheckAttempts) { | 412 if (++count >= kMaxTranslateInitCheckAttempts) { |
| 378 NotifyBrowserTranslationFailed(TranslateErrors::INITIALIZATION_ERROR); | 413 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.
| |
| 379 return; | 414 return; |
| 380 } | 415 } |
| 381 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 416 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 382 FROM_HERE, base::Bind(&TranslateHelper::TranslatePageImpl, | 417 FROM_HERE, base::Bind(&TranslateHelper::TranslatePageImpl, |
| 383 weak_method_factory_.GetWeakPtr(), count), | 418 weak_method_factory_.GetWeakPtr(), count), |
| 384 AdjustDelay(count * kTranslateInitCheckDelayMs)); | 419 AdjustDelay(count * kTranslateInitCheckDelayMs)); |
| 385 return; | 420 return; |
| 386 } | 421 } |
| 387 | 422 |
| 388 // The library is loaded, and ready for translation now. | 423 // The library is loaded, and ready for translation now. |
| 389 // Check JavaScript performance counters for UMA reports. | 424 // Check JavaScript performance counters for UMA reports. |
| 390 ReportTimeToBeReady( | 425 ReportTimeToBeReady( |
| 391 ExecuteScriptAndGetDoubleResult("cr.googleTranslate.readyTime")); | 426 ExecuteScriptAndGetDoubleResult("cr.googleTranslate.readyTime")); |
| 392 ReportTimeToLoad( | 427 ReportTimeToLoad( |
| 393 ExecuteScriptAndGetDoubleResult("cr.googleTranslate.loadTime")); | 428 ExecuteScriptAndGetDoubleResult("cr.googleTranslate.loadTime")); |
| 394 | 429 |
| 395 if (!StartTranslation()) { | 430 if (!StartTranslation()) { |
| 396 NotifyBrowserTranslationFailed(TranslateErrors::TRANSLATION_ERROR); | 431 CheckTranslateStatus(); |
| 397 return; | 432 return; |
| 398 } | 433 } |
| 399 // Check the status of the translation. | 434 // Check the status of the translation. |
| 400 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 435 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 401 FROM_HERE, base::Bind(&TranslateHelper::CheckTranslateStatus, | 436 FROM_HERE, base::Bind(&TranslateHelper::CheckTranslateStatus, |
| 402 weak_method_factory_.GetWeakPtr()), | 437 weak_method_factory_.GetWeakPtr()), |
| 403 AdjustDelay(kTranslateStatusCheckDelayMs)); | 438 AdjustDelay(kTranslateStatusCheckDelayMs)); |
| 404 } | 439 } |
| 405 | 440 |
| 406 void TranslateHelper::NotifyBrowserTranslationFailed( | 441 void TranslateHelper::NotifyBrowserTranslationFailed( |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 424 binding_.Close(); | 459 binding_.Close(); |
| 425 translate_callback_pending_.Reset(); | 460 translate_callback_pending_.Reset(); |
| 426 CancelPendingTranslation(); | 461 CancelPendingTranslation(); |
| 427 } | 462 } |
| 428 | 463 |
| 429 void TranslateHelper::OnDestruct() { | 464 void TranslateHelper::OnDestruct() { |
| 430 delete this; | 465 delete this; |
| 431 } | 466 } |
| 432 | 467 |
| 433 } // namespace translate | 468 } // namespace translate |
| OLD | NEW |