| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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_RANKER_IMPL_H_ | 5 #ifndef COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_RANKER_IMPL_H_ |
| 6 #define COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_RANKER_IMPL_H_ | 6 #define COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_RANKER_IMPL_H_ |
| 7 | 7 |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| (...skipping 23 matching lines...) Expand all Loading... |
| 34 | 34 |
| 35 namespace translate { | 35 namespace translate { |
| 36 | 36 |
| 37 class TranslatePrefs; | 37 class TranslatePrefs; |
| 38 | 38 |
| 39 // Features used to enable ranker query, enforcement and logging. Note that | 39 // Features used to enable ranker query, enforcement and logging. Note that |
| 40 // enabling enforcement implies (forces) enabling queries. | 40 // enabling enforcement implies (forces) enabling queries. |
| 41 extern const base::Feature kTranslateRankerQuery; | 41 extern const base::Feature kTranslateRankerQuery; |
| 42 extern const base::Feature kTranslateRankerEnforcement; | 42 extern const base::Feature kTranslateRankerEnforcement; |
| 43 extern const base::Feature kTranslateRankerLogging; | 43 extern const base::Feature kTranslateRankerLogging; |
| 44 extern const base::Feature kTranslateRankerDecisionOverride; |
| 44 | 45 |
| 45 struct TranslateRankerFeatures { | 46 struct TranslateRankerFeatures { |
| 46 TranslateRankerFeatures(); | 47 TranslateRankerFeatures(); |
| 47 | 48 |
| 48 TranslateRankerFeatures(int accepted, | 49 TranslateRankerFeatures(int accepted, |
| 49 int denied, | 50 int denied, |
| 50 int ignored, | 51 int ignored, |
| 51 const std::string& src, | 52 const std::string& src, |
| 52 const std::string& dst, | 53 const std::string& dst, |
| 53 const std::string& cntry, | 54 const std::string& cntry, |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 92 static base::FilePath GetModelPath(const base::FilePath& data_dir); | 93 static base::FilePath GetModelPath(const base::FilePath& data_dir); |
| 93 | 94 |
| 94 // Get the URL from which the download the translate ranker model, by default | 95 // Get the URL from which the download the translate ranker model, by default |
| 95 // from Finch. | 96 // from Finch. |
| 96 static GURL GetModelURL(); | 97 static GURL GetModelURL(); |
| 97 | 98 |
| 98 // Over-ride the default enabled/disabled state of translate event logging. | 99 // Over-ride the default enabled/disabled state of translate event logging. |
| 99 void EnableLogging(bool value); | 100 void EnableLogging(bool value); |
| 100 | 101 |
| 101 // TranslateRanker... | 102 // TranslateRanker... |
| 102 bool IsLoggingEnabled() override; | 103 uint32_t GetModelVersion() const override; |
| 103 bool IsQueryEnabled() override; | 104 bool ShouldOfferTranslation( |
| 104 bool IsEnforcementEnabled() override; | 105 const TranslatePrefs& translate_prefs, |
| 105 int GetModelVersion() const override; | 106 const std::string& src_lang, |
| 106 bool ShouldOfferTranslation(const TranslatePrefs& translate_prefs, | 107 const std::string& dst_lang, |
| 107 const std::string& src_lang, | 108 metrics::TranslateEventProto* translate_event) override; |
| 108 const std::string& dst_lang) override; | |
| 109 void AddTranslateEvent(const metrics::TranslateEventProto& translate_event, | |
| 110 const GURL& url) override; | |
| 111 void FlushTranslateEvents( | 109 void FlushTranslateEvents( |
| 112 std::vector<metrics::TranslateEventProto>* events) override; | 110 std::vector<metrics::TranslateEventProto>* events) override; |
| 111 void RecordTranslateEvent( |
| 112 int event_type, |
| 113 const GURL& url, |
| 114 metrics::TranslateEventProto* translate_event) override; |
| 115 bool ShouldOverrideDecision( |
| 116 int event_type, |
| 117 const GURL& url, |
| 118 metrics::TranslateEventProto* translate_event) override; |
| 113 | 119 |
| 114 void OnModelAvailable( | 120 void OnModelAvailable( |
| 115 std::unique_ptr<chrome_intelligence::RankerModel> model); | 121 std::unique_ptr<chrome_intelligence::RankerModel> model); |
| 116 | 122 |
| 117 // Calculate the score given to |features| by the |model|. | 123 // Calculate the score given to |features| by the |model|. |
| 118 double CalculateScore(const TranslateRankerFeatures& features); | 124 double CalculateScore(const TranslateRankerFeatures& features); |
| 119 | 125 |
| 120 // Check if the ModelLoader has been initialized. Used to test ModelLoader | 126 // Check if the ModelLoader has been initialized. Used to test ModelLoader |
| 121 // logic. | 127 // logic. |
| 122 bool CheckModelLoaderForTesting(); | 128 bool CheckModelLoaderForTesting(); |
| 123 | 129 |
| 124 private: | 130 private: |
| 125 void SendEventToUKM(const metrics::TranslateEventProto& translate_event, | 131 void SendEventToUKM(const metrics::TranslateEventProto& translate_event, |
| 126 const GURL& url); | 132 const GURL& url); |
| 127 | 133 |
| 134 // Caches the translate event. |
| 135 void AddTranslateEvent(const metrics::TranslateEventProto& translate_event, |
| 136 const GURL& url); |
| 137 |
| 128 // Used to log URL-keyed metrics. This pointer will outlive |this|. | 138 // Used to log URL-keyed metrics. This pointer will outlive |this|. |
| 129 ukm::UkmService* ukm_service_; | 139 ukm::UkmService* ukm_service_; |
| 130 | 140 |
| 131 // Used to sanity check the threading of this ranker. | 141 // Used to sanity check the threading of this ranker. |
| 132 base::SequenceChecker sequence_checker_; | 142 base::SequenceChecker sequence_checker_; |
| 133 | 143 |
| 134 // A helper to load the translate ranker model from disk cache or a URL. | 144 // A helper to load the translate ranker model from disk cache or a URL. |
| 135 std::unique_ptr<RankerModelLoader> model_loader_; | 145 std::unique_ptr<RankerModelLoader> model_loader_; |
| 136 | 146 |
| 137 // The translation ranker model. | 147 // The translation ranker model. |
| 138 std::unique_ptr<chrome_intelligence::RankerModel> model_; | 148 std::unique_ptr<chrome_intelligence::RankerModel> model_; |
| 139 | 149 |
| 140 // Tracks whether or not translate event logging is enabled. | 150 // Tracks whether or not translate event logging is enabled. |
| 141 bool is_logging_enabled_ = true; | 151 bool is_logging_enabled_ = true; |
| 142 | 152 |
| 143 // Tracks whether or not translate ranker querying is enabled. | 153 // Tracks whether or not translate ranker querying is enabled. |
| 144 bool is_query_enabled_ = true; | 154 bool is_query_enabled_ = true; |
| 145 | 155 |
| 146 // Tracks whether or not translate ranker enforcement is enabled. Note that | 156 // Tracks whether or not translate ranker enforcement is enabled. Note that |
| 147 // that also enables the code paths for translate ranker querying. | 157 // that also enables the code paths for translate ranker querying. |
| 148 bool is_enforcement_enabled_ = true; | 158 bool is_enforcement_enabled_ = true; |
| 149 | 159 |
| 160 // Tracks whether or not translate ranker decision override is enabled. This |
| 161 // will override suppression heuristics and follow ranker's decision. Note |
| 162 // that that also enables the code paths for translate ranker querying and |
| 163 // enforcement. |
| 164 bool is_decision_override_enabled_ = true; |
| 165 |
| 150 // Saved cache of translate event protos. | 166 // Saved cache of translate event protos. |
| 151 std::vector<metrics::TranslateEventProto> event_cache_; | 167 std::vector<metrics::TranslateEventProto> event_cache_; |
| 152 | 168 |
| 153 base::WeakPtrFactory<TranslateRankerImpl> weak_ptr_factory_; | 169 base::WeakPtrFactory<TranslateRankerImpl> weak_ptr_factory_; |
| 154 | 170 |
| 155 DISALLOW_COPY_AND_ASSIGN(TranslateRankerImpl); | 171 DISALLOW_COPY_AND_ASSIGN(TranslateRankerImpl); |
| 156 }; | 172 }; |
| 157 | 173 |
| 158 } // namespace translate | 174 } // namespace translate |
| 159 | 175 |
| 160 std::ostream& operator<<(std::ostream& stream, | 176 std::ostream& operator<<(std::ostream& stream, |
| 161 const translate::TranslateRankerFeatures& features); | 177 const translate::TranslateRankerFeatures& features); |
| 162 | 178 |
| 163 #endif // COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_RANKER_IMPL_H_ | 179 #endif // COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_RANKER_IMPL_H_ |
| OLD | NEW |