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

Unified 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, 10 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 side-by-side diff with in-line comments
Download patch
Index: components/translate/core/browser/translate_ranker.h
diff --git a/components/translate/core/browser/translate_ranker.h b/components/translate/core/browser/translate_ranker.h
index bb2da8b15eeed42f8c87a201b2752d949a9ec73d..781677a1fdaf5fea1e7d04eb41087863f4ac5a58 100644
--- a/components/translate/core/browser/translate_ranker.h
+++ b/components/translate/core/browser/translate_ranker.h
@@ -7,119 +7,55 @@
#include <memory>
#include <string>
+#include <vector>
-#include "base/feature_list.h"
-#include "base/gtest_prod_util.h"
-#include "base/memory/singleton.h"
-
-namespace chrome_intelligence {
-class TranslateRankerModel;
-}
+#include "base/macros.h"
+#include "components/keyed_service/core/keyed_service.h"
namespace metrics {
class TranslateEventProto;
-}
+} // namespace metrics
namespace translate {
class TranslatePrefs;
-class TranslateURLFetcher;
-
-// Features used to enable ranker query, enforcement and logging. Note that
-// enabling enforcement implies (forces) enabling queries.
-extern const base::Feature kTranslateRankerQuery;
-extern const base::Feature kTranslateRankerEnforcement;
-extern const base::Feature kTranslateRankerLogging;
// If enabled, downloads a translate ranker model and uses it to determine
// whether the user should be given a translation prompt or not.
-class TranslateRanker {
+class TranslateRanker : public KeyedService {
public:
- ~TranslateRanker();
-
- // Returns true if query or enforcement is enabled.
- static bool IsEnabled();
+ TranslateRanker() = default;
// Returns true if translate events logging is enabled.
- static bool IsLoggingEnabled();
-
- // Returns true if querying is enabled.
- static bool IsQueryEnabled();
+ virtual bool IsLoggingEnabled() = 0;
// Returns true if enforcement is enabled.
- static bool IsEnforcementEnabled();
+ virtual bool IsEnforcementEnabled() = 0;
- // Returns the singleton TranslateRanker instance.
- static TranslateRanker* GetInstance();
-
- // For testing only. Returns a TranslateRanker instance preloaded with a
- // TranslateRankerModel as defined by |model_data|.
- static std::unique_ptr<TranslateRanker> CreateForTesting(
- const std::string& model_data);
+ // Returns true if querying is enabled.
+ virtual bool IsQueryEnabled() = 0;
- // Initiates downloading of the assist model data. This is a NOP if the model
- // data has already been downloaded.
- void FetchModelData();
+ // Returns the version id for the ranker model.
+ virtual int GetModelVersion() const = 0;
// Returns true if executing the ranker model in the translation prompt
// context described by |translate_prefs|, |src_lang|, |dst_lang| and possibly
// other global browser context attributes suggests that the user should be
// prompted as to whether translation should be performed.
- bool ShouldOfferTranslation(const TranslatePrefs& translate_prefs,
- const std::string& src_lang,
- const std::string& dst_lang);
-
- // Returns the model version (a date stamp) or 0 if there is no valid model.
- int GetModelVersion() const;
+ virtual bool ShouldOfferTranslation(const TranslatePrefs& translate_prefs,
+ const std::string& src_lang,
+ const std::string& dst_lang) = 0;
// Caches the translate event.
- void RecordTranslateEvent(
- const metrics::TranslateEventProto& translate_event);
+ virtual void AddTranslateEvent(
+ const metrics::TranslateEventProto& translate_event) = 0;
// Transfers cached translate events to the given vector pointer and clears
// the cache.
- void FlushTranslateEvents(
- std::vector<metrics::TranslateEventProto>* translate_events);
+ virtual void FlushTranslateEvents(
+ std::vector<metrics::TranslateEventProto>* events) = 0;
private:
- // The ID which is assigned to the underlying URLFetcher.
- static constexpr int kFetcherId = 2;
-
- TranslateRanker();
-
- // Exposed for testing via FRIEND_TEST.
- double CalculateScore(int accept_count,
- int decline_count,
- int ignore_count,
- const std::string& src_lang,
- const std::string& dst_lang,
- const std::string& app_locale,
- const std::string& country);
-
- // Called when the model download has completed.
- void ParseModel(int id, bool success, const std::string& model_data);
-
- // The translation ranker model.
- std::unique_ptr<chrome_intelligence::TranslateRankerModel> model_;
-
- // A URL fetcher to download translation ranker model data.
- std::unique_ptr<TranslateURLFetcher> model_fetcher_;
-
- // The next time before which no new attempts to download the model should be
- // attempted.
- base::Time next_earliest_download_time_;
-
- // Tracks the last time the translate ranker attempted to download its model.
- // Used for UMA reporting of timing.
- base::Time download_start_time_;
-
- // Saved cache of translate event protos.
- std::vector<metrics::TranslateEventProto> translate_events_cache_;
-
- FRIEND_TEST_ALL_PREFIXES(TranslateRankerTest, CalculateScore);
-
- friend struct base::DefaultSingletonTraits<TranslateRanker>;
-
DISALLOW_COPY_AND_ASSIGN(TranslateRanker);
};
« 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