1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "components/translate/content/browser/content_translate_driver.h"
8 #include "base/location.h"
9 #include "base/logging.h"
10 #include "base/single_thread_task_runner.h"
11 #include "base/thread_task_runner_handle.h"
12 #include "components/translate/content/common/translate_messages.h"
13 #include "components/translate/core/browser/translate_download_manager.h"
14 #include "components/translate/core/browser/translate_manager.h"
15 #include "content/public/browser/browser_context.h"
16 #include "content/public/browser/navigation_controller.h"
17 #include "content/public/browser/navigation_details.h"
18 #include "content/public/browser/navigation_entry.h"
19 #include "content/public/browser/page_navigator.h"
20 #include "content/public/browser/render_view_host.h"
21 #include "content/public/browser/web_contents.h"
22 #include "content/public/common/referrer.h"
23 #include "net/http/http_status_code.h"
28 // The maximum number of attempts we'll do to see if the page has finshed
29 // loading before giving up the translation
30 const int kMaxTranslateLoadCheckAttempts
= 20;
36 ContentTranslateDriver::ContentTranslateDriver(
37 content::NavigationController
* nav_controller
)
38 : content::WebContentsObserver(nav_controller
->GetWebContents()),
39 navigation_controller_(nav_controller
),
40 translate_manager_(NULL
),
41 max_reload_check_attempts_(kMaxTranslateLoadCheckAttempts
),
42 weak_pointer_factory_(this) {
43 DCHECK(navigation_controller_
);
46 ContentTranslateDriver::~ContentTranslateDriver() {}
48 void ContentTranslateDriver::AddObserver(Observer
* observer
) {
49 observer_list_
.AddObserver(observer
);
52 void ContentTranslateDriver::RemoveObserver(Observer
* observer
) {
53 observer_list_
.RemoveObserver(observer
);
56 void ContentTranslateDriver::InitiateTranslation(const std::string
& page_lang
,
58 if (translate_manager_
->GetLanguageState().translation_pending())
61 // During a reload we need web content to be available before the
62 // translate script is executed. Otherwise we will run the translate script on
63 // an empty DOM which will fail. Therefore we wait a bit to see if the page
65 if (web_contents()->IsLoading() && attempt
< max_reload_check_attempts_
) {
66 int backoff
= attempt
* kMaxTranslateLoadCheckAttempts
;
67 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
69 base::Bind(&ContentTranslateDriver::InitiateTranslation
,
70 weak_pointer_factory_
.GetWeakPtr(), page_lang
, attempt
+ 1),
71 base::TimeDelta::FromMilliseconds(backoff
));
75 translate_manager_
->InitiateTranslation(
76 translate::TranslateDownloadManager::GetLanguageCode(page_lang
));
79 // TranslateDriver methods
81 bool ContentTranslateDriver::IsLinkNavigation() {
82 return navigation_controller_
&&
83 navigation_controller_
->GetLastCommittedEntry() &&
84 navigation_controller_
->GetLastCommittedEntry()->GetTransitionType() ==
85 ui::PAGE_TRANSITION_LINK
;
88 void ContentTranslateDriver::OnTranslateEnabledChanged() {
89 content::WebContents
* web_contents
= navigation_controller_
->GetWebContents();
91 Observer
, observer_list_
, OnTranslateEnabledChanged(web_contents
));
94 void ContentTranslateDriver::OnIsPageTranslatedChanged() {
95 content::WebContents
* web_contents
=
96 navigation_controller_
->GetWebContents();
98 Observer
, observer_list_
, OnIsPageTranslatedChanged(web_contents
));
101 void ContentTranslateDriver::TranslatePage(int page_seq_no
,
102 const std::string
& translate_script
,
103 const std::string
& source_lang
,
104 const std::string
& target_lang
) {
105 content::WebContents
* web_contents
= navigation_controller_
->GetWebContents();
106 web_contents
->GetRenderViewHost()->Send(
107 new ChromeViewMsg_TranslatePage(
108 web_contents
->GetRenderViewHost()->GetRoutingID(),
115 void ContentTranslateDriver::RevertTranslation(int page_seq_no
) {
116 content::WebContents
* web_contents
= navigation_controller_
->GetWebContents();
117 web_contents
->GetRenderViewHost()->Send(
118 new ChromeViewMsg_RevertTranslation(
119 web_contents
->GetRenderViewHost()->GetRoutingID(),
123 bool ContentTranslateDriver::IsOffTheRecord() {
124 return navigation_controller_
->GetBrowserContext()->IsOffTheRecord();
127 const std::string
& ContentTranslateDriver::GetContentsMimeType() {
128 return navigation_controller_
->GetWebContents()->GetContentsMimeType();
131 const GURL
& ContentTranslateDriver::GetLastCommittedURL() {
132 return navigation_controller_
->GetWebContents()->GetLastCommittedURL();
135 const GURL
& ContentTranslateDriver::GetVisibleURL() {
136 return navigation_controller_
->GetWebContents()->GetVisibleURL();
139 bool ContentTranslateDriver::HasCurrentPage() {
140 return (navigation_controller_
->GetLastCommittedEntry() != NULL
);
143 void ContentTranslateDriver::OpenUrlInNewTab(const GURL
& url
) {
144 content::OpenURLParams
params(url
,
147 ui::PAGE_TRANSITION_LINK
,
149 navigation_controller_
->GetWebContents()->OpenURL(params
);
152 // content::WebContentsObserver methods
154 void ContentTranslateDriver::NavigationEntryCommitted(
155 const content::LoadCommittedDetails
& load_details
) {
156 // Check whether this is a reload: When doing a page reload, the
157 // TranslateLanguageDetermined IPC is not sent so the translation needs to be
158 // explicitly initiated.
160 content::NavigationEntry
* entry
=
161 web_contents()->GetController().GetLastCommittedEntry();
167 // If the navigation happened while offline don't show the translate
168 // bar since there will be nothing to translate.
169 if (load_details
.http_status_code
== 0 ||
170 load_details
.http_status_code
== net::HTTP_INTERNAL_SERVER_ERROR
) {
174 if (!load_details
.is_main_frame
&&
175 translate_manager_
->GetLanguageState().translation_declined()) {
176 // Some sites (such as Google map) may trigger sub-frame navigations
177 // when the user interacts with the page. We don't want to show a new
178 // infobar if the user already dismissed one in that case.
182 // If not a reload, return.
183 if (entry
->GetTransitionType() != ui::PAGE_TRANSITION_RELOAD
&&
184 load_details
.type
!= content::NAVIGATION_TYPE_SAME_PAGE
) {
188 if (!translate_manager_
->GetLanguageState().page_needs_translation())
191 // Note that we delay it as the ordering of the processing of this callback
192 // by WebContentsObservers is undefined and might result in the current
193 // infobars being removed. Since the translation initiation process might add
194 // an infobar, it must be done after that.
195 base::ThreadTaskRunnerHandle::Get()->PostTask(
197 base::Bind(&ContentTranslateDriver::InitiateTranslation
,
198 weak_pointer_factory_
.GetWeakPtr(),
199 translate_manager_
->GetLanguageState().original_language(),
203 void ContentTranslateDriver::DidNavigateAnyFrame(
204 content::RenderFrameHost
* render_frame_host
,
205 const content::LoadCommittedDetails
& details
,
206 const content::FrameNavigateParams
& params
) {
207 // Let the LanguageState clear its state.
209 details
.entry
->GetTransitionType() == ui::PAGE_TRANSITION_RELOAD
||
210 details
.type
== content::NAVIGATION_TYPE_SAME_PAGE
;
211 translate_manager_
->GetLanguageState().DidNavigate(
212 details
.is_in_page
, details
.is_main_frame
, reload
);
215 bool ContentTranslateDriver::OnMessageReceived(const IPC::Message
& message
) {
217 IPC_BEGIN_MESSAGE_MAP(ContentTranslateDriver
, message
)
218 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_TranslateAssignedSequenceNumber
,
219 OnTranslateAssignedSequenceNumber
)
220 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_TranslateLanguageDetermined
,
221 OnLanguageDetermined
)
222 IPC_MESSAGE_HANDLER(ChromeViewHostMsg_PageTranslated
, OnPageTranslated
)
223 IPC_MESSAGE_UNHANDLED(handled
= false)
224 IPC_END_MESSAGE_MAP()
228 void ContentTranslateDriver::OnTranslateAssignedSequenceNumber(
230 translate_manager_
->set_current_seq_no(page_seq_no
);
233 void ContentTranslateDriver::OnLanguageDetermined(
234 const LanguageDetectionDetails
& details
,
235 bool page_needs_translation
) {
236 translate_manager_
->GetLanguageState().LanguageDetermined(
237 details
.adopted_language
, page_needs_translation
);
240 translate_manager_
->InitiateTranslation(details
.adopted_language
);
242 FOR_EACH_OBSERVER(Observer
, observer_list_
, OnLanguageDetermined(details
));
245 void ContentTranslateDriver::OnPageTranslated(
246 const std::string
& original_lang
,
247 const std::string
& translated_lang
,
248 TranslateErrors::Type error_type
) {
249 translate_manager_
->PageTranslated(
250 original_lang
, translated_lang
, error_type
);
254 OnPageTranslated(original_lang
, translated_lang
, error_type
));
257 } // namespace translate