| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "chrome/renderer/translate_helper.h" | 5 #include "chrome/renderer/translate_helper.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
| 9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
| 10 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 #include "v8/include/v8.h" | 21 #include "v8/include/v8.h" |
| 22 #include "webkit/glue/dom_operations.h" | 22 #include "webkit/glue/dom_operations.h" |
| 23 | 23 |
| 24 using WebKit::WebDocument; | 24 using WebKit::WebDocument; |
| 25 using WebKit::WebElement; | 25 using WebKit::WebElement; |
| 26 using WebKit::WebFrame; | 26 using WebKit::WebFrame; |
| 27 using WebKit::WebScriptSource; | 27 using WebKit::WebScriptSource; |
| 28 using WebKit::WebString; | 28 using WebKit::WebString; |
| 29 using WebKit::WebView; | 29 using WebKit::WebView; |
| 30 | 30 |
| 31 // The delay in millliseconds that we'll wait before checking to see if the | 31 // The delay in milliseconds that we'll wait before checking to see if the |
| 32 // translate library injected in the page is ready. | 32 // translate library injected in the page is ready. |
| 33 static const int kTranslateInitCheckDelayMs = 150; | 33 static const int kTranslateInitCheckDelayMs = 150; |
| 34 | 34 |
| 35 // The maximum number of times we'll check to see if the translate library | 35 // The maximum number of times we'll check to see if the translate library |
| 36 // injected in the page is ready. | 36 // injected in the page is ready. |
| 37 static const int kMaxTranslateInitCheckAttempts = 5; | 37 static const int kMaxTranslateInitCheckAttempts = 5; |
| 38 | 38 |
| 39 // The delay we wait in milliseconds before checking whether the translation has | 39 // The delay we wait in milliseconds before checking whether the translation has |
| 40 // finished. | 40 // finished. |
| 41 static const int kTranslateStatusCheckDelayMs = 400; | 41 static const int kTranslateStatusCheckDelayMs = 400; |
| (...skipping 290 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 332 translation_pending_ = false; | 332 translation_pending_ = false; |
| 333 | 333 |
| 334 // Notify the browser we are done. | 334 // Notify the browser we are done. |
| 335 render_view()->Send(new ChromeViewHostMsg_PageTranslated( | 335 render_view()->Send(new ChromeViewHostMsg_PageTranslated( |
| 336 render_view()->GetRoutingId(), render_view()->GetPageId(), | 336 render_view()->GetRoutingId(), render_view()->GetPageId(), |
| 337 actual_source_lang, target_lang_, TranslateErrors::NONE)); | 337 actual_source_lang, target_lang_, TranslateErrors::NONE)); |
| 338 return; | 338 return; |
| 339 } | 339 } |
| 340 | 340 |
| 341 // The translation is still pending, check again later. | 341 // The translation is still pending, check again later. |
| 342 MessageLoop::current()->PostDelayedTask(FROM_HERE, | 342 MessageLoop::current()->PostDelayedTask( |
| 343 FROM_HERE, |
| 343 base::Bind(&TranslateHelper::CheckTranslateStatus, | 344 base::Bind(&TranslateHelper::CheckTranslateStatus, |
| 344 weak_method_factory_.GetWeakPtr()), | 345 weak_method_factory_.GetWeakPtr()), |
| 345 DontDelayTasks() ? 0 : kTranslateStatusCheckDelayMs); | 346 base::TimeDelta::FromMilliseconds( |
| 347 DontDelayTasks() ? 0 : kTranslateStatusCheckDelayMs)); |
| 346 } | 348 } |
| 347 | 349 |
| 348 bool TranslateHelper::ExecuteScript(const std::string& script) { | 350 bool TranslateHelper::ExecuteScript(const std::string& script) { |
| 349 WebFrame* main_frame = GetMainFrame(); | 351 WebFrame* main_frame = GetMainFrame(); |
| 350 if (!main_frame) | 352 if (!main_frame) |
| 351 return false; | 353 return false; |
| 352 main_frame->executeScript(WebScriptSource(ASCIIToUTF16(script))); | 354 main_frame->executeScript(WebScriptSource(ASCIIToUTF16(script))); |
| 353 return true; | 355 return true; |
| 354 } | 356 } |
| 355 | 357 |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 394 if (page_id_ != render_view()->GetPageId() || !render_view()->GetWebView()) | 396 if (page_id_ != render_view()->GetPageId() || !render_view()->GetWebView()) |
| 395 return; | 397 return; |
| 396 | 398 |
| 397 if (!IsTranslateLibReady()) { | 399 if (!IsTranslateLibReady()) { |
| 398 // The library is not ready, try again later, unless we have tried several | 400 // The library is not ready, try again later, unless we have tried several |
| 399 // times unsucessfully already. | 401 // times unsucessfully already. |
| 400 if (++count >= kMaxTranslateInitCheckAttempts) { | 402 if (++count >= kMaxTranslateInitCheckAttempts) { |
| 401 NotifyBrowserTranslationFailed(TranslateErrors::INITIALIZATION_ERROR); | 403 NotifyBrowserTranslationFailed(TranslateErrors::INITIALIZATION_ERROR); |
| 402 return; | 404 return; |
| 403 } | 405 } |
| 404 MessageLoop::current()->PostDelayedTask(FROM_HERE, | 406 MessageLoop::current()->PostDelayedTask( |
| 407 FROM_HERE, |
| 405 base::Bind(&TranslateHelper::TranslatePageImpl, | 408 base::Bind(&TranslateHelper::TranslatePageImpl, |
| 406 weak_method_factory_.GetWeakPtr(), count), | 409 weak_method_factory_.GetWeakPtr(), count), |
| 407 DontDelayTasks() ? 0 : count * kTranslateInitCheckDelayMs); | 410 base::TimeDelta::FromMilliseconds( |
| 411 DontDelayTasks() ? 0 : count * kTranslateInitCheckDelayMs)); |
| 408 return; | 412 return; |
| 409 } | 413 } |
| 410 | 414 |
| 411 if (!StartTranslation()) { | 415 if (!StartTranslation()) { |
| 412 NotifyBrowserTranslationFailed(TranslateErrors::TRANSLATION_ERROR); | 416 NotifyBrowserTranslationFailed(TranslateErrors::TRANSLATION_ERROR); |
| 413 return; | 417 return; |
| 414 } | 418 } |
| 415 // Check the status of the translation. | 419 // Check the status of the translation. |
| 416 MessageLoop::current()->PostDelayedTask(FROM_HERE, | 420 MessageLoop::current()->PostDelayedTask( |
| 421 FROM_HERE, |
| 417 base::Bind(&TranslateHelper::CheckTranslateStatus, | 422 base::Bind(&TranslateHelper::CheckTranslateStatus, |
| 418 weak_method_factory_.GetWeakPtr()), | 423 weak_method_factory_.GetWeakPtr()), |
| 419 DontDelayTasks() ? 0 : kTranslateStatusCheckDelayMs); | 424 base::TimeDelta::FromMilliseconds( |
| 425 DontDelayTasks() ? 0 : kTranslateStatusCheckDelayMs)); |
| 420 } | 426 } |
| 421 | 427 |
| 422 void TranslateHelper::NotifyBrowserTranslationFailed( | 428 void TranslateHelper::NotifyBrowserTranslationFailed( |
| 423 TranslateErrors::Type error) { | 429 TranslateErrors::Type error) { |
| 424 translation_pending_ = false; | 430 translation_pending_ = false; |
| 425 // Notify the browser there was an error. | 431 // Notify the browser there was an error. |
| 426 render_view()->Send(new ChromeViewHostMsg_PageTranslated( | 432 render_view()->Send(new ChromeViewHostMsg_PageTranslated( |
| 427 render_view()->GetRoutingId(), page_id_, source_lang_, | 433 render_view()->GetRoutingId(), page_id_, source_lang_, |
| 428 target_lang_, error)); | 434 target_lang_, error)); |
| 429 } | 435 } |
| 430 | 436 |
| 431 WebFrame* TranslateHelper::GetMainFrame() { | 437 WebFrame* TranslateHelper::GetMainFrame() { |
| 432 WebView* web_view = render_view()->GetWebView(); | 438 WebView* web_view = render_view()->GetWebView(); |
| 433 if (!web_view) { | 439 if (!web_view) { |
| 434 // When the WebView is going away, the render view should have called | 440 // When the WebView is going away, the render view should have called |
| 435 // CancelPendingTranslation() which should have stopped any pending work, so | 441 // CancelPendingTranslation() which should have stopped any pending work, so |
| 436 // that case should not happen. | 442 // that case should not happen. |
| 437 NOTREACHED(); | 443 NOTREACHED(); |
| 438 return NULL; | 444 return NULL; |
| 439 } | 445 } |
| 440 return web_view->mainFrame(); | 446 return web_view->mainFrame(); |
| 441 } | 447 } |
| OLD | NEW |