Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(539)

Side by Side Diff: components/translate/core/browser/translate_ranker.h

Issue 2565873002: [translate] Add translate ranker model loader. (Closed)
Patch Set: comments from groby and fdoray Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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>
10 11
11 #include "base/feature_list.h" 12 #include "base/macros.h"
12 #include "base/gtest_prod_util.h" 13 #include "components/keyed_service/core/keyed_service.h"
13 #include "base/memory/singleton.h"
14
15 namespace chrome_intelligence {
16 class TranslateRankerModel;
17 }
18 14
19 namespace metrics { 15 namespace metrics {
20 class TranslateEventProto; 16 class TranslateEventProto;
21 } 17 } // namespace metrics
22 18
23 namespace translate { 19 namespace translate {
24 20
25 class TranslatePrefs; 21 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;
33 22
34 // If enabled, downloads a translate ranker model and uses it to determine 23 // If enabled, downloads a translate ranker model and uses it to determine
35 // whether the user should be given a translation prompt or not. 24 // whether the user should be given a translation prompt or not.
36 class TranslateRanker { 25 class TranslateRanker : public KeyedService {
37 public: 26 public:
38 ~TranslateRanker(); 27 TranslateRanker() = default;
39
40 // Returns true if query or enforcement is enabled.
41 static bool IsEnabled();
42 28
43 // Returns true if translate events logging is enabled. 29 // Returns true if translate events logging is enabled.
44 static bool IsLoggingEnabled(); 30 virtual bool IsLoggingEnabled() = 0;
45
46 // Returns true if querying is enabled.
47 static bool IsQueryEnabled();
48 31
49 // Returns true if enforcement is enabled. 32 // Returns true if enforcement is enabled.
50 static bool IsEnforcementEnabled(); 33 virtual bool IsEnforcementEnabled() = 0;
51 34
52 // Returns the singleton TranslateRanker instance. 35 // Returns true if querying is enabled. This is implicitly true if either
53 static TranslateRanker* GetInstance(); 36 // enforcement or logging are enabled, but can also be enabled independently.
hamelphi 2017/02/23 23:05:03 Logging is independent of querying (if not queries
Roger McFarlane (Chromium) 2017/02/24 02:49:53 Done.
37 virtual bool IsQueryEnabled() = 0;
54 38
55 // For testing only. Returns a TranslateRanker instance preloaded with a 39 // Returns the version id for the ranker model.
56 // TranslateRankerModel as defined by |model_data|. 40 virtual int GetModelVersion() const = 0;
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();
63 41
64 // Returns true if executing the ranker model in the translation prompt 42 // Returns true if executing the ranker model in the translation prompt
65 // context described by |translate_prefs|, |src_lang|, |dst_lang| and possibly 43 // context described by |translate_prefs|, |src_lang|, |dst_lang| and possibly
66 // other global browser context attributes suggests that the user should be 44 // other global browser context attributes suggests that the user should be
67 // prompted as to whether translation should be performed. 45 // prompted as to whether translation should be performed.
68 bool ShouldOfferTranslation(const TranslatePrefs& translate_prefs, 46 virtual bool ShouldOfferTranslation(const TranslatePrefs& translate_prefs,
69 const std::string& src_lang, 47 const std::string& src_lang,
70 const std::string& dst_lang); 48 const std::string& dst_lang) = 0;
71
72 // Returns the model version (a date stamp) or 0 if there is no valid model.
73 int GetModelVersion() const;
74 49
75 // Caches the translate event. 50 // Caches the translate event.
76 void RecordTranslateEvent( 51 virtual void AddTranslateEvent(
77 const metrics::TranslateEventProto& translate_event); 52 const metrics::TranslateEventProto& translate_event) = 0;
78 53
79 // Transfers cached translate events to the given vector pointer and clears 54 // Transfers cached translate events to the given vector pointer and clears
80 // the cache. 55 // the cache.
81 void FlushTranslateEvents( 56 virtual void FlushTranslateEvents(
82 std::vector<metrics::TranslateEventProto>* translate_events); 57 std::vector<metrics::TranslateEventProto>* events) = 0;
83 58
84 private: 59 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
123 DISALLOW_COPY_AND_ASSIGN(TranslateRanker); 60 DISALLOW_COPY_AND_ASSIGN(TranslateRanker);
124 }; 61 };
125 62
126 } // namespace translate 63 } // namespace translate
127 64
128 #endif // COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_RANKER_H_ 65 #endif // COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_RANKER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698