summaryrefslogtreecommitdiffstats
path: root/chrome/renderer
diff options
context:
space:
mode:
authorjcampan@chromium.org <jcampan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-02-02 09:05:05 +0000
committerjcampan@chromium.org <jcampan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-02-02 09:05:05 +0000
commit4f4c43ca4eed4bff261f6e4ff760a02455ef50aa (patch)
tree0f6cdb74bf936c03fc73e3df43e73764e1f00866 /chrome/renderer
parent2278050814810fb79661792acff847fb2b806e17 (diff)
downloadchromium_src-4f4c43ca4eed4bff261f6e4ff760a02455ef50aa.zip
chromium_src-4f4c43ca4eed4bff261f6e4ff760a02455ef50aa.tar.gz
chromium_src-4f4c43ca4eed4bff261f6e4ff760a02455ef50aa.tar.bz2
Add the capability to translate an already translated page to
a different language. We are keeping the text nodes and their original text around. If another translate request arrives, we translate directly based on the text nodes and text stored. The text nodes and original text chunks are deleted when a navigation to a new page occurs. BUG=None TEST=Visit a page in a foreign language. Have it translated. On the resulting infobar, change the target language. Make sure the page is translated correctly to the new selected language. Review URL: http://codereview.chromium.org/567014 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@37815 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/renderer')
-rw-r--r--chrome/renderer/render_view.cc7
-rw-r--r--chrome/renderer/render_view.h4
-rw-r--r--chrome/renderer/translate/page_translator.cc68
-rw-r--r--chrome/renderer/translate/page_translator.h20
4 files changed, 52 insertions, 47 deletions
diff --git a/chrome/renderer/render_view.cc b/chrome/renderer/render_view.cc
index e959f14..c474e72 100644
--- a/chrome/renderer/render_view.cc
+++ b/chrome/renderer/render_view.cc
@@ -3401,13 +3401,6 @@ void RenderView::OnTranslateTextResponse(
text_translator_.OnTranslationResponse(work_id, error_id, text_chunks);
}
-void RenderView::OnUndoTranslate(int page_id) {
- if (page_id != page_id_)
- return; // Not the page we expected, nothing to do.
-
- page_translator_->UndoTranslation();
-}
-
void RenderView::OnInstallMissingPlugin() {
// This could happen when the first default plugin is deleted.
if (first_default_plugin_)
diff --git a/chrome/renderer/render_view.h b/chrome/renderer/render_view.h
index dbf4440..0c5ad52 100644
--- a/chrome/renderer/render_view.h
+++ b/chrome/renderer/render_view.h
@@ -724,10 +724,6 @@ class RenderView : public RenderWidget,
const std::string& source_lang,
const std::string& target_lang);
- // Tells the renderer to revert the page contents to its original
- // non-translated content.
- void OnUndoTranslate(int page_id);
-
// Message that provides the translated text for a request.
void OnTranslateTextResponse(int work_id,
int error_id,
diff --git a/chrome/renderer/translate/page_translator.cc b/chrome/renderer/translate/page_translator.cc
index ab4d806..9bed78e 100644
--- a/chrome/renderer/translate/page_translator.cc
+++ b/chrome/renderer/translate/page_translator.cc
@@ -48,7 +48,8 @@ PageTranslator::PageTranslator(TextTranslator* text_translator,
PageTranslatorDelegate* delegate)
: delegate_(delegate),
text_translator_(text_translator),
- page_id_(-1) {
+ page_id_(-1),
+ secure_page_(false) {
for (size_t i = 0; i < arraysize(kSkippedTags); ++i)
ignored_tags_.insert(WebKit::WebString(ASCIIToUTF16(kSkippedTags[i])));
for (size_t i = 0; i < arraysize(kInlineTags); ++i)
@@ -56,7 +57,7 @@ PageTranslator::PageTranslator(TextTranslator* text_translator,
}
PageTranslator::~PageTranslator() {
- ResetPageStates(); // This deletes pending translations.
+ ResetPageStates();
}
void PageTranslator::Translate(int page_id,
@@ -67,18 +68,35 @@ void PageTranslator::Translate(int page_id,
// This is a new page, our states are invalid.
ResetPageStates();
page_id_ = page_id;
+ secure_page_ = static_cast<GURL>(web_frame->top()->url()).SchemeIsSecure();
}
if (original_language_.empty()) {
original_language_ = source_lang;
current_language_ = source_lang;
}
+
if (original_language_ != current_language_) {
// The page has already been translated.
if (target_lang == current_language_) {
NOTREACHED();
return;
}
- // TODO(jcampan): implement translation of an already translated page.
+ // Any pending translation is now useless.
+ ClearPendingTranslations();
+ // No need to parse again the DOM, we have all the text nodes and their
+ // original text in |text_nodes_| and |text_chunks_|.
+ std::vector<NodeList*>::iterator text_nodes_iter = text_nodes_.begin();
+ std::vector<TextChunks*>::iterator text_chunks_iter = text_chunks_.begin();
+ for (;text_nodes_iter != text_nodes_.end();
+ ++text_nodes_iter, ++text_chunks_iter) {
+ DCHECK(text_chunks_iter != text_chunks_.end());
+ int work_id =
+ text_translator_->Translate(**text_chunks_iter,
+ source_lang, target_lang,
+ secure_page_, this);
+ pending_translations_[work_id] = *text_nodes_iter;
+ }
+ current_language_ = target_lang;
return;
}
@@ -93,26 +111,27 @@ void PageTranslator::Translate(int page_id,
for (iter = text_node_lists.begin(); iter != text_node_lists.end(); ++iter) {
if ((*iter)->empty()) {
// Nothing to translate.
- delete *iter;
continue;
}
- std::vector<string16> text_chunks; // The text chunks to translate.
- std::vector<WebKit::WebNode>::iterator text_nodes_iter;
+ TextChunks* text_chunks = new TextChunks; // The text chunks to translate.
+ NodeList::iterator text_nodes_iter;
for (text_nodes_iter = (*iter)->begin();
text_nodes_iter != (*iter)->end(); ++text_nodes_iter) {
DCHECK(text_nodes_iter->isTextNode());
string16 text = static_cast<string16>(text_nodes_iter->nodeValue());
DCHECK(!ContainsOnlyWhitespace(text));
- text_chunks.push_back(text);
- // Store the original text so we can undo the translation if requested.
- text_nodes_.push_back(NodeTextPair(*text_nodes_iter, text));
+ text_chunks->push_back(text);
}
+
// Send the text for translation.
- bool secure = static_cast<GURL>(web_frame->top()->url()).SchemeIsSecure();
int work_id =
- text_translator_->Translate(text_chunks, source_lang, target_lang,
- secure, this);
+ text_translator_->Translate(*text_chunks, source_lang, target_lang,
+ secure_page_, this);
pending_translations_[work_id] = *iter;
+ // Also store the text nodes and their original text so we can translate to
+ // another language if necessary.
+ text_nodes_.push_back(*iter);
+ text_chunks_.push_back(text_chunks);
}
}
@@ -121,19 +140,6 @@ void PageTranslator::NavigatedToNewPage() {
ResetPageStates();
}
-void PageTranslator::UndoTranslation() {
- // Revert all text nodes to their original contents.
- std::vector<NodeTextPair>::iterator iter;
- for (iter = text_nodes_.begin(); iter != text_nodes_.end(); ++iter)
- iter->first.setNodeValue(iter->second);
-
- // The page is back to its original content, we can dop all our states but the
- // page id.
- int page_id = page_id_;
- ResetPageStates();
- page_id_ = page_id;
-}
-
bool PageTranslator::ShouldElementBeTraversed(WebKit::WebElement element) {
return ignored_tags_.find(element.tagName()) == ignored_tags_.end();
}
@@ -149,7 +155,6 @@ void PageTranslator::ClearNodeZone(int work_id) {
"work id=" << work_id;
return;
}
- delete iter->second;
pending_translations_.erase(iter);
}
@@ -238,9 +243,14 @@ void PageTranslator::TraverseNode(WebKit::WebNode node,
void PageTranslator::ResetPageStates() {
page_id_ = -1;
+ secure_page_ = false;
+ STLDeleteElements(&text_nodes_);
+ STLDeleteElements(&text_chunks_);
original_language_.clear();
current_language_.clear();
- text_nodes_.clear();
- // Remove pending translations.
- STLDeleteValues(&pending_translations_);
+ ClearPendingTranslations();
+}
+
+void PageTranslator::ClearPendingTranslations() {
+ pending_translations_.clear();
}
diff --git a/chrome/renderer/translate/page_translator.h b/chrome/renderer/translate/page_translator.h
index 08de0b1..000ebca 100644
--- a/chrome/renderer/translate/page_translator.h
+++ b/chrome/renderer/translate/page_translator.h
@@ -58,9 +58,6 @@ class PageTranslator : public TextTranslator::Delegate {
// Notification that the associated RenderView has navigated to a new page.
void NavigatedToNewPage();
- // Reverts the page to its original non-translated contents.
- void UndoTranslation();
-
// TextTranslator::Delegate implentation:
virtual void TranslationError(int work_id, int error_id);
virtual void TextTranslated(
@@ -74,6 +71,7 @@ class PageTranslator : public TextTranslator::Delegate {
};
typedef std::vector<WebKit::WebNode> NodeList;
+ typedef std::vector<string16> TextChunks;
// Traverses the tree starting at |node| and fills |nodes| with the
// elements necessary for translation.
@@ -96,6 +94,10 @@ class PageTranslator : public TextTranslator::Delegate {
// Clears all the states related to the page's contents.
void ResetPageStates();
+ // Clears any pending translation requests. Any response for a pending
+ // request received after this call will be ignored.
+ void ClearPendingTranslations();
+
// Our delegate (notified when a page is translated).
PageTranslatorDelegate* delegate_;
@@ -121,10 +123,14 @@ class PageTranslator : public TextTranslator::Delegate {
// The page id of the page last time we translated.
int page_id_;
- // The list of text nodes in the current page with their original text.
- // Used to undo the translation.
- typedef std::pair<WebKit::WebNode, WebKit::WebString> NodeTextPair;
- std::vector<NodeTextPair> text_nodes_;
+ // True if the page is served over HTTPS.
+ bool secure_page_;
+
+ // The list of text zones in the current page, grouped in text zones (text
+ // nodes grouped in a same context).
+ std::vector<NodeList*> text_nodes_;
+ // The original text of the text nodes in |text_nodes_|.
+ std::vector<TextChunks*> text_chunks_;
DISALLOW_COPY_AND_ASSIGN(PageTranslator);
};