diff options
Diffstat (limited to 'chrome')
-rw-r--r-- | chrome/browser/renderer_host/translation_service.cc | 10 | ||||
-rw-r--r-- | chrome/browser/renderer_host/translation_service.h | 4 | ||||
-rw-r--r-- | chrome/renderer/translate/page_translator.cc | 20 | ||||
-rw-r--r-- | chrome/renderer/translate/page_translator.h | 3 |
4 files changed, 31 insertions, 6 deletions
diff --git a/chrome/browser/renderer_host/translation_service.cc b/chrome/browser/renderer_host/translation_service.cc index 095802d..6ddef41 100644 --- a/chrome/browser/renderer_host/translation_service.cc +++ b/chrome/browser/renderer_host/translation_service.cc @@ -134,6 +134,10 @@ class SendTranslationRequestTask : public CancelableTask { } // namespace // static +// The string is: '&' + kTextParam + '='. +size_t TranslationService::text_param_length_ = 1 + arraysize(kTextParam) + 1; + +// static base::LazyInstance<std::set<std::string> > TranslationService::supported_languages_(base::LINKER_INITIALIZED); @@ -260,8 +264,8 @@ void TranslationService::Translate(int routing_id, // after we updated the request. translation_request->send_query_task->Cancel(); translation_request->send_query_task = NULL; - if (translation_request->query.size() + text.size() >= - kTextRequestMaxSize) { + if (translation_request->query.size() + text.size() + + text_param_length_ >= kTextRequestMaxSize) { // The request would be too big with that last addition of text, send // the request now. (Single requests too big to be sent in 1 translation // request are dealt with below.) @@ -616,6 +620,8 @@ void TranslationService::AddTextToRequestString(std::string* request, request->append("=1"); } } + // IMPORTANT NOTE: if you make any change below, make sure to reflect them in + // text_param_length_ in TranslationService constructor. request->append("&"); request->append(kTextParam); request->append("="); diff --git a/chrome/browser/renderer_host/translation_service.h b/chrome/browser/renderer_host/translation_service.h index dc84b24..273a142 100644 --- a/chrome/browser/renderer_host/translation_service.h +++ b/chrome/browser/renderer_host/translation_service.h @@ -160,6 +160,10 @@ class TranslationService : public URLFetcher::Delegate { TranslationRequestMap pending_translation_requests_; TranslationRequestMap pending_secure_translation_requests_; + // The size taken by the parameters and separators needed when adding text to + // a request string. + static size_t text_param_length_; + // The language supported by the translation server. static base::LazyInstance<std::set<std::string> > supported_languages_; diff --git a/chrome/renderer/translate/page_translator.cc b/chrome/renderer/translate/page_translator.cc index 61f7281..e2c5692 100644 --- a/chrome/renderer/translate/page_translator.cc +++ b/chrome/renderer/translate/page_translator.cc @@ -31,6 +31,10 @@ const char* const kInlineTags[] = { "A", "ABBR", "ACRONYM", "B", "BIG", "DEL", "EM", "I", "INS", "S", "SPAN", "STRIKE", "STRONG", "SUB", "SUP", "U" }; } +// A text node containing only characters in kIgnoredCharacters is not +// translated. +const char* const kIgnoredCharacters = ":,.[|]0123456789"; + // Returns true when s1 < s2. bool PageTranslator::WebStringCompare::operator()( const WebKit::WebString& s1, const WebKit::WebString& s2) const { @@ -56,6 +60,8 @@ PageTranslator::PageTranslator(TextTranslator* text_translator, ignored_tags_.insert(WebKit::WebString(ASCIIToUTF16(kSkippedTags[i]))); for (size_t i = 0; i < arraysize(kInlineTags); ++i) inline_tags_.insert(WebKit::WebString(ASCIIToUTF16(kInlineTags[i]))); + ignore_characters_ = ASCIIToUTF16(kIgnoredCharacters); + ignore_characters_.append(kWhitespaceUTF16); } PageTranslator::~PageTranslator() { @@ -211,10 +217,14 @@ void PageTranslator::TextTranslated( NodeList* nodes = iter->second; // Check the integrity of the response. if (translated_text_chunks.size() != nodes->size()) { - // TODO(jcampan) reenable when we figured out why the server messed up the - // anchor tags. + // The server might merge or split chunks in some cases. + // TODO(jcampan): once the issue is resolved on the server, reenable that + // NOTREACHED(). // NOTREACHED() << "Translation results received are inconsistent with the " // "request"; + LOG(ERROR) << "translation response for work id " << work_id << + " length is " << translated_text_chunks.size() << " expected " << + nodes->size(); ClearNodeZone(work_id); return; } @@ -232,8 +242,10 @@ void PageTranslator::TraverseNode(WebKit::WebNode node, std::stack<NodeList*>* element_stack, std::vector<NodeList*>* text_nodes_list) { if (node.isTextNode()) { - if (ContainsOnlyWhitespace(static_cast<string16>(node.nodeValue()))) - return; // Ignore text nodes with only white-spaces. + string16 text = static_cast<string16>(node.nodeValue()); + if (ContainsOnlyChars(text, ignore_characters_)) + return; // Ignore text nodes which contains only white-spaces or + // separators. DCHECK(!element_stack->empty()); NodeList* text_nodes = element_stack->top(); diff --git a/chrome/renderer/translate/page_translator.h b/chrome/renderer/translate/page_translator.h index ed7836e..1d8bbc7 100644 --- a/chrome/renderer/translate/page_translator.h +++ b/chrome/renderer/translate/page_translator.h @@ -144,6 +144,9 @@ class PageTranslator : public TextTranslator::Delegate { // The original text of the text nodes in |text_nodes_|. std::vector<TextChunks*> text_chunks_; + // A text node containing only the characters in this list is not translated. + string16 ignore_characters_; + DISALLOW_COPY_AND_ASSIGN(PageTranslator); }; |