Chromium Code Reviews| 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 { | 19 namespace metrics { |
| 20 class TranslateEventProto; | 20 class TranslateEventProto; |
| 21 } | 21 } |
| 22 | 22 |
| 23 namespace translate { | 23 namespace translate { |
| 24 | 24 |
| 25 class TranslatePrefs; | 25 class TranslatePrefs; |
| 26 class TranslateURLFetcher; | 26 class TranslateRankerModelTraits; |
| 27 template <typename T> | |
| 28 class ModelLoader; | |
| 29 typedef ModelLoader<TranslateRankerModelTraits> TranslateRankerModelLoader; | |
|
pasko
2016/12/19 14:26:49
is this repetition of the same typedef intentional
Roger McFarlane (Chromium)
2017/02/08 23:08:07
There's no repetition here.
Forward declare Trans
| |
| 27 | 30 |
| 28 // Features used to enable ranker query, enforcement and logging. Note that | 31 // Features used to enable ranker query, enforcement and logging. Note that |
| 29 // enabling enforcement implies (forces) enabling queries. | 32 // enabling enforcement implies (forces) enabling queries. |
| 30 extern const base::Feature kTranslateRankerQuery; | 33 extern const base::Feature kTranslateRankerQuery; |
| 31 extern const base::Feature kTranslateRankerEnforcement; | 34 extern const base::Feature kTranslateRankerEnforcement; |
| 32 extern const base::Feature kTranslateRankerLogging; | 35 extern const base::Feature kTranslateRankerLogging; |
| 33 | 36 |
| 34 // If enabled, downloads a translate ranker model and uses it to determine | 37 // If enabled, downloads a translate ranker model and uses it to determine |
| 35 // whether the user should be given a translation prompt or not. | 38 // whether the user should be given a translation prompt or not. |
| 36 class TranslateRanker { | 39 class TranslateRanker { |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 50 static bool IsEnforcementEnabled(); | 53 static bool IsEnforcementEnabled(); |
| 51 | 54 |
| 52 // Returns the singleton TranslateRanker instance. | 55 // Returns the singleton TranslateRanker instance. |
| 53 static TranslateRanker* GetInstance(); | 56 static TranslateRanker* GetInstance(); |
| 54 | 57 |
| 55 // For testing only. Returns a TranslateRanker instance preloaded with a | 58 // For testing only. Returns a TranslateRanker instance preloaded with a |
| 56 // TranslateRankerModel as defined by |model_data|. | 59 // TranslateRankerModel as defined by |model_data|. |
| 57 static std::unique_ptr<TranslateRanker> CreateForTesting( | 60 static std::unique_ptr<TranslateRanker> CreateForTesting( |
| 58 const std::string& model_data); | 61 const std::string& model_data); |
| 59 | 62 |
| 60 // Initiates downloading of the assist model data. This is a NOP if the model | 63 // Starts the process to load the translate ranker model in the background. |
| 61 // data has already been downloaded. | 64 void StartModelLoader(); |
| 62 void FetchModelData(); | |
| 63 | 65 |
| 64 // Returns true if executing the ranker model in the translation prompt | 66 // Returns true if executing the ranker model in the translation prompt |
| 65 // context described by |translate_prefs|, |src_lang|, |dst_lang| and possibly | 67 // context described by |translate_prefs|, |src_lang|, |dst_lang| and possibly |
| 66 // other global browser context attributes suggests that the user should be | 68 // other global browser context attributes suggests that the user should be |
| 67 // prompted as to whether translation should be performed. | 69 // prompted as to whether translation should be performed. |
| 68 bool ShouldOfferTranslation(const TranslatePrefs& translate_prefs, | 70 bool ShouldOfferTranslation(const TranslatePrefs& translate_prefs, |
| 69 const std::string& src_lang, | 71 const std::string& src_lang, |
| 70 const std::string& dst_lang); | 72 const std::string& dst_lang); |
| 71 | 73 |
| 72 // Returns the model version (a date stamp) or 0 if there is no valid model. | 74 // Returns the model version (a date stamp) or 0 if there is no valid model. |
| 73 int GetModelVersion() const; | 75 int GetModelVersion() const; |
| 74 | 76 |
| 75 // Caches the translate event. | 77 // Caches the translate event. |
| 76 void RecordTranslateEvent( | 78 void RecordTranslateEvent( |
| 77 const metrics::TranslateEventProto& translate_event); | 79 const metrics::TranslateEventProto& translate_event); |
| 78 | 80 |
| 79 // Transfers cached translate events to the given vector pointer and clears | 81 // Transfers cached translate events to the given vector pointer and clears |
| 80 // the cache. | 82 // the cache. |
| 81 void FlushTranslateEvents( | 83 void FlushTranslateEvents( |
| 82 std::vector<metrics::TranslateEventProto>* translate_events); | 84 std::vector<metrics::TranslateEventProto>* translate_events); |
| 83 | 85 |
| 84 private: | 86 private: |
| 85 // The ID which is assigned to the underlying URLFetcher. | 87 typedef scoped_refptr< |
| 86 static constexpr int kFetcherId = 2; | 88 base::RefCountedData<chrome_intelligence::TranslateRankerModel>> |
| 89 SharedModelPtr; | |
| 90 typedef scoped_refptr< | |
| 91 const base::RefCountedData<chrome_intelligence::TranslateRankerModel>> | |
| 92 ConstSharedModelPtr; | |
| 87 | 93 |
| 88 TranslateRanker(); | 94 TranslateRanker(); |
| 89 | 95 |
| 90 // Exposed for testing via FRIEND_TEST. | 96 // Exposed for testing via FRIEND_TEST. |
| 91 double CalculateScore(double accept_ratio, | 97 double CalculateScore(const chrome_intelligence::TranslateRankerModel& model, |
| 98 double accept_ratio, | |
| 92 double decline_ratio, | 99 double decline_ratio, |
| 93 double ignore_ratio, | 100 double ignore_ratio, |
| 94 const std::string& src_lang, | 101 const std::string& src_lang, |
| 95 const std::string& dst_lang, | 102 const std::string& dst_lang, |
| 96 const std::string& app_locale, | 103 const std::string& app_locale, |
| 97 const std::string& country); | 104 const std::string& country); |
| 98 | 105 |
| 99 // Called when the model download has completed. | 106 // Called by the model loader when a new model is available. |
| 100 void ParseModel(int id, bool success, const std::string& model_data); | 107 void SetSharedModelPtr( |
| 108 std::unique_ptr<chrome_intelligence::TranslateRankerModel> p); | |
| 109 | |
| 110 // Returns a reference to a const shared model. | |
| 111 ConstSharedModelPtr GetSharedModelPtr() const; | |
| 112 | |
| 113 // Protects copies/updates of the |shared_model_ptr_|. | |
| 114 mutable base::Lock lock_; | |
|
pasko
2016/12/19 14:26:49
generally we discourage locks as a measure to avoi
gab
2016/12/19 21:00:46
+1, prefer non-thread-safe classes running on sequ
Roger McFarlane (Chromium)
2017/02/08 23:08:07
Done.
Roger McFarlane (Chromium)
2017/02/08 23:08:08
Done.
| |
| 115 | |
| 116 // A helper to load the model from disk cache or a URL. | |
| 117 std::unique_ptr<TranslateRankerModelLoader> model_loader_; | |
| 101 | 118 |
| 102 // The translation ranker model. | 119 // The translation ranker model. |
| 103 std::unique_ptr<chrome_intelligence::TranslateRankerModel> model_; | 120 ConstSharedModelPtr shared_model_ptr_; |
| 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 | 121 |
| 116 // Saved cache of translate event protos. | 122 // Saved cache of translate event protos. |
| 117 std::vector<metrics::TranslateEventProto> translate_events_cache_; | 123 std::vector<metrics::TranslateEventProto> translate_events_cache_; |
| 118 | 124 |
| 119 FRIEND_TEST_ALL_PREFIXES(TranslateRankerTest, CalculateScore); | 125 FRIEND_TEST_ALL_PREFIXES(TranslateRankerTest, CalculateScore); |
| 120 | 126 |
| 121 friend struct base::DefaultSingletonTraits<TranslateRanker>; | 127 friend struct base::DefaultSingletonTraits<TranslateRanker>; |
| 122 | 128 |
| 123 DISALLOW_COPY_AND_ASSIGN(TranslateRanker); | 129 DISALLOW_COPY_AND_ASSIGN(TranslateRanker); |
| 124 }; | 130 }; |
| 125 | 131 |
| 126 } // namespace translate | 132 } // namespace translate |
| 127 | 133 |
| 128 #endif // COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_RANKER_H_ | 134 #endif // COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_RANKER_H_ |
| OLD | NEW |