summaryrefslogtreecommitdiffstats
path: root/chrome/renderer
diff options
context:
space:
mode:
authorjcampan@chromium.org <jcampan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-01-30 00:40:58 +0000
committerjcampan@chromium.org <jcampan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2010-01-30 00:40:58 +0000
commit4c408fdba489ac7d42c0bf0128a6df832eec307d (patch)
tree6888c0213b9db8cadcfd2529beafd54b2bc8e942 /chrome/renderer
parent0b6e0a2ba1c90550d2dc4e33e2d0d282e09ebfdb (diff)
downloadchromium_src-4c408fdba489ac7d42c0bf0128a6df832eec307d.zip
chromium_src-4c408fdba489ac7d42c0bf0128a6df832eec307d.tar.gz
chromium_src-4c408fdba489ac7d42c0bf0128a6df832eec307d.tar.bz2
Implemented the undo translation in the renderer.
This reverts the page's text to its original (pre-translation) value. BUG=None TEST=None Review http://codereview.chromium.org/554104/show Review URL: http://codereview.chromium.org/555175 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@37583 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/renderer')
-rw-r--r--chrome/renderer/render_view.cc10
-rw-r--r--chrome/renderer/render_view.h4
-rw-r--r--chrome/renderer/translate/page_translator.cc22
-rw-r--r--chrome/renderer/translate/page_translator.h14
4 files changed, 50 insertions, 0 deletions
diff --git a/chrome/renderer/render_view.cc b/chrome/renderer/render_view.cc
index a27792f4..48e5ebf 100644
--- a/chrome/renderer/render_view.cc
+++ b/chrome/renderer/render_view.cc
@@ -1146,6 +1146,9 @@ void RenderView::UpdateURL(WebFrame* frame) {
host_zoom_levels_.erase(host);
}
+ // Drop the translated nodes.
+ page_translator_->NavigatedToNewPage();
+
// Update contents MIME type for main frame.
params.contents_mime_type = ds->response().mimeType().utf8();
@@ -3333,6 +3336,13 @@ 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 dfc4cdb..3ca5baa 100644
--- a/chrome/renderer/render_view.h
+++ b/chrome/renderer/render_view.h
@@ -699,6 +699,10 @@ 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 8119a8d..df0a59b 100644
--- a/chrome/renderer/translate/page_translator.cc
+++ b/chrome/renderer/translate/page_translator.cc
@@ -79,6 +79,8 @@ void PageTranslator::Translate(WebKit::WebFrame* web_frame,
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));
}
// Send the text for translation.
bool secure = static_cast<GURL>(web_frame->top()->url()).SchemeIsSecure();
@@ -89,6 +91,21 @@ void PageTranslator::Translate(WebKit::WebFrame* web_frame,
}
}
+void PageTranslator::NavigatedToNewPage() {
+ // We can drop all our states, they were related to the previous page.
+ ResetPageState();
+}
+
+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.
+ ResetPageState();
+}
+
bool PageTranslator::ShouldElementBeTraversed(WebKit::WebElement element) {
return ignored_tags_.find(element.tagName()) == ignored_tags_.end();
}
@@ -108,6 +125,11 @@ void PageTranslator::ClearNodeZone(int work_id) {
pending_translations_.erase(iter);
}
+void PageTranslator::ResetPageState() {
+ pending_translations_.clear();
+ text_nodes_.clear();
+}
+
void PageTranslator::TranslationError(int work_id, int error_id) {
// TODO(jcampan): may be we should show somehow that something went wrong to
// the user?
diff --git a/chrome/renderer/translate/page_translator.h b/chrome/renderer/translate/page_translator.h
index f7135be..14139227 100644
--- a/chrome/renderer/translate/page_translator.h
+++ b/chrome/renderer/translate/page_translator.h
@@ -41,6 +41,12 @@ class PageTranslator : public TextTranslator::Delegate {
std::string from_lang,
std::string to_lang);
+ // 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(
@@ -73,6 +79,9 @@ class PageTranslator : public TextTranslator::Delegate {
// Removes and deletes the NodeZone for |work_id| in pending_translations_.
void ClearNodeZone(int work_id);
+ // Clears all the states related to the page's contents.
+ void ResetPageState();
+
// The RenderView we are providing translations for.
RenderView* render_view_;
@@ -89,6 +98,11 @@ class PageTranslator : public TextTranslator::Delegate {
// Mapping from a translation engine work id to the associated nodes.
std::map<int, NodeList*> pending_translations_;
+ // 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_;
+
DISALLOW_COPY_AND_ASSIGN(PageTranslator);
};