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

Unified Diff: components/translate/core/browser/translate_ranker.h

Issue 2736853004: Revert "[Translate] Add translate ranker model loader." (Closed)
Patch Set: merge 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 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 0be5f5fbbc71f65282fe21b169809af9bef2bb95..bb2da8b15eeed42f8c87a201b2752d949a9ec73d 100644
--- a/components/translate/core/browser/translate_ranker.h
+++ b/components/translate/core/browser/translate_ranker.h
@@ -7,56 +7,119 @@
#include <memory>
#include <string>
-#include <vector>
-#include "base/macros.h"
-#include "components/keyed_service/core/keyed_service.h"
+#include "base/feature_list.h"
+#include "base/gtest_prod_util.h"
+#include "base/memory/singleton.h"
+
+namespace chrome_intelligence {
+class TranslateRankerModel;
+}
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 : public KeyedService {
+class TranslateRanker {
public:
- TranslateRanker() = default;
+ ~TranslateRanker();
+
+ // Returns true if query or enforcement is enabled.
+ static bool IsEnabled();
// Returns true if translate events logging is enabled.
- virtual bool IsLoggingEnabled() = 0;
+ static bool IsLoggingEnabled();
+
+ // Returns true if querying is enabled.
+ static bool IsQueryEnabled();
// Returns true if enforcement is enabled.
- virtual bool IsEnforcementEnabled() = 0;
+ static bool IsEnforcementEnabled();
+
+ // Returns the singleton TranslateRanker instance.
+ static TranslateRanker* GetInstance();
- // Returns true if querying is enabled. Enabling enforcement will
- // automatically enable Query.
- virtual bool IsQueryEnabled() = 0;
+ // 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 the version id for the ranker model.
- virtual int GetModelVersion() const = 0;
+ // Initiates downloading of the assist model data. This is a NOP if the model
+ // data has already been downloaded.
+ void FetchModelData();
// 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.
- virtual bool ShouldOfferTranslation(const TranslatePrefs& translate_prefs,
- const std::string& src_lang,
- const std::string& dst_lang) = 0;
+ 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;
// Caches the translate event.
- virtual void AddTranslateEvent(
- const metrics::TranslateEventProto& translate_event) = 0;
+ void RecordTranslateEvent(
+ const metrics::TranslateEventProto& translate_event);
// Transfers cached translate events to the given vector pointer and clears
// the cache.
- virtual void FlushTranslateEvents(
- std::vector<metrics::TranslateEventProto>* events) = 0;
+ void FlushTranslateEvents(
+ std::vector<metrics::TranslateEventProto>* translate_events);
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