| 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 "chrome/renderer/translate/translate_helper.h" | 5 #include "chrome/renderer/translate/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/files/memory_mapped_file.h" |
| 9 #include "base/logging.h" | 10 #include "base/logging.h" |
| 10 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
| 11 #include "base/strings/string16.h" | 12 #include "base/strings/string16.h" |
| 12 #include "base/strings/string_util.h" | 13 #include "base/strings/string_util.h" |
| 13 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
| 14 #include "chrome/common/render_messages.h" | 15 #include "chrome/common/render_messages.h" |
| 15 #include "chrome/renderer/extensions/extension_groups.h" | 16 #include "chrome/renderer/extensions/extension_groups.h" |
| 16 #include "chrome/renderer/isolated_world_ids.h" | 17 #include "chrome/renderer/isolated_world_ids.h" |
| 17 #include "components/translate/core/common/translate_constants.h" | 18 #include "components/translate/core/common/translate_constants.h" |
| 18 #include "components/translate/core/common/translate_metrics.h" | 19 #include "components/translate/core/common/translate_metrics.h" |
| 19 #include "components/translate/core/common/translate_util.h" | 20 #include "components/translate/core/common/translate_util.h" |
| 20 #include "components/translate/language_detection/language_detection_util.h" | 21 #include "components/translate/language_detection/language_detection_util.h" |
| 21 #include "content/public/renderer/render_view.h" | 22 #include "content/public/renderer/render_view.h" |
| 23 #include "extensions/common/constants.h" |
| 24 #include "ipc/ipc_platform_file.h" |
| 25 #if defined(CLD2_DYNAMIC_MODE) |
| 26 #include "third_party/cld_2/src/public/compact_lang_det.h" |
| 27 #endif |
| 22 #include "third_party/WebKit/public/web/WebDocument.h" | 28 #include "third_party/WebKit/public/web/WebDocument.h" |
| 23 #include "third_party/WebKit/public/web/WebElement.h" | 29 #include "third_party/WebKit/public/web/WebElement.h" |
| 24 #include "third_party/WebKit/public/web/WebFrame.h" | 30 #include "third_party/WebKit/public/web/WebFrame.h" |
| 25 #include "third_party/WebKit/public/web/WebNode.h" | 31 #include "third_party/WebKit/public/web/WebNode.h" |
| 26 #include "third_party/WebKit/public/web/WebNodeList.h" | 32 #include "third_party/WebKit/public/web/WebNodeList.h" |
| 27 #include "third_party/WebKit/public/web/WebScriptSource.h" | 33 #include "third_party/WebKit/public/web/WebScriptSource.h" |
| 28 #include "third_party/WebKit/public/web/WebView.h" | 34 #include "third_party/WebKit/public/web/WebView.h" |
| 29 #include "third_party/WebKit/public/web/WebWidget.h" | 35 #include "third_party/WebKit/public/web/WebWidget.h" |
| 30 #include "url/gurl.h" | 36 #include "url/gurl.h" |
| 31 #include "v8/include/v8.h" | 37 #include "v8/include/v8.h" |
| (...skipping 14 matching lines...) Expand all Loading... |
| 46 | 52 |
| 47 // The delay in milliseconds that we'll wait before checking to see if the | 53 // The delay in milliseconds that we'll wait before checking to see if the |
| 48 // translate library injected in the page is ready. | 54 // translate library injected in the page is ready. |
| 49 const int kTranslateInitCheckDelayMs = 150; | 55 const int kTranslateInitCheckDelayMs = 150; |
| 50 | 56 |
| 51 // The maximum number of times we'll check to see if the translate library | 57 // The maximum number of times we'll check to see if the translate library |
| 52 // injected in the page is ready. | 58 // injected in the page is ready. |
| 53 const int kMaxTranslateInitCheckAttempts = 5; | 59 const int kMaxTranslateInitCheckAttempts = 5; |
| 54 | 60 |
| 55 // The delay we wait in milliseconds before checking whether the translation has | 61 // The delay we wait in milliseconds before checking whether the translation has |
| 56 // finished. | 62 // finished.cld2_data_file = NULL; |
| 57 const int kTranslateStatusCheckDelayMs = 400; | 63 const int kTranslateStatusCheckDelayMs = 400; |
| 58 | 64 |
| 59 // Language name passed to the Translate element for it to detect the language. | 65 // Language name passed to the Translate element for it to detect the language. |
| 60 const char kAutoDetectionLanguage[] = "auto"; | 66 const char kAutoDetectionLanguage[] = "auto"; |
| 61 | 67 |
| 62 // Isolated world sets following content-security-policy. | 68 // Isolated world sets following content-security-policy. |
| 63 const char kContentSecurityPolicy[] = "script-src 'self' 'unsafe-eval'"; | 69 const char kContentSecurityPolicy[] = "script-src 'self' 'unsafe-eval'"; |
| 64 | 70 |
| 65 } // namespace | 71 } // namespace |
| 66 | 72 |
| 67 //////////////////////////////////////////////////////////////////////////////// | 73 //////////////////////////////////////////////////////////////////////////////// |
| 68 // TranslateHelper, public: | 74 // TranslateHelper, public: |
| 69 // | 75 // |
| 70 TranslateHelper::TranslateHelper(content::RenderView* render_view) | 76 TranslateHelper::TranslateHelper(content::RenderView* render_view) |
| 71 : content::RenderViewObserver(render_view), | 77 : content::RenderViewObserver(render_view), |
| 72 page_id_(-1), | 78 page_id_(-1), |
| 73 translation_pending_(false), | 79 translation_pending_(false), |
| 74 weak_method_factory_(this) { | 80 weak_method_factory_(this) { |
| 81 #if defined(CLD2_DYNAMIC_MODE) |
| 82 cld2_data_file_polling_canceled = false; |
| 83 deferred_page_capture_ = false; |
| 84 deferred_page_id_ = -1; |
| 85 deferred_contents_ = ASCIIToUTF16(""); |
| 86 #endif |
| 75 } | 87 } |
| 76 | 88 |
| 77 TranslateHelper::~TranslateHelper() { | 89 TranslateHelper::~TranslateHelper() { |
| 78 CancelPendingTranslation(); | 90 CancelPendingTranslation(); |
| 91 #if defined(CLD2_DYNAMIC_MODE) |
| 92 CancelCLD2DataFilePolling(); |
| 93 #endif |
| 79 } | 94 } |
| 80 | 95 |
| 96 void TranslateHelper::PrepareForUrl(const GURL& url) { |
| 97 #if defined(CLD2_DYNAMIC_MODE) |
| 98 // Try to load the CLD data if we haven't already done so. |
| 99 static bool startedTranslationPolling = false; |
| 100 if (startedTranslationPolling) return; |
| 101 |
| 102 // There is no reason to ever bother loading CLD data for an extension URL. |
| 103 // There's also no render_view_host_observer to answer us, so don't ask for |
| 104 // CLD data! |
| 105 if (url.SchemeIs(extensions::kExtensionScheme)) return; |
| 106 |
| 107 // If the URL isn't an extension URL, we might conceivably want translation |
| 108 // capabilities. Start polling for CLD data. |
| 109 startedTranslationPolling = true; |
| 110 TranslateHelper::SendCLD2DataFileRequest(0, 1000); |
| 111 #endif |
| 112 } |
| 113 |
| 114 #if defined(CLD2_DYNAMIC_MODE) |
| 115 void TranslateHelper::DeferPageCaptured(const int page_id, |
| 116 const base::string16& contents) { |
| 117 deferred_page_capture_ = true; |
| 118 deferred_page_id_ = page_id; |
| 119 deferred_contents_.clear(); |
| 120 deferred_contents_.append(contents); |
| 121 } |
| 122 #endif |
| 123 |
| 81 void TranslateHelper::PageCaptured(int page_id, | 124 void TranslateHelper::PageCaptured(int page_id, |
| 82 const base::string16& contents) { | 125 const base::string16& contents) { |
| 83 // Get the document language as set by WebKit from the http-equiv | 126 // Get the document language as set by WebKit from the http-equiv |
| 84 // meta tag for "content-language". This may or may not also | 127 // meta tag for "content-language". This may or may not also |
| 85 // have a value derived from the actual Content-Language HTTP | 128 // have a value derived from the actual Content-Language HTTP |
| 86 // header. The two actually have different meanings (despite the | 129 // header. The two actually have different meanings (despite the |
| 87 // original intent of http-equiv to be an equivalent) with the former | 130 // original intent of http-equiv to be an equivalent) with the former |
| 88 // being the language of the document and the latter being the | 131 // being the language of the document and the latter being the |
| 89 // language of the intended audience (a distinction really only | 132 // language of the intended audience (a distinction really only |
| 90 // relevant for things like langauge textbooks). This distinction | 133 // relevant for things like langauge textbooks). This distinction |
| 91 // shouldn't affect translation. | 134 // shouldn't affect translation. |
| 92 WebFrame* main_frame = GetMainFrame(); | 135 WebFrame* main_frame = GetMainFrame(); |
| 93 if (!main_frame || render_view()->GetPageId() != page_id) | 136 if (!main_frame || render_view()->GetPageId() != page_id) |
| 94 return; | 137 return; |
| 138 |
| 139 // TODO(andrewhayden): UMA insertion point here: Track if data is available. |
| 140 // TODO(andrewhayden): Retry insertion point here, retry till data available. |
| 141 #if defined(CLD2_DYNAMIC_MODE) |
| 142 if (!CLD2::isDataLoaded()) { |
| 143 // We're in dynamic mode and CLD data isn't loaded. Retry when CLD data |
| 144 // is loaded, if ever. |
| 145 TranslateHelper::DeferPageCaptured(page_id, contents); |
| 146 return; |
| 147 } |
| 148 #endif |
| 95 page_id_ = page_id; | 149 page_id_ = page_id; |
| 96 WebDocument document = main_frame->document(); | 150 WebDocument document = main_frame->document(); |
| 97 std::string content_language = document.contentLanguage().utf8(); | 151 std::string content_language = document.contentLanguage().utf8(); |
| 98 WebElement html_element = document.documentElement(); | 152 WebElement html_element = document.documentElement(); |
| 99 std::string html_lang; | 153 std::string html_lang; |
| 100 // |html_element| can be null element, e.g. in | 154 // |html_element| can be null element, e.g. in |
| 101 // BrowserTest.WindowOpenClose. | 155 // BrowserTest.WindowOpenClose. |
| 102 if (!html_element.isNull()) | 156 if (!html_element.isNull()) |
| 103 html_lang = html_element.getAttribute("lang").utf8(); | 157 html_lang = html_element.getAttribute("lang").utf8(); |
| 104 std::string cld_language; | 158 std::string cld_language; |
| (...skipping 24 matching lines...) Expand all Loading... |
| 129 routing_id(), | 183 routing_id(), |
| 130 details, | 184 details, |
| 131 IsTranslationAllowed(&document) && !language.empty())); | 185 IsTranslationAllowed(&document) && !language.empty())); |
| 132 } | 186 } |
| 133 | 187 |
| 134 void TranslateHelper::CancelPendingTranslation() { | 188 void TranslateHelper::CancelPendingTranslation() { |
| 135 weak_method_factory_.InvalidateWeakPtrs(); | 189 weak_method_factory_.InvalidateWeakPtrs(); |
| 136 translation_pending_ = false; | 190 translation_pending_ = false; |
| 137 source_lang_.clear(); | 191 source_lang_.clear(); |
| 138 target_lang_.clear(); | 192 target_lang_.clear(); |
| 193 #if defined(CLD2_DYNAMIC_MODE) |
| 194 CancelCLD2DataFilePolling(); |
| 195 #endif |
| 139 } | 196 } |
| 140 | 197 |
| 141 //////////////////////////////////////////////////////////////////////////////// | 198 //////////////////////////////////////////////////////////////////////////////// |
| 142 // TranslateHelper, protected: | 199 // TranslateHelper, protected: |
| 143 // | 200 // |
| 144 bool TranslateHelper::IsTranslateLibAvailable() { | 201 bool TranslateHelper::IsTranslateLibAvailable() { |
| 145 return ExecuteScriptAndGetBoolResult( | 202 return ExecuteScriptAndGetBoolResult( |
| 146 "typeof cr != 'undefined' && typeof cr.googleTranslate != 'undefined' && " | 203 "typeof cr != 'undefined' && typeof cr.googleTranslate != 'undefined' && " |
| 147 "typeof cr.googleTranslate.translate == 'function'", false); | 204 "typeof cr.googleTranslate.translate == 'function'", false); |
| 148 } | 205 } |
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 303 return false; | 360 return false; |
| 304 } | 361 } |
| 305 return true; | 362 return true; |
| 306 } | 363 } |
| 307 | 364 |
| 308 bool TranslateHelper::OnMessageReceived(const IPC::Message& message) { | 365 bool TranslateHelper::OnMessageReceived(const IPC::Message& message) { |
| 309 bool handled = true; | 366 bool handled = true; |
| 310 IPC_BEGIN_MESSAGE_MAP(TranslateHelper, message) | 367 IPC_BEGIN_MESSAGE_MAP(TranslateHelper, message) |
| 311 IPC_MESSAGE_HANDLER(ChromeViewMsg_TranslatePage, OnTranslatePage) | 368 IPC_MESSAGE_HANDLER(ChromeViewMsg_TranslatePage, OnTranslatePage) |
| 312 IPC_MESSAGE_HANDLER(ChromeViewMsg_RevertTranslation, OnRevertTranslation) | 369 IPC_MESSAGE_HANDLER(ChromeViewMsg_RevertTranslation, OnRevertTranslation) |
| 370 #if defined(CLD2_DYNAMIC_MODE) |
| 371 IPC_MESSAGE_HANDLER(ChromeViewMsg_CLDDataAvailable, OnCLDDataAvailable); |
| 372 #endif |
| 313 IPC_MESSAGE_UNHANDLED(handled = false) | 373 IPC_MESSAGE_UNHANDLED(handled = false) |
| 314 IPC_END_MESSAGE_MAP() | 374 IPC_END_MESSAGE_MAP() |
| 315 return handled; | 375 return handled; |
| 316 } | 376 } |
| 317 | 377 |
| 318 void TranslateHelper::OnTranslatePage(int page_id, | 378 void TranslateHelper::OnTranslatePage(int page_id, |
| 319 const std::string& translate_script, | 379 const std::string& translate_script, |
| 320 const std::string& source_lang, | 380 const std::string& source_lang, |
| 321 const std::string& target_lang) { | 381 const std::string& target_lang) { |
| 322 WebFrame* main_frame = GetMainFrame(); | 382 WebFrame* main_frame = GetMainFrame(); |
| (...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 492 | 552 |
| 493 WebFrame* TranslateHelper::GetMainFrame() { | 553 WebFrame* TranslateHelper::GetMainFrame() { |
| 494 WebView* web_view = render_view()->GetWebView(); | 554 WebView* web_view = render_view()->GetWebView(); |
| 495 | 555 |
| 496 // When the tab is going to be closed, the web_view can be NULL. | 556 // When the tab is going to be closed, the web_view can be NULL. |
| 497 if (!web_view) | 557 if (!web_view) |
| 498 return NULL; | 558 return NULL; |
| 499 | 559 |
| 500 return web_view->mainFrame(); | 560 return web_view->mainFrame(); |
| 501 } | 561 } |
| 562 |
| 563 #if defined(CLD2_DYNAMIC_MODE) |
| 564 void TranslateHelper::CancelCLD2DataFilePolling() { |
| 565 cld2_data_file_polling_canceled = true; |
| 566 } |
| 567 |
| 568 void TranslateHelper::SendCLD2DataFileRequest(int delayMillis, |
| 569 int nextDelayMillis) { |
| 570 // We terminate immediately if we've been told to stop polling |
| 571 if (cld2_data_file_polling_canceled) return; |
| 572 |
| 573 // We terminate immediately if we've already loaded the data. |
| 574 if (CLD2::isDataLoaded()) return; |
| 575 |
| 576 // Else, send the IPC message to the browser process requesting the data... |
| 577 Send(new ChromeViewHostMsg_NeedCLDData(routing_id())); |
| 578 |
| 579 // ... and enqueue another delayed task to call again. This will start a |
| 580 // chain of polling that will last until the pointer stops being null, |
| 581 // which is the right thing to do. |
| 582 // NB: In the great majority of cases, the data file will be available and |
| 583 // the very first delayed task will be a no-op that terminates the chain. |
| 584 // It's only while downloading the file that we expect this to chain for a |
| 585 // nontrivial amount of time. |
| 586 // Use a weak pointer so that we don't keep this helper object around forever. |
| 587 base::MessageLoop::current()->PostDelayedTask( |
| 588 FROM_HERE, |
| 589 base::Bind(&TranslateHelper::SendCLD2DataFileRequest, |
| 590 weak_method_factory_.GetWeakPtr(), |
| 591 nextDelayMillis, nextDelayMillis), |
| 592 base::TimeDelta::FromMilliseconds(nextDelayMillis)); |
| 593 } |
| 594 |
| 595 void TranslateHelper::OnCLDDataAvailable( |
| 596 IPC::PlatformFileForTransit ipc_file_handle) { |
| 597 LoadCLDDData(ipc_file_handle); |
| 598 if (deferred_page_capture_) { |
| 599 if (CLD2::isDataLoaded()) { |
| 600 // We have data available now, and we previously deferred a request to |
| 601 // start translation. Finish that request up and clear our state. |
| 602 // The real situation this translates to is as follows: |
| 603 // The user loaded a page before the CLD data was available - either |
| 604 // because the browser process took too long to respond to our CLD data |
| 605 // request, or because the data truly hasn't been available; |
| 606 // The data is now available, so we should get that language check |
| 607 // done and post the message back to the browser ASAP. |
| 608 deferred_page_capture_ = false; // Don't do this a second time. |
| 609 PageCaptured(deferred_page_id_, deferred_contents_); |
| 610 deferred_page_id_ = -1; // clean up for sanity |
| 611 deferred_contents_.clear(); // clean up for sanity |
| 612 } |
| 613 } |
| 614 } |
| 615 |
| 616 void TranslateHelper::LoadCLDDData( |
| 617 IPC::PlatformFileForTransit ipc_file_handle) { |
| 618 // We terminate immediately if we've been told to stop polling |
| 619 if (cld2_data_file_polling_canceled) return; |
| 620 |
| 621 // We terminate immediately if we've already loaded the data. |
| 622 if (CLD2::isDataLoaded()) return; |
| 623 |
| 624 // The mmap must outlive this function and must not be destroyed because |
| 625 // the destructor for mmap will unmap the memory segment and close the file |
| 626 // handle. Thus, declare it static. |
| 627 static base::MemoryMappedFile* mmap = NULL; |
| 628 DCHECK(mmap != NULL) << "Lost CLD mmap!"; // Should be impossible! |
| 629 |
| 630 // Grab the file handle |
| 631 base::PlatformFile platformFile = |
| 632 IPC::PlatformFileForTransitToPlatformFile(ipc_file_handle); |
| 633 DCHECK(platformFile > 0) << "Can't find the CLD data file!"; |
| 634 base::File basicFile(platformFile); |
| 635 |
| 636 // mmap the file |
| 637 mmap = new base::MemoryMappedFile(); |
| 638 bool loadedOk = mmap->Initialize(basicFile.Pass()); |
| 639 DCHECK(loadedOk) << "Failed to initialize CLD data mmap"; |
| 640 if (!loadedOk) { |
| 641 delete mmap; |
| 642 mmap = NULL; |
| 643 return; |
| 644 } |
| 645 |
| 646 // Initialize the CLD subsystem... we're done! |
| 647 CLD2::loadDataFromRawAddress(mmap->data(), mmap->length()); |
| 648 DCHECK(CLD2::isDataLoaded()) << "Failed to load CLD data from mmap"; |
| 649 } |
| 650 #endif |
| OLD | NEW |