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

Unified Diff: components/translate/content/renderer/translate_helper.cc

Issue 2577203002: Remove use of WebNode/WebElement in translate_helper (Closed)
Patch Set: Add WebLanguageDetectionDetails Created 4 years 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 side-by-side diff with in-line comments
Download patch
Index: components/translate/content/renderer/translate_helper.cc
diff --git a/components/translate/content/renderer/translate_helper.cc b/components/translate/content/renderer/translate_helper.cc
index 23c251943d3078cf75376e9f5ae7fff5cce9847c..e494a71006b0e25c3c2dec81d4d2edaa08a3ad4c 100644
--- a/components/translate/content/renderer/translate_helper.cc
+++ b/components/translate/content/renderer/translate_helper.cc
@@ -24,18 +24,15 @@
#include "content/public/renderer/render_thread.h"
#include "services/service_manager/public/cpp/interface_provider.h"
#include "third_party/WebKit/public/web/WebDocument.h"
-#include "third_party/WebKit/public/web/WebElement.h"
#include "third_party/WebKit/public/web/WebLocalFrame.h"
-#include "third_party/WebKit/public/web/WebNode.h"
#include "third_party/WebKit/public/web/WebScriptSource.h"
#include "url/gurl.h"
#include "v8/include/v8.h"
using base::ASCIIToUTF16;
using blink::WebDocument;
-using blink::WebElement;
+using blink::WebLanguageDetectionDetails;
using blink::WebLocalFrame;
-using blink::WebNode;
using blink::WebScriptSource;
using blink::WebSecurityOrigin;
using blink::WebString;
@@ -61,46 +58,6 @@ const char kAutoDetectionLanguage[] = "auto";
// Isolated world sets following content-security-policy.
const char kContentSecurityPolicy[] = "script-src 'self' 'unsafe-eval'";
-// Returns whether the page associated with |document| is a candidate for
-// translation. Some pages can explictly specify (via a meta-tag) that they
-// should not be translated.
-// TODO(dglazkov): This logic should be moved into Blink.
-bool HasNoTranslateMeta(WebDocument* document) {
- WebElement head = document->head();
- if (head.isNull() || head.firstChild().isNull())
- return false;
-
- const WebString meta(ASCIIToUTF16("meta"));
- const WebString name(ASCIIToUTF16("name"));
- const WebString google(ASCIIToUTF16("google"));
- const WebString value(ASCIIToUTF16("value"));
- const WebString content(ASCIIToUTF16("content"));
-
- for (WebNode child = head.firstChild(); !child.isNull();
- child = child.nextSibling()) {
- if (!child.isElementNode())
- continue;
- WebElement element = child.to<WebElement>();
- // Check if a tag is <meta>.
- if (!element.hasHTMLTagName(meta))
- continue;
- // Check if the tag contains name="google".
- WebString attribute = element.getAttribute(name);
- if (attribute.isNull() || attribute != google)
- continue;
- // Check if the tag contains value="notranslate", or content="notranslate".
- attribute = element.getAttribute(value);
- if (attribute.isNull())
- attribute = element.getAttribute(content);
- if (attribute.isNull())
- continue;
- if (base::LowerCaseEqualsASCII(base::StringPiece16(attribute),
- "notranslate"))
- return true;
- }
- return false;
-}
-
} // namespace
namespace translate {
@@ -141,13 +98,11 @@ void TranslateHelper::PageCaptured(const base::string16& contents) {
return;
WebDocument document = main_frame->document();
- std::string content_language = document.contentLanguage().utf8();
- WebElement html_element = document.documentElement();
- std::string html_lang;
- // |html_element| can be null element, e.g. in
- // BrowserTest.WindowOpenClose.
- if (!html_element.isNull())
- html_lang = html_element.getAttribute("lang").utf8();
+ WebLanguageDetectionDetails web_detection_details =
+ document.collectLanguageDetectionDetails();
+
+ std::string content_language = web_detection_details.contentLanguage.utf8();
+ std::string html_lang = web_detection_details.htmlLanguage.utf8();
std::string cld_language;
bool is_cld_reliable;
std::string language = DeterminePageLanguage(
@@ -158,14 +113,13 @@ void TranslateHelper::PageCaptured(const base::string16& contents) {
language_determined_time_ = base::TimeTicks::Now();
- GURL url(document.url());
LanguageDetectionDetails details;
details.time = base::Time::Now();
- details.url = url;
+ details.url = web_detection_details.url;
details.content_language = content_language;
details.cld_language = cld_language;
details.is_cld_reliable = is_cld_reliable;
- details.has_notranslate = HasNoTranslateMeta(&document);
+ details.has_notranslate = web_detection_details.hasNoTranslateMeta;
details.html_root_language = html_lang;
details.adopted_language = language;
« no previous file with comments | « no previous file | third_party/WebKit/Source/core/dom/Document.h » ('j') | third_party/WebKit/Source/core/dom/Document.cpp » ('J')

Powered by Google App Engine
This is Rietveld 408576698