| 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_H_ | 5 #ifndef COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_RANKER_H_ |
| 6 #define COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_RANKER_H_ | 6 #define COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_RANKER_H_ |
| 7 | 7 |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 #include "base/feature_list.h" | 11 #include "base/feature_list.h" |
| 12 #include "base/gtest_prod_util.h" | 12 #include "base/gtest_prod_util.h" |
| 13 #include "base/memory/singleton.h" | 13 #include "base/memory/singleton.h" |
| 14 | 14 |
| 15 namespace chrome_intelligence { | 15 namespace chrome_intelligence { |
| 16 class TranslateRankerModel; | 16 class TranslateRankerModel; |
| 17 } | 17 } |
| 18 | 18 |
| 19 namespace metrics { |
| 20 class TranslateEventProto; |
| 21 } |
| 22 |
| 19 namespace translate { | 23 namespace translate { |
| 20 | 24 |
| 21 class TranslatePrefs; | 25 class TranslatePrefs; |
| 22 class TranslateRankerMetricsProvider; | 26 class TranslateRankerMetricsProvider; |
| 23 class TranslateURLFetcher; | 27 class TranslateURLFetcher; |
| 24 | 28 |
| 25 // Features used to enable ranker query and enforcement. Note that enabling | 29 // Features used to enable ranker query, enforcement and logging. Note that |
| 26 // enforcement implies (forces) enabling queries. | 30 // enabling enforcement implies (forces) enabling queries. |
| 27 extern const base::Feature kTranslateRankerQuery; | 31 extern const base::Feature kTranslateRankerQuery; |
| 28 extern const base::Feature kTranslateRankerEnforcement; | 32 extern const base::Feature kTranslateRankerEnforcement; |
| 33 extern const base::Feature kTranslateRankerLogging; |
| 29 | 34 |
| 30 // If enabled, downloads a translate ranker model and uses it to determine | 35 // If enabled, downloads a translate ranker model and uses it to determine |
| 31 // whether the user should be given a translation prompt or not. | 36 // whether the user should be given a translation prompt or not. |
| 32 class TranslateRanker { | 37 class TranslateRanker { |
| 33 public: | 38 public: |
| 34 ~TranslateRanker(); | 39 ~TranslateRanker(); |
| 35 | 40 |
| 36 // Returns true if query or enforcement is enabled. | 41 // Returns true if query or enforcement is enabled. |
| 37 static bool IsEnabled(); | 42 static bool IsEnabled(); |
| 38 | 43 |
| 44 // Returns true if translate events logging is enabled. |
| 45 static bool IsLoggingEnabled(); |
| 46 |
| 39 // Returns the singleton TranslateRanker instance. | 47 // Returns the singleton TranslateRanker instance. |
| 40 static TranslateRanker* GetInstance(); | 48 static TranslateRanker* GetInstance(); |
| 41 | 49 |
| 42 // For testing only. Returns a TranslateRanker instance preloaded with a | 50 // For testing only. Returns a TranslateRanker instance preloaded with a |
| 43 // TranslateRankerModel as defined by |model_data|. | 51 // TranslateRankerModel as defined by |model_data|. |
| 44 static std::unique_ptr<TranslateRanker> CreateForTesting( | 52 static std::unique_ptr<TranslateRanker> CreateForTesting( |
| 45 const std::string& model_data); | 53 const std::string& model_data); |
| 46 | 54 |
| 47 // Initiates downloading of the assist model data. This is a NOP if the model | 55 // Initiates downloading of the assist model data. This is a NOP if the model |
| 48 // data has already been downloaded. | 56 // data has already been downloaded. |
| 49 void FetchModelData(); | 57 void FetchModelData(); |
| 50 | 58 |
| 51 // Returns true if executing the ranker model in the translation prompt | 59 // Returns true if executing the ranker model in the translation prompt |
| 52 // context described by |translate_prefs|, |src_lang|, |dst_lang| and possibly | 60 // context described by |translate_prefs|, |src_lang|, |dst_lang| and possibly |
| 53 // other global browser context attributes suggests that the user should be | 61 // other global browser context attributes suggests that the user should be |
| 54 // prompted as to whether translation should be performed. | 62 // prompted as to whether translation should be performed. |
| 55 bool ShouldOfferTranslation(const TranslatePrefs& translate_prefs, | 63 bool ShouldOfferTranslation(const TranslatePrefs& translate_prefs, |
| 56 const std::string& src_lang, | 64 const std::string& src_lang, |
| 57 const std::string& dst_lang); | 65 const std::string& dst_lang); |
| 58 | 66 |
| 59 // Returns the model version (a date stamp) or 0 if there is no valid model. | 67 // Returns the model version (a date stamp) or 0 if there is no valid model. |
| 60 int GetModelVersion() const; | 68 int GetModelVersion() const; |
| 61 | 69 |
| 70 // Caches the translate event. |
| 71 void RecordTranslateEvent( |
| 72 const metrics::TranslateEventProto& translate_event); |
| 73 |
| 74 // Transfers cached translate events to the given vector pointer and clears |
| 75 // the cache. |
| 76 void FlushTranslateEvents( |
| 77 std::vector<metrics::TranslateEventProto>* translate_events); |
| 78 |
| 62 private: | 79 private: |
| 63 // The ID which is assigned to the underlying URLFetcher. | 80 // The ID which is assigned to the underlying URLFetcher. |
| 64 static constexpr int kFetcherId = 2; | 81 static constexpr int kFetcherId = 2; |
| 65 | 82 |
| 66 TranslateRanker(); | 83 TranslateRanker(); |
| 67 | 84 |
| 68 // Exposed for testing via FRIEND_TEST. | 85 // Exposed for testing via FRIEND_TEST. |
| 69 double CalculateScore(double accept_ratio, | 86 double CalculateScore(double accept_ratio, |
| 70 double decline_ratio, | 87 double decline_ratio, |
| 71 double ignore_ratio, | 88 double ignore_ratio, |
| (...skipping 12 matching lines...) Expand all Loading... |
| 84 std::unique_ptr<TranslateURLFetcher> model_fetcher_; | 101 std::unique_ptr<TranslateURLFetcher> model_fetcher_; |
| 85 | 102 |
| 86 // The next time before which no new attempts to download the model should be | 103 // The next time before which no new attempts to download the model should be |
| 87 // attempted. | 104 // attempted. |
| 88 base::Time next_earliest_download_time_; | 105 base::Time next_earliest_download_time_; |
| 89 | 106 |
| 90 // Tracks the last time the translate ranker attempted to download its model. | 107 // Tracks the last time the translate ranker attempted to download its model. |
| 91 // Used for UMA reporting of timing. | 108 // Used for UMA reporting of timing. |
| 92 base::Time download_start_time_; | 109 base::Time download_start_time_; |
| 93 | 110 |
| 111 // Saved cache of translate event protos. |
| 112 std::vector<metrics::TranslateEventProto> translate_events_cache_; |
| 113 |
| 94 FRIEND_TEST_ALL_PREFIXES(TranslateRankerTest, CalculateScore); | 114 FRIEND_TEST_ALL_PREFIXES(TranslateRankerTest, CalculateScore); |
| 95 | 115 |
| 96 friend struct base::DefaultSingletonTraits<TranslateRanker>; | 116 friend struct base::DefaultSingletonTraits<TranslateRanker>; |
| 97 | 117 |
| 98 DISALLOW_COPY_AND_ASSIGN(TranslateRanker); | 118 DISALLOW_COPY_AND_ASSIGN(TranslateRanker); |
| 99 }; | 119 }; |
| 100 | 120 |
| 101 } // namespace translate | 121 } // namespace translate |
| 102 | 122 |
| 103 #endif // COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_RANKER_H_ | 123 #endif // COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_RANKER_H_ |
| OLD | NEW |