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

Side by Side Diff: components/translate/content/renderer/translate_helper.cc

Issue 2577203002: Remove use of WebNode/WebElement in translate_helper (Closed)
Patch Set: Rebase again Created 3 years, 11 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 | « no previous file | third_party/WebKit/Source/web/BUILD.gn » ('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 "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" 27 #include "third_party/WebKit/public/web/WebLanguageDetectionDetails.h"
28 #include "third_party/WebKit/public/web/WebLocalFrame.h" 28 #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" 29 #include "third_party/WebKit/public/web/WebScriptSource.h"
31 #include "url/gurl.h" 30 #include "url/gurl.h"
32 #include "v8/include/v8.h" 31 #include "v8/include/v8.h"
33 32
34 using base::ASCIIToUTF16; 33 using base::ASCIIToUTF16;
35 using blink::WebDocument; 34 using blink::WebDocument;
36 using blink::WebElement;
37 using blink::WebLocalFrame; 35 using blink::WebLocalFrame;
38 using blink::WebNode;
39 using blink::WebScriptSource; 36 using blink::WebScriptSource;
40 using blink::WebSecurityOrigin; 37 using blink::WebSecurityOrigin;
41 using blink::WebString; 38 using blink::WebString;
42 using blink::WebVector; 39 using blink::WebVector;
40 using blink::WebLanguageDetectionDetails;
43 41
44 namespace { 42 namespace {
45 43
46 // The delay in milliseconds that we'll wait before checking to see if the 44 // The delay in milliseconds that we'll wait before checking to see if the
47 // translate library injected in the page is ready. 45 // translate library injected in the page is ready.
48 const int kTranslateInitCheckDelayMs = 150; 46 const int kTranslateInitCheckDelayMs = 150;
49 47
50 // The maximum number of times we'll check to see if the translate library 48 // The maximum number of times we'll check to see if the translate library
51 // injected in the page is ready. 49 // injected in the page is ready.
52 const int kMaxTranslateInitCheckAttempts = 5; 50 const int kMaxTranslateInitCheckAttempts = 5;
53 51
54 // The delay we wait in milliseconds before checking whether the translation has 52 // The delay we wait in milliseconds before checking whether the translation has
55 // finished. 53 // finished.
56 const int kTranslateStatusCheckDelayMs = 400; 54 const int kTranslateStatusCheckDelayMs = 400;
57 55
58 // Language name passed to the Translate element for it to detect the language. 56 // Language name passed to the Translate element for it to detect the language.
59 const char kAutoDetectionLanguage[] = "auto"; 57 const char kAutoDetectionLanguage[] = "auto";
60 58
61 // Isolated world sets following content-security-policy. 59 // Isolated world sets following content-security-policy.
62 const char kContentSecurityPolicy[] = "script-src 'self' 'unsafe-eval'"; 60 const char kContentSecurityPolicy[] = "script-src 'self' 'unsafe-eval'";
63 61
64 // Returns whether the page associated with |document| is a candidate for
65 // translation. Some pages can explictly specify (via a meta-tag) that they
66 // should not be translated.
67 // TODO(dglazkov): This logic should be moved into Blink.
68 bool HasNoTranslateMeta(WebDocument* document) {
69 WebElement head = document->head();
70 if (head.isNull() || head.firstChild().isNull())
71 return false;
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;
100 }
101 return false;
102 }
103
104 } // namespace 62 } // namespace
105 63
106 namespace translate { 64 namespace translate {
107 65
108 //////////////////////////////////////////////////////////////////////////////// 66 ////////////////////////////////////////////////////////////////////////////////
109 // TranslateHelper, public: 67 // TranslateHelper, public:
110 TranslateHelper::TranslateHelper(content::RenderFrame* render_frame, 68 TranslateHelper::TranslateHelper(content::RenderFrame* render_frame,
111 int world_id, 69 int world_id,
112 const std::string& extension_scheme) 70 const std::string& extension_scheme)
113 : content::RenderFrameObserver(render_frame), 71 : content::RenderFrameObserver(render_frame),
(...skipping 18 matching lines...) Expand all
132 // original intent of http-equiv to be an equivalent) with the former 90 // original intent of http-equiv to be an equivalent) with the former
133 // being the language of the document and the latter being the 91 // being the language of the document and the latter being the
134 // language of the intended audience (a distinction really only 92 // language of the intended audience (a distinction really only
135 // relevant for things like langauge textbooks). This distinction 93 // relevant for things like langauge textbooks). This distinction
136 // shouldn't affect translation. 94 // shouldn't affect translation.
137 WebLocalFrame* main_frame = render_frame()->GetWebFrame(); 95 WebLocalFrame* main_frame = render_frame()->GetWebFrame();
138 if (!main_frame) 96 if (!main_frame)
139 return; 97 return;
140 98
141 WebDocument document = main_frame->document(); 99 WebDocument document = main_frame->document();
142 std::string content_language = document.contentLanguage().utf8(); 100 WebLanguageDetectionDetails web_detection_details =
143 WebElement html_element = document.documentElement(); 101 WebLanguageDetectionDetails::collectLanguageDetectionDetails(document);
144 std::string html_lang; 102 std::string content_language = web_detection_details.contentLanguage.utf8();
groby-ooo-7-16 2017/01/17 18:23:19 Sidebar: It's sad we need to jump through conversi
145 // |html_element| can be null element, e.g. in 103 std::string html_lang = web_detection_details.htmlLanguage.utf8();
146 // BrowserTest.WindowOpenClose.
147 if (!html_element.isNull())
148 html_lang = html_element.getAttribute("lang").utf8();
149 std::string cld_language; 104 std::string cld_language;
150 bool is_cld_reliable; 105 bool is_cld_reliable;
151 std::string language = DeterminePageLanguage( 106 std::string language = DeterminePageLanguage(
152 content_language, html_lang, contents, &cld_language, &is_cld_reliable); 107 content_language, html_lang, contents, &cld_language, &is_cld_reliable);
153 108
154 if (language.empty()) 109 if (language.empty())
155 return; 110 return;
156 111
157 language_determined_time_ = base::TimeTicks::Now(); 112 language_determined_time_ = base::TimeTicks::Now();
158 113
159 GURL url(document.url());
160 LanguageDetectionDetails details; 114 LanguageDetectionDetails details;
161 details.time = base::Time::Now(); 115 details.time = base::Time::Now();
162 details.url = url; 116 details.url = web_detection_details.url;
163 details.content_language = content_language; 117 details.content_language = content_language;
164 details.cld_language = cld_language; 118 details.cld_language = cld_language;
165 details.is_cld_reliable = is_cld_reliable; 119 details.is_cld_reliable = is_cld_reliable;
166 details.has_notranslate = HasNoTranslateMeta(&document); 120 details.has_notranslate = web_detection_details.hasNoTranslateMeta;
167 details.html_root_language = html_lang; 121 details.html_root_language = html_lang;
168 details.adopted_language = language; 122 details.adopted_language = language;
169 123
170 // TODO(hajimehoshi): If this affects performance, it should be set only if 124 // TODO(hajimehoshi): If this affects performance, it should be set only if
171 // translate-internals tab exists. 125 // translate-internals tab exists.
172 details.contents = contents; 126 details.contents = contents;
173 127
174 // For the same render frame with the same url, each time when its texts are 128 // For the same render frame with the same url, each time when its texts are
175 // captured, it should be treated as a new page to do translation. 129 // captured, it should be treated as a new page to do translation.
176 ResetPage(); 130 ResetPage();
(...skipping 291 matching lines...) Expand 10 before | Expand all | Expand 10 after
468 binding_.Close(); 422 binding_.Close();
469 translate_callback_pending_.Reset(); 423 translate_callback_pending_.Reset();
470 CancelPendingTranslation(); 424 CancelPendingTranslation();
471 } 425 }
472 426
473 void TranslateHelper::OnDestruct() { 427 void TranslateHelper::OnDestruct() {
474 delete this; 428 delete this;
475 } 429 }
476 430
477 } // namespace translate 431 } // namespace translate
OLDNEW
« no previous file with comments | « no previous file | third_party/WebKit/Source/web/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698