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_TRANSLATE_UI_DELEGATE_H_
      6 #define COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_UI_DELEGATE_H_
      7 
      8 #include <string>
      9 #include <vector>
     10 
     11 #include "base/logging.h"
     12 #include "base/memory/scoped_ptr.h"
     13 #include "base/memory/weak_ptr.h"
     14 #include "base/strings/string16.h"
     15 #include "components/translate/core/common/translate_errors.h"
     16 
     17 namespace translate {
     18 
     19 class LanguageState;
     20 class TranslateClient;
     21 class TranslateDriver;
     22 class TranslateManager;
     23 class TranslatePrefs;
     24 
     25 // The TranslateUIDelegate is a generic delegate for UI which offers Translate
     26 // feature to the user.
     27 class TranslateUIDelegate {
     28  public:
     29   static const size_t kNoIndex = static_cast<size_t>(-1);
     30 
     31   TranslateUIDelegate(const base::WeakPtr<TranslateManager>& translate_manager,
     32                       const std::string& original_language,
     33                       const std::string& target_language);
     34   virtual ~TranslateUIDelegate();
     35 
     36   // Handles when an error message is shown.
     37   void OnErrorShown(TranslateErrors::Type error_type);
     38 
     39   // Returns the LanguageState associated with this object.
     40   const LanguageState& GetLanguageState();
     41 
     42   // Returns the number of languages supported.
     43   size_t GetNumberOfLanguages() const;
     44 
     45   // Returns the original language index.
     46   size_t GetOriginalLanguageIndex() const;
     47 
     48   // Updates the original language index.
     49   void UpdateOriginalLanguageIndex(size_t language_index);
     50 
     51   // Returns the target language index.
     52   size_t GetTargetLanguageIndex() const;
     53 
     54   // Updates the target language index.
     55   void UpdateTargetLanguageIndex(size_t language_index);
     56 
     57   // Returns the ISO code for the language at |index|.
     58   std::string GetLanguageCodeAt(size_t index) const;
     59 
     60   // Returns the displayable name for the language at |index|.
     61   base::string16 GetLanguageNameAt(size_t index) const;
     62 
     63   // The original language for Translate.
     64   std::string GetOriginalLanguageCode() const;
     65 
     66   // The target language for Translate.
     67   std::string GetTargetLanguageCode() const;
     68 
     69   // Starts translating the current page.
     70   void Translate();
     71 
     72   // Reverts translation.
     73   void RevertTranslation();
     74 
     75   // Processes when the user declines translation.
     76   void TranslationDeclined(bool explicitly_closed);
     77 
     78   // Returns true if the current language is blocked.
     79   bool IsLanguageBlocked();
     80 
     81   // Sets the value if the current language is blocked.
     82   void SetLanguageBlocked(bool value);
     83 
     84   // Returns true if the current webpage is blacklisted.
     85   bool IsSiteBlacklisted();
     86 
     87   // Sets the value if the current webpage is blacklisted.
     88   void SetSiteBlacklist(bool value);
     89 
     90   // Returns true if the webpage in the current original language should be
     91   // translated into the current target language automatically.
     92   bool ShouldAlwaysTranslate();
     93 
     94   // Sets the value if the webpage in the current original language should be
     95   // translated into the current target language automatically.
     96   void SetAlwaysTranslate(bool value);
     97 
     98  private:
     99   // Gets the host of the page being translated, or an empty string if no URL is
    100   // associated with the current page.
    101   std::string GetPageHost();
    102 
    103   TranslateDriver* translate_driver_;
    104   base::WeakPtr<TranslateManager> translate_manager_;
    105 
    106   typedef std::pair<std::string, base::string16> LanguageNamePair;
    107 
    108   // The list supported languages for translation.
    109   // The pair first string is the language ISO code (ex: en, fr...), the second
    110   // string is the displayable name on the current locale.
    111   // The languages are sorted alphabetically based on the displayable name.
    112   std::vector<LanguageNamePair> languages_;
    113 
    114   // The index for language the page is originally in.
    115   size_t original_language_index_;
    116 
    117   // The index for language the page is originally in that was originally
    118   // reported (original_language_index_ changes if the user selects a new
    119   // original language, but this one does not).  This is necessary to report
    120   // language detection errors with the right original language even if the user
    121   // changed the original language.
    122   size_t initial_original_language_index_;
    123 
    124   // The index for language the page should be translated to.
    125   size_t target_language_index_;
    126 
    127   // The translation related preferences.
    128   scoped_ptr<TranslatePrefs> prefs_;
    129 
    130   DISALLOW_COPY_AND_ASSIGN(TranslateUIDelegate);
    131 };
    132 
    133 }  // namespace translate
    134 
    135 #endif  // COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_UI_DELEGATE_H_
    136