Linux: Depend on liberation-fonts package for RPMs.
[chromium-blink-merge.git] / components / translate / content / browser / content_translate_driver.cc
blob4f0e38a0dad8b49bad25c2d3f01720c0173b6f49
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"
7 #include "base/bind.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"
24 #include "url/gurl.h"
26 namespace {
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;
32 } // namespace
34 namespace translate {
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,
57 int attempt) {
58 if (translate_manager_->GetLanguageState().translation_pending())
59 return;
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
64 // has finished.
65 if (web_contents()->IsLoading() && attempt < max_reload_check_attempts_) {
66 int backoff = attempt * kMaxTranslateLoadCheckAttempts;
67 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
68 FROM_HERE,
69 base::Bind(&ContentTranslateDriver::InitiateTranslation,
70 weak_pointer_factory_.GetWeakPtr(), page_lang, attempt + 1),
71 base::TimeDelta::FromMilliseconds(backoff));
72 return;
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();
90 FOR_EACH_OBSERVER(
91 Observer, observer_list_, OnTranslateEnabledChanged(web_contents));
94 void ContentTranslateDriver::OnIsPageTranslatedChanged() {
95 content::WebContents* web_contents =
96 navigation_controller_->GetWebContents();
97 FOR_EACH_OBSERVER(
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(),
109 page_seq_no,
110 translate_script,
111 source_lang,
112 target_lang));
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(),
120 page_seq_no));
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,
145 content::Referrer(),
146 NEW_FOREGROUND_TAB,
147 ui::PAGE_TRANSITION_LINK,
148 false);
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();
162 if (!entry) {
163 NOTREACHED();
164 return;
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) {
171 return;
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.
179 return;
182 // If not a reload, return.
183 if (entry->GetTransitionType() != ui::PAGE_TRANSITION_RELOAD &&
184 load_details.type != content::NAVIGATION_TYPE_SAME_PAGE) {
185 return;
188 if (!translate_manager_->GetLanguageState().page_needs_translation())
189 return;
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(
196 FROM_HERE,
197 base::Bind(&ContentTranslateDriver::InitiateTranslation,
198 weak_pointer_factory_.GetWeakPtr(),
199 translate_manager_->GetLanguageState().original_language(),
200 0));
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.
208 const bool reload =
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) {
216 bool handled = true;
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()
225 return handled;
228 void ContentTranslateDriver::OnTranslateAssignedSequenceNumber(
229 int page_seq_no) {
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);
239 if (web_contents())
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);
251 FOR_EACH_OBSERVER(
252 Observer,
253 observer_list_,
254 OnPageTranslated(original_lang, translated_lang, error_type));
257 } // namespace translate