| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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/chrome_translate_client.h" | 5 #include "chrome/browser/translate/chrome_translate_client.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/prefs/pref_service.h" | 10 #include "base/prefs/pref_service.h" |
| 11 #include "base/strings/string_split.h" | 11 #include "base/strings/string_split.h" |
| 12 #include "chrome/browser/chrome_notification_types.h" | 12 #include "chrome/browser/chrome_notification_types.h" |
| 13 #include "chrome/browser/infobars/infobar_service.h" | 13 #include "chrome/browser/infobars/infobar_service.h" |
| 14 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
| 15 #include "chrome/browser/translate/translate_accept_languages_factory.h" | 15 #include "chrome/browser/translate/translate_accept_languages_factory.h" |
| 16 #include "chrome/browser/translate/translate_service.h" | 16 #include "chrome/browser/translate/translate_service.h" |
| 17 #include "chrome/browser/ui/browser.h" | 17 #include "chrome/browser/ui/browser.h" |
| 18 #include "chrome/browser/ui/browser_finder.h" | 18 #include "chrome/browser/ui/browser_finder.h" |
| 19 #include "chrome/browser/ui/browser_tabstrip.h" | 19 #include "chrome/browser/ui/browser_tabstrip.h" |
| 20 #include "chrome/browser/ui/browser_window.h" | 20 #include "chrome/browser/ui/browser_window.h" |
| 21 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 21 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 22 #include "chrome/browser/ui/translate/translate_bubble_factory.h" | 22 #include "chrome/browser/ui/translate/translate_bubble_factory.h" |
| 23 #include "chrome/common/pref_names.h" | 23 #include "chrome/common/pref_names.h" |
| 24 #include "components/translate/content/common/translate_messages.h" | 24 #include "components/translate/content/common/translate_messages.h" |
| 25 #include "components/translate/core/browser/language_state.h" |
| 25 #include "components/translate/core/browser/page_translated_details.h" | 26 #include "components/translate/core/browser/page_translated_details.h" |
| 26 #include "components/translate/core/browser/translate_accept_languages.h" | 27 #include "components/translate/core/browser/translate_accept_languages.h" |
| 27 #include "components/translate/core/browser/translate_download_manager.h" | 28 #include "components/translate/core/browser/translate_download_manager.h" |
| 28 #include "components/translate/core/browser/translate_infobar_delegate.h" | 29 #include "components/translate/core/browser/translate_infobar_delegate.h" |
| 29 #include "components/translate/core/browser/translate_manager.h" | 30 #include "components/translate/core/browser/translate_manager.h" |
| 30 #include "components/translate/core/browser/translate_prefs.h" | 31 #include "components/translate/core/browser/translate_prefs.h" |
| 31 #include "components/translate/core/common/language_detection_details.h" | 32 #include "components/translate/core/common/language_detection_details.h" |
| 32 #include "content/public/browser/navigation_details.h" | 33 #include "content/public/browser/navigation_details.h" |
| 33 #include "content/public/browser/navigation_entry.h" | 34 #include "content/public/browser/navigation_entry.h" |
| 34 #include "content/public/browser/notification_service.h" | 35 #include "content/public/browser/notification_service.h" |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 75 max_reload_check_attempts_(kMaxTranslateLoadCheckAttempts), | 76 max_reload_check_attempts_(kMaxTranslateLoadCheckAttempts), |
| 76 translate_driver_(&web_contents->GetController()), | 77 translate_driver_(&web_contents->GetController()), |
| 77 translate_manager_(new TranslateManager(this, prefs::kAcceptLanguages)), | 78 translate_manager_(new TranslateManager(this, prefs::kAcceptLanguages)), |
| 78 weak_pointer_factory_(this) { | 79 weak_pointer_factory_(this) { |
| 79 } | 80 } |
| 80 | 81 |
| 81 ChromeTranslateClient::~ChromeTranslateClient() { | 82 ChromeTranslateClient::~ChromeTranslateClient() { |
| 82 } | 83 } |
| 83 | 84 |
| 84 LanguageState& ChromeTranslateClient::GetLanguageState() { | 85 LanguageState& ChromeTranslateClient::GetLanguageState() { |
| 85 return translate_driver_.GetLanguageState(); | 86 return translate_manager_->GetLanguageState(); |
| 86 } | 87 } |
| 87 | 88 |
| 88 // static | 89 // static |
| 89 scoped_ptr<TranslatePrefs> ChromeTranslateClient::CreateTranslatePrefs( | 90 scoped_ptr<TranslatePrefs> ChromeTranslateClient::CreateTranslatePrefs( |
| 90 PrefService* prefs) { | 91 PrefService* prefs) { |
| 91 #if defined(OS_CHROMEOS) | 92 #if defined(OS_CHROMEOS) |
| 92 const char* preferred_languages_prefs = prefs::kLanguagePreferredLanguages; | 93 const char* preferred_languages_prefs = prefs::kLanguagePreferredLanguages; |
| 93 #else | 94 #else |
| 94 const char* preferred_languages_prefs = NULL; | 95 const char* preferred_languages_prefs = NULL; |
| 95 #endif | 96 #endif |
| (...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 271 } | 272 } |
| 272 | 273 |
| 273 // If the navigation happened while offline don't show the translate | 274 // If the navigation happened while offline don't show the translate |
| 274 // bar since there will be nothing to translate. | 275 // bar since there will be nothing to translate. |
| 275 if (load_details.http_status_code == 0 || | 276 if (load_details.http_status_code == 0 || |
| 276 load_details.http_status_code == net::HTTP_INTERNAL_SERVER_ERROR) { | 277 load_details.http_status_code == net::HTTP_INTERNAL_SERVER_ERROR) { |
| 277 return; | 278 return; |
| 278 } | 279 } |
| 279 | 280 |
| 280 if (!load_details.is_main_frame && | 281 if (!load_details.is_main_frame && |
| 281 translate_driver_.GetLanguageState().translation_declined()) { | 282 GetLanguageState().translation_declined()) { |
| 282 // Some sites (such as Google map) may trigger sub-frame navigations | 283 // Some sites (such as Google map) may trigger sub-frame navigations |
| 283 // when the user interacts with the page. We don't want to show a new | 284 // when the user interacts with the page. We don't want to show a new |
| 284 // infobar if the user already dismissed one in that case. | 285 // infobar if the user already dismissed one in that case. |
| 285 return; | 286 return; |
| 286 } | 287 } |
| 287 | 288 |
| 288 // If not a reload, return. | 289 // If not a reload, return. |
| 289 if (entry->GetTransitionType() != content::PAGE_TRANSITION_RELOAD && | 290 if (entry->GetTransitionType() != content::PAGE_TRANSITION_RELOAD && |
| 290 load_details.type != content::NAVIGATION_TYPE_SAME_PAGE) { | 291 load_details.type != content::NAVIGATION_TYPE_SAME_PAGE) { |
| 291 return; | 292 return; |
| 292 } | 293 } |
| 293 | 294 |
| 294 if (!translate_driver_.GetLanguageState().page_needs_translation()) | 295 if (!GetLanguageState().page_needs_translation()) |
| 295 return; | 296 return; |
| 296 | 297 |
| 297 // Note that we delay it as the ordering of the processing of this callback | 298 // Note that we delay it as the ordering of the processing of this callback |
| 298 // by WebContentsObservers is undefined and might result in the current | 299 // by WebContentsObservers is undefined and might result in the current |
| 299 // infobars being removed. Since the translation initiation process might add | 300 // infobars being removed. Since the translation initiation process might add |
| 300 // an infobar, it must be done after that. | 301 // an infobar, it must be done after that. |
| 301 base::MessageLoop::current()->PostTask( | 302 base::MessageLoop::current()->PostTask( |
| 302 FROM_HERE, | 303 FROM_HERE, |
| 303 base::Bind(&ChromeTranslateClient::InitiateTranslation, | 304 base::Bind(&ChromeTranslateClient::InitiateTranslation, |
| 304 weak_pointer_factory_.GetWeakPtr(), | 305 weak_pointer_factory_.GetWeakPtr(), |
| 305 translate_driver_.GetLanguageState().original_language(), | 306 GetLanguageState().original_language(), |
| 306 0)); | 307 0)); |
| 307 } | 308 } |
| 308 | 309 |
| 309 void ChromeTranslateClient::DidNavigateAnyFrame( | 310 void ChromeTranslateClient::DidNavigateAnyFrame( |
| 310 const content::LoadCommittedDetails& details, | 311 const content::LoadCommittedDetails& details, |
| 311 const content::FrameNavigateParams& params) { | 312 const content::FrameNavigateParams& params) { |
| 312 // Let the LanguageState clear its state. | 313 // Let the LanguageState clear its state. |
| 313 translate_driver_.DidNavigate(details); | 314 const bool reload = |
| 315 details.entry->GetTransitionType() == content::PAGE_TRANSITION_RELOAD || |
| 316 details.type == content::NAVIGATION_TYPE_SAME_PAGE; |
| 317 GetLanguageState().DidNavigate( |
| 318 details.is_in_page, details.is_main_frame, reload); |
| 314 } | 319 } |
| 315 | 320 |
| 316 void ChromeTranslateClient::WebContentsDestroyed() { | 321 void ChromeTranslateClient::WebContentsDestroyed() { |
| 317 // Translation process can be interrupted. | 322 // Translation process can be interrupted. |
| 318 // Destroying the TranslateManager now guarantees that it never has to deal | 323 // Destroying the TranslateManager now guarantees that it never has to deal |
| 319 // with NULL WebContents. | 324 // with NULL WebContents. |
| 320 translate_manager_.reset(); | 325 translate_manager_.reset(); |
| 321 } | 326 } |
| 322 | 327 |
| 323 #if defined(CLD2_DYNAMIC_MODE) | 328 #if defined(CLD2_DYNAMIC_MODE) |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 450 s_cached_data_offset_ = data_offset; | 455 s_cached_data_offset_ = data_offset; |
| 451 s_cached_data_length_ = data_length; | 456 s_cached_data_length_ = data_length; |
| 452 } | 457 } |
| 453 } | 458 } |
| 454 } | 459 } |
| 455 | 460 |
| 456 #endif // defined(CLD2_DYNAMIC_MODE) | 461 #endif // defined(CLD2_DYNAMIC_MODE) |
| 457 | 462 |
| 458 void ChromeTranslateClient::InitiateTranslation(const std::string& page_lang, | 463 void ChromeTranslateClient::InitiateTranslation(const std::string& page_lang, |
| 459 int attempt) { | 464 int attempt) { |
| 460 if (translate_driver_.GetLanguageState().translation_pending()) | 465 if (GetLanguageState().translation_pending()) |
| 461 return; | 466 return; |
| 462 | 467 |
| 463 // During a reload we need web content to be available before the | 468 // During a reload we need web content to be available before the |
| 464 // translate script is executed. Otherwise we will run the translate script on | 469 // translate script is executed. Otherwise we will run the translate script on |
| 465 // an empty DOM which will fail. Therefore we wait a bit to see if the page | 470 // an empty DOM which will fail. Therefore we wait a bit to see if the page |
| 466 // has finished. | 471 // has finished. |
| 467 if (web_contents()->IsLoading() && attempt < max_reload_check_attempts_) { | 472 if (web_contents()->IsLoading() && attempt < max_reload_check_attempts_) { |
| 468 int backoff = attempt * kMaxTranslateLoadCheckAttempts; | 473 int backoff = attempt * kMaxTranslateLoadCheckAttempts; |
| 469 base::MessageLoop::current()->PostDelayedTask( | 474 base::MessageLoop::current()->PostDelayedTask( |
| 470 FROM_HERE, | 475 FROM_HERE, |
| 471 base::Bind(&ChromeTranslateClient::InitiateTranslation, | 476 base::Bind(&ChromeTranslateClient::InitiateTranslation, |
| 472 weak_pointer_factory_.GetWeakPtr(), | 477 weak_pointer_factory_.GetWeakPtr(), |
| 473 page_lang, | 478 page_lang, |
| 474 ++attempt), | 479 ++attempt), |
| 475 base::TimeDelta::FromMilliseconds(backoff)); | 480 base::TimeDelta::FromMilliseconds(backoff)); |
| 476 return; | 481 return; |
| 477 } | 482 } |
| 478 | 483 |
| 479 translate_manager_->InitiateTranslation( | 484 translate_manager_->InitiateTranslation( |
| 480 TranslateDownloadManager::GetLanguageCode(page_lang)); | 485 TranslateDownloadManager::GetLanguageCode(page_lang)); |
| 481 } | 486 } |
| 482 | 487 |
| 483 void ChromeTranslateClient::OnLanguageDetermined( | 488 void ChromeTranslateClient::OnLanguageDetermined( |
| 484 const LanguageDetectionDetails& details, | 489 const LanguageDetectionDetails& details, |
| 485 bool page_needs_translation) { | 490 bool page_needs_translation) { |
| 486 translate_driver_.GetLanguageState().LanguageDetermined( | 491 GetLanguageState().LanguageDetermined(details.adopted_language, |
| 487 details.adopted_language, page_needs_translation); | 492 page_needs_translation); |
| 488 | 493 |
| 489 if (web_contents()) | 494 if (web_contents()) |
| 490 translate_manager_->InitiateTranslation(details.adopted_language); | 495 translate_manager_->InitiateTranslation(details.adopted_language); |
| 491 | 496 |
| 492 content::NotificationService::current()->Notify( | 497 content::NotificationService::current()->Notify( |
| 493 chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED, | 498 chrome::NOTIFICATION_TAB_LANGUAGE_DETERMINED, |
| 494 content::Source<content::WebContents>(web_contents()), | 499 content::Source<content::WebContents>(web_contents()), |
| 495 content::Details<const LanguageDetectionDetails>(&details)); | 500 content::Details<const LanguageDetectionDetails>(&details)); |
| 496 } | 501 } |
| 497 | 502 |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 545 if (GetLanguageState().InTranslateNavigation()) | 550 if (GetLanguageState().InTranslateNavigation()) |
| 546 return; | 551 return; |
| 547 } | 552 } |
| 548 | 553 |
| 549 TranslateBubbleFactory::Show( | 554 TranslateBubbleFactory::Show( |
| 550 browser->window(), web_contents(), step, error_type); | 555 browser->window(), web_contents(), step, error_type); |
| 551 #else | 556 #else |
| 552 NOTREACHED(); | 557 NOTREACHED(); |
| 553 #endif | 558 #endif |
| 554 } | 559 } |
| OLD | NEW |