summaryrefslogtreecommitdiffstats
path: root/chrome/renderer/translate/page_translator.cc
blob: f18de8a1120780b341719d24ad6a0c0d0ac728e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/renderer/translate/page_translator.h"

#include "base/compiler_specific.h"
#include "base/message_loop.h"
#include "base/stl_util-inl.h"
#include "base/string_util.h"
#include "base/task.h"
#include "third_party/WebKit/WebKit/chromium/public/WebDocument.h"
#include "third_party/WebKit/WebKit/chromium/public/WebElement.h"
#include "third_party/WebKit/WebKit/chromium/public/WebFrame.h"
#include "third_party/WebKit/WebKit/chromium/public/WebNode.h"
#include "third_party/WebKit/WebKit/chromium/public/WebNodeList.h"
#include "third_party/WebKit/WebKit/chromium/public/WebString.h"

namespace {

// The following elements are not supposed to be translated.
const char* const kSkippedTags[] = { "APPLET", "AREA", "BASE", "FRAME",
    "FRAMESET", "HR", "IFRAME", "IMG", "INPUT", "LINK", "META", "MAP",
    "OBJECT", "PARAM", "SCRIPT", "STYLE", "TEXTAREA" };

// The following tags are not considered as breaking a block of text.
// Notes: does SPAN belong to this list?
const char* const kInlineTags[] = { "A", "ABBR", "ACRONYM", "B", "BIG", "DEL",
    "EM", "I", "INS", "S", "SPAN", "STRIKE", "STRONG", "SUB", "SUP", "U" };
}

// Returns true when s1 < s2.
bool PageTranslator::WebStringCompare::operator()(
    const WebKit::WebString& s1, const WebKit::WebString& s2) const {
  int len1 = s1.length();
  int len2 = s2.length();
  int r = base::strncmp16(s1.data(), s2.data(), std::min(len1, len2));

  if (r < 0)
    return true;
  else if (r > 0)
    return false;

  return len1 < len2;
}

PageTranslator::PageTranslator(TextTranslator* text_translator,
                               Delegate* delegate)
    : delegate_(delegate),
      text_translator_(text_translator),
      page_id_(-1) {
  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)
    inline_tags_.insert(WebKit::WebString(ASCIIToUTF16(kInlineTags[i])));
}

PageTranslator::~PageTranslator() {
  ResetPageStates();  // This deletes pending translations.
}

void PageTranslator::Translate(int page_id,
                               WebKit::WebFrame* web_frame,
                               std::string source_lang,
                               std::string target_lang) {
  if (page_id != page_id_) {
    // This is a new page, our states are invalid.
    ResetPageStates();
    page_id_ = page_id;
  }
  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.
    return;
  }

  // We are about to start the translation process.
  current_language_ = target_lang;

  std::stack<NodeList*> node_list_stack;
  std::vector<NodeList*> text_node_lists;
  TraverseNode(web_frame->document(), &node_list_stack, &text_node_lists);

  std::vector<NodeList*>::iterator iter;
  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;
    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));
    }
    // 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);
    pending_translations_[work_id] = *iter;
  }
}

void PageTranslator::NavigatedToNewPage() {
  // We can drop all our states, they were related to the previous page.
  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();
}

bool PageTranslator::IsInlineElement(WebKit::WebElement element) {
  return inline_tags_.find(element.tagName()) != inline_tags_.end();
}

void PageTranslator::ClearNodeZone(int work_id) {
  std::map<int, NodeList*>::iterator iter = pending_translations_.find(work_id);
  if (iter == pending_translations_.end()) {
    NOTREACHED() << "Clearing unknown node zone in pending_translations_, "
        "work id=" << work_id;
    return;
  }
  delete iter->second;
  pending_translations_.erase(iter);
}

void PageTranslator::TranslationError(int work_id, int error_id) {
  // TODO(jcampan): may be we should show somehow that something went wrong to
  //                the user?
  ClearNodeZone(work_id);
}

void PageTranslator::TextTranslated(
    int work_id, const std::vector<string16>& translated_text_chunks) {
  std::map<int, NodeList*>::iterator iter = pending_translations_.find(work_id);
  if (iter == pending_translations_.end()) {
    // We received some translated text we were not expecting.  It could be we
    // navigated away from the page or that the translation was undone.
    return;
  }

  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.
    // NOTREACHED() << "Translation results received are inconsistent with the "
    //    "request";
    ClearNodeZone(work_id);
    return;
  }

  for (size_t i = 0; i < translated_text_chunks.size(); ++i)
    (*nodes)[i].setNodeValue(WebKit::WebString(translated_text_chunks[i]));

  ClearNodeZone(work_id);

  if (delegate_ && pending_translations_.empty())
    delegate_->PageTranslated(page_id_, original_language_, current_language_);
}

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.

    DCHECK(!element_stack->empty());
    NodeList* text_nodes = element_stack->top();
    if (text_nodes->empty()) {
      // This node zone is empty, meaning it has not yet been added to
      // |text_nodes|.
      text_nodes_list->push_back(text_nodes);
    }
    text_nodes->push_back(node);
    return;
  }

  if (!node.hasChildNodes())
    return;

  bool new_text_block = false;
  if (node.isElementNode()) {
    WebKit::WebElement element = node.toElement<WebKit::WebElement>();
    if (!ShouldElementBeTraversed(element))
      return;

    if (!IsInlineElement(element)) {
      new_text_block = true;
      NodeList* text_nodes = new NodeList();
      element_stack->push(text_nodes);
    }
  }

  WebKit::WebNodeList children = node.childNodes();
  for (size_t i = 0; i < children.length(); i++)
    TraverseNode(children.item(i), element_stack, text_nodes_list);

  if (new_text_block) {
    NodeList* text_nodes = element_stack->top();
    element_stack->pop();
    // If no nodes were added to text_nodes, then it has not been added to
    // text_nodes_list and must be deleted.
    if (text_nodes->empty())
      delete text_nodes;
  }
}

void PageTranslator::ResetPageStates() {
  page_id_ = -1;
  original_language_.clear();
  current_language_.clear();
  text_nodes_.clear();
  // Remove pending translations.
  STLDeleteValues(&pending_translations_);
}