| OLD | NEW |
| 1 // Copyright (c) 2012 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" |
| 11 #include "base/utf_string_conversions.h" | 11 #include "base/utf_string_conversions.h" |
| 12 #include "chrome/common/chrome_constants.h" | 12 #include "chrome/common/chrome_constants.h" |
| 13 #include "chrome/common/render_messages.h" | 13 #include "chrome/common/render_messages.h" |
| 14 #include "content/public/renderer/render_view.h" | 14 #include "content/public/renderer/render_view.h" |
| 15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" | 15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" |
| 16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h" | 16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h" |
| 17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
| 18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebScriptSource.h" | 18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebScriptSource.h" |
| 19 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" | 19 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" |
| 20 #include "third_party/cld/encodings/compact_lang_det/win/cld_unicodetext.h" | 20 #include "third_party/cld/encodings/compact_lang_det/win/cld_unicodetext.h" |
| 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 milliseconds that we'll wait before checking to see if the | 31 // The delay in millliseconds 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( | 342 MessageLoop::current()->PostDelayedTask(FROM_HERE, |
| 343 FROM_HERE, | |
| 344 base::Bind(&TranslateHelper::CheckTranslateStatus, | 343 base::Bind(&TranslateHelper::CheckTranslateStatus, |
| 345 weak_method_factory_.GetWeakPtr()), | 344 weak_method_factory_.GetWeakPtr()), |
| 346 base::TimeDelta::FromMilliseconds( | 345 DontDelayTasks() ? 0 : kTranslateStatusCheckDelayMs); |
| 347 DontDelayTasks() ? 0 : kTranslateStatusCheckDelayMs)); | |
| 348 } | 346 } |
| 349 | 347 |
| 350 bool TranslateHelper::ExecuteScript(const std::string& script) { | 348 bool TranslateHelper::ExecuteScript(const std::string& script) { |
| 351 WebFrame* main_frame = GetMainFrame(); | 349 WebFrame* main_frame = GetMainFrame(); |
| 352 if (!main_frame) | 350 if (!main_frame) |
| 353 return false; | 351 return false; |
| 354 main_frame->executeScript(WebScriptSource(ASCIIToUTF16(script))); | 352 main_frame->executeScript(WebScriptSource(ASCIIToUTF16(script))); |
| 355 return true; | 353 return true; |
| 356 } | 354 } |
| 357 | 355 |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 396 if (page_id_ != render_view()->GetPageId() || !render_view()->GetWebView()) | 394 if (page_id_ != render_view()->GetPageId() || !render_view()->GetWebView()) |
| 397 return; | 395 return; |
| 398 | 396 |
| 399 if (!IsTranslateLibReady()) { | 397 if (!IsTranslateLibReady()) { |
| 400 // The library is not ready, try again later, unless we have tried several | 398 // The library is not ready, try again later, unless we have tried several |
| 401 // times unsucessfully already. | 399 // times unsucessfully already. |
| 402 if (++count >= kMaxTranslateInitCheckAttempts) { | 400 if (++count >= kMaxTranslateInitCheckAttempts) { |
| 403 NotifyBrowserTranslationFailed(TranslateErrors::INITIALIZATION_ERROR); | 401 NotifyBrowserTranslationFailed(TranslateErrors::INITIALIZATION_ERROR); |
| 404 return; | 402 return; |
| 405 } | 403 } |
| 406 MessageLoop::current()->PostDelayedTask( | 404 MessageLoop::current()->PostDelayedTask(FROM_HERE, |
| 407 FROM_HERE, | |
| 408 base::Bind(&TranslateHelper::TranslatePageImpl, | 405 base::Bind(&TranslateHelper::TranslatePageImpl, |
| 409 weak_method_factory_.GetWeakPtr(), count), | 406 weak_method_factory_.GetWeakPtr(), count), |
| 410 base::TimeDelta::FromMilliseconds( | 407 DontDelayTasks() ? 0 : count * kTranslateInitCheckDelayMs); |
| 411 DontDelayTasks() ? 0 : count * kTranslateInitCheckDelayMs)); | |
| 412 return; | 408 return; |
| 413 } | 409 } |
| 414 | 410 |
| 415 if (!StartTranslation()) { | 411 if (!StartTranslation()) { |
| 416 NotifyBrowserTranslationFailed(TranslateErrors::TRANSLATION_ERROR); | 412 NotifyBrowserTranslationFailed(TranslateErrors::TRANSLATION_ERROR); |
| 417 return; | 413 return; |
| 418 } | 414 } |
| 419 // Check the status of the translation. | 415 // Check the status of the translation. |
| 420 MessageLoop::current()->PostDelayedTask( | 416 MessageLoop::current()->PostDelayedTask(FROM_HERE, |
| 421 FROM_HERE, | |
| 422 base::Bind(&TranslateHelper::CheckTranslateStatus, | 417 base::Bind(&TranslateHelper::CheckTranslateStatus, |
| 423 weak_method_factory_.GetWeakPtr()), | 418 weak_method_factory_.GetWeakPtr()), |
| 424 base::TimeDelta::FromMilliseconds( | 419 DontDelayTasks() ? 0 : kTranslateStatusCheckDelayMs); |
| 425 DontDelayTasks() ? 0 : kTranslateStatusCheckDelayMs)); | |
| 426 } | 420 } |
| 427 | 421 |
| 428 void TranslateHelper::NotifyBrowserTranslationFailed( | 422 void TranslateHelper::NotifyBrowserTranslationFailed( |
| 429 TranslateErrors::Type error) { | 423 TranslateErrors::Type error) { |
| 430 translation_pending_ = false; | 424 translation_pending_ = false; |
| 431 // Notify the browser there was an error. | 425 // Notify the browser there was an error. |
| 432 render_view()->Send(new ChromeViewHostMsg_PageTranslated( | 426 render_view()->Send(new ChromeViewHostMsg_PageTranslated( |
| 433 render_view()->GetRoutingId(), page_id_, source_lang_, | 427 render_view()->GetRoutingId(), page_id_, source_lang_, |
| 434 target_lang_, error)); | 428 target_lang_, error)); |
| 435 } | 429 } |
| 436 | 430 |
| 437 WebFrame* TranslateHelper::GetMainFrame() { | 431 WebFrame* TranslateHelper::GetMainFrame() { |
| 438 WebView* web_view = render_view()->GetWebView(); | 432 WebView* web_view = render_view()->GetWebView(); |
| 439 if (!web_view) { | 433 if (!web_view) { |
| 440 // When the WebView is going away, the render view should have called | 434 // When the WebView is going away, the render view should have called |
| 441 // CancelPendingTranslation() which should have stopped any pending work, so | 435 // CancelPendingTranslation() which should have stopped any pending work, so |
| 442 // that case should not happen. | 436 // that case should not happen. |
| 443 NOTREACHED(); | 437 NOTREACHED(); |
| 444 return NULL; | 438 return NULL; |
| 445 } | 439 } |
| 446 return web_view->mainFrame(); | 440 return web_view->mainFrame(); |
| 447 } | 441 } |
| OLD | NEW |