Chromium Code Reviews| 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 "components/translate/content/renderer/translate_helper.h" | 5 #include "components/translate/content/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/location.h" | 9 #include "base/location.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/metrics/histogram_macros.h" | 11 #include "base/metrics/histogram_macros.h" |
| 12 #include "base/single_thread_task_runner.h" | 12 #include "base/single_thread_task_runner.h" |
| 13 #include "base/strings/string16.h" | 13 #include "base/strings/string16.h" |
| 14 #include "base/strings/string_util.h" | 14 #include "base/strings/string_util.h" |
| 15 #include "base/strings/utf_string_conversions.h" | 15 #include "base/strings/utf_string_conversions.h" |
| 16 #include "base/threading/thread_task_runner_handle.h" | 16 #include "base/threading/thread_task_runner_handle.h" |
| 17 #include "components/translate/core/common/translate_constants.h" | 17 #include "components/translate/core/common/translate_constants.h" |
| 18 #include "components/translate/core/common/translate_metrics.h" | 18 #include "components/translate/core/common/translate_metrics.h" |
| 19 #include "components/translate/core/common/translate_util.h" | 19 #include "components/translate/core/common/translate_util.h" |
| 20 #include "components/translate/core/language_detection/language_detection_util.h " | 20 #include "components/translate/core/language_detection/language_detection_util.h " |
| 21 #include "content/public/common/content_constants.h" | 21 #include "content/public/common/content_constants.h" |
| 22 #include "content/public/common/url_constants.h" | 22 #include "content/public/common/url_constants.h" |
| 23 #include "content/public/renderer/render_frame.h" | 23 #include "content/public/renderer/render_frame.h" |
| 24 #include "content/public/renderer/render_thread.h" | 24 #include "content/public/renderer/render_thread.h" |
| 25 #include "services/service_manager/public/cpp/interface_provider.h" | 25 #include "services/service_manager/public/cpp/interface_provider.h" |
| 26 #include "third_party/WebKit/public/web/WebDocument.h" | 26 #include "third_party/WebKit/public/web/WebDocument.h" |
| 27 #include "third_party/WebKit/public/web/WebElement.h" | |
| 28 #include "third_party/WebKit/public/web/WebLocalFrame.h" | 27 #include "third_party/WebKit/public/web/WebLocalFrame.h" |
| 29 #include "third_party/WebKit/public/web/WebNode.h" | |
| 30 #include "third_party/WebKit/public/web/WebScriptSource.h" | 28 #include "third_party/WebKit/public/web/WebScriptSource.h" |
| 31 #include "url/gurl.h" | 29 #include "url/gurl.h" |
| 32 #include "v8/include/v8.h" | 30 #include "v8/include/v8.h" |
| 33 | 31 |
| 34 using base::ASCIIToUTF16; | 32 using base::ASCIIToUTF16; |
| 35 using blink::WebDocument; | 33 using blink::WebDocument; |
| 36 using blink::WebElement; | |
| 37 using blink::WebLocalFrame; | 34 using blink::WebLocalFrame; |
| 38 using blink::WebNode; | |
| 39 using blink::WebScriptSource; | 35 using blink::WebScriptSource; |
| 40 using blink::WebSecurityOrigin; | 36 using blink::WebSecurityOrigin; |
| 41 using blink::WebString; | 37 using blink::WebString; |
| 42 using blink::WebVector; | 38 using blink::WebVector; |
| 43 | 39 |
| 44 namespace { | 40 namespace { |
| 45 | 41 |
| 46 // The delay in milliseconds that we'll wait before checking to see if the | 42 // The delay in milliseconds that we'll wait before checking to see if the |
| 47 // translate library injected in the page is ready. | 43 // translate library injected in the page is ready. |
| 48 const int kTranslateInitCheckDelayMs = 150; | 44 const int kTranslateInitCheckDelayMs = 150; |
| 49 | 45 |
| 50 // The maximum number of times we'll check to see if the translate library | 46 // The maximum number of times we'll check to see if the translate library |
| 51 // injected in the page is ready. | 47 // injected in the page is ready. |
| 52 const int kMaxTranslateInitCheckAttempts = 5; | 48 const int kMaxTranslateInitCheckAttempts = 5; |
| 53 | 49 |
| 54 // The delay we wait in milliseconds before checking whether the translation has | 50 // The delay we wait in milliseconds before checking whether the translation has |
| 55 // finished. | 51 // finished. |
| 56 const int kTranslateStatusCheckDelayMs = 400; | 52 const int kTranslateStatusCheckDelayMs = 400; |
| 57 | 53 |
| 58 // Language name passed to the Translate element for it to detect the language. | 54 // Language name passed to the Translate element for it to detect the language. |
| 59 const char kAutoDetectionLanguage[] = "auto"; | 55 const char kAutoDetectionLanguage[] = "auto"; |
| 60 | 56 |
| 61 // Isolated world sets following content-security-policy. | 57 // Isolated world sets following content-security-policy. |
| 62 const char kContentSecurityPolicy[] = "script-src 'self' 'unsafe-eval'"; | 58 const char kContentSecurityPolicy[] = "script-src 'self' 'unsafe-eval'"; |
| 63 | 59 |
| 64 // Returns whether the page associated with |document| is a candidate for | 60 // Returns whether the page associated with |document| is a candidate for |
| 65 // translation. Some pages can explictly specify (via a meta-tag) that they | 61 // translation. Some pages can explictly specify (via a meta-tag) that they |
| 66 // should not be translated. | 62 // should not be translated. |
| 67 // TODO(dglazkov): This logic should be moved into Blink. | 63 // TODO(dglazkov): This logic should be moved into Blink. |
|
jbroman
2016/12/16 16:17:42
I think this CL fixes what this comment as looking
adithyas
2016/12/16 21:57:17
Done.
| |
| 68 bool HasNoTranslateMeta(WebDocument* document) { | 64 bool HasNoTranslateMeta(WebDocument* document) { |
| 69 WebElement head = document->head(); | 65 WebVector<WebString> values = document->getMetaValues(ASCIIToUTF16("google")); |
|
jbroman
2016/12/16 16:17:42
super-nit: ASCIIToUTF16 shouldn't be necessary her
adithyas
2016/12/16 21:57:17
Okay, changed it.
| |
| 70 if (head.isNull() || head.firstChild().isNull()) | 66 for (auto value : values) { |
|
jbroman
2016/12/16 16:17:42
nit: this unnecessarily copies WebString (which re
adithyas
2016/12/16 21:57:17
Changed to use a const reference and any_of.
| |
| 71 return false; | 67 if (base::LowerCaseEqualsASCII(base::StringPiece16(value), "notranslate")) |
| 72 | |
| 73 const WebString meta(ASCIIToUTF16("meta")); | |
| 74 const WebString name(ASCIIToUTF16("name")); | |
| 75 const WebString google(ASCIIToUTF16("google")); | |
| 76 const WebString value(ASCIIToUTF16("value")); | |
| 77 const WebString content(ASCIIToUTF16("content")); | |
| 78 | |
| 79 for (WebNode child = head.firstChild(); !child.isNull(); | |
| 80 child = child.nextSibling()) { | |
| 81 if (!child.isElementNode()) | |
| 82 continue; | |
| 83 WebElement element = child.to<WebElement>(); | |
| 84 // Check if a tag is <meta>. | |
| 85 if (!element.hasHTMLTagName(meta)) | |
| 86 continue; | |
| 87 // Check if the tag contains name="google". | |
| 88 WebString attribute = element.getAttribute(name); | |
| 89 if (attribute.isNull() || attribute != google) | |
| 90 continue; | |
| 91 // Check if the tag contains value="notranslate", or content="notranslate". | |
| 92 attribute = element.getAttribute(value); | |
| 93 if (attribute.isNull()) | |
| 94 attribute = element.getAttribute(content); | |
| 95 if (attribute.isNull()) | |
| 96 continue; | |
| 97 if (base::LowerCaseEqualsASCII(base::StringPiece16(attribute), | |
| 98 "notranslate")) | |
| 99 return true; | 68 return true; |
| 100 } | 69 } |
| 101 return false; | 70 return false; |
| 102 } | 71 } |
| 103 | 72 |
| 104 } // namespace | 73 } // namespace |
| 105 | 74 |
| 106 namespace translate { | 75 namespace translate { |
| 107 | 76 |
| 108 //////////////////////////////////////////////////////////////////////////////// | 77 //////////////////////////////////////////////////////////////////////////////// |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 135 // being the language of the document and the latter being the | 104 // being the language of the document and the latter being the |
| 136 // language of the intended audience (a distinction really only | 105 // language of the intended audience (a distinction really only |
| 137 // relevant for things like langauge textbooks). This distinction | 106 // relevant for things like langauge textbooks). This distinction |
| 138 // shouldn't affect translation. | 107 // shouldn't affect translation. |
| 139 WebLocalFrame* main_frame = render_frame()->GetWebFrame(); | 108 WebLocalFrame* main_frame = render_frame()->GetWebFrame(); |
| 140 if (!main_frame) | 109 if (!main_frame) |
| 141 return; | 110 return; |
| 142 | 111 |
| 143 WebDocument document = main_frame->document(); | 112 WebDocument document = main_frame->document(); |
| 144 std::string content_language = document.contentLanguage().utf8(); | 113 std::string content_language = document.contentLanguage().utf8(); |
| 145 WebElement html_element = document.documentElement(); | 114 std::string html_lang = document.documentLanguage().utf8(); |
| 146 std::string html_lang; | |
| 147 // |html_element| can be null element, e.g. in | |
| 148 // BrowserTest.WindowOpenClose. | |
| 149 if (!html_element.isNull()) | |
| 150 html_lang = html_element.getAttribute("lang").utf8(); | |
| 151 std::string cld_language; | 115 std::string cld_language; |
| 152 bool is_cld_reliable; | 116 bool is_cld_reliable; |
| 153 std::string language = DeterminePageLanguage( | 117 std::string language = DeterminePageLanguage( |
| 154 content_language, html_lang, contents, &cld_language, &is_cld_reliable); | 118 content_language, html_lang, contents, &cld_language, &is_cld_reliable); |
| 155 | 119 |
| 156 if (language.empty()) | 120 if (language.empty()) |
| 157 return; | 121 return; |
| 158 | 122 |
| 159 language_determined_time_ = base::TimeTicks::Now(); | 123 language_determined_time_ = base::TimeTicks::Now(); |
| 160 | 124 |
| (...skipping 313 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 474 binding_.Close(); | 438 binding_.Close(); |
| 475 translate_callback_pending_.Reset(); | 439 translate_callback_pending_.Reset(); |
| 476 CancelPendingTranslation(); | 440 CancelPendingTranslation(); |
| 477 } | 441 } |
| 478 | 442 |
| 479 void TranslateHelper::OnDestruct() { | 443 void TranslateHelper::OnDestruct() { |
| 480 delete this; | 444 delete this; |
| 481 } | 445 } |
| 482 | 446 |
| 483 } // namespace translate | 447 } // namespace translate |
| OLD | NEW |