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