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

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

Issue 2565873002: [translate] Add translate ranker model loader. (Closed)
Patch Set: comments from hamelphi Created 3 years, 11 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 28
40 // Returns true if query or enforcement is enabled. 29 // Returns true if query or enforcement is enabled.
41 static bool IsEnabled(); 30 virtual bool IsEnabled() = 0;
42 31
43 // Returns true if translate events logging is enabled. 32 // Returns true if translate events logging is enabled.
44 static bool IsLoggingEnabled(); 33 virtual bool IsLoggingEnabled() = 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 true if enforcement is enabled.
50 static bool IsEnforcementEnabled(); 39 virtual bool IsEnforcementEnabled() = 0;
51 40
52 // Returns the singleton TranslateRanker instance. 41 // Returns the version id for the ranker model.
53 static TranslateRanker* GetInstance(); 42 virtual int GetModelVersion() const = 0;
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 43
64 // Returns true if executing the ranker model in the translation prompt 44 // Returns true if executing the ranker model in the translation prompt
65 // context described by |translate_prefs|, |src_lang|, |dst_lang| and possibly 45 // context described by |translate_prefs|, |src_lang|, |dst_lang| and possibly
66 // other global browser context attributes suggests that the user should be 46 // other global browser context attributes suggests that the user should be
67 // prompted as to whether translation should be performed. 47 // prompted as to whether translation should be performed.
68 bool ShouldOfferTranslation(const TranslatePrefs& translate_prefs, 48 virtual bool ShouldOfferTranslation(const TranslatePrefs& translate_prefs,
69 const std::string& src_lang, 49 const std::string& src_lang,
70 const std::string& dst_lang); 50 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 51
75 // Caches the translate event. 52 // Caches the translate event.
76 void RecordTranslateEvent( 53 virtual void AddTranslateEvent(
77 const metrics::TranslateEventProto& translate_event); 54 std::unique_ptr<metrics::TranslateEventProto> translate_event) = 0;
78 55
79 // Transfers cached translate events to the given vector pointer and clears 56 // Transfers cached translate events to the given vector pointer and clears
80 // the cache. 57 // the cache.
81 void FlushTranslateEvents( 58 virtual void FlushTranslateEvents(
82 std::vector<metrics::TranslateEventProto>* translate_events); 59 std::vector<std::unique_ptr<metrics::TranslateEventProto>>* events) = 0;
83 60
84 private: 61 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(double accept_ratio,
92 double decline_ratio,
93 double ignore_ratio,
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); 62 DISALLOW_COPY_AND_ASSIGN(TranslateRanker);
124 }; 63 };
125 64
126 } // namespace translate 65 } // namespace translate
127 66
128 #endif // COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_RANKER_H_ 67 #endif // COMPONENTS_TRANSLATE_CORE_BROWSER_TRANSLATE_RANKER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698