| OLD | NEW |
| (Empty) |
| 1 // Copyright 2017 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/callback.h" | |
| 12 #include "base/files/file_path.h" | |
| 13 #include "base/memory/ref_counted.h" | |
| 14 #include "base/memory/weak_ptr.h" | |
| 15 #include "base/sequence_checker.h" | |
| 16 #include "base/time/time.h" | |
| 17 #include "url/gurl.h" | |
| 18 | |
| 19 namespace base { | |
| 20 class SequencedTaskRunner; | |
| 21 } // namespace base | |
| 22 | |
| 23 namespace chrome_intelligence { | |
| 24 class RankerModel; | |
| 25 } // namespace chrome_intelligence | |
| 26 | |
| 27 namespace translate { | |
| 28 | |
| 29 class TranslateURLFetcher; | |
| 30 | |
| 31 // Enumeration denoting the outcome of an attempt to download the model. This | |
| 32 // must be kept in sync with the RankerModelStatus enum in histograms.xml | |
| 33 enum class RankerModelStatus { | |
| 34 OK = 0, | |
| 35 DOWNLOAD_THROTTLED = 1, | |
| 36 DOWNLOAD_FAILED = 2, | |
| 37 PARSE_FAILED = 3, | |
| 38 VALIDATION_FAILED = 4, | |
| 39 INCOMPATIBLE = 5, | |
| 40 LOAD_FROM_CACHE_FAILED = 6, | |
| 41 MODEL_LOADING_ABANDONED = 7, | |
| 42 | |
| 43 // Insert new values above this line. | |
| 44 MAX | |
| 45 }; | |
| 46 | |
| 47 // If enabled, downloads a translate ranker model and uses it to determine | |
| 48 // whether the user should be given a translation prompt or not. | |
| 49 class RankerModelLoader { | |
| 50 public: | |
| 51 // Callback to validate a ranker model on behalf of the model loader client. | |
| 52 // For example, the callback might validate that the model is compatible with | |
| 53 // the features generated when ranking translation offerings. This will be | |
| 54 // called on the sequence on which the model loader was constructed. | |
| 55 using ValidateModelCallback = base::RepeatingCallback<RankerModelStatus( | |
| 56 const chrome_intelligence::RankerModel&)>; | |
| 57 | |
| 58 // Called to transfer ownership of a loaded model back to the model loader | |
| 59 // client. This will be called on the sequence on which the model loader was | |
| 60 // constructed. | |
| 61 using OnModelAvailableCallback = base::RepeatingCallback<void( | |
| 62 std::unique_ptr<chrome_intelligence::RankerModel>)>; | |
| 63 | |
| 64 // |validate_model_callback| may be called on any sequence; it must be thread | |
| 65 // safe. | |
| 66 // | |
| 67 // |on_model_available_callback| will be called on the sequence on which the | |
| 68 // ranker model loader is constructed. | |
| 69 // | |
| 70 // |model_path| denotes the file path at which the model is cached. The loader | |
| 71 // will attempt to load the model from this path first, falling back to the | |
| 72 // |model_url| if the model cannot be loaded or has expired. Upon downloading | |
| 73 // a fresh model from |model_url| the model will be persisted to |model_path| | |
| 74 // for subsequent caching. | |
| 75 // | |
| 76 // |model_url| denotes the URL from which the model should be loaded, if it | |
| 77 // has not already been cached at |model_path|. | |
| 78 // | |
| 79 // |uma_prefix| will be used as a prefix for the names of all UMA metrics | |
| 80 // generated by this loader. | |
| 81 RankerModelLoader(ValidateModelCallback validate_model_callback, | |
| 82 OnModelAvailableCallback on_model_available_callback, | |
| 83 base::FilePath model_path, | |
| 84 GURL model_url, | |
| 85 std::string uma_prefix); | |
| 86 | |
| 87 ~RankerModelLoader(); | |
| 88 | |
| 89 // Call this method periodically to notify the model loader the ranker is | |
| 90 // actively in use. The user's engagement with the ranked feature is used | |
| 91 // as a proxy for network availability and activity. If a model download | |
| 92 // is pending, this will trigger (subject to retry and frequency limits) a | |
| 93 // model download attempt. | |
| 94 void NotifyOfRankerActivity(); | |
| 95 | |
| 96 private: | |
| 97 // A enum to track the current loader state. | |
| 98 enum class LoaderState { | |
| 99 // The loader is newly created and has not started trying to load the model. | |
| 100 // This state can transition to LOADING_FROM_FILE or, if |model_path_| is | |
| 101 // empty, to LOADING_FROM_URL. If both |model_path_| and |model_url_| are | |
| 102 // empty/invalid then it can transition to FINISHED. | |
| 103 NOT_STARTED, | |
| 104 // The loader is busy loading the model from |model_path_| in the | |
| 105 // background. This state can transition to FINISHED if the loaded model is | |
| 106 // compatible and up to date; otherwise, this state can transition to IDLE. | |
| 107 LOADING_FROM_FILE, | |
| 108 // The loader is not currently busy. The loader can transition to the | |
| 109 // LOADING_FROM_URL_ state if |model_url_| is valid; the loader can also | |
| 110 // transition to FINISHED if it the maximum number of download attempts | |
| 111 // has been reached. | |
| 112 IDLE, | |
| 113 // The loader is busy loading the model from |model_url_| in the background. | |
| 114 // This state can transition to FINISHED if the loaded model is valid; | |
| 115 // otherwise, this state can re-transition to IDLE. | |
| 116 LOADING_FROM_URL, | |
| 117 // The loader has finished. This is the terminal state. | |
| 118 FINISHED | |
| 119 }; | |
| 120 | |
| 121 // Asynchronously initiates loading the model from model_path_; | |
| 122 void StartLoadFromFile(); | |
| 123 | |
| 124 // Called when the background worker has finished loading |data| from | |
| 125 // |model_path_|. If |data| is empty, the load from |model_path_| failed. | |
| 126 void OnFileLoaded(const std::string& data); | |
| 127 | |
| 128 // Asynchronously initiates loading the model from |model_url_|. | |
| 129 void StartLoadFromURL(); | |
| 130 | |
| 131 // Called when |url_fetcher_| has finished loading |data| from |model_url_|. | |
| 132 // | |
| 133 // This call signature is mandated by TranslateURLFetcher. | |
| 134 // | |
| 135 // id - the id given to the TranslateURLFetcher on creation | |
| 136 // success - true of the download was successful | |
| 137 // data - the body of the downloads response | |
| 138 void OnURLFetched(int id, bool success, const std::string& data); | |
| 139 | |
| 140 // Parse |data| and return a validated model. Returns nullptr on failure. | |
| 141 std::unique_ptr<chrome_intelligence::RankerModel> CreateAndValidateModel( | |
| 142 const std::string& data); | |
| 143 | |
| 144 // Helper function to log |model_status| to UMA and return it. | |
| 145 RankerModelStatus ReportModelStatus(RankerModelStatus model_status); | |
| 146 | |
| 147 // Validates that ranker model loader tasks are all performed on the same | |
| 148 // sequence. | |
| 149 base::SequenceChecker sequence_checker_; | |
| 150 | |
| 151 // The task runner on which background tasks are performed. | |
| 152 const scoped_refptr<base::SequencedTaskRunner> background_task_runner_; | |
| 153 | |
| 154 // Validates a ranker model on behalf of the model loader client. This will be | |
| 155 // called on the sequence on which the model leader was constructed. | |
| 156 const ValidateModelCallback validate_model_cb_; | |
| 157 | |
| 158 // Transfers ownership of a loaded model back to the model loader client. | |
| 159 // This will be called on the sequence on which the model loader was | |
| 160 // constructed. | |
| 161 const OnModelAvailableCallback on_model_available_cb_; | |
| 162 | |
| 163 // The path at which the model is (or should be) cached. | |
| 164 const base::FilePath model_path_; | |
| 165 | |
| 166 // The URL from which to download the model if the model is not in the cache | |
| 167 // or the cached model is invalid/expired. | |
| 168 const GURL model_url_; | |
| 169 | |
| 170 // This will prefix all UMA metrics generated by the model loader. | |
| 171 const std::string uma_prefix_; | |
| 172 | |
| 173 // Used to download model data from |model_url_|. | |
| 174 std::unique_ptr<TranslateURLFetcher> url_fetcher_; | |
| 175 | |
| 176 // The next time before which no new attempts to download the model should be | |
| 177 // attempted. | |
| 178 base::TimeTicks next_earliest_download_time_; | |
| 179 | |
| 180 // Tracks the last time of the last attempt to load a model, either from file | |
| 181 // of from URL. Used for UMA reporting of load durations. | |
| 182 base::TimeTicks load_start_time_; | |
| 183 | |
| 184 // The current state of the loader. | |
| 185 LoaderState state_ = LoaderState::NOT_STARTED; | |
| 186 | |
| 187 // Creates weak pointer references to the loader. | |
| 188 base::WeakPtrFactory<RankerModelLoader> weak_ptr_factory_; | |
| 189 | |
| 190 DISALLOW_COPY_AND_ASSIGN(RankerModelLoader); | |
| 191 }; | |
| 192 | |
| 193 } // namespace translate | |
| 194 | |
| 195 #endif // COMPONENTS_TRANSLATE_CORE_BROWSER_RANKER_MODEL_LOADER_H_ | |
| OLD | NEW |