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 <stdint.h> | |
8 | |
7 #include "base/bind.h" | 9 #include "base/bind.h" |
8 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
11 #include "base/files/memory_mapped_file.h" | |
9 #include "base/logging.h" | 12 #include "base/logging.h" |
10 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
11 #include "base/strings/string16.h" | 14 #include "base/strings/string16.h" |
12 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
13 #include "base/strings/utf_string_conversions.h" | 16 #include "base/strings/utf_string_conversions.h" |
14 #include "chrome/common/render_messages.h" | 17 #include "chrome/common/render_messages.h" |
15 #include "chrome/renderer/extensions/extension_groups.h" | 18 #include "chrome/renderer/extensions/extension_groups.h" |
16 #include "chrome/renderer/isolated_world_ids.h" | 19 #include "chrome/renderer/isolated_world_ids.h" |
17 #include "components/translate/core/common/translate_constants.h" | 20 #include "components/translate/core/common/translate_constants.h" |
18 #include "components/translate/core/common/translate_metrics.h" | 21 #include "components/translate/core/common/translate_metrics.h" |
19 #include "components/translate/core/common/translate_util.h" | 22 #include "components/translate/core/common/translate_util.h" |
20 #include "components/translate/language_detection/language_detection_util.h" | 23 #include "components/translate/language_detection/language_detection_util.h" |
21 #include "content/public/renderer/render_view.h" | 24 #include "content/public/renderer/render_view.h" |
25 #include "extensions/common/constants.h" | |
26 #include "ipc/ipc_platform_file.h" | |
27 #if defined(CLD2_DYNAMIC_MODE) | |
28 #include "content/public/common/url_constants.h" | |
29 #include "third_party/cld_2/src/public/compact_lang_det.h" | |
30 #endif | |
22 #include "third_party/WebKit/public/web/WebDocument.h" | 31 #include "third_party/WebKit/public/web/WebDocument.h" |
23 #include "third_party/WebKit/public/web/WebElement.h" | 32 #include "third_party/WebKit/public/web/WebElement.h" |
24 #include "third_party/WebKit/public/web/WebFrame.h" | 33 #include "third_party/WebKit/public/web/WebFrame.h" |
25 #include "third_party/WebKit/public/web/WebNode.h" | 34 #include "third_party/WebKit/public/web/WebNode.h" |
26 #include "third_party/WebKit/public/web/WebNodeList.h" | 35 #include "third_party/WebKit/public/web/WebNodeList.h" |
27 #include "third_party/WebKit/public/web/WebScriptSource.h" | 36 #include "third_party/WebKit/public/web/WebScriptSource.h" |
28 #include "third_party/WebKit/public/web/WebView.h" | 37 #include "third_party/WebKit/public/web/WebView.h" |
29 #include "third_party/WebKit/public/web/WebWidget.h" | 38 #include "third_party/WebKit/public/web/WebWidget.h" |
30 #include "url/gurl.h" | 39 #include "url/gurl.h" |
31 #include "v8/include/v8.h" | 40 #include "v8/include/v8.h" |
(...skipping 25 matching lines...) Expand all Loading... | |
57 const int kTranslateStatusCheckDelayMs = 400; | 66 const int kTranslateStatusCheckDelayMs = 400; |
58 | 67 |
59 // Language name passed to the Translate element for it to detect the language. | 68 // Language name passed to the Translate element for it to detect the language. |
60 const char kAutoDetectionLanguage[] = "auto"; | 69 const char kAutoDetectionLanguage[] = "auto"; |
61 | 70 |
62 // Isolated world sets following content-security-policy. | 71 // Isolated world sets following content-security-policy. |
63 const char kContentSecurityPolicy[] = "script-src 'self' 'unsafe-eval'"; | 72 const char kContentSecurityPolicy[] = "script-src 'self' 'unsafe-eval'"; |
64 | 73 |
65 } // namespace | 74 } // namespace |
66 | 75 |
76 // The mmap for the CLD2 data must be held forever once it is available in the | |
77 // process. This is declared static in the translate_helper.h. | |
78 base::LazyInstance<TranslateHelper::CLDMmapWrapper>::Leaky | |
79 TranslateHelper::s_cld_mmap_ = LAZY_INSTANCE_INITIALIZER; | |
80 | |
67 //////////////////////////////////////////////////////////////////////////////// | 81 //////////////////////////////////////////////////////////////////////////////// |
68 // TranslateHelper, public: | 82 // TranslateHelper, public: |
69 // | 83 // |
70 TranslateHelper::TranslateHelper(content::RenderView* render_view) | 84 TranslateHelper::TranslateHelper(content::RenderView* render_view) |
71 : content::RenderViewObserver(render_view), | 85 : content::RenderViewObserver(render_view), |
72 page_id_(-1), | 86 page_id_(-1), |
73 translation_pending_(false), | 87 translation_pending_(false), |
74 weak_method_factory_(this) { | 88 weak_method_factory_(this) |
89 #if defined(CLD2_DYNAMIC_MODE) | |
90 ,cld2_data_file_polling_started_(false), | |
91 cld2_data_file_polling_canceled_(false), | |
92 deferred_page_capture_(false), | |
93 deferred_page_id_(-1), | |
94 deferred_contents_(ASCIIToUTF16("")) | |
95 #endif | |
96 { | |
75 } | 97 } |
76 | 98 |
77 TranslateHelper::~TranslateHelper() { | 99 TranslateHelper::~TranslateHelper() { |
78 CancelPendingTranslation(); | 100 CancelPendingTranslation(); |
101 #if defined(CLD2_DYNAMIC_MODE) | |
102 CancelCLD2DataFilePolling(); | |
103 #endif | |
79 } | 104 } |
80 | 105 |
106 void TranslateHelper::PrepareForUrl(const GURL& url) { | |
107 #if defined(CLD2_DYNAMIC_MODE) | |
108 deferred_page_capture_ = false; | |
109 deferred_contents_.clear(); | |
110 if (cld2_data_file_polling_started_) | |
111 return; | |
112 | |
113 // TODO(andrewhayden): Refactor translate_manager.cc's IsTranslatableURL to | |
114 // components/translate/core/common/translate_util.cc, and ignore any URL | |
115 // that fails that check. This will require moving unit tests and rewiring | |
116 // other function calls as well, so for now replicate the logic here. | |
117 if (url.is_empty()) | |
118 return; | |
119 if (url.SchemeIs(content::kChromeUIScheme)) | |
120 return; | |
121 if (url.SchemeIs(content::kChromeDevToolsScheme)) | |
122 return; | |
123 if (url.SchemeIs(content::kFtpScheme)) | |
124 return; | |
125 #if defined(OS_CHROMEOS) | |
126 if (url.SchemeIs(extensions::kExtensionScheme) && | |
127 url.DomainIs(file_manager::kFileManagerAppId)) | |
128 return; | |
129 #endif | |
130 | |
131 // Start polling for CLD data. | |
132 cld2_data_file_polling_started_ = true; | |
133 TranslateHelper::SendCLD2DataFileRequest(0, 1000); | |
134 #endif | |
135 } | |
136 | |
137 #if defined(CLD2_DYNAMIC_MODE) | |
138 void TranslateHelper::DeferPageCaptured(const int page_id, | |
139 const base::string16& contents) { | |
140 deferred_page_capture_ = true; | |
141 deferred_page_id_ = page_id; | |
142 deferred_contents_ = contents; | |
143 } | |
144 #endif | |
145 | |
81 void TranslateHelper::PageCaptured(int page_id, | 146 void TranslateHelper::PageCaptured(int page_id, |
82 const base::string16& contents) { | 147 const base::string16& contents) { |
83 // Get the document language as set by WebKit from the http-equiv | 148 // Get the document language as set by WebKit from the http-equiv |
84 // meta tag for "content-language". This may or may not also | 149 // meta tag for "content-language". This may or may not also |
85 // have a value derived from the actual Content-Language HTTP | 150 // have a value derived from the actual Content-Language HTTP |
86 // header. The two actually have different meanings (despite the | 151 // header. The two actually have different meanings (despite the |
87 // original intent of http-equiv to be an equivalent) with the former | 152 // original intent of http-equiv to be an equivalent) with the former |
88 // being the language of the document and the latter being the | 153 // being the language of the document and the latter being the |
89 // language of the intended audience (a distinction really only | 154 // language of the intended audience (a distinction really only |
90 // relevant for things like langauge textbooks). This distinction | 155 // relevant for things like langauge textbooks). This distinction |
91 // shouldn't affect translation. | 156 // shouldn't affect translation. |
92 WebFrame* main_frame = GetMainFrame(); | 157 WebFrame* main_frame = GetMainFrame(); |
93 if (!main_frame || render_view()->GetPageId() != page_id) | 158 if (!main_frame || render_view()->GetPageId() != page_id) |
94 return; | 159 return; |
160 | |
161 // TODO(andrewhayden): UMA insertion point here: Track if data is available. | |
162 // TODO(andrewhayden): Retry insertion point here, retry till data available. | |
163 #if defined(CLD2_DYNAMIC_MODE) | |
164 if (!CLD2::isDataLoaded()) { | |
165 // We're in dynamic mode and CLD data isn't loaded. Retry when CLD data | |
166 // is loaded, if ever. | |
167 TranslateHelper::DeferPageCaptured(page_id, contents); | |
168 return; | |
169 } | |
170 #endif | |
95 page_id_ = page_id; | 171 page_id_ = page_id; |
96 WebDocument document = main_frame->document(); | 172 WebDocument document = main_frame->document(); |
97 std::string content_language = document.contentLanguage().utf8(); | 173 std::string content_language = document.contentLanguage().utf8(); |
98 WebElement html_element = document.documentElement(); | 174 WebElement html_element = document.documentElement(); |
99 std::string html_lang; | 175 std::string html_lang; |
100 // |html_element| can be null element, e.g. in | 176 // |html_element| can be null element, e.g. in |
101 // BrowserTest.WindowOpenClose. | 177 // BrowserTest.WindowOpenClose. |
102 if (!html_element.isNull()) | 178 if (!html_element.isNull()) |
103 html_lang = html_element.getAttribute("lang").utf8(); | 179 html_lang = html_element.getAttribute("lang").utf8(); |
104 std::string cld_language; | 180 std::string cld_language; |
(...skipping 24 matching lines...) Expand all Loading... | |
129 routing_id(), | 205 routing_id(), |
130 details, | 206 details, |
131 IsTranslationAllowed(&document) && !language.empty())); | 207 IsTranslationAllowed(&document) && !language.empty())); |
132 } | 208 } |
133 | 209 |
134 void TranslateHelper::CancelPendingTranslation() { | 210 void TranslateHelper::CancelPendingTranslation() { |
135 weak_method_factory_.InvalidateWeakPtrs(); | 211 weak_method_factory_.InvalidateWeakPtrs(); |
136 translation_pending_ = false; | 212 translation_pending_ = false; |
137 source_lang_.clear(); | 213 source_lang_.clear(); |
138 target_lang_.clear(); | 214 target_lang_.clear(); |
215 #if defined(CLD2_DYNAMIC_MODE) | |
216 CancelCLD2DataFilePolling(); | |
217 #endif | |
139 } | 218 } |
140 | 219 |
141 //////////////////////////////////////////////////////////////////////////////// | 220 //////////////////////////////////////////////////////////////////////////////// |
142 // TranslateHelper, protected: | 221 // TranslateHelper, protected: |
143 // | 222 // |
144 bool TranslateHelper::IsTranslateLibAvailable() { | 223 bool TranslateHelper::IsTranslateLibAvailable() { |
145 return ExecuteScriptAndGetBoolResult( | 224 return ExecuteScriptAndGetBoolResult( |
146 "typeof cr != 'undefined' && typeof cr.googleTranslate != 'undefined' && " | 225 "typeof cr != 'undefined' && typeof cr.googleTranslate != 'undefined' && " |
147 "typeof cr.googleTranslate.translate == 'function'", false); | 226 "typeof cr.googleTranslate.translate == 'function'", false); |
148 } | 227 } |
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
303 return false; | 382 return false; |
304 } | 383 } |
305 return true; | 384 return true; |
306 } | 385 } |
307 | 386 |
308 bool TranslateHelper::OnMessageReceived(const IPC::Message& message) { | 387 bool TranslateHelper::OnMessageReceived(const IPC::Message& message) { |
309 bool handled = true; | 388 bool handled = true; |
310 IPC_BEGIN_MESSAGE_MAP(TranslateHelper, message) | 389 IPC_BEGIN_MESSAGE_MAP(TranslateHelper, message) |
311 IPC_MESSAGE_HANDLER(ChromeViewMsg_TranslatePage, OnTranslatePage) | 390 IPC_MESSAGE_HANDLER(ChromeViewMsg_TranslatePage, OnTranslatePage) |
312 IPC_MESSAGE_HANDLER(ChromeViewMsg_RevertTranslation, OnRevertTranslation) | 391 IPC_MESSAGE_HANDLER(ChromeViewMsg_RevertTranslation, OnRevertTranslation) |
392 #if defined(CLD2_DYNAMIC_MODE) | |
393 IPC_MESSAGE_HANDLER(ChromeViewMsg_CLDDataAvailable, OnCLDDataAvailable); | |
394 #endif | |
313 IPC_MESSAGE_UNHANDLED(handled = false) | 395 IPC_MESSAGE_UNHANDLED(handled = false) |
314 IPC_END_MESSAGE_MAP() | 396 IPC_END_MESSAGE_MAP() |
315 return handled; | 397 return handled; |
316 } | 398 } |
317 | 399 |
318 void TranslateHelper::OnTranslatePage(int page_id, | 400 void TranslateHelper::OnTranslatePage(int page_id, |
319 const std::string& translate_script, | 401 const std::string& translate_script, |
320 const std::string& source_lang, | 402 const std::string& source_lang, |
321 const std::string& target_lang) { | 403 const std::string& target_lang) { |
322 WebFrame* main_frame = GetMainFrame(); | 404 WebFrame* main_frame = GetMainFrame(); |
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
492 | 574 |
493 WebFrame* TranslateHelper::GetMainFrame() { | 575 WebFrame* TranslateHelper::GetMainFrame() { |
494 WebView* web_view = render_view()->GetWebView(); | 576 WebView* web_view = render_view()->GetWebView(); |
495 | 577 |
496 // When the tab is going to be closed, the web_view can be NULL. | 578 // When the tab is going to be closed, the web_view can be NULL. |
497 if (!web_view) | 579 if (!web_view) |
498 return NULL; | 580 return NULL; |
499 | 581 |
500 return web_view->mainFrame(); | 582 return web_view->mainFrame(); |
501 } | 583 } |
584 | |
585 #if defined(CLD2_DYNAMIC_MODE) | |
586 void TranslateHelper::CancelCLD2DataFilePolling() { | |
587 cld2_data_file_polling_canceled_ = true; | |
588 } | |
589 | |
590 void TranslateHelper::SendCLD2DataFileRequest(const int delay_millis, | |
591 const int next_delay_millis) { | |
592 // Terminate immediately if told to stop polling. | |
593 if (cld2_data_file_polling_canceled_) | |
594 return; | |
595 | |
596 // Terminate immediately if data is already loaded. | |
597 if (CLD2::isDataLoaded()) | |
598 return; | |
599 | |
600 // Else, send the IPC message to the browser process requesting the data... | |
601 Send(new ChromeViewHostMsg_NeedCLDData(routing_id())); | |
602 | |
603 // ... and enqueue another delayed task to call again. This will start a | |
604 // chain of polling that will last until the pointer stops being NULL, | |
605 // which is the right thing to do. | |
606 // NB: In the great majority of cases, the data file will be available and | |
607 // the very first delayed task will be a no-op that terminates the chain. | |
608 // It's only while downloading the file that this will chain for a | |
609 // nontrivial amount of time. | |
610 // Use a weak pointer to avoid keeping this helper object around forever. | |
611 base::MessageLoop::current()->PostDelayedTask( | |
612 FROM_HERE, | |
613 base::Bind(&TranslateHelper::SendCLD2DataFileRequest, | |
614 weak_method_factory_.GetWeakPtr(), | |
615 next_delay_millis, next_delay_millis), | |
616 base::TimeDelta::FromMilliseconds(delay_millis)); | |
617 } | |
618 | |
619 void TranslateHelper::OnCLDDataAvailable( | |
620 const IPC::PlatformFileForTransit ipc_file_handle, | |
621 const int64 data_offset, | |
622 const int64 data_length) { | |
623 LoadCLDDData(ipc_file_handle, data_offset, data_length); | |
624 if (deferred_page_capture_ && CLD2::isDataLoaded()) { | |
625 deferred_page_capture_ = false; // Don't do this a second time. | |
626 PageCaptured(deferred_page_id_, deferred_contents_); | |
627 deferred_page_id_ = -1; // Clean up for sanity | |
628 deferred_contents_.clear(); // Clean up for sanity | |
629 } | |
630 } | |
631 | |
632 void TranslateHelper::LoadCLDDData( | |
633 const IPC::PlatformFileForTransit ipc_file_handle, | |
634 const int64 data_offset, | |
635 const int64 data_length) { | |
636 // Terminate immediately if told to stop polling. | |
637 if (cld2_data_file_polling_canceled_) | |
638 return; | |
639 | |
640 // Terminate immediately if data is already loaded. | |
641 if (CLD2::isDataLoaded()) | |
642 return; | |
643 | |
644 // Grab the file handle | |
645 base::PlatformFile platform_file = | |
646 IPC::PlatformFileForTransitToPlatformFile(ipc_file_handle); | |
647 if (platform_file == base::kInvalidPlatformFileValue) { | |
648 LOG(ERROR) << "Can't find the CLD data file."; | |
649 return; | |
650 } | |
651 base::File basic_file(platform_file); | |
652 | |
653 // mmap the file | |
654 s_cld_mmap_.Get().value = new base::MemoryMappedFile(); | |
655 bool initialized = s_cld_mmap_.Get().value->Initialize(basic_file.Pass()); | |
656 if (!initialized) { | |
657 LOG(ERROR) << "mmap initialization failed"; | |
658 delete s_cld_mmap_.Get().value; | |
659 s_cld_mmap_.Get().value = NULL; | |
660 return; | |
661 } | |
662 | |
663 // Sanity checks | |
664 int64 max_int32 = (( ((int64) 1) << 31) - 1); | |
665 if (((uint64) (data_length + data_offset) > s_cld_mmap_.Get().value->length()) | |
bulach
2014/03/26 19:06:46
nit: static_cast<uint64> instead of (c-style)?
andrewhayden
2014/03/27 09:36:59
Even better, use uint64 for all the offsets and si
| |
666 || data_length > max_int32) { // max signed 32 bit integer | |
667 LOG(ERROR) << "Illegal mmap config: data_offset=" | |
668 << data_offset << ", data_length=" << data_length | |
669 << ", mmap->length()=" << s_cld_mmap_.Get().value->length(); | |
670 delete s_cld_mmap_.Get().value; | |
671 s_cld_mmap_.Get().value = NULL; | |
672 return; | |
673 } | |
674 | |
675 // Initialize the CLD subsystem... and it's all done! | |
676 const uint8* data_ptr = s_cld_mmap_.Get().value->data() + data_offset; | |
677 CLD2::loadDataFromRawAddress(data_ptr, data_length); | |
678 DCHECK(CLD2::isDataLoaded()) << "Failed to load CLD data from mmap"; | |
679 } | |
680 #endif | |
OLD | NEW |