| Index: components/translate/core/browser/ranker_model_loader.h
|
| diff --git a/components/translate/core/browser/ranker_model_loader.h b/components/translate/core/browser/ranker_model_loader.h
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..3e739017b8b651ed9de5449023d849d3ad4d104a
|
| --- /dev/null
|
| +++ b/components/translate/core/browser/ranker_model_loader.h
|
| @@ -0,0 +1,150 @@
|
| +// Copyright 2016 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#ifndef COMPONENTS_TRANSLATE_CORE_BROWSER_RANKER_MODEL_LOADER_H_
|
| +#define COMPONENTS_TRANSLATE_CORE_BROWSER_RANKER_MODEL_LOADER_H_
|
| +
|
| +#include <memory>
|
| +#include <string>
|
| +
|
| +#include "base/files/file_path.h"
|
| +#include "base/memory/ref_counted.h"
|
| +#include "base/sequence_checker.h"
|
| +#include "base/threading/thread.h"
|
| +#include "components/translate/core/browser/proto/ranker_model.pb.h"
|
| +#include "url/gurl.h"
|
| +
|
| +namespace chrome_intelligence {
|
| +class RankerModel;
|
| +} // namespace chrome_intelligence
|
| +
|
| +namespace translate {
|
| +
|
| +class TranslateURLFetcher;
|
| +
|
| +// Enumeration denoting the outcome of an attempt to download the model. This
|
| +// must be kept in sync with the TranslateRankerModelStatus enum in
|
| +// histograms.xml
|
| +// TODO(rogerm): rename the enum in histograms.xml to be more generic
|
| +enum class RankerModelStatus {
|
| + OK = 0,
|
| + DOWNLOAD_THROTTLED = 1,
|
| + DOWNLOAD_FAILED = 2,
|
| + PARSE_FAILED = 3,
|
| + VALIDATION_FAILED = 4,
|
| + INCOMPATIBLE = 5,
|
| +
|
| + // Insert new values above this line.
|
| + MAX
|
| +};
|
| +
|
| +// Interface exposed by a Model Loader's Observer. The observer must outlive
|
| +// the model loader.
|
| +class RankerModelObserver {
|
| + public:
|
| + virtual ~RankerModelObserver() = default;
|
| +
|
| + // The type of model the observer is willing/able to accept. Must be
|
| + // thread-safe.
|
| + virtual RankerModelStatus Validate(
|
| + const chrome_intelligence::RankerModel& model) const = 0;
|
| +
|
| + // Called when a model having the expected model type and compatibility level
|
| + // is loaded the model loader. This call will be executed by the task_runner
|
| + // used by this observer.
|
| + virtual void OnModelAvailable(
|
| + std::unique_ptr<chrome_intelligence::RankerModel> model) = 0;
|
| +
|
| + protected:
|
| + RankerModelObserver() = default;
|
| +
|
| + private:
|
| + DISALLOW_COPY_AND_ASSIGN(RankerModelObserver);
|
| +};
|
| +
|
| +// If enabled, downloads a translate ranker model and uses it to determine
|
| +// whether the user should be given a translation prompt or not.
|
| +class RankerModelLoader {
|
| + public:
|
| + RankerModelLoader(RankerModelObserver* observer,
|
| + const base::FilePath& model_path,
|
| + const GURL& model_url,
|
| + const std::string& uma_prefix);
|
| +
|
| + ~RankerModelLoader();
|
| +
|
| + // Asynchronously initiates loading the model from the cache file path and URL
|
| + // previously configured.
|
| + void Start();
|
| +
|
| + // Call this method periodically to notify the model loader the network might
|
| + // be available. If a model download is pending, this will trigger (subject to
|
| + // retry and frequency limits) a download attempt.
|
| + void NotifyOfNetworkAvailability();
|
| +
|
| + private:
|
| + // The maximum number of model download attempts to make. Download may fail
|
| + // due to server error or network availability issues.
|
| + const int kMaxRetryOn5xx = 8;
|
| +
|
| + // The minimum duration, in minutes, between download attempts.
|
| + const int kDownloadRefractoryPeriodMin = 3;
|
| +
|
| + // Log the result of loading a model to UMA.
|
| + void ReportModelStatus(RankerModelStatus model_status);
|
| +
|
| + // Called to construct a model from the given |data|.
|
| + std::unique_ptr<chrome_intelligence::RankerModel> Parse(
|
| + const std::string& data);
|
| +
|
| + // Attempt to read the model from the path returns by T::GerModelPath()
|
| + void LoadFromFile();
|
| +
|
| + // Attempt to read the model from the URL returns by T::GetModelURL()
|
| + void LoadFromURL();
|
| +
|
| + // Called when the background task to download the model from |model_url_|
|
| + // has completed.
|
| + void OnDownloadComplete(int id, bool success, const std::string& data);
|
| +
|
| + // Transfer owhersnip of |model| to the |observer_|.
|
| + void TransferModelToObserver(
|
| + std::unique_ptr<chrome_intelligence::RankerModel> model);
|
| +
|
| + // The thread on which to perform background IO to read, download and
|
| + // cache the model.
|
| + base::Thread thread_;
|
| +
|
| + // Used to download model data from |model_url_|.
|
| + // TODO(rogerm): Use net::URLFetcher directly?
|
| + std::unique_ptr<TranslateURLFetcher> url_fetcher_;
|
| +
|
| + // The next time before which no new attempts to download the model should be
|
| + // attempted.
|
| + base::TimeTicks next_earliest_download_time_;
|
| +
|
| + // Tracks the last time of the last attempt to download a model. Used for UMA
|
| + // reporting of download duration.
|
| + base::TimeTicks download_start_time_;
|
| +
|
| + // A pointer to the observer for this model loader. The observer must outlive
|
| + // the loader.
|
| + RankerModelObserver* const observer_; // Weak.
|
| +
|
| + // The path at which the model is (or should be) cached.
|
| + const base::FilePath model_path_;
|
| +
|
| + // The URL from which to download the model if the model is not in the cache
|
| + // or the cached model is invalid/expired.
|
| + const GURL model_url_;
|
| +
|
| + // This will prefix all UMA metrics generated by the model loader.
|
| + const std::string uma_prefix_;
|
| +
|
| + DISALLOW_COPY_AND_ASSIGN(RankerModelLoader);
|
| +};
|
| +
|
| +} // namespace translate
|
| +
|
| +#endif // COMPONENTS_TRANSLATE_CORE_BROWSER_RANKER_MODEL_LOADER_H_
|
|
|