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

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

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

Powered by Google App Engine
This is Rietveld 408576698