OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef COMPONENTS_TRANSLATE_CORE_BROWSER_RANKER_MODEL_LOADER_H_ |
| 6 #define COMPONENTS_TRANSLATE_CORE_BROWSER_RANKER_MODEL_LOADER_H_ |
| 7 |
| 8 #include <memory> |
| 9 #include <string> |
| 10 |
| 11 #include "base/files/file_path.h" |
| 12 #include "base/memory/ref_counted.h" |
| 13 #include "base/sequence_checker.h" |
| 14 #include "base/threading/thread.h" |
| 15 #include "components/translate/core/browser/proto/ranker_model.pb.h" |
| 16 #include "url/gurl.h" |
| 17 |
| 18 namespace chrome_intelligence { |
| 19 class RankerModel; |
| 20 } // namespace chrome_intelligence |
| 21 |
| 22 namespace translate { |
| 23 |
| 24 class TranslateURLFetcher; |
| 25 |
| 26 // Enumeration denoting the outcome of an attempt to download the model. This |
| 27 // must be kept in sync with the TranslateRankerModelStatus enum in |
| 28 // histograms.xml |
| 29 // TODO(rogerm): rename the enum in histograms.xml to be more generic |
| 30 enum class RankerModelStatus { |
| 31 OK = 0, |
| 32 DOWNLOAD_THROTTLED = 1, |
| 33 DOWNLOAD_FAILED = 2, |
| 34 PARSE_FAILED = 3, |
| 35 VALIDATION_FAILED = 4, |
| 36 INCOMPATIBLE = 5, |
| 37 |
| 38 // Insert new values above this line. |
| 39 MAX |
| 40 }; |
| 41 |
| 42 // Interface exposed by a Model Loader's Observer. The observer must outlive |
| 43 // the model loader. |
| 44 class RankerModelObserver { |
| 45 public: |
| 46 virtual ~RankerModelObserver() = default; |
| 47 |
| 48 // The type of model the observer is willing/able to accept. Must be |
| 49 // thread-safe. |
| 50 virtual RankerModelStatus Validate( |
| 51 const chrome_intelligence::RankerModel& model) const = 0; |
| 52 |
| 53 // Called when a model having the expected model type and compatibility level |
| 54 // is loaded the model loader. This call will be executed by the task_runner |
| 55 // used by this observer. |
| 56 virtual void OnModelAvailable( |
| 57 std::unique_ptr<chrome_intelligence::RankerModel> model) = 0; |
| 58 |
| 59 protected: |
| 60 RankerModelObserver() = default; |
| 61 |
| 62 private: |
| 63 DISALLOW_COPY_AND_ASSIGN(RankerModelObserver); |
| 64 }; |
| 65 |
| 66 // If enabled, downloads a translate ranker model and uses it to determine |
| 67 // whether the user should be given a translation prompt or not. |
| 68 class RankerModelLoader { |
| 69 public: |
| 70 RankerModelLoader(RankerModelObserver* observer, |
| 71 const base::FilePath& model_path, |
| 72 const GURL& model_url, |
| 73 const std::string& uma_prefix); |
| 74 |
| 75 ~RankerModelLoader(); |
| 76 |
| 77 // Asynchronously initiates loading the model from the cache file path and URL |
| 78 // previously configured. |
| 79 void Start(); |
| 80 |
| 81 // Call this method periodically to notify the model loader the network might |
| 82 // be available. If a model download is pending, this will trigger (subject to |
| 83 // retry and frequency limits) a download attempt. |
| 84 void NotifyOfNetworkAvailability(); |
| 85 |
| 86 private: |
| 87 // The maximum number of model download attempts to make. Download may fail |
| 88 // due to server error or network availability issues. |
| 89 const int kMaxRetryOn5xx = 8; |
| 90 |
| 91 // The minimum duration, in minutes, between download attempts. |
| 92 const int kDownloadRefractoryPeriodMin = 3; |
| 93 |
| 94 // Log the result of loading a model to UMA. |
| 95 void ReportModelStatus(RankerModelStatus model_status); |
| 96 |
| 97 // Called to construct a model from the given |data|. |
| 98 std::unique_ptr<chrome_intelligence::RankerModel> Parse( |
| 99 const std::string& data); |
| 100 |
| 101 // Attempt to read the model from the path returns by T::GerModelPath() |
| 102 void LoadFromFile(); |
| 103 |
| 104 // Attempt to read the model from the URL returns by T::GetModelURL() |
| 105 void LoadFromURL(); |
| 106 |
| 107 // Called when the background task to download the model from |model_url_| |
| 108 // has completed. |
| 109 void OnDownloadComplete(int id, bool success, const std::string& data); |
| 110 |
| 111 // Transfer owhersnip of |model| to the |observer_|. |
| 112 void TransferModelToObserver( |
| 113 std::unique_ptr<chrome_intelligence::RankerModel> model); |
| 114 |
| 115 // The thread on which to perform background IO to read, download and |
| 116 // cache the model. |
| 117 base::Thread thread_; |
| 118 |
| 119 // Used to download model data from |model_url_|. |
| 120 // TODO(rogerm): Use net::URLFetcher directly? |
| 121 std::unique_ptr<TranslateURLFetcher> url_fetcher_; |
| 122 |
| 123 // The next time before which no new attempts to download the model should be |
| 124 // attempted. |
| 125 base::TimeTicks next_earliest_download_time_; |
| 126 |
| 127 // Tracks the last time of the last attempt to download a model. Used for UMA |
| 128 // reporting of download duration. |
| 129 base::TimeTicks download_start_time_; |
| 130 |
| 131 // A pointer to the observer for this model loader. The observer must outlive |
| 132 // the loader. |
| 133 RankerModelObserver* const observer_; // Weak. |
| 134 |
| 135 // The path at which the model is (or should be) cached. |
| 136 const base::FilePath model_path_; |
| 137 |
| 138 // The URL from which to download the model if the model is not in the cache |
| 139 // or the cached model is invalid/expired. |
| 140 const GURL model_url_; |
| 141 |
| 142 // This will prefix all UMA metrics generated by the model loader. |
| 143 const std::string uma_prefix_; |
| 144 |
| 145 DISALLOW_COPY_AND_ASSIGN(RankerModelLoader); |
| 146 }; |
| 147 |
| 148 } // namespace translate |
| 149 |
| 150 #endif // COMPONENTS_TRANSLATE_CORE_BROWSER_RANKER_MODEL_LOADER_H_ |
OLD | NEW |