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" | |
|
Lei Zhang
2016/11/17 07:03:28
Not needed, but I imagine it's here because you wa
| |
| 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 extern const base::Feature kTranslateLanguageByULP; | |
| 27 | |
| 24 class TranslateClient; | 28 class TranslateClient; |
| 25 class TranslateDriver; | 29 class TranslateDriver; |
| 26 class TranslatePrefs; | 30 class TranslatePrefs; |
| 31 | |
| 32 namespace testing { | |
| 33 class TranslateManagerTest; | |
| 34 } // namespace testing | |
| 35 | |
| 27 struct TranslateErrorDetails; | 36 struct TranslateErrorDetails; |
| 28 | 37 |
| 29 // The TranslateManager class is responsible for showing an info-bar when a page | 38 // 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 | 39 // in a language different than the user language is loaded. It triggers the |
| 31 // page translation the user requests. | 40 // page translation the user requests. |
| 32 | 41 |
| 33 class TranslateManager { | 42 class TranslateManager { |
| 34 public: | 43 public: |
| 35 // |translate_client| is expected to outlive the TranslateManager. | 44 // |translate_client| is expected to outlive the TranslateManager. |
| 36 // |accept_language_pref_name| is the path for the preference for the | 45 // |accept_language_pref_name| is the path for the preference for the |
| 37 // accept-languages. | 46 // accept-languages. |
| 38 TranslateManager(TranslateClient* translate_client, | 47 TranslateManager(TranslateClient* translate_client, |
| 39 const std::string& accept_language_pref_name); | 48 const std::string& accept_language_pref_name); |
| 40 virtual ~TranslateManager(); | 49 virtual ~TranslateManager(); |
| 41 | 50 |
| 42 // Returns a weak pointer to this instance. | 51 // Returns a weak pointer to this instance. |
| 43 base::WeakPtr<TranslateManager> GetWeakPtr(); | 52 base::WeakPtr<TranslateManager> GetWeakPtr(); |
| 44 | 53 |
| 45 // Cannot return NULL. | 54 // Cannot return NULL. |
| 46 TranslateClient* translate_client() { return translate_client_; } | 55 TranslateClient* translate_client() { return translate_client_; } |
| 47 | 56 |
| 48 // Sets the sequence number of the current page, for use while sending | 57 // Sets the sequence number of the current page, for use while sending |
| 49 // messages to the renderer. | 58 // messages to the renderer. |
| 50 void set_current_seq_no(int page_seq_no) { page_seq_no_ = page_seq_no; } | 59 void set_current_seq_no(int page_seq_no) { page_seq_no_ = page_seq_no; } |
| 51 | 60 |
| 52 // Returns the language to translate to. The language returned is the | 61 // Returns the language to translate to. The language returned is the |
| 53 // first language found in the following list that is supported by the | 62 // first language found in the following list that is supported by the |
| 54 // translation service: | 63 // translation service: |
| 64 // High confidence and high probability reading language in ULP | |
| 55 // the UI language | 65 // the UI language |
| 56 // the accept-language list | 66 // the accept-language list |
| 57 // If no language is found then an empty string is returned. | 67 // If no language is found then an empty string is returned. |
| 58 static std::string GetTargetLanguage(const TranslatePrefs* prefs); | 68 static std::string GetTargetLanguage(const TranslatePrefs* prefs); |
| 59 | 69 |
| 60 // Returns the language to automatically translate to. |original_language| is | 70 // Returns the language to automatically translate to. |original_language| is |
| 61 // the webpage's original language. | 71 // the webpage's original language. |
| 62 static std::string GetAutoTargetLanguage(const std::string& original_language, | 72 static std::string GetAutoTargetLanguage(const std::string& original_language, |
| 63 TranslatePrefs* translate_prefs); | 73 TranslatePrefs* translate_prefs); |
| 64 | 74 |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 96 RegisterTranslateErrorCallback(const TranslateErrorCallback& callback); | 106 RegisterTranslateErrorCallback(const TranslateErrorCallback& callback); |
| 97 | 107 |
| 98 // Gets the LanguageState associated with the TranslateManager | 108 // Gets the LanguageState associated with the TranslateManager |
| 99 LanguageState& GetLanguageState(); | 109 LanguageState& GetLanguageState(); |
| 100 | 110 |
| 101 // By default, don't offer to translate in builds lacking an API key. For | 111 // By default, don't offer to translate in builds lacking an API key. For |
| 102 // testing, set to true to offer anyway. | 112 // testing, set to true to offer anyway. |
| 103 static void SetIgnoreMissingKeyForTesting(bool ignore); | 113 static void SetIgnoreMissingKeyForTesting(bool ignore); |
| 104 | 114 |
| 105 private: | 115 private: |
| 116 friend class translate::testing::TranslateManagerTest; | |
| 117 | |
| 106 // Sends a translation request to the TranslateDriver. | 118 // Sends a translation request to the TranslateDriver. |
| 107 void DoTranslatePage(const std::string& translate_script, | 119 void DoTranslatePage(const std::string& translate_script, |
| 108 const std::string& source_lang, | 120 const std::string& source_lang, |
| 109 const std::string& target_lang); | 121 const std::string& target_lang); |
| 110 | 122 |
| 123 // Returns the language to translate to by looking at ULP. Return empty string | |
| 124 // If it cannot conclude from ULP. | |
| 125 static std::string GetTargetLanguageFromULP(const TranslatePrefs* prefs); | |
| 126 | |
| 127 // Return true if the language is in the ULP with high confidence and high | |
| 128 // probability. | |
| 129 bool LanguageInULP(const std::string& language) const; | |
| 130 | |
| 111 // Notifies all registered callbacks of translate errors. | 131 // Notifies all registered callbacks of translate errors. |
| 112 void NotifyTranslateError(TranslateErrors::Type error_type); | 132 void NotifyTranslateError(TranslateErrors::Type error_type); |
| 113 | 133 |
| 114 // Called when the Translate script has been fetched. | 134 // Called when the Translate script has been fetched. |
| 115 // Initiates the translation. | 135 // Initiates the translation. |
| 116 void OnTranslateScriptFetchComplete(const std::string& source_lang, | 136 void OnTranslateScriptFetchComplete(const std::string& source_lang, |
| 117 const std::string& target_lang, | 137 const std::string& target_lang, |
| 118 bool success, | 138 bool success, |
| 119 const std::string& data); | 139 const std::string& data); |
| 120 | 140 |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 134 // By default, don't offer to translate in builds lacking an API key. For | 154 // By default, don't offer to translate in builds lacking an API key. For |
| 135 // testing, set to true to offer anyway. | 155 // testing, set to true to offer anyway. |
| 136 static bool ignore_missing_key_for_testing_; | 156 static bool ignore_missing_key_for_testing_; |
| 137 | 157 |
| 138 DISALLOW_COPY_AND_ASSIGN(TranslateManager); | 158 DISALLOW_COPY_AND_ASSIGN(TranslateManager); |
| 139 }; | 159 }; |
| 140 | 160 |
| 141 } // namespace translate | 161 } // namespace translate |
| 142 | 162 |
| 143 #endif // COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_MANAGER_H_ | 163 #endif // COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_MANAGER_H_ |
| OLD | NEW |