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