Home | History | Annotate | Download | only in translate
      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_TRANSLATE_TRANSLATE_INFOBAR_DELEGATE_H_
      6 #define CHROME_BROWSER_TRANSLATE_TRANSLATE_INFOBAR_DELEGATE_H_
      7 #pragma once
      8 
      9 #include <string>
     10 #include <vector>
     11 
     12 #include "chrome/browser/tab_contents/confirm_infobar_delegate.h"
     13 #include "chrome/browser/translate/translate_prefs.h"
     14 #include "chrome/common/translate_errors.h"
     15 
     16 class SkBitmap;
     17 class TranslateInfoBarView;
     18 
     19 class TranslateInfoBarDelegate : public InfoBarDelegate {
     20  public:
     21   // The different types of infobars that can be shown for translation.
     22   enum Type {
     23     BEFORE_TRANSLATE,
     24     TRANSLATING,
     25     AFTER_TRANSLATE,
     26     TRANSLATION_ERROR
     27   };
     28 
     29   // The types of background color animations.
     30   enum BackgroundAnimationType {
     31     NONE,
     32     NORMAL_TO_ERROR,
     33     ERROR_TO_NORMAL
     34   };
     35 
     36   static const size_t kNoIndex;
     37 
     38   // Factory method to create a non-error translate infobar.
     39   // The original and target language specified are the ASCII language codes
     40   // (ex: en, fr...).
     41   // Returns NULL if it failed, typically if |original_language| or
     42   // |target_language| is not a supported language.
     43   static TranslateInfoBarDelegate* CreateDelegate(
     44       Type infobar_type,
     45       TabContents* tab_contents,
     46       const std::string& original_language,
     47       const std::string& target_language);
     48 
     49   // Factory method to create an error translate infobar.
     50   static TranslateInfoBarDelegate* CreateErrorDelegate(
     51       TranslateErrors::Type error_type,
     52       TabContents* tab_contents,
     53       const std::string& original_language,
     54       const std::string& target_language);
     55 
     56   virtual ~TranslateInfoBarDelegate();
     57 
     58   // Returns the number of languages supported.
     59   size_t GetLanguageCount() const { return languages_.size(); }
     60 
     61   // Returns the ISO code for the language at |index|.
     62   std::string GetLanguageCodeAt(size_t index) const;
     63 
     64   // Returns the displayable name for the language at |index|.
     65   string16 GetLanguageDisplayableNameAt(size_t index) const;
     66 
     67   TabContents* tab_contents() const { return tab_contents_; }
     68 
     69   Type type() const { return type_; }
     70 
     71   TranslateErrors::Type error() const { return error_; }
     72 
     73   size_t original_language_index() const { return original_language_index_; }
     74   size_t target_language_index() const { return target_language_index_; }
     75 
     76   // Convenience methods.
     77   std::string GetOriginalLanguageCode() const;
     78   std::string GetTargetLanguageCode() const;
     79 
     80   // Called by the InfoBar to notify that the original/target language has
     81   // changed and is now the language at |language_index|.
     82   virtual void SetOriginalLanguage(size_t language_index);
     83   virtual void SetTargetLanguage(size_t language_index);
     84 
     85   // Returns true if the current infobar indicates an error (in which case it
     86   // should get a yellow background instead of a blue one).
     87   bool IsError() const { return type_ == TRANSLATION_ERROR; }
     88 
     89   // Returns what kind of background fading effect the infobar should use when
     90   // its is shown.
     91   BackgroundAnimationType background_animation_type() const {
     92     return background_animation_;
     93   }
     94 
     95   virtual void Translate();
     96   virtual void RevertTranslation();
     97   virtual void ReportLanguageDetectionError();
     98 
     99   // Called when the user declines to translate a page, by either closing the
    100   // infobar or pressing the "Don't translate" button.
    101   void TranslationDeclined();
    102 
    103   // Methods called by the Options menu delegate.
    104   virtual bool IsLanguageBlacklisted();
    105   virtual void ToggleLanguageBlacklist();
    106   virtual bool IsSiteBlacklisted();
    107   virtual void ToggleSiteBlacklist();
    108   virtual bool ShouldAlwaysTranslate();
    109   virtual void ToggleAlwaysTranslate();
    110 
    111   // Methods called by the extra-buttons that can appear on the "before
    112   // translate" infobar (when the user has accepted/declined the translation
    113   // several times).
    114   virtual void AlwaysTranslatePageLanguage();
    115   virtual void NeverTranslatePageLanguage();
    116 
    117   // The following methods are called by the infobar that displays the status
    118   // while translating and also the one displaying the error message.
    119   string16 GetMessageInfoBarText();
    120   string16 GetMessageInfoBarButtonText();
    121   void MessageInfoBarButtonPressed();
    122   bool ShouldShowMessageInfoBarButton();
    123 
    124   // Called by the before translate infobar to figure-out if it should show
    125   // an extra button to let the user black-list/white-list that language (based
    126   // on how many times the user accepted/declined translation).
    127   bool ShouldShowNeverTranslateButton();
    128   bool ShouldShowAlwaysTranslateButton();
    129 
    130   // Sets this infobar background animation based on the previous infobar shown.
    131   // A fading background effect is used when transitioning from a normal state
    132   // to an error state (and vice-versa).
    133   void UpdateBackgroundAnimation(TranslateInfoBarDelegate* previous_infobar);
    134 
    135   // Convenience method that returns the displayable language name for
    136   // |language_code| in the current application locale.
    137   static string16 GetLanguageDisplayableName(const std::string& language_code);
    138 
    139   // Adds the strings that should be displayed in the after translate infobar to
    140   // |strings|. The text in that infobar is:
    141   // "The page has been translated from <lang1> to <lang2>."
    142   // Because <lang1> and <lang2> are displayed in menu buttons, the text is
    143   // split in 3 chunks.  |swap_languages| is set to true if <lang1> and <lang2>
    144   // should be inverted (some languages express the sentense as "The page has
    145   // been translate to <lang2> from <lang1>.").
    146   static void GetAfterTranslateStrings(std::vector<string16>* strings,
    147                                        bool* swap_languages);
    148 
    149  protected:
    150   // For testing.
    151   TranslateInfoBarDelegate(Type infobar_type,
    152                            TranslateErrors::Type error,
    153                            TabContents* tab_contents,
    154                            const std::string& original_language,
    155                            const std::string& target_language);
    156   Type type_;
    157 
    158  private:
    159   typedef std::pair<std::string, string16> LanguageNamePair;
    160 
    161   // InfoBarDelegate:
    162   virtual InfoBar* CreateInfoBar();
    163   virtual void InfoBarDismissed();
    164   virtual void InfoBarClosed();
    165   virtual SkBitmap* GetIcon() const;
    166   virtual InfoBarDelegate::Type GetInfoBarType() const;
    167   virtual TranslateInfoBarDelegate* AsTranslateInfoBarDelegate();
    168 
    169   // Gets the host of the page being translated, or an empty string if no URL is
    170   // associated with the current page.
    171   std::string GetPageHost();
    172 
    173   // The type of fading animation if any that should be used when showing this
    174   // infobar.
    175   BackgroundAnimationType background_animation_;
    176 
    177   TabContents* tab_contents_;
    178 
    179   // The list supported languages for translation.
    180   // The pair first string is the language ISO code (ex: en, fr...), the second
    181   // string is the displayable name on the current locale.
    182   // The languages are sorted alphabetically based on the displayable name.
    183   std::vector<LanguageNamePair> languages_;
    184 
    185   // The index for language the page is originally in.
    186   size_t original_language_index_;
    187 
    188   // The index for language the page is originally in that was originally
    189   // reported (original_language_index_ changes if the user selects a new
    190   // original language, but this one does not).  This is necessary to report
    191   // language detection errors with the right original language even if the user
    192   // changed the original language.
    193   size_t initial_original_language_index_;
    194 
    195   // The index for language the page should be translated to.
    196   size_t target_language_index_;
    197 
    198   // The error that occurred when trying to translate (NONE if no error).
    199   TranslateErrors::Type error_;
    200 
    201   // The current infobar view.
    202   TranslateInfoBarView* infobar_view_;
    203 
    204   // The translation related preferences.
    205   TranslatePrefs prefs_;
    206 
    207   DISALLOW_COPY_AND_ASSIGN(TranslateInfoBarDelegate);
    208 };
    209 
    210 #endif  // CHROME_BROWSER_TRANSLATE_TRANSLATE_INFOBAR_DELEGATE_H_
    211