OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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_RENDERER_TRANSLATE_TRANSLATE_HELPER_H_ | 5 #ifndef CHROME_RENDERER_TRANSLATE_TRANSLATE_HELPER_H_ |
6 #define CHROME_RENDERER_TRANSLATE_TRANSLATE_HELPER_H_ | 6 #define CHROME_RENDERER_TRANSLATE_TRANSLATE_HELPER_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/gtest_prod_util.h" | 10 #include "base/gtest_prod_util.h" |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
80 // run successfully. Otherwise, returns empty string. | 80 // run successfully. Otherwise, returns empty string. |
81 virtual std::string ExecuteScriptAndGetStringResult( | 81 virtual std::string ExecuteScriptAndGetStringResult( |
82 const std::string& script); | 82 const std::string& script); |
83 | 83 |
84 // Executes the JavaScript code in |script| in the main frame of RenderView. | 84 // Executes the JavaScript code in |script| in the main frame of RenderView. |
85 // and returns the number returned by the script evaluation if the script was | 85 // and returns the number returned by the script evaluation if the script was |
86 // run successfully. Otherwise, returns 0.0. | 86 // run successfully. Otherwise, returns 0.0. |
87 virtual double ExecuteScriptAndGetDoubleResult(const std::string& script); | 87 virtual double ExecuteScriptAndGetDoubleResult(const std::string& script); |
88 | 88 |
89 private: | 89 private: |
90 FRIEND_TEST_ALL_PREFIXES(TranslateHelperTest, IsValidLanguageCode); | |
91 FRIEND_TEST_ALL_PREFIXES(TranslateHelperTest, AdoptHtmlLang); | 90 FRIEND_TEST_ALL_PREFIXES(TranslateHelperTest, AdoptHtmlLang); |
92 FRIEND_TEST_ALL_PREFIXES(TranslateHelperTest, | 91 FRIEND_TEST_ALL_PREFIXES(TranslateHelperTest, |
93 CLDAgreeWithLanguageCodeHavingCountryCode); | 92 CLDAgreeWithLanguageCodeHavingCountryCode); |
94 FRIEND_TEST_ALL_PREFIXES(TranslateHelperTest, | 93 FRIEND_TEST_ALL_PREFIXES(TranslateHelperTest, |
95 CLDDisagreeWithWrongLanguageCode); | 94 CLDDisagreeWithWrongLanguageCode); |
96 FRIEND_TEST_ALL_PREFIXES(TranslateHelperTest, | 95 FRIEND_TEST_ALL_PREFIXES(TranslateHelperTest, |
97 InvalidLanguageMetaTagProviding); | 96 InvalidLanguageMetaTagProviding); |
98 FRIEND_TEST_ALL_PREFIXES(TranslateHelperTest, LanguageCodeTypoCorrection); | 97 FRIEND_TEST_ALL_PREFIXES(TranslateHelperTest, LanguageCodeTypoCorrection); |
99 FRIEND_TEST_ALL_PREFIXES(TranslateHelperTest, LanguageCodeSynonyms); | 98 FRIEND_TEST_ALL_PREFIXES(TranslateHelperTest, LanguageCodeSynonyms); |
100 FRIEND_TEST_ALL_PREFIXES(TranslateHelperTest, ResetInvalidLanguageCode); | 99 FRIEND_TEST_ALL_PREFIXES(TranslateHelperTest, ResetInvalidLanguageCode); |
101 FRIEND_TEST_ALL_PREFIXES(TranslateHelperTest, SimilarLanguageCode); | 100 FRIEND_TEST_ALL_PREFIXES(TranslateHelperTest, SimilarLanguageCode); |
102 FRIEND_TEST_ALL_PREFIXES(TranslateHelperTest, WellKnownWrongConfiguration); | 101 FRIEND_TEST_ALL_PREFIXES(TranslateHelperTest, WellKnownWrongConfiguration); |
103 | 102 |
104 // Corrects language code if it contains well-known mistakes. | |
105 static void CorrectLanguageCodeTypo(std::string* code); | |
106 | |
107 // Converts language code to the one used in server supporting list. | 103 // Converts language code to the one used in server supporting list. |
108 static void ConvertLanguageCodeSynonym(std::string* code); | 104 static void ConvertLanguageCodeSynonym(std::string* code); |
109 | 105 |
110 // Checks if the language code's format is valid. | |
111 static bool IsValidLanguageCode(const std::string& code); | |
112 | |
113 // Applies a series of language code modification in proper order. | |
114 static void ApplyLanguageCodeCorrection(std::string* code); | |
115 | |
116 // Checks if languages are matched, or similar. This function returns true | |
117 // against a language pair containing a language which is difficult for CLD | |
118 // to distinguish. | |
119 static bool IsSameOrSimilarLanguages(const std::string& page_language, | |
120 const std::string& cld_language); | |
121 | |
122 // Checks if languages pair is one of well-known pairs of wrong server | |
123 // configuration. | |
124 static bool MaybeServerWrongConfiguration(const std::string& page_language, | |
125 const std::string& cld_language); | |
126 | |
127 // Checks if CLD can complement a sub code when the page language doesn't | |
128 // know the sub code. | |
129 static bool CanCLDComplementSubCode(const std::string& page_language, | |
130 const std::string& cld_language); | |
131 | |
132 // Determines content page language from Content-Language code and contents. | |
133 static std::string DeterminePageLanguage(const std::string& code, | |
134 const std::string& html_lang, | |
135 const string16& contents, | |
136 std::string* cld_language, | |
137 bool* is_cld_reliable); | |
138 | |
139 // Returns whether the page associated with |document| is a candidate for | 106 // Returns whether the page associated with |document| is a candidate for |
140 // translation. Some pages can explictly specify (via a meta-tag) that they | 107 // translation. Some pages can explictly specify (via a meta-tag) that they |
141 // should not be translated. | 108 // should not be translated. |
142 static bool IsTranslationAllowed(WebKit::WebDocument* document); | 109 static bool IsTranslationAllowed(WebKit::WebDocument* document); |
143 | 110 |
144 #if defined(ENABLE_LANGUAGE_DETECTION) | |
145 // Returns the ISO 639_1 language code of the specified |text|, or 'unknown' | |
146 // if it failed. | |
147 // |is_cld_reliable| will be set as true if CLD says the detection is | |
148 // reliable. | |
149 static std::string DetermineTextLanguage(const string16& text, | |
150 bool* is_cld_reliable); | |
151 #endif | |
152 | |
153 // RenderViewObserver implementation. | 111 // RenderViewObserver implementation. |
154 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; | 112 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE; |
155 | 113 |
156 // Cancels any translation that is currently being performed. This does not | 114 // Cancels any translation that is currently being performed. This does not |
157 // revert existing translations. | 115 // revert existing translations. |
158 void CancelPendingTranslation(); | 116 void CancelPendingTranslation(); |
159 | 117 |
160 // Checks if the current running page translation is finished or errored and | 118 // Checks if the current running page translation is finished or errored and |
161 // notifies the browser accordingly. If the translation has not terminated, | 119 // notifies the browser accordingly. If the translation has not terminated, |
162 // posts a task to check again later. | 120 // posts a task to check again later. |
(...skipping 24 matching lines...) Expand all Loading... |
187 // time from showing infobar to requesting translation. | 145 // time from showing infobar to requesting translation. |
188 base::TimeTicks language_determined_time_; | 146 base::TimeTicks language_determined_time_; |
189 | 147 |
190 // Method factory used to make calls to TranslatePageImpl. | 148 // Method factory used to make calls to TranslatePageImpl. |
191 base::WeakPtrFactory<TranslateHelper> weak_method_factory_; | 149 base::WeakPtrFactory<TranslateHelper> weak_method_factory_; |
192 | 150 |
193 DISALLOW_COPY_AND_ASSIGN(TranslateHelper); | 151 DISALLOW_COPY_AND_ASSIGN(TranslateHelper); |
194 }; | 152 }; |
195 | 153 |
196 #endif // CHROME_RENDERER_TRANSLATE_TRANSLATE_HELPER_H_ | 154 #endif // CHROME_RENDERER_TRANSLATE_TRANSLATE_HELPER_H_ |
OLD | NEW |