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 "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; |
|
Takashi Toyoshima
2014/03/13 18:37:37
paste it mistakenly?
Andrew Hayden (chromium.org)
2014/03/14 10:10:52
Done.
| |
| 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_started = false; | |
| 83 cld2_data_file_polling_canceled = false; | |
| 84 deferred_page_capture_ = false; | |
| 85 deferred_page_id_ = -1; | |
|
palmer
2014/03/13 17:59:50
Put these up above in the : initialization list.
Andrew Hayden (chromium.org)
2014/03/13 23:20:21
Done.
| |
| 86 deferred_contents_ = ASCIIToUTF16(""); | |
| 87 #endif | |
| 75 } | 88 } |
| 76 | 89 |
| 77 TranslateHelper::~TranslateHelper() { | 90 TranslateHelper::~TranslateHelper() { |
| 78 CancelPendingTranslation(); | 91 CancelPendingTranslation(); |
| 92 #if defined(CLD2_DYNAMIC_MODE) | |
| 93 CancelCLD2DataFilePolling(); | |
| 94 #endif | |
| 79 } | 95 } |
| 80 | 96 |
| 97 void TranslateHelper::PrepareForUrl(const GURL& url) { | |
| 98 #if defined(CLD2_DYNAMIC_MODE) | |
|
Takashi Toyoshima
2014/03/13 18:37:37
you may want to reset deferred_* here?
Andrew Hayden (chromium.org)
2014/03/14 10:10:52
Hmm, good thought. It's SAFE to leave it alone, be
| |
| 99 if (cld2_data_file_polling_started) return; | |
|
palmer
2014/03/13 17:59:50
Style nit.
Andrew Hayden (chromium.org)
2014/03/13 23:20:21
Done.
| |
| 100 | |
| 101 // There is no reason to ever bother loading CLD data for an extension URL. | |
| 102 // There's also no render_view_host_observer to answer us, so don't ask for | |
| 103 // CLD data! | |
| 104 if (url.SchemeIs(extensions::kExtensionScheme)) return; | |
|
Takashi Toyoshima
2014/03/13 18:37:37
TranslateManager::IsTranslatableURL() is the funct
Andrew Hayden (chromium.org)
2014/03/14 10:10:52
For clarity I'll submit a separate patchset in thi
| |
| 105 | |
| 106 // If the URL isn't an extension URL, we might conceivably want translation | |
| 107 // capabilities. Start polling for CLD data. | |
| 108 cld2_data_file_polling_started = true; | |
| 109 TranslateHelper::SendCLD2DataFileRequest(0, 1000); | |
| 110 #endif | |
| 111 } | |
| 112 | |
| 113 #if defined(CLD2_DYNAMIC_MODE) | |
| 114 void TranslateHelper::DeferPageCaptured(const int page_id, | |
| 115 const base::string16& contents) { | |
|
palmer
2014/03/13 17:59:50
Nit: indentation.
Takashi Toyoshima
2014/03/13 18:37:37
wrong indent
Andrew Hayden (chromium.org)
2014/03/13 23:20:21
Done.
| |
| 116 deferred_page_capture_ = true; | |
| 117 deferred_page_id_ = page_id; | |
| 118 deferred_contents_.clear(); | |
| 119 deferred_contents_.append(contents); | |
|
Takashi Toyoshima
2014/03/13 18:37:37
Just a question.
Why don't you simply assign it, b
Andrew Hayden (chromium.org)
2014/03/14 10:10:52
I'll assign it instead. I was concerned about the
| |
| 120 } | |
| 121 #endif | |
| 122 | |
| 81 void TranslateHelper::PageCaptured(int page_id, | 123 void TranslateHelper::PageCaptured(int page_id, |
| 82 const base::string16& contents) { | 124 const base::string16& contents) { |
| 83 // Get the document language as set by WebKit from the http-equiv | 125 // Get the document language as set by WebKit from the http-equiv |
| 84 // meta tag for "content-language". This may or may not also | 126 // meta tag for "content-language". This may or may not also |
| 85 // have a value derived from the actual Content-Language HTTP | 127 // have a value derived from the actual Content-Language HTTP |
| 86 // header. The two actually have different meanings (despite the | 128 // header. The two actually have different meanings (despite the |
| 87 // original intent of http-equiv to be an equivalent) with the former | 129 // original intent of http-equiv to be an equivalent) with the former |
| 88 // being the language of the document and the latter being the | 130 // being the language of the document and the latter being the |
| 89 // language of the intended audience (a distinction really only | 131 // language of the intended audience (a distinction really only |
| 90 // relevant for things like langauge textbooks). This distinction | 132 // relevant for things like langauge textbooks). This distinction |
| 91 // shouldn't affect translation. | 133 // shouldn't affect translation. |
| 92 WebFrame* main_frame = GetMainFrame(); | 134 WebFrame* main_frame = GetMainFrame(); |
| 93 if (!main_frame || render_view()->GetPageId() != page_id) | 135 if (!main_frame || render_view()->GetPageId() != page_id) |
| 94 return; | 136 return; |
| 137 | |
| 138 // TODO(andrewhayden): UMA insertion point here: Track if data is available. | |
|
Takashi Toyoshima
2014/03/13 18:37:37
Just a question:
How long time does it take from c
Andrew Hayden (chromium.org)
2014/03/14 10:10:52
I don't have data but I'd be happy to measure it :
| |
| 139 // TODO(andrewhayden): Retry insertion point here, retry till data available. | |
| 140 #if defined(CLD2_DYNAMIC_MODE) | |
| 141 if (!CLD2::isDataLoaded()) { | |
| 142 // We're in dynamic mode and CLD data isn't loaded. Retry when CLD data | |
| 143 // is loaded, if ever. | |
| 144 TranslateHelper::DeferPageCaptured(page_id, contents); | |
| 145 return; | |
| 146 } | |
| 147 #endif | |
| 95 page_id_ = page_id; | 148 page_id_ = page_id; |
| 96 WebDocument document = main_frame->document(); | 149 WebDocument document = main_frame->document(); |
| 97 std::string content_language = document.contentLanguage().utf8(); | 150 std::string content_language = document.contentLanguage().utf8(); |
| 98 WebElement html_element = document.documentElement(); | 151 WebElement html_element = document.documentElement(); |
| 99 std::string html_lang; | 152 std::string html_lang; |
| 100 // |html_element| can be null element, e.g. in | 153 // |html_element| can be null element, e.g. in |
| 101 // BrowserTest.WindowOpenClose. | 154 // BrowserTest.WindowOpenClose. |
| 102 if (!html_element.isNull()) | 155 if (!html_element.isNull()) |
| 103 html_lang = html_element.getAttribute("lang").utf8(); | 156 html_lang = html_element.getAttribute("lang").utf8(); |
| 104 std::string cld_language; | 157 std::string cld_language; |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 129 routing_id(), | 182 routing_id(), |
| 130 details, | 183 details, |
| 131 IsTranslationAllowed(&document) && !language.empty())); | 184 IsTranslationAllowed(&document) && !language.empty())); |
| 132 } | 185 } |
| 133 | 186 |
| 134 void TranslateHelper::CancelPendingTranslation() { | 187 void TranslateHelper::CancelPendingTranslation() { |
| 135 weak_method_factory_.InvalidateWeakPtrs(); | 188 weak_method_factory_.InvalidateWeakPtrs(); |
| 136 translation_pending_ = false; | 189 translation_pending_ = false; |
| 137 source_lang_.clear(); | 190 source_lang_.clear(); |
| 138 target_lang_.clear(); | 191 target_lang_.clear(); |
| 192 #if defined(CLD2_DYNAMIC_MODE) | |
| 193 CancelCLD2DataFilePolling(); | |
| 194 #endif | |
| 139 } | 195 } |
| 140 | 196 |
| 141 //////////////////////////////////////////////////////////////////////////////// | 197 //////////////////////////////////////////////////////////////////////////////// |
| 142 // TranslateHelper, protected: | 198 // TranslateHelper, protected: |
| 143 // | 199 // |
| 144 bool TranslateHelper::IsTranslateLibAvailable() { | 200 bool TranslateHelper::IsTranslateLibAvailable() { |
| 145 return ExecuteScriptAndGetBoolResult( | 201 return ExecuteScriptAndGetBoolResult( |
| 146 "typeof cr != 'undefined' && typeof cr.googleTranslate != 'undefined' && " | 202 "typeof cr != 'undefined' && typeof cr.googleTranslate != 'undefined' && " |
| 147 "typeof cr.googleTranslate.translate == 'function'", false); | 203 "typeof cr.googleTranslate.translate == 'function'", false); |
| 148 } | 204 } |
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 303 return false; | 359 return false; |
| 304 } | 360 } |
| 305 return true; | 361 return true; |
| 306 } | 362 } |
| 307 | 363 |
| 308 bool TranslateHelper::OnMessageReceived(const IPC::Message& message) { | 364 bool TranslateHelper::OnMessageReceived(const IPC::Message& message) { |
| 309 bool handled = true; | 365 bool handled = true; |
| 310 IPC_BEGIN_MESSAGE_MAP(TranslateHelper, message) | 366 IPC_BEGIN_MESSAGE_MAP(TranslateHelper, message) |
| 311 IPC_MESSAGE_HANDLER(ChromeViewMsg_TranslatePage, OnTranslatePage) | 367 IPC_MESSAGE_HANDLER(ChromeViewMsg_TranslatePage, OnTranslatePage) |
| 312 IPC_MESSAGE_HANDLER(ChromeViewMsg_RevertTranslation, OnRevertTranslation) | 368 IPC_MESSAGE_HANDLER(ChromeViewMsg_RevertTranslation, OnRevertTranslation) |
| 369 #if defined(CLD2_DYNAMIC_MODE) | |
| 370 IPC_MESSAGE_HANDLER(ChromeViewMsg_CLDDataAvailable, OnCLDDataAvailable); | |
| 371 #endif | |
| 313 IPC_MESSAGE_UNHANDLED(handled = false) | 372 IPC_MESSAGE_UNHANDLED(handled = false) |
| 314 IPC_END_MESSAGE_MAP() | 373 IPC_END_MESSAGE_MAP() |
| 315 return handled; | 374 return handled; |
| 316 } | 375 } |
| 317 | 376 |
| 318 void TranslateHelper::OnTranslatePage(int page_id, | 377 void TranslateHelper::OnTranslatePage(int page_id, |
| 319 const std::string& translate_script, | 378 const std::string& translate_script, |
| 320 const std::string& source_lang, | 379 const std::string& source_lang, |
| 321 const std::string& target_lang) { | 380 const std::string& target_lang) { |
| 322 WebFrame* main_frame = GetMainFrame(); | 381 WebFrame* main_frame = GetMainFrame(); |
| (...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 492 | 551 |
| 493 WebFrame* TranslateHelper::GetMainFrame() { | 552 WebFrame* TranslateHelper::GetMainFrame() { |
| 494 WebView* web_view = render_view()->GetWebView(); | 553 WebView* web_view = render_view()->GetWebView(); |
| 495 | 554 |
| 496 // When the tab is going to be closed, the web_view can be NULL. | 555 // When the tab is going to be closed, the web_view can be NULL. |
| 497 if (!web_view) | 556 if (!web_view) |
| 498 return NULL; | 557 return NULL; |
| 499 | 558 |
| 500 return web_view->mainFrame(); | 559 return web_view->mainFrame(); |
| 501 } | 560 } |
| 561 | |
| 562 #if defined(CLD2_DYNAMIC_MODE) | |
| 563 void TranslateHelper::CancelCLD2DataFilePolling() { | |
| 564 cld2_data_file_polling_canceled = true; | |
| 565 } | |
| 566 | |
| 567 void TranslateHelper::SendCLD2DataFileRequest(int delay_millis, | |
| 568 int next_delay_millis) { | |
| 569 // We terminate immediately if we've been told to stop polling | |
| 570 if (cld2_data_file_polling_canceled) return; | |
|
palmer
2014/03/13 17:59:50
Style (everywhere this occurs).
Andrew Hayden (chromium.org)
2014/03/13 23:20:21
Done.
| |
| 571 | |
| 572 // We terminate immediately if we've already loaded the data. | |
| 573 if (CLD2::isDataLoaded()) return; | |
| 574 | |
| 575 // Else, send the IPC message to the browser process requesting the data... | |
| 576 Send(new ChromeViewHostMsg_NeedCLDData(routing_id())); | |
| 577 | |
| 578 // ... and enqueue another delayed task to call again. This will start a | |
| 579 // chain of polling that will last until the pointer stops being null, | |
| 580 // which is the right thing to do. | |
| 581 // NB: In the great majority of cases, the data file will be available and | |
| 582 // the very first delayed task will be a no-op that terminates the chain. | |
| 583 // It's only while downloading the file that we expect this to chain for a | |
| 584 // nontrivial amount of time. | |
| 585 // Use a weak pointer so that we don't keep this helper object around forever. | |
| 586 base::MessageLoop::current()->PostDelayedTask( | |
| 587 FROM_HERE, | |
| 588 base::Bind(&TranslateHelper::SendCLD2DataFileRequest, | |
| 589 weak_method_factory_.GetWeakPtr(), | |
| 590 next_delay_millis, next_delay_millis), | |
| 591 base::TimeDelta::FromMilliseconds(delay_millis)); | |
| 592 } | |
| 593 | |
| 594 void TranslateHelper::OnCLDDataAvailable( | |
| 595 IPC::PlatformFileForTransit ipc_file_handle) { | |
| 596 LoadCLDDData(ipc_file_handle); | |
| 597 if (deferred_page_capture_) { | |
| 598 if (CLD2::isDataLoaded()) { | |
| 599 // We have data available now, and we previously deferred a request to | |
| 600 // start translation. Finish that request up and clear our state. | |
| 601 // The real situation this translates to is as follows: | |
| 602 // The user loaded a page before the CLD data was available - either | |
| 603 // because the browser process took too long to respond to our CLD data | |
| 604 // request, or because the data truly hasn't been available; | |
| 605 // The data is now available, so we should get that language check | |
| 606 // done and post the message back to the browser ASAP. | |
| 607 deferred_page_capture_ = false; // Don't do this a second time. | |
| 608 PageCaptured(deferred_page_id_, deferred_contents_); | |
| 609 deferred_page_id_ = -1; // clean up for sanity | |
| 610 deferred_contents_.clear(); // clean up for sanity | |
| 611 } | |
| 612 } | |
| 613 } | |
| 614 | |
| 615 void TranslateHelper::LoadCLDDData( | |
| 616 IPC::PlatformFileForTransit ipc_file_handle) { | |
| 617 // We terminate immediately if we've been told to stop polling | |
| 618 if (cld2_data_file_polling_canceled) return; | |
| 619 | |
| 620 // We terminate immediately if we've already loaded the data. | |
| 621 if (CLD2::isDataLoaded()) return; | |
| 622 | |
| 623 // The mmap must outlive this function and must not be destroyed because | |
| 624 // the destructor for mmap will unmap the memory segment and close the file | |
| 625 // handle. Thus, declare it static. | |
| 626 static base::MemoryMappedFile* mmap = NULL; | |
| 627 DCHECK(mmap != NULL) << "Lost CLD mmap!"; // Should be impossible! | |
| 628 | |
| 629 // Grab the file handle | |
| 630 base::PlatformFile platformFile = | |
| 631 IPC::PlatformFileForTransitToPlatformFile(ipc_file_handle); | |
| 632 DCHECK(platformFile > 0) << "Can't find the CLD data file!"; | |
| 633 base::File basicFile(platformFile); | |
| 634 | |
| 635 // mmap the file | |
| 636 mmap = new base::MemoryMappedFile(); | |
| 637 bool loadedOk = mmap->Initialize(basicFile.Pass()); | |
| 638 DCHECK(loadedOk) << "Failed to initialize CLD data mmap"; | |
| 639 if (!loadedOk) { | |
| 640 delete mmap; | |
| 641 mmap = NULL; | |
| 642 return; | |
| 643 } | |
| 644 | |
| 645 // Initialize the CLD subsystem... we're done! | |
| 646 CLD2::loadDataFromRawAddress(mmap->data(), mmap->length()); | |
| 647 DCHECK(CLD2::isDataLoaded()) << "Failed to load CLD data from mmap"; | |
| 648 } | |
| 649 #endif | |
| OLD | NEW |