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
|
// Copyright 2014 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 "components/translate/content/browser/content_translate_driver.h"
#include "base/bind.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/single_thread_task_runner.h"
#include "base/thread_task_runner_handle.h"
#include "components/translate/content/browser/browser_cld_data_provider.h"
#include "components/translate/content/browser/browser_cld_data_provider_factory.h"
#include "components/translate/content/common/translate_messages.h"
#include "components/translate/core/browser/translate_download_manager.h"
#include "components/translate/core/browser/translate_manager.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/navigation_controller.h"
#include "content/public/browser/navigation_details.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/page_navigator.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/referrer.h"
#include "net/http/http_status_code.h"
#include "url/gurl.h"
namespace {
// The maximum number of attempts we'll do to see if the page has finshed
// loading before giving up the translation
const int kMaxTranslateLoadCheckAttempts = 20;
} // namespace
namespace translate {
ContentTranslateDriver::ContentTranslateDriver(
content::NavigationController* nav_controller)
: content::WebContentsObserver(nav_controller->GetWebContents()),
navigation_controller_(nav_controller),
translate_manager_(NULL),
max_reload_check_attempts_(kMaxTranslateLoadCheckAttempts),
cld_data_provider_(
translate::BrowserCldDataProviderFactory::Get()->
CreateBrowserCldDataProvider(nav_controller->GetWebContents())),
weak_pointer_factory_(this) {
DCHECK(navigation_controller_);
}
ContentTranslateDriver::~ContentTranslateDriver() {}
void ContentTranslateDriver::AddObserver(Observer* observer) {
observer_list_.AddObserver(observer);
}
void ContentTranslateDriver::RemoveObserver(Observer* observer) {
observer_list_.RemoveObserver(observer);
}
void ContentTranslateDriver::InitiateTranslation(const std::string& page_lang,
int attempt) {
if (translate_manager_->GetLanguageState().translation_pending())
return;
// During a reload we need web content to be available before the
// translate script is executed. Otherwise we will run the translate script on
// an empty DOM which will fail. Therefore we wait a bit to see if the page
// has finished.
if (web_contents()->IsLoading() && attempt < max_reload_check_attempts_) {
int backoff = attempt * kMaxTranslateLoadCheckAttempts;
base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
FROM_HERE,
base::Bind(&ContentTranslateDriver::InitiateTranslation,
weak_pointer_factory_.GetWeakPtr(), page_lang, attempt + 1),
base::TimeDelta::FromMilliseconds(backoff));
return;
}
translate_manager_->InitiateTranslation(
translate::TranslateDownloadManager::GetLanguageCode(page_lang));
}
// TranslateDriver methods
bool ContentTranslateDriver::IsLinkNavigation() {
return navigation_controller_ &&
navigation_controller_->GetLastCommittedEntry() &&
navigation_controller_->GetLastCommittedEntry()->GetTransitionType() ==
ui::PAGE_TRANSITION_LINK;
}
void ContentTranslateDriver::OnTranslateEnabledChanged() {
content::WebContents* web_contents = navigation_controller_->GetWebContents();
FOR_EACH_OBSERVER(
Observer, observer_list_, OnTranslateEnabledChanged(web_contents));
}
void ContentTranslateDriver::OnIsPageTranslatedChanged() {
content::WebContents* web_contents =
navigation_controller_->GetWebContents();
FOR_EACH_OBSERVER(
Observer, observer_list_, OnIsPageTranslatedChanged(web_contents));
}
void ContentTranslateDriver::TranslatePage(int page_seq_no,
const std::string& translate_script,
const std::string& source_lang,
const std::string& target_lang) {
content::WebContents* web_contents = navigation_controller_->GetWebContents();
web_contents->GetMainFrame()->Send(new ChromeFrameMsg_TranslatePage(
web_contents->GetMainFrame()->GetRoutingID(), page_seq_no,
translate_script, source_lang, target_lang));
}
void ContentTranslateDriver::RevertTranslation(int page_seq_no) {
content::WebContents* web_contents = navigation_controller_->GetWebContents();
web_contents->GetMainFrame()->Send(new ChromeFrameMsg_RevertTranslation(
web_contents->GetMainFrame()->GetRoutingID(), page_seq_no));
}
bool ContentTranslateDriver::IsOffTheRecord() {
return navigation_controller_->GetBrowserContext()->IsOffTheRecord();
}
const std::string& ContentTranslateDriver::GetContentsMimeType() {
return navigation_controller_->GetWebContents()->GetContentsMimeType();
}
const GURL& ContentTranslateDriver::GetLastCommittedURL() {
return navigation_controller_->GetWebContents()->GetLastCommittedURL();
}
const GURL& ContentTranslateDriver::GetVisibleURL() {
return navigation_controller_->GetWebContents()->GetVisibleURL();
}
bool ContentTranslateDriver::HasCurrentPage() {
return (navigation_controller_->GetLastCommittedEntry() != NULL);
}
void ContentTranslateDriver::OpenUrlInNewTab(const GURL& url) {
content::OpenURLParams params(url,
content::Referrer(),
NEW_FOREGROUND_TAB,
ui::PAGE_TRANSITION_LINK,
false);
navigation_controller_->GetWebContents()->OpenURL(params);
}
// content::WebContentsObserver methods
void ContentTranslateDriver::NavigationEntryCommitted(
const content::LoadCommittedDetails& load_details) {
// Check whether this is a reload: When doing a page reload, the
// TranslateLanguageDetermined IPC is not sent so the translation needs to be
// explicitly initiated.
content::NavigationEntry* entry =
web_contents()->GetController().GetLastCommittedEntry();
if (!entry) {
NOTREACHED();
return;
}
// If the navigation happened while offline don't show the translate
// bar since there will be nothing to translate.
if (load_details.http_status_code == 0 ||
load_details.http_status_code == net::HTTP_INTERNAL_SERVER_ERROR) {
return;
}
if (!load_details.is_main_frame &&
translate_manager_->GetLanguageState().translation_declined()) {
// Some sites (such as Google map) may trigger sub-frame navigations
// when the user interacts with the page. We don't want to show a new
// infobar if the user already dismissed one in that case.
return;
}
// If not a reload, return.
if (entry->GetTransitionType() != ui::PAGE_TRANSITION_RELOAD &&
load_details.type != content::NAVIGATION_TYPE_SAME_PAGE) {
return;
}
if (!translate_manager_->GetLanguageState().page_needs_translation())
return;
// Note that we delay it as the ordering of the processing of this callback
// by WebContentsObservers is undefined and might result in the current
// infobars being removed. Since the translation initiation process might add
// an infobar, it must be done after that.
base::ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE,
base::Bind(&ContentTranslateDriver::InitiateTranslation,
weak_pointer_factory_.GetWeakPtr(),
translate_manager_->GetLanguageState().original_language(),
0));
}
void ContentTranslateDriver::DidNavigateAnyFrame(
content::RenderFrameHost* render_frame_host,
const content::LoadCommittedDetails& details,
const content::FrameNavigateParams& params) {
// Let the LanguageState clear its state.
const bool reload =
details.entry->GetTransitionType() == ui::PAGE_TRANSITION_RELOAD ||
details.type == content::NAVIGATION_TYPE_SAME_PAGE;
translate_manager_->GetLanguageState().DidNavigate(
details.is_in_page, details.is_main_frame, reload);
}
bool ContentTranslateDriver::OnMessageReceived(
const IPC::Message& message,
content::RenderFrameHost* render_frame_host) {
bool handled = true;
IPC_BEGIN_MESSAGE_MAP(ContentTranslateDriver, message)
IPC_MESSAGE_HANDLER(ChromeFrameHostMsg_TranslateAssignedSequenceNumber,
OnTranslateAssignedSequenceNumber)
IPC_MESSAGE_HANDLER(ChromeFrameHostMsg_TranslateLanguageDetermined,
OnLanguageDetermined)
IPC_MESSAGE_HANDLER(ChromeFrameHostMsg_PageTranslated, OnPageTranslated)
IPC_MESSAGE_UNHANDLED(handled = false)
IPC_END_MESSAGE_MAP()
if (!handled) {
// The CLD Data Provider may have its own embedder-specific communication,
// such as transferring the file handle for a CLD data file to the render
// process.
handled = cld_data_provider_->OnMessageReceived(message);
}
return handled;
}
void ContentTranslateDriver::OnTranslateAssignedSequenceNumber(
int page_seq_no) {
translate_manager_->set_current_seq_no(page_seq_no);
}
void ContentTranslateDriver::OnLanguageDetermined(
const LanguageDetectionDetails& details,
bool page_needs_translation) {
translate_manager_->GetLanguageState().LanguageDetermined(
details.adopted_language, page_needs_translation);
if (web_contents())
translate_manager_->InitiateTranslation(details.adopted_language);
FOR_EACH_OBSERVER(Observer, observer_list_, OnLanguageDetermined(details));
}
void ContentTranslateDriver::OnPageTranslated(
const std::string& original_lang,
const std::string& translated_lang,
TranslateErrors::Type error_type) {
translate_manager_->PageTranslated(
original_lang, translated_lang, error_type);
FOR_EACH_OBSERVER(
Observer,
observer_list_,
OnPageTranslated(original_lang, translated_lang, error_type));
}
} // namespace translate
|