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_helper.h" | 5 #include "chrome/renderer/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/logging.h" | 9 #include "base/logging.h" |
10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
11 #include "base/metrics/histogram.h" | 11 #include "base/metrics/histogram.h" |
12 #include "base/string16.h" | 12 #include "base/string16.h" |
13 #include "base/utf_string_conversions.h" | 13 #include "base/utf_string_conversions.h" |
14 #include "chrome/common/chrome_constants.h" | 14 #include "chrome/common/chrome_constants.h" |
15 #include "chrome/common/render_messages.h" | 15 #include "chrome/common/render_messages.h" |
16 #include "content/public/renderer/render_view.h" | 16 #include "content/public/renderer/render_view.h" |
17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" | 17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" |
18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h" | 18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h" |
19 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 19 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
20 #include "third_party/WebKit/Source/WebKit/chromium/public/WebScriptSource.h" | 20 #include "third_party/WebKit/Source/WebKit/chromium/public/WebScriptSource.h" |
21 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" | 21 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" |
22 #include "third_party/cld/encodings/compact_lang_det/win/cld_unicodetext.h" | |
23 #include "v8/include/v8.h" | 22 #include "v8/include/v8.h" |
24 #include "webkit/glue/dom_operations.h" | 23 #include "webkit/glue/dom_operations.h" |
25 | 24 |
| 25 #if defined(ENABLE_LANGUAGE_DETECTION) |
| 26 #include "third_party/cld/encodings/compact_lang_det/win/cld_unicodetext.h" |
| 27 #endif |
| 28 |
26 using WebKit::WebDocument; | 29 using WebKit::WebDocument; |
27 using WebKit::WebElement; | 30 using WebKit::WebElement; |
28 using WebKit::WebFrame; | 31 using WebKit::WebFrame; |
29 using WebKit::WebScriptSource; | 32 using WebKit::WebScriptSource; |
30 using WebKit::WebString; | 33 using WebKit::WebString; |
31 using WebKit::WebView; | 34 using WebKit::WebView; |
32 | 35 |
33 // The delay in milliseconds that we'll wait before checking to see if the | 36 // The delay in milliseconds that we'll wait before checking to see if the |
34 // translate library injected in the page is ready. | 37 // translate library injected in the page is ready. |
35 static const int kTranslateInitCheckDelayMs = 150; | 38 static const int kTranslateInitCheckDelayMs = 150; |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
73 // shouldn't affect translation. | 76 // shouldn't affect translation. |
74 std::string language = document.contentLanguage().utf8(); | 77 std::string language = document.contentLanguage().utf8(); |
75 size_t coma_index = language.find(','); | 78 size_t coma_index = language.find(','); |
76 if (coma_index != std::string::npos) { | 79 if (coma_index != std::string::npos) { |
77 // There are more than 1 language specified, just keep the first one. | 80 // There are more than 1 language specified, just keep the first one. |
78 language = language.substr(0, coma_index); | 81 language = language.substr(0, coma_index); |
79 } | 82 } |
80 TrimWhitespaceASCII(language, TRIM_ALL, &language); | 83 TrimWhitespaceASCII(language, TRIM_ALL, &language); |
81 language = StringToLowerASCII(language); | 84 language = StringToLowerASCII(language); |
82 | 85 |
| 86 #if defined(ENABLE_LANGUAGE_DETECTION) |
83 if (language.empty()) { | 87 if (language.empty()) { |
84 base::TimeTicks begin_time = base::TimeTicks::Now(); | 88 base::TimeTicks begin_time = base::TimeTicks::Now(); |
85 language = DetermineTextLanguage(contents); | 89 language = DetermineTextLanguage(contents); |
86 UMA_HISTOGRAM_MEDIUM_TIMES("Renderer4.LanguageDetection", | 90 UMA_HISTOGRAM_MEDIUM_TIMES("Renderer4.LanguageDetection", |
87 base::TimeTicks::Now() - begin_time); | 91 base::TimeTicks::Now() - begin_time); |
88 } else { | 92 } else { |
89 VLOG(9) << "PageLanguageFromMetaTag: " << language; | 93 VLOG(9) << "PageLanguageFromMetaTag: " << language; |
90 } | 94 } |
| 95 #else |
| 96 if (language.empty()) |
| 97 return; |
| 98 #endif // defined(ENABLE_LANGUAGE_DETECTION) |
91 | 99 |
92 Send(new ChromeViewHostMsg_TranslateLanguageDetermined( | 100 Send(new ChromeViewHostMsg_TranslateLanguageDetermined( |
93 routing_id(), language, IsPageTranslatable(&document))); | 101 routing_id(), language, IsPageTranslatable(&document))); |
94 } | 102 } |
95 | 103 |
96 void TranslateHelper::CancelPendingTranslation() { | 104 void TranslateHelper::CancelPendingTranslation() { |
97 weak_method_factory_.InvalidateWeakPtrs(); | 105 weak_method_factory_.InvalidateWeakPtrs(); |
98 translation_pending_ = false; | 106 translation_pending_ = false; |
99 page_id_ = -1; | 107 page_id_ = -1; |
100 source_lang_.clear(); | 108 source_lang_.clear(); |
(...skipping 13 matching lines...) Expand all Loading... |
114 if (attribute.isNull()) // We support both 'value' and 'content'. | 122 if (attribute.isNull()) // We support both 'value' and 'content'. |
115 attribute = iter->getAttribute("content"); | 123 attribute = iter->getAttribute("content"); |
116 if (attribute.isNull()) | 124 if (attribute.isNull()) |
117 continue; | 125 continue; |
118 if (LowerCaseEqualsASCII(attribute, "notranslate")) | 126 if (LowerCaseEqualsASCII(attribute, "notranslate")) |
119 return false; | 127 return false; |
120 } | 128 } |
121 return true; | 129 return true; |
122 } | 130 } |
123 | 131 |
| 132 #if defined(ENABLE_LANGUAGE_DETECTION) |
124 // static | 133 // static |
125 std::string TranslateHelper::DetermineTextLanguage(const string16& text) { | 134 std::string TranslateHelper::DetermineTextLanguage(const string16& text) { |
126 std::string language = chrome::kUnknownLanguageCode; | 135 std::string language = chrome::kUnknownLanguageCode; |
127 int num_languages = 0; | 136 int num_languages = 0; |
128 int text_bytes = 0; | 137 int text_bytes = 0; |
129 bool is_reliable = false; | 138 bool is_reliable = false; |
130 Language cld_language = | 139 Language cld_language = |
131 DetectLanguageOfUnicodeText(NULL, text.c_str(), true, &is_reliable, | 140 DetectLanguageOfUnicodeText(NULL, text.c_str(), true, &is_reliable, |
132 &num_languages, NULL, &text_bytes); | 141 &num_languages, NULL, &text_bytes); |
133 // We don't trust the result if the CLD reports that the detection is not | 142 // We don't trust the result if the CLD reports that the detection is not |
134 // reliable, or if the actual text used to detect the language was less than | 143 // reliable, or if the actual text used to detect the language was less than |
135 // 100 bytes (short texts can often lead to wrong results). | 144 // 100 bytes (short texts can often lead to wrong results). |
136 if (is_reliable && text_bytes >= 100 && cld_language != NUM_LANGUAGES && | 145 if (is_reliable && text_bytes >= 100 && cld_language != NUM_LANGUAGES && |
137 cld_language != UNKNOWN_LANGUAGE && cld_language != TG_UNKNOWN_LANGUAGE) { | 146 cld_language != UNKNOWN_LANGUAGE && cld_language != TG_UNKNOWN_LANGUAGE) { |
138 // We should not use LanguageCode_ISO_639_1 because it does not cover all | 147 // We should not use LanguageCode_ISO_639_1 because it does not cover all |
139 // the languages CLD can detect. As a result, it'll return the invalid | 148 // the languages CLD can detect. As a result, it'll return the invalid |
140 // language code for tradtional Chinese among others. | 149 // language code for tradtional Chinese among others. |
141 // |LanguageCodeWithDialect| will go through ISO 639-1, ISO-639-2 and | 150 // |LanguageCodeWithDialect| will go through ISO 639-1, ISO-639-2 and |
142 // 'other' tables to do the 'right' thing. In addition, it'll return zh-CN | 151 // 'other' tables to do the 'right' thing. In addition, it'll return zh-CN |
143 // for Simplified Chinese. | 152 // for Simplified Chinese. |
144 language = LanguageCodeWithDialects(cld_language); | 153 language = LanguageCodeWithDialects(cld_language); |
145 } | 154 } |
146 VLOG(9) << "Detected lang_id: " << language << ", from Text:\n" << text | 155 VLOG(9) << "Detected lang_id: " << language << ", from Text:\n" << text |
147 << "\n*************************************\n"; | 156 << "\n*************************************\n"; |
148 return language; | 157 return language; |
149 } | 158 } |
| 159 #endif // defined(ENABLE_LANGUAGE_DETECTION) |
150 | 160 |
151 //////////////////////////////////////////////////////////////////////////////// | 161 //////////////////////////////////////////////////////////////////////////////// |
152 // TranslateHelper, protected: | 162 // TranslateHelper, protected: |
153 // | 163 // |
154 bool TranslateHelper::IsTranslateLibAvailable() { | 164 bool TranslateHelper::IsTranslateLibAvailable() { |
155 bool lib_available = false; | 165 bool lib_available = false; |
156 if (!ExecuteScriptAndGetBoolResult( | 166 if (!ExecuteScriptAndGetBoolResult( |
157 "typeof cr != 'undefined' && typeof cr.googleTranslate != 'undefined' && " | 167 "typeof cr != 'undefined' && typeof cr.googleTranslate != 'undefined' && " |
158 "typeof cr.googleTranslate.translate == 'function'", &lib_available)) { | 168 "typeof cr.googleTranslate.translate == 'function'", &lib_available)) { |
159 NOTREACHED(); | 169 NOTREACHED(); |
(...skipping 266 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
426 WebView* web_view = render_view()->GetWebView(); | 436 WebView* web_view = render_view()->GetWebView(); |
427 if (!web_view) { | 437 if (!web_view) { |
428 // When the WebView is going away, the render view should have called | 438 // When the WebView is going away, the render view should have called |
429 // CancelPendingTranslation() which should have stopped any pending work, so | 439 // CancelPendingTranslation() which should have stopped any pending work, so |
430 // that case should not happen. | 440 // that case should not happen. |
431 NOTREACHED(); | 441 NOTREACHED(); |
432 return NULL; | 442 return NULL; |
433 } | 443 } |
434 return web_view->mainFrame(); | 444 return web_view->mainFrame(); |
435 } | 445 } |
OLD | NEW |