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 "content/public/common/url_constants.h" | |
27 #include "third_party/cld_2/src/public/compact_lang_det.h" | |
28 #endif | |
22 #include "third_party/WebKit/public/web/WebDocument.h" | 29 #include "third_party/WebKit/public/web/WebDocument.h" |
23 #include "third_party/WebKit/public/web/WebElement.h" | 30 #include "third_party/WebKit/public/web/WebElement.h" |
24 #include "third_party/WebKit/public/web/WebFrame.h" | 31 #include "third_party/WebKit/public/web/WebFrame.h" |
25 #include "third_party/WebKit/public/web/WebNode.h" | 32 #include "third_party/WebKit/public/web/WebNode.h" |
26 #include "third_party/WebKit/public/web/WebNodeList.h" | 33 #include "third_party/WebKit/public/web/WebNodeList.h" |
27 #include "third_party/WebKit/public/web/WebScriptSource.h" | 34 #include "third_party/WebKit/public/web/WebScriptSource.h" |
28 #include "third_party/WebKit/public/web/WebView.h" | 35 #include "third_party/WebKit/public/web/WebView.h" |
29 #include "third_party/WebKit/public/web/WebWidget.h" | 36 #include "third_party/WebKit/public/web/WebWidget.h" |
30 #include "url/gurl.h" | 37 #include "url/gurl.h" |
31 #include "v8/include/v8.h" | 38 #include "v8/include/v8.h" |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
64 | 71 |
65 } // namespace | 72 } // namespace |
66 | 73 |
67 //////////////////////////////////////////////////////////////////////////////// | 74 //////////////////////////////////////////////////////////////////////////////// |
68 // TranslateHelper, public: | 75 // TranslateHelper, public: |
69 // | 76 // |
70 TranslateHelper::TranslateHelper(content::RenderView* render_view) | 77 TranslateHelper::TranslateHelper(content::RenderView* render_view) |
71 : content::RenderViewObserver(render_view), | 78 : content::RenderViewObserver(render_view), |
72 page_id_(-1), | 79 page_id_(-1), |
73 translation_pending_(false), | 80 translation_pending_(false), |
74 weak_method_factory_(this) { | 81 weak_method_factory_(this) |
82 #if defined(CLD2_DYNAMIC_MODE) | |
83 ,cld2_data_file_polling_started(false), | |
84 cld2_data_file_polling_canceled(false), | |
85 deferred_page_capture_(false), | |
86 deferred_page_id_(-1), | |
87 deferred_contents_(ASCIIToUTF16("")) | |
bulach
2014/03/23 20:57:02
nit: I think string16 default initializes to empty
Andrew Hayden (chromium.org)
2014/03/24 15:18:24
Done.
bulach
2014/03/25 09:29:26
sorry, how about the string16 initialization, is i
| |
88 #endif | |
89 { | |
75 } | 90 } |
76 | 91 |
77 TranslateHelper::~TranslateHelper() { | 92 TranslateHelper::~TranslateHelper() { |
78 CancelPendingTranslation(); | 93 CancelPendingTranslation(); |
94 #if defined(CLD2_DYNAMIC_MODE) | |
95 CancelCLD2DataFilePolling(); | |
96 #endif | |
79 } | 97 } |
80 | 98 |
99 void TranslateHelper::PrepareForUrl(const GURL& url) { | |
100 #if defined(CLD2_DYNAMIC_MODE) | |
101 deferred_page_capture_ = false; | |
102 deferred_contents_.clear(); | |
103 if (cld2_data_file_polling_started) | |
104 return; | |
105 | |
106 // TODO(andrewhayden): Refactor translate_manager.cc's IsTranslatableURL to | |
107 // components/translate/core/common/translate_util.cc, and ignore any URL | |
108 // that fails that check. This will require moving unit tests and rewiring | |
109 // other function calls as wlel, so for now we replicate the logic here. | |
bulach
2014/03/23 20:57:02
nit: s/wlel/well/
Andrew Hayden (chromium.org)
2014/03/24 15:18:24
Done.
| |
110 if (url.is_empty()) | |
111 return; | |
bulach
2014/03/23 20:57:02
nit: here and all "return" clauses below, unindent
Andrew Hayden (chromium.org)
2014/03/24 15:18:24
Done.
| |
112 if (url.SchemeIs(content::kChromeUIScheme)) | |
113 return; | |
114 if (url.SchemeIs(content::kChromeDevToolsScheme)) | |
115 return; | |
116 if (url.SchemeIs(content::kFtpScheme)) | |
117 return; | |
118 #if defined(OS_CHROMEOS) | |
119 if (url.SchemeIs(extensions::kExtensionScheme) && | |
120 url.DomainIs(file_manager::kFileManagerAppId)) | |
121 return; | |
122 #endif | |
123 | |
124 // If the URL isn't an extension URL, we might conceivably want translation | |
125 // capabilities. Start polling for CLD data. | |
126 cld2_data_file_polling_started = true; | |
127 TranslateHelper::SendCLD2DataFileRequest(0, 1000); | |
128 #endif | |
129 } | |
130 | |
131 #if defined(CLD2_DYNAMIC_MODE) | |
132 void TranslateHelper::DeferPageCaptured(const int page_id, | |
133 const base::string16& contents) { | |
134 deferred_page_capture_ = true; | |
135 deferred_page_id_ = page_id; | |
136 deferred_contents_ = contents; | |
137 } | |
138 #endif | |
139 | |
81 void TranslateHelper::PageCaptured(int page_id, | 140 void TranslateHelper::PageCaptured(int page_id, |
82 const base::string16& contents) { | 141 const base::string16& contents) { |
83 // Get the document language as set by WebKit from the http-equiv | 142 // Get the document language as set by WebKit from the http-equiv |
84 // meta tag for "content-language". This may or may not also | 143 // meta tag for "content-language". This may or may not also |
85 // have a value derived from the actual Content-Language HTTP | 144 // have a value derived from the actual Content-Language HTTP |
86 // header. The two actually have different meanings (despite the | 145 // header. The two actually have different meanings (despite the |
87 // original intent of http-equiv to be an equivalent) with the former | 146 // original intent of http-equiv to be an equivalent) with the former |
88 // being the language of the document and the latter being the | 147 // being the language of the document and the latter being the |
89 // language of the intended audience (a distinction really only | 148 // language of the intended audience (a distinction really only |
90 // relevant for things like langauge textbooks). This distinction | 149 // relevant for things like langauge textbooks). This distinction |
91 // shouldn't affect translation. | 150 // shouldn't affect translation. |
92 WebFrame* main_frame = GetMainFrame(); | 151 WebFrame* main_frame = GetMainFrame(); |
93 if (!main_frame || render_view()->GetPageId() != page_id) | 152 if (!main_frame || render_view()->GetPageId() != page_id) |
94 return; | 153 return; |
154 | |
155 // TODO(andrewhayden): UMA insertion point here: Track if data is available. | |
156 // TODO(andrewhayden): Retry insertion point here, retry till data available. | |
157 #if defined(CLD2_DYNAMIC_MODE) | |
158 if (!CLD2::isDataLoaded()) { | |
159 // We're in dynamic mode and CLD data isn't loaded. Retry when CLD data | |
160 // is loaded, if ever. | |
161 TranslateHelper::DeferPageCaptured(page_id, contents); | |
162 return; | |
163 } | |
164 #endif | |
95 page_id_ = page_id; | 165 page_id_ = page_id; |
96 WebDocument document = main_frame->document(); | 166 WebDocument document = main_frame->document(); |
97 std::string content_language = document.contentLanguage().utf8(); | 167 std::string content_language = document.contentLanguage().utf8(); |
98 WebElement html_element = document.documentElement(); | 168 WebElement html_element = document.documentElement(); |
99 std::string html_lang; | 169 std::string html_lang; |
100 // |html_element| can be null element, e.g. in | 170 // |html_element| can be null element, e.g. in |
101 // BrowserTest.WindowOpenClose. | 171 // BrowserTest.WindowOpenClose. |
102 if (!html_element.isNull()) | 172 if (!html_element.isNull()) |
103 html_lang = html_element.getAttribute("lang").utf8(); | 173 html_lang = html_element.getAttribute("lang").utf8(); |
104 std::string cld_language; | 174 std::string cld_language; |
(...skipping 24 matching lines...) Expand all Loading... | |
129 routing_id(), | 199 routing_id(), |
130 details, | 200 details, |
131 IsTranslationAllowed(&document) && !language.empty())); | 201 IsTranslationAllowed(&document) && !language.empty())); |
132 } | 202 } |
133 | 203 |
134 void TranslateHelper::CancelPendingTranslation() { | 204 void TranslateHelper::CancelPendingTranslation() { |
135 weak_method_factory_.InvalidateWeakPtrs(); | 205 weak_method_factory_.InvalidateWeakPtrs(); |
136 translation_pending_ = false; | 206 translation_pending_ = false; |
137 source_lang_.clear(); | 207 source_lang_.clear(); |
138 target_lang_.clear(); | 208 target_lang_.clear(); |
209 #if defined(CLD2_DYNAMIC_MODE) | |
210 CancelCLD2DataFilePolling(); | |
211 #endif | |
139 } | 212 } |
140 | 213 |
141 //////////////////////////////////////////////////////////////////////////////// | 214 //////////////////////////////////////////////////////////////////////////////// |
142 // TranslateHelper, protected: | 215 // TranslateHelper, protected: |
143 // | 216 // |
144 bool TranslateHelper::IsTranslateLibAvailable() { | 217 bool TranslateHelper::IsTranslateLibAvailable() { |
145 return ExecuteScriptAndGetBoolResult( | 218 return ExecuteScriptAndGetBoolResult( |
146 "typeof cr != 'undefined' && typeof cr.googleTranslate != 'undefined' && " | 219 "typeof cr != 'undefined' && typeof cr.googleTranslate != 'undefined' && " |
147 "typeof cr.googleTranslate.translate == 'function'", false); | 220 "typeof cr.googleTranslate.translate == 'function'", false); |
148 } | 221 } |
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
303 return false; | 376 return false; |
304 } | 377 } |
305 return true; | 378 return true; |
306 } | 379 } |
307 | 380 |
308 bool TranslateHelper::OnMessageReceived(const IPC::Message& message) { | 381 bool TranslateHelper::OnMessageReceived(const IPC::Message& message) { |
309 bool handled = true; | 382 bool handled = true; |
310 IPC_BEGIN_MESSAGE_MAP(TranslateHelper, message) | 383 IPC_BEGIN_MESSAGE_MAP(TranslateHelper, message) |
311 IPC_MESSAGE_HANDLER(ChromeViewMsg_TranslatePage, OnTranslatePage) | 384 IPC_MESSAGE_HANDLER(ChromeViewMsg_TranslatePage, OnTranslatePage) |
312 IPC_MESSAGE_HANDLER(ChromeViewMsg_RevertTranslation, OnRevertTranslation) | 385 IPC_MESSAGE_HANDLER(ChromeViewMsg_RevertTranslation, OnRevertTranslation) |
386 #if defined(CLD2_DYNAMIC_MODE) | |
387 IPC_MESSAGE_HANDLER(ChromeViewMsg_CLDDataAvailable, OnCLDDataAvailable); | |
388 #endif | |
313 IPC_MESSAGE_UNHANDLED(handled = false) | 389 IPC_MESSAGE_UNHANDLED(handled = false) |
314 IPC_END_MESSAGE_MAP() | 390 IPC_END_MESSAGE_MAP() |
315 return handled; | 391 return handled; |
316 } | 392 } |
317 | 393 |
318 void TranslateHelper::OnTranslatePage(int page_id, | 394 void TranslateHelper::OnTranslatePage(int page_id, |
319 const std::string& translate_script, | 395 const std::string& translate_script, |
320 const std::string& source_lang, | 396 const std::string& source_lang, |
321 const std::string& target_lang) { | 397 const std::string& target_lang) { |
322 WebFrame* main_frame = GetMainFrame(); | 398 WebFrame* main_frame = GetMainFrame(); |
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
492 | 568 |
493 WebFrame* TranslateHelper::GetMainFrame() { | 569 WebFrame* TranslateHelper::GetMainFrame() { |
494 WebView* web_view = render_view()->GetWebView(); | 570 WebView* web_view = render_view()->GetWebView(); |
495 | 571 |
496 // When the tab is going to be closed, the web_view can be NULL. | 572 // When the tab is going to be closed, the web_view can be NULL. |
497 if (!web_view) | 573 if (!web_view) |
498 return NULL; | 574 return NULL; |
499 | 575 |
500 return web_view->mainFrame(); | 576 return web_view->mainFrame(); |
501 } | 577 } |
578 | |
579 #if defined(CLD2_DYNAMIC_MODE) | |
580 void TranslateHelper::CancelCLD2DataFilePolling() { | |
581 cld2_data_file_polling_canceled = true; | |
582 } | |
583 | |
584 void TranslateHelper::SendCLD2DataFileRequest(int delay_millis, | |
585 int next_delay_millis) { | |
586 // We terminate immediately if we've been told to stop polling | |
587 if (cld2_data_file_polling_canceled) | |
588 return; | |
589 | |
590 // We terminate immediately if we've already loaded the data. | |
591 if (CLD2::isDataLoaded()) | |
592 return; | |
593 | |
594 // Else, send the IPC message to the browser process requesting the data... | |
595 Send(new ChromeViewHostMsg_NeedCLDData(routing_id())); | |
596 | |
597 // ... and enqueue another delayed task to call again. This will start a | |
598 // chain of polling that will last until the pointer stops being null, | |
599 // which is the right thing to do. | |
600 // NB: In the great majority of cases, the data file will be available and | |
601 // the very first delayed task will be a no-op that terminates the chain. | |
602 // It's only while downloading the file that we expect this to chain for a | |
603 // nontrivial amount of time. | |
604 // Use a weak pointer so that we don't keep this helper object around forever. | |
605 base::MessageLoop::current()->PostDelayedTask( | |
606 FROM_HERE, | |
607 base::Bind(&TranslateHelper::SendCLD2DataFileRequest, | |
608 weak_method_factory_.GetWeakPtr(), | |
609 next_delay_millis, next_delay_millis), | |
610 base::TimeDelta::FromMilliseconds(delay_millis)); | |
611 } | |
612 | |
613 void TranslateHelper::OnCLDDataAvailable( | |
614 IPC::PlatformFileForTransit ipc_file_handle) { | |
615 LoadCLDDData(ipc_file_handle); | |
616 if (deferred_page_capture_) { | |
bulach
2014/03/23 20:57:02
nit: remove one level:
if (deferred_page_capture_
Andrew Hayden (chromium.org)
2014/03/24 15:18:24
Done.
| |
617 if (CLD2::isDataLoaded()) { | |
618 // We have data available now, and we previously deferred a request to | |
619 // start translation. Finish that request up and clear our state. | |
620 // The real situation this translates to is as follows: | |
621 // The user loaded a page before the CLD data was available - either | |
622 // because the browser process took too long to respond to our CLD data | |
623 // request, or because the data truly hasn't been available; | |
624 // The data is now available, so we should get that language check | |
625 // done and post the message back to the browser ASAP. | |
626 deferred_page_capture_ = false; // Don't do this a second time. | |
627 PageCaptured(deferred_page_id_, deferred_contents_); | |
628 deferred_page_id_ = -1; // clean up for sanity | |
629 deferred_contents_.clear(); // clean up for sanity | |
630 } | |
631 } | |
632 } | |
633 | |
634 void TranslateHelper::LoadCLDDData( | |
635 IPC::PlatformFileForTransit ipc_file_handle) { | |
636 // We terminate immediately if we've been told to stop polling | |
637 if (cld2_data_file_polling_canceled) | |
638 return; | |
639 | |
640 // We terminate immediately if we've already loaded the data. | |
641 if (CLD2::isDataLoaded()) | |
642 return; | |
643 | |
644 // The mmap must outlive this function and must not be destroyed because | |
645 // the destructor for mmap will unmap the memory segment and close the file | |
646 // handle. Thus, declare it static. | |
647 static base::MemoryMappedFile* mmap = NULL; | |
bulach
2014/03/23 20:57:02
no function-scoped statics... what's the lifetime
Andrew Hayden (chromium.org)
2014/03/24 15:18:24
Yes. I'll make that change.
| |
648 | |
649 // Grab the file handle | |
650 base::PlatformFile platformFile = | |
651 IPC::PlatformFileForTransitToPlatformFile(ipc_file_handle); | |
652 DCHECK(platformFile > 0) << "Can't find the CLD data file!"; | |
653 base::File basicFile(platformFile); | |
bulach
2014/03/23 20:57:02
nit: s/basicFile/basic_file/
Andrew Hayden (chromium.org)
2014/03/24 15:18:24
Done.
| |
654 | |
655 // mmap the file | |
656 mmap = new base::MemoryMappedFile(); | |
657 bool loadedOk = mmap->Initialize(basicFile.Pass()); | |
bulach
2014/03/23 20:57:02
nit: s/loadedOk/loaded_ok/
perhaps "initialized" w
Andrew Hayden (chromium.org)
2014/03/24 15:18:24
Done.
| |
658 DCHECK(loadedOk) << "Failed to initialize CLD data mmap"; | |
659 if (!loadedOk) { | |
660 delete mmap; | |
661 mmap = NULL; | |
662 return; | |
663 } | |
664 | |
665 // Initialize the CLD subsystem... we're done! | |
666 CLD2::loadDataFromRawAddress(mmap->data(), mmap->length()); | |
667 DCHECK(CLD2::isDataLoaded()) << "Failed to load CLD data from mmap"; | |
668 } | |
669 #endif | |
OLD | NEW |