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 
      8 #include <string>
      9 #include <utility>
     10 #include <vector>
     11 
     12 #include "base/logging.h"
     13 #include "base/memory/scoped_ptr.h"
     14 #include "chrome/browser/infobars/infobar_delegate.h"
     15 #include "chrome/browser/translate/translate_prefs.h"
     16 #include "chrome/browser/translate/translate_ui_delegate.h"
     17 #include "chrome/common/translate/translate_errors.h"
     18 #include "components/translate/common/translate_constants.h"
     19 
     20 class PrefService;
     21 
     22 // The defaults after which extra shortcuts for options
     23 // can be shown.
     24 struct ShortcutConfiguration {
     25   int always_translate_min_count;
     26   int never_translate_min_count;
     27 };
     28 
     29 class TranslateInfoBarDelegate : public InfoBarDelegate {
     30  public:
     31   // The different types of infobars that can be shown for translation.
     32   enum Type {
     33     BEFORE_TRANSLATE,
     34     TRANSLATING,
     35     AFTER_TRANSLATE,
     36     TRANSLATION_ERROR
     37   };
     38 
     39   // The types of background color animations.
     40   enum BackgroundAnimationType {
     41     NONE,
     42     NORMAL_TO_ERROR,
     43     ERROR_TO_NORMAL
     44   };
     45 
     46   static const size_t kNoIndex;
     47 
     48   virtual ~TranslateInfoBarDelegate();
     49 
     50   // Factory method to create a translate infobar.  |error_type| must be
     51   // specified iff |infobar_type| == TRANSLATION_ERROR.  For other infobar
     52   // types, |original_language| and |target_language| must be ASCII language
     53   // codes (e.g. "en", "fr", etc.) for languages the TranslateManager supports
     54   // translating.  The lone exception is when the user initiates translation
     55   // from the context menu, in which case it's legal to call this with
     56   // |infobar_type| == TRANSLATING and
     57   // |original_language| == kUnknownLanguageCode.
     58   //
     59   // If |replace_existing_infobar| is true, the infobar is created and added to
     60   // the infobar service for |web_contents|, replacing any other translate
     61   // infobar already present there.  Otherwise, the infobar will only be added
     62   // if there is no other translate infobar already present.
     63   static void Create(bool replace_existing_infobar,
     64                      content::WebContents* web_contents,
     65                      Type infobar_type,
     66                      const std::string& original_language,
     67                      const std::string& target_language,
     68                      TranslateErrors::Type error_type,
     69                      PrefService* prefs,
     70                      const ShortcutConfiguration& shortcut_config);
     71 
     72   // Returns the number of languages supported.
     73   size_t num_languages() const { return ui_delegate_.GetNumberOfLanguages(); }
     74 
     75   // Returns the ISO code for the language at |index|.
     76   std::string language_code_at(size_t index) const {
     77     return ui_delegate_.GetLanguageCodeAt(index);
     78   }
     79 
     80   // Returns the displayable name for the language at |index|.
     81   base::string16 language_name_at(size_t index) const {
     82     return ui_delegate_.GetLanguageNameAt(index);
     83   }
     84 
     85   Type infobar_type() const { return infobar_type_; }
     86 
     87   TranslateErrors::Type error_type() const { return error_type_; }
     88 
     89   size_t original_language_index() const {
     90     return ui_delegate_.GetOriginalLanguageIndex();
     91   }
     92   void UpdateOriginalLanguageIndex(size_t language_index);
     93 
     94   size_t target_language_index() const {
     95     return ui_delegate_.GetTargetLanguageIndex();
     96   }
     97   void UpdateTargetLanguageIndex(size_t language_index);
     98 
     99   // Convenience methods.
    100   std::string original_language_code() const {
    101     return ui_delegate_.GetOriginalLanguageCode();
    102   }
    103   std::string target_language_code() const {
    104     return ui_delegate_.GetTargetLanguageCode();
    105   }
    106 
    107   // Returns true if the current infobar indicates an error (in which case it
    108   // should get a yellow background instead of a blue one).
    109   bool is_error() const { return infobar_type_ == TRANSLATION_ERROR; }
    110 
    111   // Returns what kind of background fading effect the infobar should use when
    112   // its is shown.
    113   BackgroundAnimationType background_animation_type() const {
    114     return background_animation_;
    115   }
    116 
    117   virtual void Translate();
    118   virtual void RevertTranslation();
    119   void ReportLanguageDetectionError();
    120 
    121   // Called when the user declines to translate a page, by either closing the
    122   // infobar or pressing the "Don't translate" button.
    123   virtual void TranslationDeclined();
    124 
    125   // Methods called by the Options menu delegate.
    126   virtual bool IsTranslatableLanguageByPrefs();
    127   virtual void ToggleTranslatableLanguageByPrefs();
    128   virtual bool IsSiteBlacklisted();
    129   virtual void ToggleSiteBlacklist();
    130   virtual bool ShouldAlwaysTranslate();
    131   virtual void ToggleAlwaysTranslate();
    132 
    133   // Methods called by the extra-buttons that can appear on the "before
    134   // translate" infobar (when the user has accepted/declined the translation
    135   // several times).
    136   void AlwaysTranslatePageLanguage();
    137   void NeverTranslatePageLanguage();
    138 
    139   // The following methods are called by the infobar that displays the status
    140   // while translating and also the one displaying the error message.
    141   base::string16 GetMessageInfoBarText();
    142   base::string16 GetMessageInfoBarButtonText();
    143   void MessageInfoBarButtonPressed();
    144   bool ShouldShowMessageInfoBarButton();
    145 
    146   // Called by the before translate infobar to figure-out if it should show
    147   // an extra shortcut to let the user black-list/white-list that language
    148   // (based on how many times the user accepted/declined translation).
    149   // The shortcut itself is platform specific, it can be a button or a new bar
    150   // for example.
    151   bool ShouldShowNeverTranslateShortcut();
    152   bool ShouldShowAlwaysTranslateShortcut();
    153 
    154   // Convenience method that returns the displayable language name for
    155   // |language_code| in the current application locale.
    156   static base::string16 GetLanguageDisplayableName(
    157       const std::string& language_code);
    158 
    159   // Adds the strings that should be displayed in the after translate infobar to
    160   // |strings|. If |autodetermined_source_language| is false, the text in that
    161   // infobar is:
    162   // "The page has been translated from <lang1> to <lang2>."
    163   // Otherwise:
    164   // "The page has been translated to <lang1>."
    165   // Because <lang1>, or <lang1> and <lang2> are displayed in menu buttons, the
    166   // text is split in 2 or 3 chunks. |swap_languages| is set to true if
    167   // |autodetermined_source_language| is false, and <lang1> and <lang2>
    168   // should be inverted (some languages express the sentense as "The page has
    169   // been translate to <lang2> from <lang1>."). It is ignored if
    170   // |autodetermined_source_language| is true.
    171   static void GetAfterTranslateStrings(std::vector<base::string16>* strings,
    172                                        bool* swap_languages,
    173                                        bool autodetermined_source_language);
    174 
    175  protected:
    176   TranslateInfoBarDelegate(content::WebContents* web_contents,
    177                            Type infobar_type,
    178                            TranslateInfoBarDelegate* old_delegate,
    179                            const std::string& original_language,
    180                            const std::string& target_language,
    181                            TranslateErrors::Type error_type,
    182                            PrefService* prefs,
    183                            ShortcutConfiguration shortcut_config);
    184 
    185  private:
    186   friend class TranslationInfoBarTest;
    187   typedef std::pair<std::string, base::string16> LanguageNamePair;
    188 
    189   // Returns a translate infobar that owns |delegate|.
    190   static scoped_ptr<InfoBar> CreateInfoBar(
    191       scoped_ptr<TranslateInfoBarDelegate> delegate);
    192 
    193   // InfoBarDelegate:
    194   virtual void InfoBarDismissed() OVERRIDE;
    195   virtual int GetIconID() const OVERRIDE;
    196   virtual InfoBarDelegate::Type GetInfoBarType() const OVERRIDE;
    197   virtual bool ShouldExpire(
    198        const content::LoadCommittedDetails& details) const OVERRIDE;
    199   virtual TranslateInfoBarDelegate* AsTranslateInfoBarDelegate() OVERRIDE;
    200 
    201   Type infobar_type_;
    202 
    203   // The type of fading animation if any that should be used when showing this
    204   // infobar.
    205   BackgroundAnimationType background_animation_;
    206 
    207   TranslateUIDelegate ui_delegate_;
    208 
    209   // The error that occurred when trying to translate (NONE if no error).
    210   TranslateErrors::Type error_type_;
    211 
    212   // The translation related preferences.
    213   TranslatePrefs prefs_;
    214 
    215   // Translation shortcut configuration
    216   ShortcutConfiguration shortcut_config_;
    217   DISALLOW_COPY_AND_ASSIGN(TranslateInfoBarDelegate);
    218 };
    219 
    220 #endif  // CHROME_BROWSER_TRANSLATE_TRANSLATE_INFOBAR_DELEGATE_H_
    221