1 // Copyright (c) 2011 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. 4 5 #include "chrome/browser/tab_contents/language_state.h" 6 7 #include "content/browser/tab_contents/navigation_controller.h" 8 #include "content/browser/tab_contents/navigation_entry.h" 9 LanguageState(NavigationController * nav_controller)10LanguageState::LanguageState(NavigationController* nav_controller) 11 : navigation_controller_(nav_controller), 12 page_translatable_(false), 13 translation_pending_(false), 14 translation_declined_(false), 15 in_page_navigation_(false) { 16 } 17 ~LanguageState()18LanguageState::~LanguageState() { 19 } 20 DidNavigate(const NavigationController::LoadCommittedDetails & details)21void LanguageState::DidNavigate( 22 const NavigationController::LoadCommittedDetails& details) { 23 in_page_navigation_ = details.is_in_page; 24 if (in_page_navigation_ || !details.is_main_frame) 25 return; // Don't reset our states, the page has not changed. 26 27 bool reload = details.entry->transition_type() == PageTransition::RELOAD || 28 details.type == NavigationType::SAME_PAGE; 29 if (reload) { 30 // We might not get a LanguageDetermined notifications on reloads. Make sure 31 // to keep the original language and to set current_lang_ so 32 // IsPageTranslated() returns false. 33 current_lang_ = original_lang_; 34 } else { 35 prev_original_lang_ = original_lang_; 36 prev_current_lang_ = current_lang_; 37 original_lang_.clear(); 38 current_lang_.clear(); 39 } 40 41 translation_pending_ = false; 42 translation_declined_ = false; 43 } 44 LanguageDetermined(const std::string & page_language,bool page_translatable)45void LanguageState::LanguageDetermined(const std::string& page_language, 46 bool page_translatable) { 47 if (in_page_navigation_ && !original_lang_.empty()) { 48 // In-page navigation, we don't expect our states to change. 49 // Note that we'll set the languages if original_lang_ is empty. This might 50 // happen if the we did not get called on the top-page. 51 return; 52 } 53 page_translatable_ = page_translatable; 54 original_lang_ = page_language; 55 current_lang_ = page_language; 56 } 57 AutoTranslateTo() const58std::string LanguageState::AutoTranslateTo() const { 59 // Only auto-translate if: 60 // - no translation is pending 61 // - this page is in the same language as the previous page 62 // - the previous page had been translated 63 // - this page is not already translated 64 // - the new page was navigated through a link. 65 if (!translation_pending_ && 66 prev_original_lang_ == original_lang_ && 67 prev_original_lang_ != prev_current_lang_ && 68 original_lang_ == current_lang_ && 69 navigation_controller_->GetActiveEntry() && 70 navigation_controller_->GetActiveEntry()->transition_type() == 71 PageTransition::LINK) { 72 return prev_current_lang_; 73 } 74 75 return std::string(); 76 } 77