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

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

Issue 2577203002: Remove use of WebNode/WebElement in translate_helper (Closed)
Patch Set: Move translate specific DOM operations into separate file 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..943a044174df7dfefb95b921ca6fd0f8bf1b93a1 100644
--- a/components/translate/content/renderer/translate_helper.cc
+++ b/components/translate/content/renderer/translate_helper.cc
@@ -24,18 +24,16 @@
#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/WebLanguageDetectionDetails.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 +59,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 {
@@ -118,8 +76,7 @@ TranslateHelper::TranslateHelper(content::RenderFrame* render_frame,
binding_(this),
weak_method_factory_(this) {}
-TranslateHelper::~TranslateHelper() {
-}
+TranslateHelper::~TranslateHelper() {}
void TranslateHelper::PrepareForUrl(const GURL& url) {
// Navigated to a new url, reset current page translation.
@@ -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 =
+ WebLanguageDetectionDetails::collectLanguageDetectionDetails(document);
+
+ 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;
@@ -198,7 +152,8 @@ void TranslateHelper::CancelPendingTranslation() {
bool TranslateHelper::IsTranslateLibAvailable() {
return ExecuteScriptAndGetBoolResult(
"typeof cr != 'undefined' && typeof cr.googleTranslate != 'undefined' && "
- "typeof cr.googleTranslate.translate == 'function'", false);
+ "typeof cr.googleTranslate.translate == 'function'",
+ false);
}
bool TranslateHelper::IsTranslateLibReady() {
@@ -214,11 +169,8 @@ bool TranslateHelper::HasTranslationFailed() {
}
bool TranslateHelper::StartTranslation() {
- std::string script = "cr.googleTranslate.translate('" +
- source_lang_ +
- "','" +
- target_lang_ +
- "')";
+ std::string script = "cr.googleTranslate.translate('" + source_lang_ + "','" +
+ target_lang_ + "')";
return ExecuteScriptAndGetBoolResult(script, false);
}
@@ -238,8 +190,8 @@ void TranslateHelper::ExecuteScript(const std::string& script) {
return;
WebScriptSource source = WebScriptSource(ASCIIToUTF16(script));
- main_frame->executeScriptInIsolatedWorld(
- world_id_, &source, 1, extension_group_);
+ main_frame->executeScriptInIsolatedWorld(world_id_, &source, 1,
+ extension_group_);
}
bool TranslateHelper::ExecuteScriptAndGetBoolResult(const std::string& script,
@@ -249,10 +201,10 @@ bool TranslateHelper::ExecuteScriptAndGetBoolResult(const std::string& script,
return fallback;
v8::HandleScope handle_scope(v8::Isolate::GetCurrent());
- WebVector<v8::Local<v8::Value> > results;
+ WebVector<v8::Local<v8::Value>> results;
WebScriptSource source = WebScriptSource(ASCIIToUTF16(script));
- main_frame->executeScriptInIsolatedWorld(
- world_id_, &source, 1, extension_group_, &results);
+ main_frame->executeScriptInIsolatedWorld(world_id_, &source, 1,
+ extension_group_, &results);
if (results.size() != 1 || results[0].IsEmpty() || !results[0]->IsBoolean()) {
NOTREACHED();
return fallback;
@@ -268,10 +220,10 @@ std::string TranslateHelper::ExecuteScriptAndGetStringResult(
return std::string();
v8::HandleScope handle_scope(v8::Isolate::GetCurrent());
- WebVector<v8::Local<v8::Value> > results;
+ WebVector<v8::Local<v8::Value>> results;
WebScriptSource source = WebScriptSource(ASCIIToUTF16(script));
- main_frame->executeScriptInIsolatedWorld(
- world_id_, &source, 1, extension_group_, &results);
+ main_frame->executeScriptInIsolatedWorld(world_id_, &source, 1,
+ extension_group_, &results);
if (results.size() != 1 || results[0].IsEmpty() || !results[0]->IsString()) {
NOTREACHED();
return std::string();
@@ -291,10 +243,10 @@ double TranslateHelper::ExecuteScriptAndGetDoubleResult(
return 0.0;
v8::HandleScope handle_scope(v8::Isolate::GetCurrent());
- WebVector<v8::Local<v8::Value> > results;
+ WebVector<v8::Local<v8::Value>> results;
WebScriptSource source = WebScriptSource(ASCIIToUTF16(script));
- main_frame->executeScriptInIsolatedWorld(
- world_id_, &source, 1, extension_group_, &results);
+ main_frame->executeScriptInIsolatedWorld(world_id_, &source, 1,
+ extension_group_, &results);
if (results.size() != 1 || results[0].IsEmpty() || !results[0]->IsNumber()) {
NOTREACHED();
return 0.0;
« no previous file with comments | « no previous file | third_party/WebKit/Source/web/BUILD.gn » ('j') | third_party/WebKit/Source/web/WebLanguageDetectionDetails.cpp » ('J')

Powered by Google App Engine
This is Rietveld 408576698