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 #ifndef COMPONENTS_TRANSLATE_CORE_BROWSER_LANGUAGE_STATE_H_
      6 #define COMPONENTS_TRANSLATE_CORE_BROWSER_LANGUAGE_STATE_H_
      7 
      8 #include <string>
      9 
     10 #include "base/basictypes.h"
     11 
     12 class TranslateDriver;
     13 
     14 // This class holds the language state of the current page.
     15 // There is one LanguageState instance per tab.
     16 // It is used to determine when navigating to a new page whether it should
     17 // automatically be translated.
     18 // This auto-translate behavior is the expected behavior when:
     19 // - user is on page in language A that they had translated to language B.
     20 // - user clicks a link in that page that takes them to a page also in language
     21 //   A.
     22 class LanguageState {
     23  public:
     24   explicit LanguageState(TranslateDriver* driver);
     25   ~LanguageState();
     26 
     27   // Should be called when the page did a new navigation (whether it is a main
     28   // frame or sub-frame navigation).
     29   void DidNavigate(bool in_page_navigation, bool is_main_frame, bool reload);
     30 
     31   // Should be called when the language of the page has been determined.
     32   // |page_needs_translation| when false indicates that the browser should not
     33   // offer to translate the page.
     34   void LanguageDetermined(const std::string& page_language,
     35                           bool page_needs_translation);
     36 
     37   // Returns the language the current page should be translated to, based on the
     38   // previous page languages and the transition.  This should be called after
     39   // the language page has been determined.
     40   // Returns an empty string if the page should not be auto-translated.
     41   std::string AutoTranslateTo() const;
     42 
     43   // Returns true if the user is navigating through translated links.
     44   bool InTranslateNavigation() const;
     45 
     46   // Returns true if the current page in the associated tab has been translated.
     47   bool IsPageTranslated() const { return original_lang_ != current_lang_; }
     48 
     49   const std::string& original_language() const { return original_lang_; }
     50 
     51   void SetCurrentLanguage(const std::string& language);
     52   const std::string& current_language() const { return current_lang_; }
     53 
     54   bool page_needs_translation() const { return page_needs_translation_; }
     55 
     56   // Whether the page is currently in the process of being translated.
     57   bool translation_pending() const { return translation_pending_; }
     58   void set_translation_pending(bool value) { translation_pending_ = value; }
     59 
     60   // Whether the user has already declined to translate the page.
     61   bool translation_declined() const { return translation_declined_; }
     62   void set_translation_declined(bool value) { translation_declined_ = value; }
     63 
     64   // Whether the current page was navigated through an in-page (fragment)
     65   // navigation.
     66   bool in_page_navigation() const { return in_page_navigation_; }
     67 
     68   // Whether the translate is enabled.
     69   bool translate_enabled() const { return translate_enabled_; }
     70   void SetTranslateEnabled(bool value);
     71 
     72   // Whether the current page's language is different from the previous
     73   // language.
     74   bool HasLanguageChanged() const;
     75 
     76  private:
     77   void SetIsPageTranslated(bool value);
     78 
     79   // Whether the page is translated or not.
     80   bool is_page_translated_;
     81 
     82   // The languages this page is in. Note that current_lang_ is different from
     83   // original_lang_ when the page has been translated.
     84   // Note that these might be empty if the page language has not been determined
     85   // yet.
     86   std::string original_lang_;
     87   std::string current_lang_;
     88 
     89   // Same as above but for the previous page.
     90   std::string prev_original_lang_;
     91   std::string prev_current_lang_;
     92 
     93   // Provides driver-level context to the shared code of the component. Must
     94   // outlive this object.
     95   TranslateDriver* translate_driver_;
     96 
     97   // Whether it is OK to offer to translate the page.  Some pages explictly
     98   // specify that they should not be translated by the browser (this is the case
     99   // for GMail for example, which provides its own translation features).
    100   bool page_needs_translation_;
    101 
    102   // Whether a translation is currently pending.
    103   // This is needed to avoid sending duplicate translate requests to a page.
    104   // Translations may be initiated every time the load stops for the main frame,
    105   // which may happen several times.
    106   // TODO(jcampan): make the client send the language just once per navigation
    107   //                then we can get rid of that state.
    108   bool translation_pending_;
    109 
    110   // Whether the user has declined to translate the page (by closing the infobar
    111   // for example).  This is necessary as a new infobar could be shown if a new
    112   // load happens in the page after the user closed the infobar.
    113   bool translation_declined_;
    114 
    115   // Whether the current navigation is a fragment navigation (in page).
    116   bool in_page_navigation_;
    117 
    118   // Whether the Translate is enabled.
    119   bool translate_enabled_;
    120 
    121   DISALLOW_COPY_AND_ASSIGN(LanguageState);
    122 };
    123 
    124 #endif  // COMPONENTS_TRANSLATE_CORE_BROWSER_LANGUAGE_STATE_H_
    125