| 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" | 14 #include "base/feature_list.h" |
| 15 #include "base/gtest_prod_util.h" | 15 #include "base/gtest_prod_util.h" |
| 16 #include "base/macros.h" | 16 #include "base/macros.h" |
| 17 #include "base/memory/weak_ptr.h" | 17 #include "base/memory/weak_ptr.h" |
| 18 #include "components/metrics/proto/translate_event.pb.h" |
| 18 #include "components/translate/core/browser/language_state.h" | 19 #include "components/translate/core/browser/language_state.h" |
| 19 #include "components/translate/core/common/translate_errors.h" | 20 #include "components/translate/core/common/translate_errors.h" |
| 20 | 21 |
| 21 class GURL; | 22 class GURL; |
| 22 class PrefService; | 23 class PrefService; |
| 23 | 24 |
| 24 namespace translate { | 25 namespace translate { |
| 25 | 26 |
| 26 extern const base::Feature kTranslateLanguageByULP; | 27 extern const base::Feature kTranslateLanguageByULP; |
| 27 | 28 |
| (...skipping 23 matching lines...) Expand all Loading... |
| 51 // Returns a weak pointer to this instance. | 52 // Returns a weak pointer to this instance. |
| 52 base::WeakPtr<TranslateManager> GetWeakPtr(); | 53 base::WeakPtr<TranslateManager> GetWeakPtr(); |
| 53 | 54 |
| 54 // Cannot return NULL. | 55 // Cannot return NULL. |
| 55 TranslateClient* translate_client() { return translate_client_; } | 56 TranslateClient* translate_client() { return translate_client_; } |
| 56 | 57 |
| 57 // Sets the sequence number of the current page, for use while sending | 58 // Sets the sequence number of the current page, for use while sending |
| 58 // messages to the renderer. | 59 // messages to the renderer. |
| 59 void set_current_seq_no(int page_seq_no) { page_seq_no_ = page_seq_no; } | 60 void set_current_seq_no(int page_seq_no) { page_seq_no_ = page_seq_no; } |
| 60 | 61 |
| 62 metrics::TranslateEventProto* mutable_translate_event() { |
| 63 return &translate_event_; |
| 64 } |
| 65 |
| 61 // Returns the language to translate to. The language returned is the | 66 // Returns the language to translate to. The language returned is the |
| 62 // first language found in the following list that is supported by the | 67 // first language found in the following list that is supported by the |
| 63 // translation service: | 68 // translation service: |
| 64 // High confidence and high probability reading language in ULP | 69 // High confidence and high probability reading language in ULP |
| 65 // the UI language | 70 // the UI language |
| 66 // the accept-language list | 71 // the accept-language list |
| 67 // If no language is found then an empty string is returned. | 72 // If no language is found then an empty string is returned. |
| 68 static std::string GetTargetLanguage(const TranslatePrefs* prefs); | 73 static std::string GetTargetLanguage(const TranslatePrefs* prefs); |
| 69 | 74 |
| 70 // Returns the language to automatically translate to. |original_language| is | 75 // Returns the language to automatically translate to. |original_language| is |
| (...skipping 30 matching lines...) Expand all Loading... |
| 101 typedef base::CallbackList<void(const TranslateErrorDetails&)> | 106 typedef base::CallbackList<void(const TranslateErrorDetails&)> |
| 102 TranslateErrorCallbackList; | 107 TranslateErrorCallbackList; |
| 103 | 108 |
| 104 // Registers a callback for translate errors. | 109 // Registers a callback for translate errors. |
| 105 static std::unique_ptr<TranslateErrorCallbackList::Subscription> | 110 static std::unique_ptr<TranslateErrorCallbackList::Subscription> |
| 106 RegisterTranslateErrorCallback(const TranslateErrorCallback& callback); | 111 RegisterTranslateErrorCallback(const TranslateErrorCallback& callback); |
| 107 | 112 |
| 108 // Gets the LanguageState associated with the TranslateManager | 113 // Gets the LanguageState associated with the TranslateManager |
| 109 LanguageState& GetLanguageState(); | 114 LanguageState& GetLanguageState(); |
| 110 | 115 |
| 116 // Record an event of the given |event_type| using the currently saved |
| 117 // |translate_event_| as context. |
| 118 void RecordTranslateEvent(metrics::TranslateEventProto::EventType event_type); |
| 119 |
| 111 // By default, don't offer to translate in builds lacking an API key. For | 120 // By default, don't offer to translate in builds lacking an API key. For |
| 112 // testing, set to true to offer anyway. | 121 // testing, set to true to offer anyway. |
| 113 static void SetIgnoreMissingKeyForTesting(bool ignore); | 122 static void SetIgnoreMissingKeyForTesting(bool ignore); |
| 114 | 123 |
| 115 private: | 124 private: |
| 116 friend class translate::testing::TranslateManagerTest; | 125 friend class translate::testing::TranslateManagerTest; |
| 117 | 126 |
| 118 // Sends a translation request to the TranslateDriver. | 127 // Sends a translation request to the TranslateDriver. |
| 119 void DoTranslatePage(const std::string& translate_script, | 128 void DoTranslatePage(const std::string& translate_script, |
| 120 const std::string& source_lang, | 129 const std::string& source_lang, |
| (...skipping 10 matching lines...) Expand all Loading... |
| 131 // Notifies all registered callbacks of translate errors. | 140 // Notifies all registered callbacks of translate errors. |
| 132 void NotifyTranslateError(TranslateErrors::Type error_type); | 141 void NotifyTranslateError(TranslateErrors::Type error_type); |
| 133 | 142 |
| 134 // Called when the Translate script has been fetched. | 143 // Called when the Translate script has been fetched. |
| 135 // Initiates the translation. | 144 // Initiates the translation. |
| 136 void OnTranslateScriptFetchComplete(const std::string& source_lang, | 145 void OnTranslateScriptFetchComplete(const std::string& source_lang, |
| 137 const std::string& target_lang, | 146 const std::string& target_lang, |
| 138 bool success, | 147 bool success, |
| 139 const std::string& data); | 148 const std::string& data); |
| 140 | 149 |
| 150 // Helper function to initialize a translate event metric proto. |
| 151 void InitTranslateEvent(const std::string& src_lang, |
| 152 const std::string& dst_lang, |
| 153 const TranslatePrefs& translate_prefs); |
| 154 |
| 141 // Sequence number of the current page. | 155 // Sequence number of the current page. |
| 142 int page_seq_no_; | 156 int page_seq_no_; |
| 143 | 157 |
| 144 // Preference name for the Accept-Languages HTTP header. | 158 // Preference name for the Accept-Languages HTTP header. |
| 145 std::string accept_languages_pref_name_; | 159 std::string accept_languages_pref_name_; |
| 146 | 160 |
| 147 TranslateClient* translate_client_; // Weak. | 161 TranslateClient* translate_client_; // Weak. |
| 148 TranslateDriver* translate_driver_; // Weak. | 162 TranslateDriver* translate_driver_; // Weak. |
| 149 | 163 |
| 150 LanguageState language_state_; | 164 LanguageState language_state_; |
| 151 | 165 |
| 166 metrics::TranslateEventProto translate_event_; |
| 167 |
| 152 base::WeakPtrFactory<TranslateManager> weak_method_factory_; | 168 base::WeakPtrFactory<TranslateManager> weak_method_factory_; |
| 153 | 169 |
| 154 // By default, don't offer to translate in builds lacking an API key. For | 170 // By default, don't offer to translate in builds lacking an API key. For |
| 155 // testing, set to true to offer anyway. | 171 // testing, set to true to offer anyway. |
| 156 static bool ignore_missing_key_for_testing_; | 172 static bool ignore_missing_key_for_testing_; |
| 157 | 173 |
| 158 DISALLOW_COPY_AND_ASSIGN(TranslateManager); | 174 DISALLOW_COPY_AND_ASSIGN(TranslateManager); |
| 159 }; | 175 }; |
| 160 | 176 |
| 161 } // namespace translate | 177 } // namespace translate |
| 162 | 178 |
| 163 #endif // COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_MANAGER_H_ | 179 #endif // COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_MANAGER_H_ |
| OLD | NEW |