Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/translate/translate_manager.h" | 5 #include "chrome/browser/translate/translate_manager.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
| 9 #include "base/memory/singleton.h" | |
| 10 #include "base/metrics/field_trial.h" | 9 #include "base/metrics/field_trial.h" |
| 11 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
| 12 #include "base/prefs/pref_service.h" | 11 #include "base/prefs/pref_service.h" |
| 13 #include "base/strings/string_split.h" | 12 #include "base/strings/string_split.h" |
| 14 #include "base/strings/stringprintf.h" | 13 #include "base/strings/stringprintf.h" |
| 15 #include "base/time/time.h" | 14 #include "base/time/time.h" |
| 16 #include "chrome/browser/browser_process.h" | 15 #include "chrome/browser/browser_process.h" |
| 17 #include "chrome/browser/chrome_notification_types.h" | 16 #include "chrome/browser/chrome_notification_types.h" |
| 18 #include "chrome/browser/profiles/profile.h" | 17 #include "chrome/browser/profiles/profile.h" |
| 19 #include "chrome/browser/tab_contents/tab_util.h" | 18 #include "chrome/browser/tab_contents/tab_util.h" |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 83 // Notifies |g_callback_list_| of translate errors. | 82 // Notifies |g_callback_list_| of translate errors. |
| 84 void NotifyTranslateError(const TranslateErrorDetails& details) { | 83 void NotifyTranslateError(const TranslateErrorDetails& details) { |
| 85 if (!g_callback_list_) | 84 if (!g_callback_list_) |
| 86 return; | 85 return; |
| 87 | 86 |
| 88 g_callback_list_->Notify(details); | 87 g_callback_list_->Notify(details); |
| 89 } | 88 } |
| 90 | 89 |
| 91 } // namespace | 90 } // namespace |
| 92 | 91 |
| 93 TranslateManager::~TranslateManager() { | 92 TranslateManager::~TranslateManager() {} |
| 94 } | |
| 95 | |
| 96 // static | |
| 97 TranslateManager* TranslateManager::GetInstance() { | |
| 98 return Singleton<TranslateManager>::get(); | |
| 99 } | |
| 100 | 93 |
| 101 // static | 94 // static |
| 102 bool TranslateManager::IsTranslatableURL(const GURL& url) { | 95 bool TranslateManager::IsTranslatableURL(const GURL& url) { |
| 103 // A URLs is translatable unless it is one of the following: | 96 // A URLs is translatable unless it is one of the following: |
| 104 // - empty (can happen for popups created with window.open("")) | 97 // - empty (can happen for popups created with window.open("")) |
| 105 // - an internal URL (chrome:// and others) | 98 // - an internal URL (chrome:// and others) |
| 106 // - the devtools (which is considered UI) | 99 // - the devtools (which is considered UI) |
| 107 // - Chrome OS file manager extension | 100 // - Chrome OS file manager extension |
| 108 // - an FTP page (as FTP pages tend to have long lists of filenames that may | 101 // - an FTP page (as FTP pages tend to have long lists of filenames that may |
| 109 // confuse the CLD) | 102 // confuse the CLD) |
| 110 return !url.is_empty() && | 103 return !url.is_empty() && |
| 111 !url.SchemeIs(content::kChromeUIScheme) && | 104 !url.SchemeIs(content::kChromeUIScheme) && |
| 112 !url.SchemeIs(content::kChromeDevToolsScheme) && | 105 !url.SchemeIs(content::kChromeDevToolsScheme) && |
| 113 #if defined(OS_CHROMEOS) | 106 #if defined(OS_CHROMEOS) |
| 114 !(url.SchemeIs(extensions::kExtensionScheme) && | 107 !(url.SchemeIs(extensions::kExtensionScheme) && |
| 115 url.DomainIs(file_manager::kFileManagerAppId)) && | 108 url.DomainIs(file_manager::kFileManagerAppId)) && |
| 116 #endif | 109 #endif |
| 117 !url.SchemeIs(content::kFtpScheme); | 110 !url.SchemeIs(content::kFtpScheme); |
| 118 } | 111 } |
| 119 | 112 |
| 120 void TranslateManager::Observe(int type, | 113 void TranslateManager::Observe(int type, |
| 121 const content::NotificationSource& source, | 114 const content::NotificationSource& source, |
| 122 const content::NotificationDetails& details) { | 115 const content::NotificationDetails& details) { |
| 123 switch (type) { | 116 switch (type) { |
| 124 case content::NOTIFICATION_NAV_ENTRY_COMMITTED: { | 117 case content::NOTIFICATION_NAV_ENTRY_COMMITTED: { |
| 125 NavigationController* controller = | 118 NavigationController* controller = |
| 126 content::Source<NavigationController>(source).ptr(); | 119 content::Source<NavigationController>(source).ptr(); |
| 120 DCHECK_EQ(&translate_tab_helper_->GetWebContents()->GetController(), | |
| 121 controller); | |
| 127 content::LoadCommittedDetails* load_details = | 122 content::LoadCommittedDetails* load_details = |
| 128 content::Details<content::LoadCommittedDetails>(details).ptr(); | 123 content::Details<content::LoadCommittedDetails>(details).ptr(); |
| 129 NavigationEntry* entry = controller->GetActiveEntry(); | 124 NavigationEntry* entry = controller->GetActiveEntry(); |
| 130 if (!entry) { | 125 if (!entry) { |
| 131 NOTREACHED(); | 126 NOTREACHED(); |
| 132 return; | 127 return; |
| 133 } | 128 } |
| 134 | 129 |
| 135 TranslateTabHelper* translate_tab_helper = | |
| 136 TranslateTabHelper::FromWebContents(controller->GetWebContents()); | |
| 137 if (!translate_tab_helper) | |
| 138 return; | |
| 139 | |
| 140 // If the navigation happened while offline don't show the translate | 130 // If the navigation happened while offline don't show the translate |
| 141 // bar since there will be nothing to translate. | 131 // bar since there will be nothing to translate. |
| 142 if (load_details->http_status_code == 0 || | 132 if (load_details->http_status_code == 0 || |
| 143 load_details->http_status_code == net::HTTP_INTERNAL_SERVER_ERROR) { | 133 load_details->http_status_code == net::HTTP_INTERNAL_SERVER_ERROR) { |
| 144 return; | 134 return; |
| 145 } | 135 } |
| 146 | 136 |
| 147 if (!load_details->is_main_frame && | 137 if (!load_details->is_main_frame && |
| 148 translate_tab_helper->GetLanguageState().translation_declined()) { | 138 translate_tab_helper_->GetLanguageState().translation_declined()) { |
| 149 // Some sites (such as Google map) may trigger sub-frame navigations | 139 // Some sites (such as Google map) may trigger sub-frame navigations |
| 150 // when the user interacts with the page. We don't want to show a new | 140 // when the user interacts with the page. We don't want to show a new |
| 151 // infobar if the user already dismissed one in that case. | 141 // infobar if the user already dismissed one in that case. |
| 152 return; | 142 return; |
| 153 } | 143 } |
| 154 if (entry->GetTransitionType() != content::PAGE_TRANSITION_RELOAD && | 144 if (entry->GetTransitionType() != content::PAGE_TRANSITION_RELOAD && |
| 155 load_details->type != content::NAVIGATION_TYPE_SAME_PAGE) { | 145 load_details->type != content::NAVIGATION_TYPE_SAME_PAGE) { |
| 156 return; | 146 return; |
| 157 } | 147 } |
| 158 | 148 |
| 159 // When doing a page reload, TAB_LANGUAGE_DETERMINED is not sent, | 149 // When doing a page reload, TAB_LANGUAGE_DETERMINED is not sent, |
| 160 // so the translation needs to be explicitly initiated, but only when the | 150 // so the translation needs to be explicitly initiated, but only when the |
| 161 // page needs translation. | 151 // page needs translation. |
| 162 if (!translate_tab_helper->GetLanguageState().page_needs_translation()) | 152 if (!translate_tab_helper_->GetLanguageState().page_needs_translation()) |
| 163 return; | 153 return; |
| 164 // Note that we delay it as the TranslateManager gets this notification | 154 // Note that we delay it as the TranslateManager gets this notification |
| 165 // before the WebContents and the WebContents processing might remove the | 155 // before the WebContents and the WebContents processing might remove the |
| 166 // current infobars. Since InitTranslation might add an infobar, it must | 156 // current infobars. Since InitTranslation might add an infobar, it must |
| 167 // be done after that. | 157 // be done after that. |
| 168 base::MessageLoop::current()->PostTask(FROM_HERE, | 158 base::MessageLoop::current()->PostTask( |
| 159 FROM_HERE, | |
| 169 base::Bind( | 160 base::Bind( |
| 170 &TranslateManager::InitiateTranslationPosted, | 161 &TranslateManager::InitiateTranslationPosted, |
| 171 weak_method_factory_.GetWeakPtr(), | 162 weak_method_factory_.GetWeakPtr(), |
| 172 controller->GetWebContents()->GetRenderProcessHost()->GetID(), | 163 translate_tab_helper_->GetLanguageState().original_language(), |
| 173 controller->GetWebContents()->GetRenderViewHost()->GetRoutingID(), | 164 0)); |
| 174 translate_tab_helper->GetLanguageState().original_language(), 0)); | |
| 175 break; | 165 break; |
| 176 } | 166 } |
| 177 case chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED: { | 167 case chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED: { |
| 178 const LanguageDetectionDetails* lang_det_details = | 168 const LanguageDetectionDetails* lang_det_details = |
| 179 content::Details<const LanguageDetectionDetails>(details).ptr(); | 169 content::Details<const LanguageDetectionDetails>(details).ptr(); |
| 180 | 170 |
| 181 WebContents* tab = content::Source<WebContents>(source).ptr(); | 171 WebContents* tab = content::Source<WebContents>(source).ptr(); |
| 172 DCHECK_EQ(translate_tab_helper_->GetWebContents(), tab); | |
| 182 | 173 |
| 183 // We may get this notifications multiple times. Make sure to translate | 174 // We may get this notifications multiple times. Make sure to translate |
| 184 // only once. | 175 // only once. |
| 185 TranslateTabHelper* translate_tab_helper = | 176 LanguageState& language_state = translate_tab_helper_->GetLanguageState(); |
| 186 TranslateTabHelper::FromWebContents(tab); | |
| 187 if (!translate_tab_helper) | |
| 188 return; | |
| 189 | |
| 190 LanguageState& language_state = translate_tab_helper->GetLanguageState(); | |
| 191 if (language_state.page_needs_translation() && | 177 if (language_state.page_needs_translation() && |
| 192 !language_state.translation_pending() && | 178 !language_state.translation_pending() && |
| 193 !language_state.translation_declined() && | 179 !language_state.translation_declined() && |
| 194 !language_state.IsPageTranslated()) { | 180 !language_state.IsPageTranslated()) { |
| 195 std::string language = lang_det_details->adopted_language; | 181 std::string language = lang_det_details->adopted_language; |
| 196 InitiateTranslation(tab, language); | 182 InitiateTranslation(language); |
| 197 } | 183 } |
| 198 break; | 184 break; |
| 199 } | 185 } |
| 200 case chrome::NOTIFICATION_PAGE_TRANSLATED: { | 186 case chrome::NOTIFICATION_PAGE_TRANSLATED: { |
| 201 // Only add translate infobar if it doesn't exist; if it already exists, | 187 // Only add translate infobar if it doesn't exist; if it already exists, |
| 202 // just update the state, the actual infobar would have received the same | 188 // just update the state, the actual infobar would have received the same |
| 203 // notification and update the visual display accordingly. | 189 // notification and update the visual display accordingly. |
| 204 WebContents* tab = content::Source<WebContents>(source).ptr(); | |
| 205 PageTranslatedDetails* page_translated_details = | 190 PageTranslatedDetails* page_translated_details = |
| 206 content::Details<PageTranslatedDetails>(details).ptr(); | 191 content::Details<PageTranslatedDetails>(details).ptr(); |
| 207 PageTranslated(tab, page_translated_details); | 192 PageTranslated(page_translated_details); |
| 208 break; | 193 break; |
| 209 } | 194 } |
| 210 default: | 195 default: |
| 211 NOTREACHED(); | 196 NOTREACHED(); |
| 212 } | 197 } |
| 213 } | 198 } |
| 214 | 199 |
| 215 // static | 200 // static |
| 216 scoped_ptr<TranslateManager::TranslateErrorCallbackList::Subscription> | 201 scoped_ptr<TranslateManager::TranslateErrorCallbackList::Subscription> |
| 217 TranslateManager::RegisterTranslateErrorCallback( | 202 TranslateManager::RegisterTranslateErrorCallback( |
| 218 const TranslateManager::TranslateErrorCallback& callback) { | 203 const TranslateManager::TranslateErrorCallback& callback) { |
| 219 if (!g_callback_list_) | 204 if (!g_callback_list_) |
| 220 g_callback_list_ = new TranslateErrorCallbackList; | 205 g_callback_list_ = new TranslateErrorCallbackList; |
| 221 return g_callback_list_->Add(callback); | 206 return g_callback_list_->Add(callback); |
| 222 } | 207 } |
| 223 | 208 |
| 224 TranslateManager::TranslateManager() | 209 TranslateManager::TranslateManager(TranslateTabHelper* helper) |
| 225 : max_reload_check_attempts_(kMaxTranslateLoadCheckAttempts), | 210 : max_reload_check_attempts_(kMaxTranslateLoadCheckAttempts), |
| 226 weak_method_factory_(this) { | 211 translate_tab_helper_(helper), |
| 227 notification_registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, | 212 weak_method_factory_(this) { |
| 228 content::NotificationService::AllSources()); | 213 |
| 214 WebContents* web_contents = translate_tab_helper_->GetWebContents(); | |
| 215 | |
| 216 notification_registrar_.Add( | |
| 217 this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, | |
| 218 content::Source<NavigationController>(&web_contents->GetController())); | |
| 229 notification_registrar_.Add(this, | 219 notification_registrar_.Add(this, |
| 230 chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED, | 220 chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED, |
| 231 content::NotificationService::AllSources()); | 221 content::Source<WebContents>(web_contents)); |
| 232 notification_registrar_.Add(this, chrome::NOTIFICATION_PAGE_TRANSLATED, | 222 notification_registrar_.Add(this, |
| 233 content::NotificationService::AllSources()); | 223 chrome::NOTIFICATION_PAGE_TRANSLATED, |
| 224 content::Source<WebContents>(web_contents)); | |
| 234 } | 225 } |
| 235 | 226 |
| 236 void TranslateManager::InitiateTranslation(WebContents* web_contents, | 227 void TranslateManager::InitiateTranslation(const std::string& page_lang) { |
| 237 const std::string& page_lang) { | 228 WebContents* web_contents = translate_tab_helper_->GetWebContents(); |
| 238 TranslateTabHelper* translate_tab_helper = | |
| 239 TranslateTabHelper::FromWebContents(web_contents); | |
| 240 if (!translate_tab_helper) | |
| 241 return; | |
| 242 | |
| 243 Profile* profile = | 229 Profile* profile = |
| 244 Profile::FromBrowserContext(web_contents->GetBrowserContext()); | 230 Profile::FromBrowserContext(web_contents->GetBrowserContext()); |
| 245 Profile* original_profile = profile->GetOriginalProfile(); | 231 Profile* original_profile = profile->GetOriginalProfile(); |
| 246 PrefService* prefs = original_profile->GetPrefs(); | 232 PrefService* prefs = original_profile->GetPrefs(); |
| 247 if (!prefs->GetBoolean(prefs::kEnableTranslate)) { | 233 if (!prefs->GetBoolean(prefs::kEnableTranslate)) { |
| 248 TranslateBrowserMetrics::ReportInitiationStatus( | 234 TranslateBrowserMetrics::ReportInitiationStatus( |
| 249 TranslateBrowserMetrics::INITIATION_STATUS_DISABLED_BY_PREFS); | 235 TranslateBrowserMetrics::INITIATION_STATUS_DISABLED_BY_PREFS); |
| 250 const std::string& locale = g_browser_process->GetApplicationLocale(); | 236 const std::string& locale = g_browser_process->GetApplicationLocale(); |
| 251 TranslateBrowserMetrics::ReportLocalesOnDisabledByPrefs(locale); | 237 TranslateBrowserMetrics::ReportLocalesOnDisabledByPrefs(locale); |
| 252 return; | 238 return; |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 322 | 308 |
| 323 // If the user has previously selected "always translate" for this language we | 309 // If the user has previously selected "always translate" for this language we |
| 324 // automatically translate. Note that in incognito mode we disable that | 310 // automatically translate. Note that in incognito mode we disable that |
| 325 // feature; the user will get an infobar, so they can control whether the | 311 // feature; the user will get an infobar, so they can control whether the |
| 326 // page's text is sent to the translate server. | 312 // page's text is sent to the translate server. |
| 327 if (!web_contents->GetBrowserContext()->IsOffTheRecord()) { | 313 if (!web_contents->GetBrowserContext()->IsOffTheRecord()) { |
| 328 std::string auto_target_lang = GetAutoTargetLanguage(language_code, prefs); | 314 std::string auto_target_lang = GetAutoTargetLanguage(language_code, prefs); |
| 329 if (!auto_target_lang.empty()) { | 315 if (!auto_target_lang.empty()) { |
| 330 TranslateBrowserMetrics::ReportInitiationStatus( | 316 TranslateBrowserMetrics::ReportInitiationStatus( |
| 331 TranslateBrowserMetrics::INITIATION_STATUS_AUTO_BY_CONFIG); | 317 TranslateBrowserMetrics::INITIATION_STATUS_AUTO_BY_CONFIG); |
| 332 TranslatePage(web_contents, language_code, auto_target_lang); | 318 TranslatePage(language_code, auto_target_lang); |
| 333 return; | 319 return; |
| 334 } | 320 } |
| 335 } | 321 } |
| 336 | 322 |
| 337 LanguageState& language_state = translate_tab_helper->GetLanguageState(); | 323 LanguageState& language_state = translate_tab_helper_->GetLanguageState(); |
| 338 std::string auto_translate_to = language_state.AutoTranslateTo(); | 324 std::string auto_translate_to = language_state.AutoTranslateTo(); |
| 339 if (!auto_translate_to.empty()) { | 325 if (!auto_translate_to.empty()) { |
| 340 // This page was navigated through a click from a translated page. | 326 // This page was navigated through a click from a translated page. |
| 341 TranslateBrowserMetrics::ReportInitiationStatus( | 327 TranslateBrowserMetrics::ReportInitiationStatus( |
| 342 TranslateBrowserMetrics::INITIATION_STATUS_AUTO_BY_LINK); | 328 TranslateBrowserMetrics::INITIATION_STATUS_AUTO_BY_LINK); |
| 343 TranslatePage(web_contents, language_code, auto_translate_to); | 329 TranslatePage(language_code, auto_translate_to); |
| 344 return; | 330 return; |
| 345 } | 331 } |
| 346 | 332 |
| 347 TranslateBrowserMetrics::ReportInitiationStatus( | 333 TranslateBrowserMetrics::ReportInitiationStatus( |
| 348 TranslateBrowserMetrics::INITIATION_STATUS_SHOW_INFOBAR); | 334 TranslateBrowserMetrics::INITIATION_STATUS_SHOW_INFOBAR); |
| 349 | 335 |
| 350 // Prompts the user if he/she wants the page translated. | 336 // Prompts the user if he/she wants the page translated. |
| 351 translate_tab_helper->ShowTranslateUI(TranslateTabHelper::BEFORE_TRANSLATE, | 337 translate_tab_helper_->ShowTranslateUI(TranslateTabHelper::BEFORE_TRANSLATE, |
| 352 web_contents, | 338 language_code, |
| 353 language_code, | 339 target_lang, |
| 354 target_lang, | 340 TranslateErrors::NONE); |
| 355 TranslateErrors::NONE); | |
| 356 } | 341 } |
| 357 | 342 |
| 358 void TranslateManager::InitiateTranslationPosted(int process_id, | 343 void TranslateManager::InitiateTranslationPosted(const std::string& page_lang, |
| 359 int render_id, | |
| 360 const std::string& page_lang, | |
| 361 int attempt) { | 344 int attempt) { |
| 362 // The tab might have been closed. | |
| 363 WebContents* web_contents = | |
| 364 tab_util::GetWebContentsByID(process_id, render_id); | |
| 365 if (!web_contents) | |
| 366 return; | |
| 367 | |
| 368 TranslateTabHelper* translate_tab_helper = | |
| 369 TranslateTabHelper::FromWebContents(web_contents); | |
| 370 if (translate_tab_helper->GetLanguageState().translation_pending()) | |
| 371 return; | |
| 372 | |
| 373 // During a reload we need web content to be available before the | 345 // During a reload we need web content to be available before the |
| 374 // translate script is executed. Otherwise we will run the translate script on | 346 // translate script is executed. Otherwise we will run the translate script on |
| 375 // an empty DOM which will fail. Therefore we wait a bit to see if the page | 347 // an empty DOM which will fail. Therefore we wait a bit to see if the page |
| 376 // has finished. | 348 // has finished. |
| 349 WebContents* web_contents = translate_tab_helper_->GetWebContents(); | |
| 377 if ((web_contents->IsLoading()) && attempt < kMaxTranslateLoadCheckAttempts) { | 350 if ((web_contents->IsLoading()) && attempt < kMaxTranslateLoadCheckAttempts) { |
| 378 int backoff = attempt * max_reload_check_attempts_; | 351 int backoff = attempt * max_reload_check_attempts_; |
| 379 base::MessageLoop::current()->PostDelayedTask( | 352 base::MessageLoop::current()->PostDelayedTask( |
| 380 FROM_HERE, base::Bind(&TranslateManager::InitiateTranslationPosted, | 353 FROM_HERE, base::Bind(&TranslateManager::InitiateTranslationPosted, |
| 381 weak_method_factory_.GetWeakPtr(), process_id, | 354 weak_method_factory_.GetWeakPtr(), |
| 382 render_id, page_lang, ++attempt), | 355 page_lang, ++attempt), |
| 383 base::TimeDelta::FromMilliseconds(backoff)); | 356 base::TimeDelta::FromMilliseconds(backoff)); |
| 384 return; | 357 return; |
| 385 } | 358 } |
| 386 | 359 |
| 387 InitiateTranslation(web_contents, | 360 InitiateTranslation(TranslateDownloadManager::GetLanguageCode(page_lang)); |
| 388 TranslateDownloadManager::GetLanguageCode(page_lang)); | |
| 389 } | 361 } |
| 390 | 362 |
| 391 void TranslateManager::TranslatePage(WebContents* web_contents, | 363 void TranslateManager::TranslatePage(const std::string& original_source_lang, |
| 392 const std::string& original_source_lang, | |
| 393 const std::string& target_lang) { | 364 const std::string& target_lang) { |
| 365 WebContents* web_contents = translate_tab_helper_->GetWebContents(); | |
| 394 NavigationEntry* entry = web_contents->GetController().GetActiveEntry(); | 366 NavigationEntry* entry = web_contents->GetController().GetActiveEntry(); |
| 395 if (!entry) { | 367 if (!entry) { |
| 396 NOTREACHED(); | 368 NOTREACHED(); |
| 397 return; | 369 return; |
| 398 } | 370 } |
| 399 | 371 |
| 400 // Translation can be kicked by context menu against unsupported languages. | 372 // Translation can be kicked by context menu against unsupported languages. |
| 401 // Unsupported language strings should be replaced with | 373 // Unsupported language strings should be replaced with |
| 402 // kUnknownLanguageCode in order to send a translation request with enabling | 374 // kUnknownLanguageCode in order to send a translation request with enabling |
| 403 // server side auto language detection. | 375 // server side auto language detection. |
| 404 std::string source_lang(original_source_lang); | 376 std::string source_lang(original_source_lang); |
| 405 if (!TranslateDownloadManager::IsSupportedLanguage(source_lang)) | 377 if (!TranslateDownloadManager::IsSupportedLanguage(source_lang)) |
| 406 source_lang = std::string(translate::kUnknownLanguageCode); | 378 source_lang = std::string(translate::kUnknownLanguageCode); |
| 407 | 379 |
| 408 TranslateTabHelper* translate_tab_helper = | 380 translate_tab_helper_->ShowTranslateUI(TranslateTabHelper::TRANSLATING, |
| 409 TranslateTabHelper::FromWebContents(web_contents); | 381 source_lang, |
| 410 DCHECK(translate_tab_helper); | 382 target_lang, |
| 411 translate_tab_helper->ShowTranslateUI(TranslateTabHelper::TRANSLATING, | 383 TranslateErrors::NONE); |
| 412 web_contents, | |
| 413 source_lang, | |
| 414 target_lang, | |
| 415 TranslateErrors::NONE); | |
| 416 | 384 |
| 417 TranslateScript* script = TranslateDownloadManager::GetInstance()->script(); | 385 TranslateScript* script = TranslateDownloadManager::GetInstance()->script(); |
| 418 DCHECK(script != NULL); | 386 DCHECK(script != NULL); |
| 419 | 387 |
| 420 const std::string& script_data = script->data(); | 388 const std::string& script_data = script->data(); |
| 421 if (!script_data.empty()) { | 389 if (!script_data.empty()) { |
| 422 DoTranslatePage(web_contents, script_data, source_lang, target_lang); | 390 DoTranslatePage(script_data, source_lang, target_lang); |
| 423 return; | 391 return; |
| 424 } | 392 } |
| 425 | 393 |
| 426 // The script is not available yet. Queue that request and query for the | 394 // The script is not available yet. Queue that request and query for the |
| 427 // script. Once it is downloaded we'll do the translate. | 395 // script. Once it is downloaded we'll do the translate. |
| 428 content::RenderViewHost* rvh = web_contents->GetRenderViewHost(); | 396 TranslateScript::RequestCallback callback = |
| 429 PendingRequest request; | 397 base::Bind(&TranslateManager::OnTranslateScriptFetchComplete, |
| 430 request.render_process_id = rvh->GetProcess()->GetID(); | 398 weak_method_factory_.GetWeakPtr(), |
| 431 request.render_view_id = rvh->GetRoutingID(); | 399 entry->GetPageID(), |
| 432 request.page_id = entry->GetPageID(); | 400 source_lang, |
| 433 request.source_lang = source_lang; | 401 target_lang); |
| 434 request.target_lang = target_lang; | |
| 435 | 402 |
| 436 script->Request(base::Bind(&TranslateManager::OnTranslateScriptFetchComplete, | 403 script->Request(callback); |
| 437 weak_method_factory_.GetWeakPtr(), request)); | |
| 438 } | 404 } |
| 439 | 405 |
| 440 void TranslateManager::RevertTranslation(WebContents* web_contents) { | 406 void TranslateManager::RevertTranslation() { |
| 407 WebContents* web_contents = translate_tab_helper_->GetWebContents(); | |
| 441 NavigationEntry* entry = web_contents->GetController().GetActiveEntry(); | 408 NavigationEntry* entry = web_contents->GetController().GetActiveEntry(); |
| 442 if (!entry) { | 409 if (!entry) { |
| 443 NOTREACHED(); | 410 NOTREACHED(); |
| 444 return; | 411 return; |
| 445 } | 412 } |
| 446 web_contents->GetRenderViewHost()->Send(new ChromeViewMsg_RevertTranslation( | 413 web_contents->GetRenderViewHost()->Send(new ChromeViewMsg_RevertTranslation( |
| 447 web_contents->GetRenderViewHost()->GetRoutingID(), entry->GetPageID())); | 414 web_contents->GetRenderViewHost()->GetRoutingID(), entry->GetPageID())); |
| 448 | 415 |
| 449 TranslateTabHelper* translate_tab_helper = | 416 translate_tab_helper_->GetLanguageState().SetCurrentLanguage( |
| 450 TranslateTabHelper::FromWebContents(web_contents); | 417 translate_tab_helper_->GetLanguageState().original_language()); |
| 451 translate_tab_helper->GetLanguageState().SetCurrentLanguage( | |
| 452 translate_tab_helper->GetLanguageState().original_language()); | |
| 453 } | 418 } |
| 454 | 419 |
| 455 void TranslateManager::ReportLanguageDetectionError(WebContents* web_contents) { | 420 void TranslateManager::ReportLanguageDetectionError() { |
| 456 TranslateBrowserMetrics::ReportLanguageDetectionError(); | 421 TranslateBrowserMetrics::ReportLanguageDetectionError(); |
| 457 // We'll open the URL in a new tab so that the user can tell us more. | 422 // We'll open the URL in a new tab so that the user can tell us more. |
| 423 WebContents* web_contents = translate_tab_helper_->GetWebContents(); | |
| 458 Browser* browser = chrome::FindBrowserWithWebContents(web_contents); | 424 Browser* browser = chrome::FindBrowserWithWebContents(web_contents); |
| 459 if (!browser) { | 425 if (!browser) { |
| 460 NOTREACHED(); | 426 NOTREACHED(); |
| 461 return; | 427 return; |
| 462 } | 428 } |
| 463 | 429 |
| 464 GURL report_error_url = GURL(kReportLanguageDetectionErrorURL); | 430 GURL report_error_url = GURL(kReportLanguageDetectionErrorURL); |
| 465 | 431 |
| 466 GURL page_url = web_contents->GetController().GetActiveEntry()->GetURL(); | 432 GURL page_url = web_contents->GetController().GetActiveEntry()->GetURL(); |
| 467 report_error_url = net::AppendQueryParameter( | 433 report_error_url = net::AppendQueryParameter( |
| 468 report_error_url, | 434 report_error_url, |
| 469 kUrlQueryName, | 435 kUrlQueryName, |
| 470 page_url.spec()); | 436 page_url.spec()); |
| 471 | 437 |
| 472 TranslateTabHelper* translate_tab_helper = | |
| 473 TranslateTabHelper::FromWebContents(web_contents); | |
| 474 report_error_url = net::AppendQueryParameter( | 438 report_error_url = net::AppendQueryParameter( |
| 475 report_error_url, | 439 report_error_url, |
| 476 kSourceLanguageQueryName, | 440 kSourceLanguageQueryName, |
| 477 translate_tab_helper->GetLanguageState().original_language()); | 441 translate_tab_helper_->GetLanguageState().original_language()); |
| 478 | 442 |
| 479 report_error_url = TranslateURLUtil::AddHostLocaleToUrl(report_error_url); | 443 report_error_url = TranslateURLUtil::AddHostLocaleToUrl(report_error_url); |
| 480 report_error_url = TranslateURLUtil::AddApiKeyToUrl(report_error_url); | 444 report_error_url = TranslateURLUtil::AddApiKeyToUrl(report_error_url); |
| 481 | 445 |
| 482 chrome::AddSelectedTabWithURL(browser, report_error_url, | 446 chrome::AddSelectedTabWithURL(browser, report_error_url, |
| 483 content::PAGE_TRANSITION_AUTO_BOOKMARK); | 447 content::PAGE_TRANSITION_AUTO_BOOKMARK); |
| 484 } | 448 } |
| 485 | 449 |
| 486 void TranslateManager::DoTranslatePage(WebContents* web_contents, | 450 void TranslateManager::DoTranslatePage(const std::string& translate_script, |
| 487 const std::string& translate_script, | |
| 488 const std::string& source_lang, | 451 const std::string& source_lang, |
| 489 const std::string& target_lang) { | 452 const std::string& target_lang) { |
| 453 WebContents* web_contents = translate_tab_helper_->GetWebContents(); | |
| 490 NavigationEntry* entry = web_contents->GetController().GetActiveEntry(); | 454 NavigationEntry* entry = web_contents->GetController().GetActiveEntry(); |
| 491 if (!entry) { | 455 if (!entry) { |
| 492 NOTREACHED(); | 456 NOTREACHED(); |
| 493 return; | 457 return; |
| 494 } | 458 } |
| 495 | 459 |
| 496 TranslateTabHelper* translate_tab_helper = | 460 translate_tab_helper_->GetLanguageState().set_translation_pending(true); |
| 497 TranslateTabHelper::FromWebContents(web_contents); | |
| 498 if (!translate_tab_helper) | |
|
blundell
2014/02/17 15:26:28
I think you should keep this check, checking again
droger
2014/02/17 16:52:48
Done.
| |
| 499 return; | |
| 500 | |
| 501 translate_tab_helper->GetLanguageState().set_translation_pending(true); | |
| 502 web_contents->GetRenderViewHost()->Send(new ChromeViewMsg_TranslatePage( | 461 web_contents->GetRenderViewHost()->Send(new ChromeViewMsg_TranslatePage( |
| 503 web_contents->GetRenderViewHost()->GetRoutingID(), entry->GetPageID(), | 462 web_contents->GetRenderViewHost()->GetRoutingID(), entry->GetPageID(), |
| 504 translate_script, source_lang, target_lang)); | 463 translate_script, source_lang, target_lang)); |
| 505 } | 464 } |
| 506 | 465 |
| 507 void TranslateManager::PageTranslated(WebContents* web_contents, | 466 void TranslateManager::PageTranslated(PageTranslatedDetails* details) { |
| 508 PageTranslatedDetails* details) { | |
| 509 if ((details->error_type == TranslateErrors::NONE) && | 467 if ((details->error_type == TranslateErrors::NONE) && |
| 510 details->source_language != translate::kUnknownLanguageCode && | 468 details->source_language != translate::kUnknownLanguageCode && |
| 511 !TranslateDownloadManager::IsSupportedLanguage( | 469 !TranslateDownloadManager::IsSupportedLanguage( |
| 512 details->source_language)) { | 470 details->source_language)) { |
| 513 details->error_type = TranslateErrors::UNSUPPORTED_LANGUAGE; | 471 details->error_type = TranslateErrors::UNSUPPORTED_LANGUAGE; |
| 514 } | 472 } |
| 515 | 473 |
| 516 TranslateTabHelper* translate_tab_helper = | 474 translate_tab_helper_->ShowTranslateUI(TranslateTabHelper::AFTER_TRANSLATE, |
| 517 TranslateTabHelper::FromWebContents(web_contents); | 475 details->source_language, |
| 518 DCHECK(translate_tab_helper); | 476 details->target_language, |
|
blundell
2014/02/17 15:26:28
You can change this to DCHECK(translate_tab_helper
droger
2014/02/17 16:52:48
Done.
| |
| 519 translate_tab_helper->ShowTranslateUI(TranslateTabHelper::AFTER_TRANSLATE, | 477 details->error_type); |
| 520 web_contents, | |
| 521 details->source_language, | |
| 522 details->target_language, | |
| 523 details->error_type); | |
| 524 | 478 |
| 479 WebContents* web_contents = translate_tab_helper_->GetWebContents(); | |
| 525 if (details->error_type != TranslateErrors::NONE && | 480 if (details->error_type != TranslateErrors::NONE && |
| 526 !web_contents->GetBrowserContext()->IsOffTheRecord()) { | 481 !web_contents->GetBrowserContext()->IsOffTheRecord()) { |
| 527 TranslateErrorDetails error_details; | 482 TranslateErrorDetails error_details; |
| 528 error_details.time = base::Time::Now(); | 483 error_details.time = base::Time::Now(); |
| 529 error_details.url = web_contents->GetLastCommittedURL(); | 484 error_details.url = web_contents->GetLastCommittedURL(); |
| 530 error_details.error = details->error_type; | 485 error_details.error = details->error_type; |
| 531 NotifyTranslateError(error_details); | 486 NotifyTranslateError(error_details); |
| 532 } | 487 } |
| 533 } | 488 } |
| 534 | 489 |
| 535 void TranslateManager::OnTranslateScriptFetchComplete(PendingRequest request, | 490 void TranslateManager::OnTranslateScriptFetchComplete( |
| 536 bool success, | 491 int page_id, |
| 537 const std::string& data) { | 492 const std::string& source_lang, |
| 538 WebContents* web_contents = tab_util::GetWebContentsByID( | 493 const std::string& target_lang, |
| 539 request.render_process_id, request.render_view_id); | 494 bool success, |
| 540 if (!web_contents) { | 495 const std::string& data) { |
| 541 // The tab went away while we were retrieving the script. | 496 WebContents* web_contents = translate_tab_helper_->GetWebContents(); |
|
blundell
2014/02/17 15:26:28
This can return NULL, so you need to keep the chec
droger
2014/02/17 16:52:48
I changed TranslateTabHelper so that in theory thi
| |
| 542 return; | |
| 543 } | |
| 544 NavigationEntry* entry = web_contents->GetController().GetActiveEntry(); | 497 NavigationEntry* entry = web_contents->GetController().GetActiveEntry(); |
| 545 if (!entry || entry->GetPageID() != request.page_id) { | 498 if (!entry || entry->GetPageID() != page_id) { |
| 546 // We navigated away from the page the translation was triggered on. | 499 // We navigated away from the page the translation was triggered on. |
| 547 return; | 500 return; |
| 548 } | 501 } |
| 549 | 502 |
| 550 if (success) { | 503 if (success) { |
| 551 // Translate the page. | 504 // Translate the page. |
| 552 TranslateScript* translate_script = | 505 TranslateScript* translate_script = |
| 553 TranslateDownloadManager::GetInstance()->script(); | 506 TranslateDownloadManager::GetInstance()->script(); |
| 554 DCHECK(translate_script); | 507 DCHECK(translate_script); |
| 555 DoTranslatePage( | 508 DoTranslatePage(data, source_lang, target_lang); |
| 556 web_contents, data, request.source_lang, request.target_lang); | |
| 557 } else { | 509 } else { |
| 558 TranslateTabHelper* translate_tab_helper = | 510 translate_tab_helper_->ShowTranslateUI(TranslateTabHelper::TRANSLATE_ERROR, |
| 559 TranslateTabHelper::FromWebContents(web_contents); | 511 source_lang, |
| 560 DCHECK(translate_tab_helper); | 512 target_lang, |
| 561 translate_tab_helper->ShowTranslateUI(TranslateTabHelper::TRANSLATE_ERROR, | 513 TranslateErrors::NETWORK); |
| 562 web_contents, | |
| 563 request.source_lang, | |
| 564 request.target_lang, | |
| 565 TranslateErrors::NETWORK); | |
| 566 if (!web_contents->GetBrowserContext()->IsOffTheRecord()) { | 514 if (!web_contents->GetBrowserContext()->IsOffTheRecord()) { |
| 567 TranslateErrorDetails error_details; | 515 TranslateErrorDetails error_details; |
| 568 error_details.time = base::Time::Now(); | 516 error_details.time = base::Time::Now(); |
| 569 error_details.url = entry->GetURL(); | 517 error_details.url = entry->GetURL(); |
| 570 error_details.error = TranslateErrors::NETWORK; | 518 error_details.error = TranslateErrors::NETWORK; |
| 571 NotifyTranslateError(error_details); | 519 NotifyTranslateError(error_details); |
| 572 } | 520 } |
| 573 } | 521 } |
| 574 } | 522 } |
| 575 | 523 |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 611 &auto_target_lang)) { | 559 &auto_target_lang)) { |
| 612 // We need to confirm that the saved target language is still supported. | 560 // We need to confirm that the saved target language is still supported. |
| 613 // Also, GetLanguageCode will take care of removing country code if any. | 561 // Also, GetLanguageCode will take care of removing country code if any. |
| 614 auto_target_lang = | 562 auto_target_lang = |
| 615 TranslateDownloadManager::GetLanguageCode(auto_target_lang); | 563 TranslateDownloadManager::GetLanguageCode(auto_target_lang); |
| 616 if (TranslateDownloadManager::IsSupportedLanguage(auto_target_lang)) | 564 if (TranslateDownloadManager::IsSupportedLanguage(auto_target_lang)) |
| 617 return auto_target_lang; | 565 return auto_target_lang; |
| 618 } | 566 } |
| 619 return std::string(); | 567 return std::string(); |
| 620 } | 568 } |
| OLD | NEW |