Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_MANAGER_H_ | 5 #ifndef COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_MANAGER_H_ |
| 6 #define COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_MANAGER_H_ | 6 #define COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_MANAGER_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <string> | 10 #include <string> |
| 11 #include <vector> | 11 #include <vector> |
| 12 | 12 |
| 13 #include "base/callback_list.h" | 13 #include "base/callback_list.h" |
| 14 #include "base/feature_list.h" | |
| 15 #include "base/gtest_prod_util.h" | |
| 14 #include "base/macros.h" | 16 #include "base/macros.h" |
| 15 #include "base/memory/weak_ptr.h" | 17 #include "base/memory/weak_ptr.h" |
| 16 #include "components/translate/core/browser/language_state.h" | 18 #include "components/translate/core/browser/language_state.h" |
| 17 #include "components/translate/core/common/translate_errors.h" | 19 #include "components/translate/core/common/translate_errors.h" |
| 18 | 20 |
| 19 class GURL; | 21 class GURL; |
| 20 class PrefService; | 22 class PrefService; |
| 21 | 23 |
| 22 namespace translate { | 24 namespace translate { |
| 23 | 25 |
| 26 // Feature flag for "Translate Language by ULP" project. | |
|
groby-ooo-7-16
2016/08/04 02:33:42
You can probably skip this comment - it says nothi
ftang
2016/08/05 04:45:27
Done.
| |
| 27 extern const base::Feature kTranslateLanguageByULP; | |
| 28 | |
| 24 class TranslateClient; | 29 class TranslateClient; |
| 25 class TranslateDriver; | 30 class TranslateDriver; |
| 26 class TranslatePrefs; | 31 class TranslatePrefs; |
| 27 struct TranslateErrorDetails; | 32 struct TranslateErrorDetails; |
| 28 | 33 |
| 29 // The TranslateManager class is responsible for showing an info-bar when a page | 34 // The TranslateManager class is responsible for showing an info-bar when a page |
| 30 // in a language different than the user language is loaded. It triggers the | 35 // in a language different than the user language is loaded. It triggers the |
| 31 // page translation the user requests. | 36 // page translation the user requests. |
| 32 | 37 |
| 33 class TranslateManager { | 38 class TranslateManager { |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 45 // Cannot return NULL. | 50 // Cannot return NULL. |
| 46 TranslateClient* translate_client() { return translate_client_; } | 51 TranslateClient* translate_client() { return translate_client_; } |
| 47 | 52 |
| 48 // Sets the sequence number of the current page, for use while sending | 53 // Sets the sequence number of the current page, for use while sending |
| 49 // messages to the renderer. | 54 // messages to the renderer. |
| 50 void set_current_seq_no(int page_seq_no) { page_seq_no_ = page_seq_no; } | 55 void set_current_seq_no(int page_seq_no) { page_seq_no_ = page_seq_no; } |
| 51 | 56 |
| 52 // Returns the language to translate to. The language returned is the | 57 // Returns the language to translate to. The language returned is the |
| 53 // first language found in the following list that is supported by the | 58 // first language found in the following list that is supported by the |
| 54 // translation service: | 59 // translation service: |
| 60 // High confidence and high probability reading language in ULP | |
| 55 // the UI language | 61 // the UI language |
| 56 // the accept-language list | 62 // the accept-language list |
| 57 // If no language is found then an empty string is returned. | 63 // If no language is found then an empty string is returned. |
| 58 static std::string GetTargetLanguage(const TranslatePrefs* prefs); | 64 static std::string GetTargetLanguage(const TranslatePrefs* prefs); |
| 59 | 65 |
| 60 // Returns the language to automatically translate to. |original_language| is | 66 // Returns the language to automatically translate to. |original_language| is |
| 61 // the webpage's original language. | 67 // the webpage's original language. |
| 62 static std::string GetAutoTargetLanguage(const std::string& original_language, | 68 static std::string GetAutoTargetLanguage(const std::string& original_language, |
| 63 TranslatePrefs* translate_prefs); | 69 TranslatePrefs* translate_prefs); |
| 64 | 70 |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 96 RegisterTranslateErrorCallback(const TranslateErrorCallback& callback); | 102 RegisterTranslateErrorCallback(const TranslateErrorCallback& callback); |
| 97 | 103 |
| 98 // Gets the LanguageState associated with the TranslateManager | 104 // Gets the LanguageState associated with the TranslateManager |
| 99 LanguageState& GetLanguageState(); | 105 LanguageState& GetLanguageState(); |
| 100 | 106 |
| 101 // By default, don't offer to translate in builds lacking an API key. For | 107 // By default, don't offer to translate in builds lacking an API key. For |
| 102 // testing, set to true to offer anyway. | 108 // testing, set to true to offer anyway. |
| 103 static void SetIgnoreMissingKeyForTesting(bool ignore); | 109 static void SetIgnoreMissingKeyForTesting(bool ignore); |
| 104 | 110 |
| 105 private: | 111 private: |
| 112 FRIEND_TEST_ALL_PREFIXES(TranslateManagerTest, TestGetTargetLanguageFromULP); | |
| 113 FRIEND_TEST_ALL_PREFIXES(TranslateManagerTest, TestLanguageInULP); | |
| 114 | |
| 106 // Sends a translation request to the TranslateDriver. | 115 // Sends a translation request to the TranslateDriver. |
| 107 void DoTranslatePage(const std::string& translate_script, | 116 void DoTranslatePage(const std::string& translate_script, |
| 108 const std::string& source_lang, | 117 const std::string& source_lang, |
| 109 const std::string& target_lang); | 118 const std::string& target_lang); |
| 110 | 119 |
| 120 // Returns the language to translate to by looking at ULP. Return empty string | |
| 121 // If it cannot conclude from ULP. | |
| 122 static std::string GetTargetLanguageFromULP(const TranslatePrefs* prefs); | |
| 123 | |
| 124 // Return true if the language is in the ULP with high confidence and high | |
| 125 // probability. | |
| 126 bool LanguageInULP(const TranslatePrefs* prefs, | |
| 127 const std::string& language) const; | |
| 128 | |
| 111 // Notifies all registered callbacks of translate errors. | 129 // Notifies all registered callbacks of translate errors. |
| 112 void NotifyTranslateError(TranslateErrors::Type error_type); | 130 void NotifyTranslateError(TranslateErrors::Type error_type); |
| 113 | 131 |
| 114 // Called when the Translate script has been fetched. | 132 // Called when the Translate script has been fetched. |
| 115 // Initiates the translation. | 133 // Initiates the translation. |
| 116 void OnTranslateScriptFetchComplete(const std::string& source_lang, | 134 void OnTranslateScriptFetchComplete(const std::string& source_lang, |
| 117 const std::string& target_lang, | 135 const std::string& target_lang, |
| 118 bool success, | 136 bool success, |
| 119 const std::string& data); | 137 const std::string& data); |
| 120 | 138 |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 134 // By default, don't offer to translate in builds lacking an API key. For | 152 // By default, don't offer to translate in builds lacking an API key. For |
| 135 // testing, set to true to offer anyway. | 153 // testing, set to true to offer anyway. |
| 136 static bool ignore_missing_key_for_testing_; | 154 static bool ignore_missing_key_for_testing_; |
| 137 | 155 |
| 138 DISALLOW_COPY_AND_ASSIGN(TranslateManager); | 156 DISALLOW_COPY_AND_ASSIGN(TranslateManager); |
| 139 }; | 157 }; |
| 140 | 158 |
| 141 } // namespace translate | 159 } // namespace translate |
| 142 | 160 |
| 143 #endif // COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_MANAGER_H_ | 161 #endif // COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_MANAGER_H_ |
| OLD | NEW |