From 4c408fdba489ac7d42c0bf0128a6df832eec307d Mon Sep 17 00:00:00 2001 From: "jcampan@chromium.org" Date: Sat, 30 Jan 2010 00:40:58 +0000 Subject: 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 --- chrome/renderer/translate/page_translator.cc | 22 ++++++++++++++++++++++ chrome/renderer/translate/page_translator.h | 14 ++++++++++++++ 2 files changed, 36 insertions(+) (limited to 'chrome/renderer/translate') 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(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(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::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 pending_translations_; + // The list of text nodes in the current page with their original text. + // Used to undo the translation. + typedef std::pair NodeTextPair; + std::vector text_nodes_; + DISALLOW_COPY_AND_ASSIGN(PageTranslator); }; -- cgit v1.1