Home | History | Annotate | Download | only in browser
      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.
      4 
      5 #include "components/translate/core/browser/language_state.h"
      6 
      7 #include "base/logging.h"
      8 #include "components/translate/core/browser/translate_driver.h"
      9 
     10 LanguageState::LanguageState(TranslateDriver* driver)
     11     : is_page_translated_(false),
     12       translate_driver_(driver),
     13       page_needs_translation_(false),
     14       translation_pending_(false),
     15       translation_declined_(false),
     16       in_page_navigation_(false),
     17       translate_enabled_(false) {
     18   DCHECK(translate_driver_);
     19 }
     20 
     21 LanguageState::~LanguageState() {
     22 }
     23 
     24 void LanguageState::DidNavigate(bool in_page_navigation,
     25                                 bool is_main_frame,
     26                                 bool reload) {
     27   in_page_navigation_ = in_page_navigation;
     28   if (in_page_navigation_ || !is_main_frame)
     29     return;  // Don't reset our states, the page has not changed.
     30 
     31   if (reload) {
     32     // We might not get a LanguageDetermined notifications on reloads. Make sure
     33     // to keep the original language and to set current_lang_ so
     34     // IsPageTranslated() returns false.
     35     current_lang_ = original_lang_;
     36   } else {
     37     prev_original_lang_ = original_lang_;
     38     prev_current_lang_ = current_lang_;
     39     original_lang_.clear();
     40     current_lang_.clear();
     41   }
     42 
     43   SetIsPageTranslated(false);
     44 
     45   translation_pending_ = false;
     46   translation_declined_ = false;
     47 
     48   SetTranslateEnabled(false);
     49 }
     50 
     51 void LanguageState::LanguageDetermined(const std::string& page_language,
     52                                        bool page_needs_translation) {
     53   if (in_page_navigation_ && !original_lang_.empty()) {
     54     // In-page navigation, we don't expect our states to change.
     55     // Note that we'll set the languages if original_lang_ is empty.  This might
     56     // happen if the we did not get called on the top-page.
     57     return;
     58   }
     59   page_needs_translation_ = page_needs_translation;
     60   original_lang_ = page_language;
     61   current_lang_ = page_language;
     62   SetIsPageTranslated(false);
     63 }
     64 
     65 bool LanguageState::InTranslateNavigation() const {
     66   // The user is in the same translate session if
     67   //   - no translation is pending
     68   //   - this page is in the same language as the previous page
     69   //   - the previous page had been translated
     70   //   - the new page was navigated through a link.
     71   return
     72       !translation_pending_ &&
     73       prev_original_lang_ == original_lang_ &&
     74       prev_original_lang_ != prev_current_lang_ &&
     75       translate_driver_->IsLinkNavigation();
     76 }
     77 
     78 void LanguageState::SetCurrentLanguage(const std::string& language) {
     79   current_lang_ = language;
     80   SetIsPageTranslated(current_lang_ != original_lang_);
     81 }
     82 
     83 std::string LanguageState::AutoTranslateTo() const {
     84   if (InTranslateNavigation() && !is_page_translated_)
     85     return prev_current_lang_;
     86 
     87   return std::string();
     88 }
     89 
     90 void LanguageState::SetTranslateEnabled(bool value) {
     91   if (translate_enabled_ == value)
     92     return;
     93 
     94   translate_enabled_ = value;
     95   translate_driver_->OnTranslateEnabledChanged();
     96 }
     97 
     98 bool LanguageState::HasLanguageChanged() const {
     99   return original_lang_ != prev_original_lang_;
    100 }
    101 
    102 void LanguageState::SetIsPageTranslated(bool value) {
    103   if (is_page_translated_ == value)
    104     return;
    105 
    106   is_page_translated_ = value;
    107   translate_driver_->OnIsPageTranslatedChanged();
    108 
    109   // With the translation done, the translate feature must be enabled.
    110   if (is_page_translated_)
    111     SetTranslateEnabled(true);
    112 }
    113