Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(288)

Side by Side Diff: chrome/renderer/translate/translate_helper.cc

Issue 333603002: Modularize Compact Language Detector 2 (CLD2) data sources (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: merge https://codereview.chromium.org/326383005 Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chrome/renderer/translate/translate_helper.h ('k') | components/translate.gypi » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 #if defined(CLD2_DYNAMIC_MODE)
8 #include <stdint.h>
9 #endif
10
11 #include "base/bind.h" 7 #include "base/bind.h"
12 #include "base/compiler_specific.h" 8 #include "base/compiler_specific.h"
13 #if defined(CLD2_DYNAMIC_MODE)
14 #include "base/files/memory_mapped_file.h"
15 #endif
16 #include "base/logging.h" 9 #include "base/logging.h"
17 #include "base/message_loop/message_loop.h" 10 #include "base/message_loop/message_loop.h"
18 #include "base/strings/string16.h" 11 #include "base/strings/string16.h"
19 #include "base/strings/string_util.h" 12 #include "base/strings/string_util.h"
20 #include "base/strings/utf_string_conversions.h" 13 #include "base/strings/utf_string_conversions.h"
21 #include "chrome/renderer/isolated_world_ids.h" 14 #include "chrome/renderer/isolated_world_ids.h"
22 #include "components/translate/content/common/translate_messages.h" 15 #include "components/translate/content/common/translate_messages.h"
23 #include "components/translate/core/common/translate_constants.h" 16 #include "components/translate/core/common/translate_constants.h"
24 #include "components/translate/core/common/translate_metrics.h" 17 #include "components/translate/core/common/translate_metrics.h"
25 #include "components/translate/core/common/translate_util.h" 18 #include "components/translate/core/common/translate_util.h"
26 #include "components/translate/core/language_detection/language_detection_util.h " 19 #include "components/translate/core/language_detection/language_detection_util.h "
27 #include "content/public/renderer/render_view.h" 20 #include "content/public/renderer/render_view.h"
28 #include "extensions/common/constants.h" 21 #include "extensions/common/constants.h"
29 #include "extensions/renderer/extension_groups.h" 22 #include "extensions/renderer/extension_groups.h"
30 #include "ipc/ipc_platform_file.h" 23 #include "ipc/ipc_platform_file.h"
31 #if defined(CLD2_DYNAMIC_MODE)
32 #include "content/public/common/url_constants.h" 24 #include "content/public/common/url_constants.h"
33 #include "third_party/cld_2/src/public/compact_lang_det.h"
34 #endif
35 #include "third_party/WebKit/public/web/WebDocument.h" 25 #include "third_party/WebKit/public/web/WebDocument.h"
36 #include "third_party/WebKit/public/web/WebElement.h" 26 #include "third_party/WebKit/public/web/WebElement.h"
37 #include "third_party/WebKit/public/web/WebFrame.h" 27 #include "third_party/WebKit/public/web/WebFrame.h"
38 #include "third_party/WebKit/public/web/WebNode.h" 28 #include "third_party/WebKit/public/web/WebNode.h"
39 #include "third_party/WebKit/public/web/WebNodeList.h" 29 #include "third_party/WebKit/public/web/WebNodeList.h"
40 #include "third_party/WebKit/public/web/WebScriptSource.h" 30 #include "third_party/WebKit/public/web/WebScriptSource.h"
41 #include "third_party/WebKit/public/web/WebView.h" 31 #include "third_party/WebKit/public/web/WebView.h"
42 #include "third_party/WebKit/public/web/WebWidget.h" 32 #include "third_party/WebKit/public/web/WebWidget.h"
43 #include "url/gurl.h" 33 #include "url/gurl.h"
44 #include "v8/include/v8.h" 34 #include "v8/include/v8.h"
(...skipping 23 matching lines...) Expand all
68 // The delay we wait in milliseconds before checking whether the translation has 58 // The delay we wait in milliseconds before checking whether the translation has
69 // finished. 59 // finished.
70 const int kTranslateStatusCheckDelayMs = 400; 60 const int kTranslateStatusCheckDelayMs = 400;
71 61
72 // Language name passed to the Translate element for it to detect the language. 62 // Language name passed to the Translate element for it to detect the language.
73 const char kAutoDetectionLanguage[] = "auto"; 63 const char kAutoDetectionLanguage[] = "auto";
74 64
75 // Isolated world sets following content-security-policy. 65 // Isolated world sets following content-security-policy.
76 const char kContentSecurityPolicy[] = "script-src 'self' 'unsafe-eval'"; 66 const char kContentSecurityPolicy[] = "script-src 'self' 'unsafe-eval'";
77 67
68 // Whether or not we have set the CLD callback yet.
69 bool g_cld_callback_set = false;
70
78 } // namespace 71 } // namespace
79 72
80 #if defined(CLD2_DYNAMIC_MODE)
81 // The mmap for the CLD2 data must be held forever once it is available in the
82 // process. This is declared static in the translate_helper.h.
83 base::LazyInstance<TranslateHelper::CLDMmapWrapper>::Leaky
84 TranslateHelper::s_cld_mmap_ = LAZY_INSTANCE_INITIALIZER;
85 #endif
86 73
87 //////////////////////////////////////////////////////////////////////////////// 74 ////////////////////////////////////////////////////////////////////////////////
88 // TranslateHelper, public: 75 // TranslateHelper, public:
89 // 76 //
90 TranslateHelper::TranslateHelper(content::RenderView* render_view) 77 TranslateHelper::TranslateHelper(content::RenderView* render_view)
91 : content::RenderViewObserver(render_view), 78 : content::RenderViewObserver(render_view),
92 page_id_(-1), 79 page_id_(-1),
93 translation_pending_(false), 80 translation_pending_(false),
94 weak_method_factory_(this) 81 weak_method_factory_(this),
95 #if defined(CLD2_DYNAMIC_MODE) 82 cld_data_provider_(translate::CreateRendererCldDataProviderFor(this)),
96 ,cld2_data_file_polling_started_(false), 83 cld_data_polling_started_(false),
97 cld2_data_file_polling_canceled_(false), 84 cld_data_polling_canceled_(false),
98 deferred_page_capture_(false), 85 deferred_page_capture_(false),
99 deferred_page_id_(-1), 86 deferred_page_id_(-1),
100 deferred_contents_(ASCIIToUTF16("")) 87 deferred_contents_(ASCIIToUTF16("")) {
101 #endif
102 {
103 } 88 }
104 89
105 TranslateHelper::~TranslateHelper() { 90 TranslateHelper::~TranslateHelper() {
106 CancelPendingTranslation(); 91 CancelPendingTranslation();
107 #if defined(CLD2_DYNAMIC_MODE) 92 CancelCldDataPolling();
108 CancelCLD2DataFilePolling();
109 #endif
110 } 93 }
111 94
112 void TranslateHelper::PrepareForUrl(const GURL& url) { 95 void TranslateHelper::PrepareForUrl(const GURL& url) {
113 #if defined(CLD2_DYNAMIC_MODE)
114 deferred_page_capture_ = false; 96 deferred_page_capture_ = false;
115 deferred_contents_.clear(); 97 deferred_contents_.clear();
116 if (cld2_data_file_polling_started_) 98 if (cld_data_polling_started_)
117 return; 99 return;
118 100
119 // TODO(andrewhayden): Refactor translate_manager.cc's IsTranslatableURL to 101 // TODO(andrewhayden): Refactor translate_manager.cc's IsTranslatableURL to
120 // components/translate/core/common/translate_util.cc, and ignore any URL 102 // components/translate/core/common/translate_util.cc, and ignore any URL
121 // that fails that check. This will require moving unit tests and rewiring 103 // that fails that check. This will require moving unit tests and rewiring
122 // other function calls as well, so for now replicate the logic here. 104 // other function calls as well, so for now replicate the logic here.
123 if (url.is_empty()) 105 if (url.is_empty())
124 return; 106 return;
125 if (url.SchemeIs(content::kChromeUIScheme)) 107 if (url.SchemeIs(content::kChromeUIScheme))
126 return; 108 return;
127 if (url.SchemeIs(content::kChromeDevToolsScheme)) 109 if (url.SchemeIs(content::kChromeDevToolsScheme))
128 return; 110 return;
129 if (url.SchemeIs(url::kFtpScheme)) 111 if (url.SchemeIs(url::kFtpScheme))
130 return; 112 return;
131 #if defined(OS_CHROMEOS) 113 if (url.SchemeIs(extensions::kExtensionScheme))
132 if (url.SchemeIs(extensions::kExtensionScheme) &&
133 url.DomainIs(file_manager::kFileManagerAppId))
134 return; 114 return;
135 #endif
136 115
137 // Start polling for CLD data. 116 // Start polling for CLD data.
138 cld2_data_file_polling_started_ = true; 117 cld_data_polling_started_ = true;
139 TranslateHelper::SendCLD2DataFileRequest(0, 1000); 118 TranslateHelper::SendCldDataRequest(0, 1000);
140 #endif
141 } 119 }
142 120
143 #if defined(CLD2_DYNAMIC_MODE)
144 void TranslateHelper::DeferPageCaptured(const int page_id, 121 void TranslateHelper::DeferPageCaptured(const int page_id,
145 const base::string16& contents) { 122 const base::string16& contents) {
146 deferred_page_capture_ = true; 123 deferred_page_capture_ = true;
147 deferred_page_id_ = page_id; 124 deferred_page_id_ = page_id;
148 deferred_contents_ = contents; 125 deferred_contents_ = contents;
149 } 126 }
150 #endif
151 127
152 void TranslateHelper::PageCaptured(int page_id, 128 void TranslateHelper::PageCaptured(int page_id,
153 const base::string16& contents) { 129 const base::string16& contents) {
154 // Get the document language as set by WebKit from the http-equiv 130 // Get the document language as set by WebKit from the http-equiv
155 // meta tag for "content-language". This may or may not also 131 // meta tag for "content-language". This may or may not also
156 // have a value derived from the actual Content-Language HTTP 132 // have a value derived from the actual Content-Language HTTP
157 // header. The two actually have different meanings (despite the 133 // header. The two actually have different meanings (despite the
158 // original intent of http-equiv to be an equivalent) with the former 134 // original intent of http-equiv to be an equivalent) with the former
159 // being the language of the document and the latter being the 135 // being the language of the document and the latter being the
160 // language of the intended audience (a distinction really only 136 // language of the intended audience (a distinction really only
161 // relevant for things like langauge textbooks). This distinction 137 // relevant for things like langauge textbooks). This distinction
162 // shouldn't affect translation. 138 // shouldn't affect translation.
163 WebFrame* main_frame = GetMainFrame(); 139 WebFrame* main_frame = GetMainFrame();
164 if (!main_frame || render_view()->GetPageId() != page_id) 140 if (!main_frame || render_view()->GetPageId() != page_id)
165 return; 141 return;
166 142
167 // TODO(andrewhayden): UMA insertion point here: Track if data is available. 143 // TODO(andrewhayden): UMA insertion point here: Track if data is available.
168 // TODO(andrewhayden): Retry insertion point here, retry till data available. 144 // TODO(andrewhayden): Retry insertion point here, retry till data available.
169 #if defined(CLD2_DYNAMIC_MODE) 145 if (!cld_data_provider_->IsCldDataAvailable()) {
170 if (!CLD2::isDataLoaded()) {
171 // We're in dynamic mode and CLD data isn't loaded. Retry when CLD data 146 // We're in dynamic mode and CLD data isn't loaded. Retry when CLD data
172 // is loaded, if ever. 147 // is loaded, if ever.
173 TranslateHelper::DeferPageCaptured(page_id, contents); 148 TranslateHelper::DeferPageCaptured(page_id, contents);
174 return; 149 return;
175 } 150 }
176 #endif 151
177 page_id_ = page_id; 152 page_id_ = page_id;
178 WebDocument document = main_frame->document(); 153 WebDocument document = main_frame->document();
179 std::string content_language = document.contentLanguage().utf8(); 154 std::string content_language = document.contentLanguage().utf8();
180 WebElement html_element = document.documentElement(); 155 WebElement html_element = document.documentElement();
181 std::string html_lang; 156 std::string html_lang;
182 // |html_element| can be null element, e.g. in 157 // |html_element| can be null element, e.g. in
183 // BrowserTest.WindowOpenClose. 158 // BrowserTest.WindowOpenClose.
184 if (!html_element.isNull()) 159 if (!html_element.isNull())
185 html_lang = html_element.getAttribute("lang").utf8(); 160 html_lang = html_element.getAttribute("lang").utf8();
186 std::string cld_language; 161 std::string cld_language;
(...skipping 24 matching lines...) Expand all
211 routing_id(), 186 routing_id(),
212 details, 187 details,
213 IsTranslationAllowed(&document) && !language.empty())); 188 IsTranslationAllowed(&document) && !language.empty()));
214 } 189 }
215 190
216 void TranslateHelper::CancelPendingTranslation() { 191 void TranslateHelper::CancelPendingTranslation() {
217 weak_method_factory_.InvalidateWeakPtrs(); 192 weak_method_factory_.InvalidateWeakPtrs();
218 translation_pending_ = false; 193 translation_pending_ = false;
219 source_lang_.clear(); 194 source_lang_.clear();
220 target_lang_.clear(); 195 target_lang_.clear();
221 #if defined(CLD2_DYNAMIC_MODE) 196 CancelCldDataPolling();
222 CancelCLD2DataFilePolling();
223 #endif
224 } 197 }
225 198
226 //////////////////////////////////////////////////////////////////////////////// 199 ////////////////////////////////////////////////////////////////////////////////
227 // TranslateHelper, protected: 200 // TranslateHelper, protected:
228 // 201 //
229 bool TranslateHelper::IsTranslateLibAvailable() { 202 bool TranslateHelper::IsTranslateLibAvailable() {
230 return ExecuteScriptAndGetBoolResult( 203 return ExecuteScriptAndGetBoolResult(
231 "typeof cr != 'undefined' && typeof cr.googleTranslate != 'undefined' && " 204 "typeof cr != 'undefined' && typeof cr.googleTranslate != 'undefined' && "
232 "typeof cr.googleTranslate.translate == 'function'", false); 205 "typeof cr.googleTranslate.translate == 'function'", false);
233 } 206 }
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after
388 return false; 361 return false;
389 } 362 }
390 return true; 363 return true;
391 } 364 }
392 365
393 bool TranslateHelper::OnMessageReceived(const IPC::Message& message) { 366 bool TranslateHelper::OnMessageReceived(const IPC::Message& message) {
394 bool handled = true; 367 bool handled = true;
395 IPC_BEGIN_MESSAGE_MAP(TranslateHelper, message) 368 IPC_BEGIN_MESSAGE_MAP(TranslateHelper, message)
396 IPC_MESSAGE_HANDLER(ChromeViewMsg_TranslatePage, OnTranslatePage) 369 IPC_MESSAGE_HANDLER(ChromeViewMsg_TranslatePage, OnTranslatePage)
397 IPC_MESSAGE_HANDLER(ChromeViewMsg_RevertTranslation, OnRevertTranslation) 370 IPC_MESSAGE_HANDLER(ChromeViewMsg_RevertTranslation, OnRevertTranslation)
398 #if defined(CLD2_DYNAMIC_MODE)
399 IPC_MESSAGE_HANDLER(ChromeViewMsg_CLDDataAvailable, OnCLDDataAvailable);
400 #endif
401 IPC_MESSAGE_UNHANDLED(handled = false) 371 IPC_MESSAGE_UNHANDLED(handled = false)
402 IPC_END_MESSAGE_MAP() 372 IPC_END_MESSAGE_MAP()
373 if (!handled) {
374 handled = cld_data_provider_->OnMessageReceived(message);
375 }
403 return handled; 376 return handled;
404 } 377 }
405 378
406 void TranslateHelper::OnTranslatePage(int page_id, 379 void TranslateHelper::OnTranslatePage(int page_id,
407 const std::string& translate_script, 380 const std::string& translate_script,
408 const std::string& source_lang, 381 const std::string& source_lang,
409 const std::string& target_lang) { 382 const std::string& target_lang) {
410 WebFrame* main_frame = GetMainFrame(); 383 WebFrame* main_frame = GetMainFrame();
411 if (!main_frame || 384 if (!main_frame ||
412 page_id_ != page_id || 385 page_id_ != page_id ||
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after
581 WebFrame* TranslateHelper::GetMainFrame() { 554 WebFrame* TranslateHelper::GetMainFrame() {
582 WebView* web_view = render_view()->GetWebView(); 555 WebView* web_view = render_view()->GetWebView();
583 556
584 // When the tab is going to be closed, the web_view can be NULL. 557 // When the tab is going to be closed, the web_view can be NULL.
585 if (!web_view) 558 if (!web_view)
586 return NULL; 559 return NULL;
587 560
588 return web_view->mainFrame(); 561 return web_view->mainFrame();
589 } 562 }
590 563
591 #if defined(CLD2_DYNAMIC_MODE) 564 void TranslateHelper::CancelCldDataPolling() {
592 void TranslateHelper::CancelCLD2DataFilePolling() { 565 cld_data_polling_canceled_ = true;
593 cld2_data_file_polling_canceled_ = true;
594 } 566 }
595 567
596 void TranslateHelper::SendCLD2DataFileRequest(const int delay_millis, 568 void TranslateHelper::SendCldDataRequest(const int delay_millis,
597 const int next_delay_millis) { 569 const int next_delay_millis) {
598 // Terminate immediately if told to stop polling. 570 // Terminate immediately if told to stop polling.
599 if (cld2_data_file_polling_canceled_) 571 if (cld_data_polling_canceled_)
600 return; 572 return;
601 573
602 // Terminate immediately if data is already loaded. 574 // Terminate immediately if data is already loaded.
603 if (CLD2::isDataLoaded()) 575 if (cld_data_provider_->IsCldDataAvailable())
604 return; 576 return;
605 577
606 // Else, send the IPC message to the browser process requesting the data... 578 if (!g_cld_callback_set) {
607 Send(new ChromeViewHostMsg_NeedCLDData(routing_id())); 579 g_cld_callback_set = true;
580 cld_data_provider_->SetCldAvailableCallback(
581 base::Bind(&TranslateHelper::OnCldDataAvailable,
582 weak_method_factory_.GetWeakPtr()));
583 }
584
585 // Else, make an asynchronous request to get the data we need.
586 cld_data_provider_->SendCldDataRequest();
608 587
609 // ... and enqueue another delayed task to call again. This will start a 588 // ... and enqueue another delayed task to call again. This will start a
610 // chain of polling that will last until the pointer stops being NULL, 589 // chain of polling that will last until the pointer stops being NULL,
611 // which is the right thing to do. 590 // which is the right thing to do.
612 // NB: In the great majority of cases, the data file will be available and 591 // NB: In the great majority of cases, the data file will be available and
613 // the very first delayed task will be a no-op that terminates the chain. 592 // the very first delayed task will be a no-op that terminates the chain.
614 // It's only while downloading the file that this will chain for a 593 // It's only while downloading the file that this will chain for a
615 // nontrivial amount of time. 594 // nontrivial amount of time.
616 // Use a weak pointer to avoid keeping this helper object around forever. 595 // Use a weak pointer to avoid keeping this helper object around forever.
617 base::MessageLoop::current()->PostDelayedTask( 596 base::MessageLoop::current()->PostDelayedTask(
618 FROM_HERE, 597 FROM_HERE,
619 base::Bind(&TranslateHelper::SendCLD2DataFileRequest, 598 base::Bind(&TranslateHelper::SendCldDataRequest,
620 weak_method_factory_.GetWeakPtr(), 599 weak_method_factory_.GetWeakPtr(),
621 next_delay_millis, next_delay_millis), 600 next_delay_millis,
601 next_delay_millis),
622 base::TimeDelta::FromMilliseconds(delay_millis)); 602 base::TimeDelta::FromMilliseconds(delay_millis));
623 } 603 }
624 604
625 void TranslateHelper::OnCLDDataAvailable( 605 void TranslateHelper::OnCldDataAvailable() {
626 const IPC::PlatformFileForTransit ipc_file_handle, 606 if (deferred_page_capture_) {
627 const uint64 data_offset,
628 const uint64 data_length) {
629 LoadCLDDData(IPC::PlatformFileForTransitToFile(ipc_file_handle), data_offset,
630 data_length);
631 if (deferred_page_capture_ && CLD2::isDataLoaded()) {
632 deferred_page_capture_ = false; // Don't do this a second time. 607 deferred_page_capture_ = false; // Don't do this a second time.
633 PageCaptured(deferred_page_id_, deferred_contents_); 608 PageCaptured(deferred_page_id_, deferred_contents_);
634 deferred_page_id_ = -1; // Clean up for sanity 609 deferred_page_id_ = -1; // Clean up for sanity
635 deferred_contents_.clear(); // Clean up for sanity 610 deferred_contents_.clear(); // Clean up for sanity
636 } 611 }
637 } 612 }
638
639 void TranslateHelper::LoadCLDDData(
640 base::File file,
641 const uint64 data_offset,
642 const uint64 data_length) {
643 // Terminate immediately if told to stop polling.
644 if (cld2_data_file_polling_canceled_)
645 return;
646
647 // Terminate immediately if data is already loaded.
648 if (CLD2::isDataLoaded())
649 return;
650
651 if (!file.IsValid()) {
652 LOG(ERROR) << "Can't find the CLD data file.";
653 return;
654 }
655
656 // mmap the file
657 s_cld_mmap_.Get().value = new base::MemoryMappedFile();
658 bool initialized = s_cld_mmap_.Get().value->Initialize(file.Pass());
659 if (!initialized) {
660 LOG(ERROR) << "mmap initialization failed";
661 delete s_cld_mmap_.Get().value;
662 s_cld_mmap_.Get().value = NULL;
663 return;
664 }
665
666 // Sanity checks
667 uint64 max_int32 = std::numeric_limits<int32>::max();
668 if (data_length + data_offset > s_cld_mmap_.Get().value->length()
669 || data_length > max_int32) { // max signed 32 bit integer
670 LOG(ERROR) << "Illegal mmap config: data_offset="
671 << data_offset << ", data_length=" << data_length
672 << ", mmap->length()=" << s_cld_mmap_.Get().value->length();
673 delete s_cld_mmap_.Get().value;
674 s_cld_mmap_.Get().value = NULL;
675 return;
676 }
677
678 // Initialize the CLD subsystem... and it's all done!
679 const uint8* data_ptr = s_cld_mmap_.Get().value->data() + data_offset;
680 CLD2::loadDataFromRawAddress(data_ptr, data_length);
681 DCHECK(CLD2::isDataLoaded()) << "Failed to load CLD data from mmap";
682 }
683 #endif
OLDNEW
« no previous file with comments | « chrome/renderer/translate/translate_helper.h ('k') | components/translate.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698