summaryrefslogtreecommitdiffstats
path: root/chrome/browser/translate/translate_infobars_delegates.h
blob: 02d905d92812273fb2a1b7387845047f15720452 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_TRANSLATE_TRANSLATE_INFOBARS_DELEGATES_H_
#define CHROME_BROWSER_TRANSLATE_TRANSLATE_INFOBARS_DELEGATES_H_

#include "chrome/browser/tab_contents/infobar_delegate.h"
#include "chrome/browser/translate/translate_prefs.h"

class SkBitmap;

// An interface derived from InfoBarDelegate to form the base interface for
// translate infobars.
class TranslateInfoBarDelegate : public InfoBarDelegate {
 public:
  enum TranslateState {
    kBeforeTranslate = 1,
    kTranslating,
    kAfterTranslate,
    kTranslationFailed,
  };

  // Instantiates a TranslateInfoBarDelegate. Can return NULL if the passed
  // languages are not supported.
  static TranslateInfoBarDelegate* Create(TabContents* contents,
                                          PrefService* user_prefs,
                                          TranslateState state,
                                          const GURL& url,
                                          const std::string& original_language,
                                          const std::string& target_language);

  void UpdateState(TranslateState new_state);
  void GetAvailableOriginalLanguages(std::vector<std::string>* languages);
  void GetAvailableTargetLanguages(std::vector<std::string>* languages);
  void ModifyOriginalLanguage(int lang_index);
  void ModifyTargetLanguage(int lang_index);
  virtual void Translate();
  virtual void TranslationDeclined();
  virtual bool IsLanguageBlacklisted();
  virtual void ToggleLanguageBlacklist();
  virtual bool IsSiteBlacklisted();
  virtual void ToggleSiteBlacklist();
  virtual bool ShouldAlwaysTranslate();
  virtual void ToggleAlwaysTranslate();

  int original_lang_index() const {
    return original_lang_index_;
  }
  int target_lang_index() const {
    return target_lang_index_;
  }
  const std::string& original_lang_code() const {
    return supported_languages_[original_lang_index_];
  }
  const std::string& target_lang_code() const {
    return supported_languages_[target_lang_index_];
  }
  const std::string& GetLocaleFromIndex(int lang_index) const {
    return supported_languages_[lang_index];
  }
  TabContents* tab_contents() const {
    return tab_contents_;
  }
  TranslateState state() const {
    return state_;
  }

  // Retrieve the text for the toolbar label.  The toolbar label is a bit
  // strange since we need to place popup menus inside the string in question.
  // To do this we use two placeholders.
  //
  // |message_text| is the text to display for the label.
  // |offsets| contains the offsets of the number of placeholders in the text
  // + message_text->length() i.e. it can contain 2 or 3 elements.
  // offsets[0] < offsets[1] even in cases where the languages need to be
  // displayed in reverse order.
  // |swapped_language_placeholders| is true if we need to flip the order
  // of the menus in the current locale.
  void GetMessageText(string16 *message_text,
                      std::vector<size_t> *offsets,
                      bool *swapped_language_placeholders);

  // Overridden from InfoBarDelegate.
  virtual Type GetInfoBarType() {
    return PAGE_ACTION_TYPE;
  }
  virtual SkBitmap* GetIcon() const;
  virtual TranslateInfoBarDelegate* AsTranslateInfoBarDelegate() {
    return this;
  }
  virtual bool EqualsDelegate(InfoBarDelegate* delegate) const;
  virtual void InfoBarDismissed();
  virtual void InfoBarClosed();

  // Returns the printable version of the language code |language_code|.
  virtual string16 GetDisplayNameForLocale(const std::string& language_code);

  // Overridden from InfoBarDelegate:
  virtual InfoBar* CreateInfoBar();

 protected:
  // For testing.
  TranslateInfoBarDelegate() : InfoBarDelegate(NULL) {}

 private:
  TranslateInfoBarDelegate(TabContents* contents,
                           PrefService* user_prefs,
                           TranslateState state,
                           const GURL& url,
                           int original_language_index,
                           int target_language_index);

  TabContents* tab_contents_;  // Weak.
  scoped_ptr<TranslatePrefs> prefs_;
  TranslateState state_;
  std::string site_;
  int original_lang_index_;
  int target_lang_index_;
  // The list of languages supported.
  std::vector<std::string> supported_languages_;
  bool never_translate_language_;
  bool never_translate_site_;
  bool always_translate_;

  DISALLOW_COPY_AND_ASSIGN(TranslateInfoBarDelegate);
};

#endif  // CHROME_BROWSER_TRANSLATE_TRANSLATE_INFOBARS_DELEGATES_H_