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/metrics/field_trial.h" | 9 #include "base/metrics/field_trial.h" |
| 10 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
| 11 #include "base/prefs/pref_service.h" | 11 #include "base/prefs/pref_service.h" |
| 12 #include "base/strings/string_split.h" | 12 #include "base/strings/string_split.h" |
| 13 #include "base/strings/stringprintf.h" | 13 #include "base/strings/stringprintf.h" |
| 14 #include "base/time/time.h" | 14 #include "base/time/time.h" |
| 15 #include "chrome/browser/translate/translate_tab_helper.h" | 15 #include "chrome/browser/translate/translate_tab_helper.h" |
| 16 #include "chrome/browser/ui/browser.h" | 16 #include "chrome/browser/ui/browser.h" |
| 17 #include "chrome/browser/ui/browser_finder.h" | 17 #include "chrome/browser/ui/browser_finder.h" |
| 18 #include "chrome/browser/ui/browser_tabstrip.h" | 18 #include "chrome/browser/ui/browser_tabstrip.h" |
| 19 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 19 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 20 #include "chrome/common/url_constants.h" | |
| 21 #include "components/translate/content/common/translate_messages.h" | |
| 22 #include "components/translate/core/browser/language_state.h" | 20 #include "components/translate/core/browser/language_state.h" |
| 23 #include "components/translate/core/browser/page_translated_details.h" | 21 #include "components/translate/core/browser/page_translated_details.h" |
| 24 #include "components/translate/core/browser/translate_accept_languages.h" | 22 #include "components/translate/core/browser/translate_accept_languages.h" |
| 25 #include "components/translate/core/browser/translate_browser_metrics.h" | 23 #include "components/translate/core/browser/translate_browser_metrics.h" |
| 26 #include "components/translate/core/browser/translate_client.h" | 24 #include "components/translate/core/browser/translate_client.h" |
| 27 #include "components/translate/core/browser/translate_download_manager.h" | 25 #include "components/translate/core/browser/translate_download_manager.h" |
| 28 #include "components/translate/core/browser/translate_driver.h" | 26 #include "components/translate/core/browser/translate_driver.h" |
| 29 #include "components/translate/core/browser/translate_error_details.h" | 27 #include "components/translate/core/browser/translate_error_details.h" |
| 30 #include "components/translate/core/browser/translate_language_list.h" | 28 #include "components/translate/core/browser/translate_language_list.h" |
| 31 #include "components/translate/core/browser/translate_prefs.h" | 29 #include "components/translate/core/browser/translate_prefs.h" |
| 32 #include "components/translate/core/browser/translate_script.h" | 30 #include "components/translate/core/browser/translate_script.h" |
| 33 #include "components/translate/core/browser/translate_url_util.h" | 31 #include "components/translate/core/browser/translate_url_util.h" |
| 34 #include "components/translate/core/common/language_detection_details.h" | 32 #include "components/translate/core/common/language_detection_details.h" |
| 35 #include "components/translate/core/common/translate_constants.h" | 33 #include "components/translate/core/common/translate_constants.h" |
| 36 #include "components/translate/core/common/translate_pref_names.h" | 34 #include "components/translate/core/common/translate_pref_names.h" |
| 37 #include "components/translate/core/common/translate_switches.h" | 35 #include "components/translate/core/common/translate_switches.h" |
| 38 #include "content/public/browser/navigation_controller.h" | |
| 39 #include "content/public/browser/navigation_entry.h" | |
| 40 #include "content/public/browser/render_process_host.h" | |
| 41 #include "content/public/browser/render_view_host.h" | |
| 42 #include "content/public/browser/web_contents.h" | 36 #include "content/public/browser/web_contents.h" |
| 43 #include "net/base/url_util.h" | 37 #include "net/base/url_util.h" |
| 44 #include "net/http/http_status_code.h" | 38 #include "net/http/http_status_code.h" |
| 45 | 39 |
| 46 using content::NavigationController; | |
| 47 using content::NavigationEntry; | |
| 48 using content::WebContents; | 40 using content::WebContents; |
| 49 | 41 |
| 50 namespace { | 42 namespace { |
| 51 | 43 |
| 52 // Callbacks for translate errors. | 44 // Callbacks for translate errors. |
| 53 TranslateManager::TranslateErrorCallbackList* g_callback_list_ = NULL; | 45 TranslateManager::TranslateErrorCallbackList* g_callback_list_ = NULL; |
| 54 | 46 |
| 55 const char kReportLanguageDetectionErrorURL[] = | 47 const char kReportLanguageDetectionErrorURL[] = |
| 56 "https://translate.google.com/translate_error?client=cr&action=langidc"; | 48 "https://translate.google.com/translate_error?client=cr&action=langidc"; |
| 57 | 49 |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 117 // automated browser testing. | 109 // automated browser testing. |
| 118 if (CommandLine::ForCurrentProcess()->HasSwitch( | 110 if (CommandLine::ForCurrentProcess()->HasSwitch( |
| 119 translate::switches::kDisableTranslate)) { | 111 translate::switches::kDisableTranslate)) { |
| 120 TranslateBrowserMetrics::ReportInitiationStatus( | 112 TranslateBrowserMetrics::ReportInitiationStatus( |
| 121 TranslateBrowserMetrics::INITIATION_STATUS_DISABLED_BY_SWITCH); | 113 TranslateBrowserMetrics::INITIATION_STATUS_DISABLED_BY_SWITCH); |
| 122 return; | 114 return; |
| 123 } | 115 } |
| 124 | 116 |
| 125 // MHTML pages currently cannot be translated. | 117 // MHTML pages currently cannot be translated. |
| 126 // See bug: 217945. | 118 // See bug: 217945. |
| 127 WebContents* web_contents = translate_tab_helper_->GetWebContents(); | 119 if (translate_driver_->GetContentsMimeType() == "multipart/related") { |
| 128 if (web_contents->GetContentsMimeType() == "multipart/related") { | |
| 129 TranslateBrowserMetrics::ReportInitiationStatus( | 120 TranslateBrowserMetrics::ReportInitiationStatus( |
| 130 TranslateBrowserMetrics::INITIATION_STATUS_MIME_TYPE_IS_NOT_SUPPORTED); | 121 TranslateBrowserMetrics::INITIATION_STATUS_MIME_TYPE_IS_NOT_SUPPORTED); |
| 131 return; | 122 return; |
| 132 } | 123 } |
| 133 | 124 |
| 134 // Don't translate any Chrome specific page, e.g., New Tab Page, Download, | 125 // Don't translate any Chrome specific page, e.g., New Tab Page, Download, |
| 135 // History, and so on. | 126 // History, and so on. |
| 136 GURL page_url = web_contents->GetURL(); | 127 const GURL& page_url = translate_driver_->GetVisibleURL(); |
| 137 if (!translate_client_->IsTranslatableURL(page_url)) { | 128 if (!translate_client_->IsTranslatableURL(page_url)) { |
| 138 TranslateBrowserMetrics::ReportInitiationStatus( | 129 TranslateBrowserMetrics::ReportInitiationStatus( |
| 139 TranslateBrowserMetrics::INITIATION_STATUS_URL_IS_NOT_SUPPORTED); | 130 TranslateBrowserMetrics::INITIATION_STATUS_URL_IS_NOT_SUPPORTED); |
| 140 return; | 131 return; |
| 141 } | 132 } |
| 142 | 133 |
| 143 // Get the accepted languages list. | 134 // Get the accepted languages list. |
| 144 std::vector<std::string> accept_languages_list; | 135 std::vector<std::string> accept_languages_list; |
| 145 base::SplitString(prefs->GetString(accept_languages_pref_name_.c_str()), ',', | 136 base::SplitString(prefs->GetString(accept_languages_pref_name_.c_str()), ',', |
| 146 &accept_languages_list); | 137 &accept_languages_list); |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 217 translate_client_->ShowTranslateUI(translate::TRANSLATE_STEP_BEFORE_TRANSLATE, | 208 translate_client_->ShowTranslateUI(translate::TRANSLATE_STEP_BEFORE_TRANSLATE, |
| 218 language_code, | 209 language_code, |
| 219 target_lang, | 210 target_lang, |
| 220 TranslateErrors::NONE, | 211 TranslateErrors::NONE, |
| 221 false); | 212 false); |
| 222 } | 213 } |
| 223 | 214 |
| 224 void TranslateManager::TranslatePage(const std::string& original_source_lang, | 215 void TranslateManager::TranslatePage(const std::string& original_source_lang, |
| 225 const std::string& target_lang, | 216 const std::string& target_lang, |
| 226 bool triggered_from_menu) { | 217 bool triggered_from_menu) { |
| 227 WebContents* web_contents = translate_tab_helper_->GetWebContents(); | 218 if (!translate_driver_->HasCurrentPage()) { |
| 228 DCHECK(web_contents); | |
| 229 NavigationEntry* entry = web_contents->GetController().GetActiveEntry(); | |
| 230 if (!entry) { | |
| 231 NOTREACHED(); | 219 NOTREACHED(); |
| 232 return; | 220 return; |
| 233 } | 221 } |
| 234 | 222 |
| 235 // Translation can be kicked by context menu against unsupported languages. | 223 // Translation can be kicked by context menu against unsupported languages. |
| 236 // Unsupported language strings should be replaced with | 224 // Unsupported language strings should be replaced with |
| 237 // kUnknownLanguageCode in order to send a translation request with enabling | 225 // kUnknownLanguageCode in order to send a translation request with enabling |
| 238 // server side auto language detection. | 226 // server side auto language detection. |
| 239 std::string source_lang(original_source_lang); | 227 std::string source_lang(original_source_lang); |
| 240 if (!TranslateDownloadManager::IsSupportedLanguage(source_lang)) | 228 if (!TranslateDownloadManager::IsSupportedLanguage(source_lang)) |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 253 if (!script_data.empty()) { | 241 if (!script_data.empty()) { |
| 254 DoTranslatePage(script_data, source_lang, target_lang); | 242 DoTranslatePage(script_data, source_lang, target_lang); |
| 255 return; | 243 return; |
| 256 } | 244 } |
| 257 | 245 |
| 258 // The script is not available yet. Queue that request and query for the | 246 // The script is not available yet. Queue that request and query for the |
| 259 // script. Once it is downloaded we'll do the translate. | 247 // script. Once it is downloaded we'll do the translate. |
| 260 TranslateScript::RequestCallback callback = | 248 TranslateScript::RequestCallback callback = |
| 261 base::Bind(&TranslateManager::OnTranslateScriptFetchComplete, | 249 base::Bind(&TranslateManager::OnTranslateScriptFetchComplete, |
| 262 weak_method_factory_.GetWeakPtr(), | 250 weak_method_factory_.GetWeakPtr(), |
| 263 entry->GetPageID(), | 251 translate_driver_->GetCurrentPageID(), |
| 264 source_lang, | 252 source_lang, |
| 265 target_lang); | 253 target_lang); |
| 266 | 254 |
| 267 script->Request(callback); | 255 script->Request(callback); |
| 268 } | 256 } |
| 269 | 257 |
| 270 void TranslateManager::RevertTranslation() { | 258 void TranslateManager::RevertTranslation() { |
| 271 translate_driver_->RevertTranslation(); | 259 translate_driver_->RevertTranslation(); |
| 272 translate_driver_->GetLanguageState().SetCurrentLanguage( | 260 translate_driver_->GetLanguageState().SetCurrentLanguage( |
| 273 translate_driver_->GetLanguageState().original_language()); | 261 translate_driver_->GetLanguageState().original_language()); |
| 274 } | 262 } |
| 275 | 263 |
| 276 void TranslateManager::ReportLanguageDetectionError() { | 264 void TranslateManager::ReportLanguageDetectionError() { |
| 277 TranslateBrowserMetrics::ReportLanguageDetectionError(); | 265 TranslateBrowserMetrics::ReportLanguageDetectionError(); |
| 278 // We'll open the URL in a new tab so that the user can tell us more. | 266 // We'll open the URL in a new tab so that the user can tell us more. |
| 279 WebContents* web_contents = translate_tab_helper_->GetWebContents(); | 267 WebContents* web_contents = translate_tab_helper_->GetWebContents(); |
| 280 Browser* browser = chrome::FindBrowserWithWebContents(web_contents); | 268 Browser* browser = chrome::FindBrowserWithWebContents(web_contents); |
| 281 if (!browser) { | 269 if (!browser) { |
| 282 NOTREACHED(); | 270 NOTREACHED(); |
| 283 return; | 271 return; |
| 284 } | 272 } |
| 285 | 273 |
| 286 GURL report_error_url = GURL(kReportLanguageDetectionErrorURL); | 274 GURL report_error_url = GURL(kReportLanguageDetectionErrorURL); |
| 287 | 275 |
| 288 GURL page_url = web_contents->GetController().GetActiveEntry()->GetURL(); | 276 report_error_url = |
| 289 report_error_url = net::AppendQueryParameter( | 277 net::AppendQueryParameter(report_error_url, |
| 290 report_error_url, | 278 kUrlQueryName, |
| 291 kUrlQueryName, | 279 translate_driver_->GetActiveURL().spec()); |
|
droger
2014/04/08 12:01:28
I'm sure you know this is related to http://crbug.
blundell
2014/04/08 12:46:02
I was planning on leaving this code in TranslateMa
droger
2014/04/08 13:01:21
I think this is fine, yes. Initially I had another
| |
| 292 page_url.spec()); | |
| 293 | 280 |
| 294 report_error_url = net::AppendQueryParameter( | 281 report_error_url = net::AppendQueryParameter( |
| 295 report_error_url, | 282 report_error_url, |
| 296 kSourceLanguageQueryName, | 283 kSourceLanguageQueryName, |
| 297 translate_driver_->GetLanguageState().original_language()); | 284 translate_driver_->GetLanguageState().original_language()); |
| 298 | 285 |
| 299 report_error_url = TranslateURLUtil::AddHostLocaleToUrl(report_error_url); | 286 report_error_url = TranslateURLUtil::AddHostLocaleToUrl(report_error_url); |
| 300 report_error_url = TranslateURLUtil::AddApiKeyToUrl(report_error_url); | 287 report_error_url = TranslateURLUtil::AddApiKeyToUrl(report_error_url); |
| 301 | 288 |
| 302 chrome::AddSelectedTabWithURL(browser, report_error_url, | 289 chrome::AddSelectedTabWithURL(browser, report_error_url, |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 321 !TranslateDownloadManager::IsSupportedLanguage(source_lang)) { | 308 !TranslateDownloadManager::IsSupportedLanguage(source_lang)) { |
| 322 error_type = TranslateErrors::UNSUPPORTED_LANGUAGE; | 309 error_type = TranslateErrors::UNSUPPORTED_LANGUAGE; |
| 323 } | 310 } |
| 324 | 311 |
| 325 translate_client_->ShowTranslateUI(translate::TRANSLATE_STEP_AFTER_TRANSLATE, | 312 translate_client_->ShowTranslateUI(translate::TRANSLATE_STEP_AFTER_TRANSLATE, |
| 326 source_lang, | 313 source_lang, |
| 327 target_lang, | 314 target_lang, |
| 328 error_type, | 315 error_type, |
| 329 false); | 316 false); |
| 330 | 317 |
| 331 WebContents* web_contents = translate_tab_helper_->GetWebContents(); | |
| 332 if (error_type != TranslateErrors::NONE && | 318 if (error_type != TranslateErrors::NONE && |
| 333 !translate_driver_->IsOffTheRecord()) { | 319 !translate_driver_->IsOffTheRecord()) { |
| 334 TranslateErrorDetails error_details; | 320 TranslateErrorDetails error_details; |
| 335 error_details.time = base::Time::Now(); | 321 error_details.time = base::Time::Now(); |
| 336 error_details.url = web_contents->GetLastCommittedURL(); | 322 error_details.url = translate_driver_->GetLastCommittedURL(); |
| 337 error_details.error = error_type; | 323 error_details.error = error_type; |
| 338 NotifyTranslateError(error_details); | 324 NotifyTranslateError(error_details); |
| 339 } | 325 } |
| 340 } | 326 } |
| 341 | 327 |
| 342 void TranslateManager::OnTranslateScriptFetchComplete( | 328 void TranslateManager::OnTranslateScriptFetchComplete( |
| 343 int page_id, | 329 int page_id, |
| 344 const std::string& source_lang, | 330 const std::string& source_lang, |
| 345 const std::string& target_lang, | 331 const std::string& target_lang, |
| 346 bool success, | 332 bool success, |
| 347 const std::string& data) { | 333 const std::string& data) { |
| 348 WebContents* web_contents = translate_tab_helper_->GetWebContents(); | 334 if (!translate_driver_->HasCurrentPage() || |
| 349 DCHECK(web_contents); | 335 translate_driver_->GetCurrentPageID() != page_id) { |
| 350 NavigationEntry* entry = web_contents->GetController().GetActiveEntry(); | |
| 351 if (!entry || entry->GetPageID() != page_id) { | |
| 352 // We navigated away from the page the translation was triggered on. | 336 // We navigated away from the page the translation was triggered on. |
| 353 return; | 337 return; |
| 354 } | 338 } |
| 355 | 339 |
| 356 if (success) { | 340 if (success) { |
| 357 // Translate the page. | 341 // Translate the page. |
| 358 TranslateScript* translate_script = | 342 TranslateScript* translate_script = |
| 359 TranslateDownloadManager::GetInstance()->script(); | 343 TranslateDownloadManager::GetInstance()->script(); |
| 360 DCHECK(translate_script); | 344 DCHECK(translate_script); |
| 361 DoTranslatePage(translate_script->data(), source_lang, target_lang); | 345 DoTranslatePage(translate_script->data(), source_lang, target_lang); |
| 362 } else { | 346 } else { |
| 363 translate_client_->ShowTranslateUI( | 347 translate_client_->ShowTranslateUI( |
| 364 translate::TRANSLATE_STEP_TRANSLATE_ERROR, | 348 translate::TRANSLATE_STEP_TRANSLATE_ERROR, |
| 365 source_lang, | 349 source_lang, |
| 366 target_lang, | 350 target_lang, |
| 367 TranslateErrors::NETWORK, | 351 TranslateErrors::NETWORK, |
| 368 false); | 352 false); |
| 369 if (!translate_driver_->IsOffTheRecord()) { | 353 if (!translate_driver_->IsOffTheRecord()) { |
| 370 TranslateErrorDetails error_details; | 354 TranslateErrorDetails error_details; |
| 371 error_details.time = base::Time::Now(); | 355 error_details.time = base::Time::Now(); |
| 372 error_details.url = entry->GetURL(); | 356 error_details.url = translate_driver_->GetActiveURL(); |
| 373 error_details.error = TranslateErrors::NETWORK; | 357 error_details.error = TranslateErrors::NETWORK; |
| 374 NotifyTranslateError(error_details); | 358 NotifyTranslateError(error_details); |
| 375 } | 359 } |
| 376 } | 360 } |
| 377 } | 361 } |
| 378 | 362 |
| 379 // static | 363 // static |
| 380 std::string TranslateManager::GetTargetLanguage( | 364 std::string TranslateManager::GetTargetLanguage( |
| 381 const std::vector<std::string>& accept_languages_list) { | 365 const std::vector<std::string>& accept_languages_list) { |
| 382 std::string ui_lang = TranslatePrefs::ConvertLangCodeForTranslation( | 366 std::string ui_lang = TranslatePrefs::ConvertLangCodeForTranslation( |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 409 &auto_target_lang)) { | 393 &auto_target_lang)) { |
| 410 // We need to confirm that the saved target language is still supported. | 394 // We need to confirm that the saved target language is still supported. |
| 411 // Also, GetLanguageCode will take care of removing country code if any. | 395 // Also, GetLanguageCode will take care of removing country code if any. |
| 412 auto_target_lang = | 396 auto_target_lang = |
| 413 TranslateDownloadManager::GetLanguageCode(auto_target_lang); | 397 TranslateDownloadManager::GetLanguageCode(auto_target_lang); |
| 414 if (TranslateDownloadManager::IsSupportedLanguage(auto_target_lang)) | 398 if (TranslateDownloadManager::IsSupportedLanguage(auto_target_lang)) |
| 415 return auto_target_lang; | 399 return auto_target_lang; |
| 416 } | 400 } |
| 417 return std::string(); | 401 return std::string(); |
| 418 } | 402 } |
| OLD | NEW |