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

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

Issue 1650303002: Move DOM-inspecting language detection logic to Blink. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebased. Created 4 years, 10 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 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 b42620a4dba10a248bd23ca28a541eb2b5a7e12e..7b80f7e02d76cf5a376bd581ad9465ff01c1968e 100644
--- a/components/translate/content/renderer/translate_helper.cc
+++ b/components/translate/content/renderer/translate_helper.cc
@@ -78,46 +78,6 @@ scoped_ptr<translate::RendererCldDataProvider> CreateDataProvider(
->CreateRendererCldDataProvider(render_frame_observer));
}
-// 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.hasChildNodes())
- 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 {
@@ -153,7 +113,8 @@ void TranslateHelper::PrepareForUrl(const GURL& url) {
page_seq_no_));
deferred_page_capture_ = false;
deferred_page_seq_no_ = -1;
- deferred_contents_.clear();
+ // TODO(dglazkov): This should be unnecessary.
+ detection_details_ = LanguageDetectionDetails();
if (cld_data_polling_started_)
return;
@@ -177,23 +138,23 @@ void TranslateHelper::PrepareForUrl(const GURL& url) {
TranslateHelper::SendCldDataRequest(0, 1000);
}
-void TranslateHelper::PageCaptured(const base::string16& contents) {
- PageCapturedImpl(page_seq_no_, contents);
+void TranslateHelper::LanguageDetectionDetailsCaptured(
+ const LanguageDetectionDetails& details) {
+ detection_details_ = details;
+ DetectLanguage(page_seq_no_);
}
-void TranslateHelper::PageCapturedImpl(int page_seq_no,
- const base::string16& contents) {
- // Get the document language as set by WebKit from the http-equiv
- // meta tag for "content-language". This may or may not also
- // have a value derived from the actual Content-Language HTTP
+void TranslateHelper::DetectLanguage(int page_seq_no) {
+ // Document language, set by WebKit in http-equiv
+ // meta tag for "content-language", may or may not also
+ // be the same as the value derived from the actual Content-Language HTTP
// header. The two actually have different meanings (despite the
// original intent of http-equiv to be an equivalent) with the former
// being the language of the document and the latter being the
// language of the intended audience (a distinction really only
// relevant for things like langauge textbooks). This distinction
// shouldn't affect translation.
- WebLocalFrame* main_frame = render_frame()->GetWebFrame();
- if (!main_frame || page_seq_no_ != page_seq_no)
+ if (page_seq_no_ != page_seq_no)
return;
if (!cld_data_provider_->IsCldDataAvailable()) {
@@ -201,7 +162,6 @@ void TranslateHelper::PageCapturedImpl(int page_seq_no,
// is loaded, if ever.
deferred_page_capture_ = true;
deferred_page_seq_no_ = page_seq_no;
- deferred_contents_ = contents;
RecordLanguageDetectionTiming(DEFERRED);
return;
}
@@ -215,41 +175,31 @@ void TranslateHelper::PageCapturedImpl(int page_seq_no,
RecordLanguageDetectionTiming(RESUMED);
}
- 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();
std::string cld_language;
bool is_cld_reliable;
std::string language = DeterminePageLanguage(
- content_language, html_lang, contents, &cld_language, &is_cld_reliable);
+ detection_details_.content_language,
+ detection_details_.html_root_language, detection_details_.contents,
+ &cld_language, &is_cld_reliable);
if (language.empty())
return;
language_determined_time_ = base::TimeTicks::Now();
+ detection_details_.time = base::Time::Now();
+ detection_details_.cld_language = cld_language;
+ detection_details_.is_cld_reliable = is_cld_reliable;
+ detection_details_.adopted_language = language;
- GURL url(document.url());
- LanguageDetectionDetails details;
- details.time = base::Time::Now();
- details.url = url;
- details.content_language = content_language;
- details.cld_language = cld_language;
- details.is_cld_reliable = is_cld_reliable;
- details.has_notranslate = HasNoTranslateMeta(&document);
- details.html_root_language = html_lang;
- details.adopted_language = language;
-
- // TODO(hajimehoshi): If this affects performance, it should be set only if
- // translate-internals tab exists.
- details.contents = contents;
+ // TODO(hajimehoshi): If this affects performance, details.contents should be
+ // sent
+ // only if translate-internals tab exists.
Send(new ChromeFrameHostMsg_TranslateLanguageDetermined(
- routing_id(), details, !details.has_notranslate && !language.empty()));
+ routing_id(), detection_details_,
+ !detection_details_.has_notranslate && !language.empty()));
+
+ detection_details_ = LanguageDetectionDetails(); // Clean up for sanity
}
void TranslateHelper::CancelPendingTranslation() {
@@ -606,9 +556,8 @@ void TranslateHelper::SendCldDataRequest(const int delay_millis,
void TranslateHelper::OnCldDataAvailable() {
if (deferred_page_capture_) {
deferred_page_capture_ = false; // Don't do this a second time.
- PageCapturedImpl(deferred_page_seq_no_, deferred_contents_);
+ DetectLanguage(deferred_page_seq_no_);
deferred_page_seq_no_ = -1; // Clean up for sanity
- deferred_contents_.clear(); // Clean up for sanity
}
}

Powered by Google App Engine
This is Rietveld 408576698