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

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

Issue 2565873002: [translate] Add translate ranker model loader. (Closed)
Patch Set: comments from sdefresne 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;
31
32 // Returns true if enforcement is enabled.
33 virtual bool IsEnforcementEnabled() = 0;
45 34
46 // Returns true if querying is enabled. 35 // Returns true if querying is enabled.
47 static bool IsQueryEnabled(); 36 virtual bool IsQueryEnabled() = 0;
48 37
49 // Returns true if enforcement is enabled. 38 // Returns the version id for the ranker model.
50 static bool IsEnforcementEnabled(); 39 virtual int GetModelVersion() const = 0;
51
52 // Returns the singleton TranslateRanker instance.
53 static TranslateRanker* GetInstance();
54
55 // For testing only. Returns a TranslateRanker instance preloaded with a
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();
63 40
64 // Returns true if executing the ranker model in the translation prompt 41 // Returns true if executing the ranker model in the translation prompt
65 // context described by |translate_prefs|, |src_lang|, |dst_lang| and possibly 42 // context described by |translate_prefs|, |src_lang|, |dst_lang| and possibly
66 // other global browser context attributes suggests that the user should be 43 // other global browser context attributes suggests that the user should be
67 // prompted as to whether translation should be performed. 44 // prompted as to whether translation should be performed.
68 bool ShouldOfferTranslation(const TranslatePrefs& translate_prefs, 45 virtual bool ShouldOfferTranslation(const TranslatePrefs& translate_prefs,
69 const std::string& src_lang, 46 const std::string& src_lang,
70 const std::string& dst_lang); 47 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 48
75 // Caches the translate event. 49 // Caches the translate event.
76 void RecordTranslateEvent( 50 virtual void AddTranslateEvent(
77 const metrics::TranslateEventProto& translate_event); 51 const metrics::TranslateEventProto& translate_event) = 0;
78 52
79 // Transfers cached translate events to the given vector pointer and clears 53 // Transfers cached translate events to the given vector pointer and clears
80 // the cache. 54 // the cache.
81 void FlushTranslateEvents( 55 virtual void FlushTranslateEvents(
82 std::vector<metrics::TranslateEventProto>* translate_events); 56 std::vector<metrics::TranslateEventProto>* events) = 0;
83 57
84 private: 58 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); 59 DISALLOW_COPY_AND_ASSIGN(TranslateRanker);
124 }; 60 };
125 61
126 } // namespace translate 62 } // namespace translate
127 63
128 #endif // COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_RANKER_H_ 64 #endif // COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_RANKER_H_
OLDNEW
« no previous file with comments | « components/translate/core/browser/translate_manager_unittest.cc ('k') | components/translate/core/browser/translate_ranker.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698