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