OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_TRANSLATE_TRANSLATE_UI_DELEGATE_H_ | 5 #ifndef CHROME_BROWSER_TRANSLATE_TRANSLATE_UI_DELEGATE_H_ |
6 #define CHROME_BROWSER_TRANSLATE_TRANSLATE_UI_DELEGATE_H_ | 6 #define CHROME_BROWSER_TRANSLATE_TRANSLATE_UI_DELEGATE_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
13 #include "base/strings/string16.h" | 13 #include "base/strings/string16.h" |
14 #include "chrome/common/chrome_constants.h" | 14 #include "chrome/common/chrome_constants.h" |
15 #include "chrome/common/translate/translate_errors.h" | 15 #include "chrome/common/translate/translate_errors.h" |
16 | 16 |
17 class TranslatePrefs; | 17 class TranslatePrefs; |
18 | 18 |
19 namespace content { | 19 namespace content { |
20 class WebContents; | 20 class WebContents; |
21 } // namespace content | 21 } // namespace content |
22 | 22 |
23 // The TranslateUIDelegate is a generic delegate for UI which offers Translate | 23 // The TranslateUIDelegate is a generic delegate for UI which offers Translate |
24 // feature to the user. | 24 // feature to the user. |
25 // | |
26 // Note: TranslateInfobarDelegate should be updated if this implementation is | |
27 // updated. | |
28 class TranslateUIDelegate { | 25 class TranslateUIDelegate { |
29 public: | 26 public: |
30 enum { | 27 enum { |
31 NO_INDEX = -1, | 28 NO_INDEX = -1, |
32 }; | 29 }; |
33 | 30 |
34 // A pair whose key is a language code and value is the language's | |
35 // displayable name. | |
36 typedef std::pair<std::string, string16> LanguageNamePair; | |
37 | |
38 TranslateUIDelegate(content::WebContents* web_contents, | 31 TranslateUIDelegate(content::WebContents* web_contents, |
39 const std::string& original_language, | 32 const std::string& original_language, |
40 const std::string& target_language, | 33 const std::string& target_language, |
41 TranslateErrors::Type error_type); | 34 TranslateErrors::Type error_type); |
42 virtual ~TranslateUIDelegate(); | 35 virtual ~TranslateUIDelegate(); |
43 | 36 |
44 // Retrieves the displayable names of the supported languages, based on | |
45 // |locale|. The returned names are sorted in alphabetical order. | |
46 static std::vector<LanguageNamePair> GetSortedLanguageNames( | |
47 const std::string& locale); | |
48 | |
49 // Gets the host of the page of |web_contents|, or an empty string if no URL | |
50 // is associated with the current page. | |
51 static std::string GetPageHost(content::WebContents* web_contents); | |
52 | |
53 content::WebContents* web_contents() { return web_contents_; } | 37 content::WebContents* web_contents() { return web_contents_; } |
54 | 38 |
55 TranslateErrors::Type error_type() const { return error_type_; } | 39 TranslateErrors::Type error_type() const { return error_type_; } |
56 | 40 |
57 void set_error_type(TranslateErrors::Type error_type) { | 41 void set_error_type(TranslateErrors::Type error_type) { |
58 error_type_ = error_type; | 42 error_type_ = error_type; |
59 } | 43 } |
60 | 44 |
61 // Returns the number of languages supported. | 45 // Returns the number of languages supported. |
62 size_t GetNumberOfLanguages() const; | 46 size_t GetNumberOfLanguages() const; |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
108 | 92 |
109 // Returns true if the webpage in the current original language should be | 93 // Returns true if the webpage in the current original language should be |
110 // translated into the current target language automatically. | 94 // translated into the current target language automatically. |
111 bool ShouldAlwaysTranslate(); | 95 bool ShouldAlwaysTranslate(); |
112 | 96 |
113 // Sets the value if the webpage in the current original language should be | 97 // Sets the value if the webpage in the current original language should be |
114 // translated into the current target language automatically. | 98 // translated into the current target language automatically. |
115 void SetAlwaysTranslate(bool value); | 99 void SetAlwaysTranslate(bool value); |
116 | 100 |
117 private: | 101 private: |
| 102 // Gets the host of the page being translated, or an empty string if no URL is |
| 103 // associated with the current page. |
| 104 std::string GetPageHost(); |
| 105 |
118 content::WebContents* web_contents_; | 106 content::WebContents* web_contents_; |
119 | 107 |
| 108 typedef std::pair<std::string, string16> LanguageNamePair; |
| 109 |
120 // The list supported languages for translation. | 110 // The list supported languages for translation. |
121 // The pair first string is the language ISO code (ex: en, fr...), the second | 111 // The pair first string is the language ISO code (ex: en, fr...), the second |
122 // string is the displayable name on the current locale. | 112 // string is the displayable name on the current locale. |
123 // The languages are sorted alphabetically based on the displayable name. | 113 // The languages are sorted alphabetically based on the displayable name. |
124 std::vector<LanguageNamePair> languages_; | 114 std::vector<LanguageNamePair> languages_; |
125 | 115 |
126 // The index for language the page is originally in. | 116 // The index for language the page is originally in. |
127 size_t original_language_index_; | 117 size_t original_language_index_; |
128 | 118 |
| 119 // The index for language the page is originally in that was originally |
| 120 // reported (original_language_index_ changes if the user selects a new |
| 121 // original language, but this one does not). This is necessary to report |
| 122 // language detection errors with the right original language even if the user |
| 123 // changed the original language. |
| 124 size_t initial_original_language_index_; |
| 125 |
129 // The index for language the page should be translated to. | 126 // The index for language the page should be translated to. |
130 size_t target_language_index_; | 127 size_t target_language_index_; |
131 | 128 |
132 // The translation related preferences. | 129 // The translation related preferences. |
133 scoped_ptr<TranslatePrefs> prefs_; | 130 scoped_ptr<TranslatePrefs> prefs_; |
134 | 131 |
135 // The error type. | 132 // The error type. |
136 TranslateErrors::Type error_type_; | 133 TranslateErrors::Type error_type_; |
137 | 134 |
138 DISALLOW_COPY_AND_ASSIGN(TranslateUIDelegate); | 135 DISALLOW_COPY_AND_ASSIGN(TranslateUIDelegate); |
139 }; | 136 }; |
140 | 137 |
141 #endif // CHROME_BROWSER_TRANSLATE_TRANSLATE_UI_DELEGATE_H_ | 138 #endif // CHROME_BROWSER_TRANSLATE_TRANSLATE_UI_DELEGATE_H_ |
OLD | NEW |