summaryrefslogtreecommitdiffstats
path: root/chrome/renderer/translate/page_translator.cc
blob: 22e24be23fc7ca4552625645cd90ec2d7996d03e (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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
// 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 "chrome/renderer/navigation_state.h"
#include "third_party/WebKit/WebKit/chromium/public/WebDocument.h"
#include "third_party/WebKit/WebKit/chromium/public/WebDataSource.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" };
}

// 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 {
  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,
                               PageTranslatorDelegate* delegate)
    : delegate_(delegate),
      text_translator_(text_translator),
      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)
    inline_tags_.insert(WebKit::WebString(ASCIIToUTF16(kInlineTags[i])));
  ignore_characters_ = ASCIIToUTF16(kIgnoredCharacters);
  ignore_characters_.append(kWhitespaceUTF16);
}

PageTranslator::~PageTranslator() {
  ResetPageStates();
}

void PageTranslator::TranslatePage(int page_id,
                                   WebKit::WebFrame* main_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;
    secure_page_ = static_cast<GURL>(main_frame->top()->url()).SchemeIsSecure();

    original_language_ = source_lang;
    current_language_ = target_lang;

    // Translate all frames contained within the main-frame.
    for (WebKit::WebFrame* frame = main_frame;
         frame; frame = frame->traverseNext(false)) {
      TranslateFrame(frame);
    }
    return;
  }

  // The page has already been translated, the text nodes are already available.
  DCHECK(!text_nodes_.empty());

  if (target_lang == original_language_) {
    // Special case where we want to revert to the original language.
    RevertTranslation();
    return;
  }

  // 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;
}

void PageTranslator::TranslateFrame(WebKit::WebFrame* web_frame) {
  if (page_id_ == -1)
    return;  // The page has not been translated, ignore.

  DCHECK(!original_language_.empty() && !current_language_.empty());

  WebKit::WebDataSource* ds = web_frame->dataSource();
  NavigationState* navigation_state = NavigationState::FromDataSource(ds);
  DCHECK(navigation_state);
  if (navigation_state->was_translated())
    return;  // This frame has already been translated, nothing to do.

  // If the frame has no document or an empty document, it may not have been
  // loaded yet.
  if (web_frame->document().isNull() ||
      web_frame->document().childNodes().length() == 0) {
    return;
  }

  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.
      continue;
    }
    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);
    }

    // Send the text for translation.
    int work_id =
        text_translator_->Translate(*text_chunks,
                                    original_language_, current_language_,
                                    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);
  }

  navigation_state->set_was_translated(true);
}

void PageTranslator::MainFrameNavigated() {
  // We can drop all our states, they were related to the previous page.
  ResetPageStates();
}

bool PageTranslator::IsPageTranslated() {
  return original_language_ != current_language_;
}

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;
  }
  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()) {
    // 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;
  }

  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()) {
    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();
    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;
  secure_page_ = false;
  STLDeleteElements(&text_nodes_);
  STLDeleteElements(&text_chunks_);
  original_language_.clear();
  current_language_.clear();
  ClearPendingTranslations();
}

void PageTranslator::ClearPendingTranslations() {
  pending_translations_.clear();
}

void PageTranslator::RevertTranslation() {
  ClearPendingTranslations();

  DCHECK(!text_nodes_.empty());

  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());
    DCHECK((*text_nodes_iter)->size() == (*text_chunks_iter)->size());
    NodeList::iterator node_iter = (*text_nodes_iter)->begin();
    TextChunks::const_iterator text_iter = (*text_chunks_iter)->begin();
    for (; node_iter != (*text_nodes_iter)->end(); ++node_iter, ++text_iter) {
      node_iter->setNodeValue(*text_iter);
    }
  }
  current_language_ = original_language_;
}