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