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/callback.h" | 11 #include "base/callback.h" |
| 12 #include "base/files/file_path.h" | |
| 12 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
| 13 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
| 14 #include "base/sequence_checker.h" | 15 #include "base/sequence_checker.h" |
| 15 | 16 #include "base/time/time.h" |
| 16 class GURL; | 17 #include "url/gurl.h" |
| 17 | 18 |
| 18 namespace base { | 19 namespace base { |
| 19 class FilePath; | |
| 20 class SequencedTaskRunner; | 20 class SequencedTaskRunner; |
| 21 } // namespace base | 21 } // namespace base |
| 22 | 22 |
| 23 namespace chrome_intelligence { | 23 namespace chrome_intelligence { |
| 24 class RankerModel; | 24 class RankerModel; |
| 25 } // namespace chrome_intelligence | 25 } // namespace chrome_intelligence |
| 26 | 26 |
| 27 namespace translate { | 27 namespace translate { |
| 28 | 28 |
| 29 class TranslateURLFetcher; | |
| 30 | |
| 29 // Enumeration denoting the outcome of an attempt to download the model. This | 31 // 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 | 32 // must be kept in sync with the RankerModelStatus enum in histograms.xml |
| 31 enum class RankerModelStatus { | 33 enum class RankerModelStatus { |
| 32 OK = 0, | 34 OK = 0, |
| 33 DOWNLOAD_THROTTLED = 1, | 35 DOWNLOAD_THROTTLED = 1, |
| 34 DOWNLOAD_FAILED = 2, | 36 DOWNLOAD_FAILED = 2, |
| 35 PARSE_FAILED = 3, | 37 PARSE_FAILED = 3, |
| 36 VALIDATION_FAILED = 4, | 38 VALIDATION_FAILED = 4, |
| 37 INCOMPATIBLE = 5, | 39 INCOMPATIBLE = 5, |
| 40 LOAD_FROM_CACHE_FAILED = 6, | |
| 41 MODEL_LOADING_ABANDONED = 7, | |
| 38 | 42 |
| 39 // Insert new values above this line. | 43 // Insert new values above this line. |
| 40 MAX | 44 MAX |
| 41 }; | 45 }; |
| 42 | 46 |
| 43 // If enabled, downloads a translate ranker model and uses it to determine | 47 // If enabled, downloads a translate ranker model and uses it to determine |
| 44 // whether the user should be given a translation prompt or not. | 48 // whether the user should be given a translation prompt or not. |
| 45 class RankerModelLoader { | 49 class RankerModelLoader { |
| 46 public: | 50 public: |
| 47 // Callback to validate a ranker model on behalf of the model loader client. | 51 // 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 | 52 // For example, the callback might validate that the model is compatible with |
| 49 // the features generated when ranking translation offerings. This callback | 53 // the features generated when ranking translation offerings. This will be |
| 50 // may be called on any sequence and must, therefore, be thread-safe. This | 54 // called on the sequence on which the model loader was constructed. |
| 51 // callback may be invoked after the RankerModelLoader has been destroyed | 55 using ValidateModelCallback = base::RepeatingCallback<RankerModelStatus( |
| 52 // and must, therefore, only access memory that it can guarantee to be | 56 const chrome_intelligence::RankerModel&)>; |
| 53 // valid. | |
| 54 using ValidateModelCallback = base::Callback<RankerModelStatus( | |
| 55 const chrome_intelligence::RankerModel& model)>; | |
| 56 | 57 |
| 57 // Called to transfer ownership of a loaded model back to the model loader | 58 // 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 | 59 // 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 | 60 // constructed. |
| 60 // been destroyed and must, therefore, only access memory that it can | 61 using OnModelAvailableCallback = base::RepeatingCallback<void( |
| 61 // guarantee to be valid. | 62 std::unique_ptr<chrome_intelligence::RankerModel>)>; |
| 62 using OnModelAvailableCallback = base::Callback<void( | |
| 63 std::unique_ptr<chrome_intelligence::RankerModel> model)>; | |
| 64 | 63 |
| 65 // |validate_model_callback| may be called on any sequence; it must be thread | 64 // |validate_model_callback| may be called on any sequence; it must be thread |
| 66 // safe. | 65 // safe. |
| 67 // | 66 // |
| 68 // |on_model_available_callback| will be called on the sequence on which the | 67 // |on_model_available_callback| will be called on the sequence on which the |
| 69 // ranker model loader is constructed. | 68 // ranker model loader is constructed. |
| 70 // | 69 // |
| 71 // |model_path| denotes the file path at which the model is cached. The loader | 70 // |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 | 71 // 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 | 72 // |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| | 73 // a fresh model from |model_url| the model will be persisted to |model_path| |
| 75 // for subsequent caching. | 74 // for subsequent caching. |
| 76 // | 75 // |
| 77 // |model_url| denotes the URL from which the model should be loaded, if it | 76 // |model_url| denotes the URL from which the model should be loaded, if it |
| 78 // has not already been cached at |model_path|. | 77 // has not already been cached at |model_path|. |
| 79 // | 78 // |
| 80 // |uma_prefix| will be used as a prefix for the names of all UMA metrics | 79 // |uma_prefix| will be used as a prefix for the names of all UMA metrics |
| 81 // generated by this loader. | 80 // generated by this loader. |
| 82 RankerModelLoader(const ValidateModelCallback& validate_model_callback, | 81 RankerModelLoader(const ValidateModelCallback& validate_model_callback, |
| 83 const OnModelAvailableCallback& on_model_available_callback, | 82 const OnModelAvailableCallback& on_model_available_callback, |
| 84 const base::FilePath& model_path, | 83 const base::FilePath& model_path, |
| 85 const GURL& model_url, | 84 const GURL& model_url, |
| 86 const std::string& uma_prefix); | 85 const std::string& uma_prefix); |
| 87 | 86 |
| 88 ~RankerModelLoader(); | 87 ~RankerModelLoader(); |
| 89 | 88 |
| 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 | 89 // 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 | 90 // 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 | 91 // as a proxy for network availability and activity. If a model download |
| 97 // (subject to retry and frequency limits) a model download attempt. | 92 // is pending, this will trigger (subject to retry and frequency limits) a |
| 93 // model download attempt. | |
| 98 void NotifyOfRankerActivity(); | 94 void NotifyOfRankerActivity(); |
| 99 | 95 |
| 100 private: | 96 private: |
| 101 // The model loader backend to which the actual loading functionality is | 97 // A enum to track the current loader state. |
| 102 // delegated. | 98 enum class LoaderState { |
| 103 class Backend; | 99 // The loader is newly created and has not started trying to load the model. |
| 104 friend class Backend; | 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 | |
|
fdoray
2017/04/25 22:09:08
|model_path_| and |model_url_|
Roger McFarlane (Chromium)
2017/04/26 02:18:50
Done.
| |
| 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 background. | |
|
fdoray
2017/04/25 22:09:08
|model_path_|
Roger McFarlane (Chromium)
2017/04/26 02:18:50
Done.
| |
| 105 // This state can transition to FINISHED if the loaded model is compatible | |
| 106 // 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. | |
|
fdoray
2017/04/25 22:09:08
|model_url_|
Roger McFarlane (Chromium)
2017/04/26 02:18:50
Done.
| |
| 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 }; | |
| 105 | 120 |
| 106 // A enum to track the current loader state. | 121 // Asynchronously initiates loading the model from model_path_; |
| 107 enum class LoaderState { NOT_STARTED, RUNNING, FINISHED }; | 122 void StartLoadFromFile(); |
| 108 | 123 |
| 109 // Helper method to forward OnModelAvailable callbacks while noting whether | 124 // Called when the background worker has finished loading |data| from |
| 110 // or not the loader has finished its work. | 125 // |model_path_|. If |data| is empty, the load from |model_path_| failed. |
| 111 void InternalOnModelAvailable( | 126 void OnFileLoaded(const std::string& data); |
| 112 const OnModelAvailableCallback& callback, | 127 |
| 113 std::unique_ptr<chrome_intelligence::RankerModel> model, | 128 // Asynchronously initiates loading the model from model_url_; |
| 114 bool finished); | 129 void StartLoadFromURL(); |
| 130 | |
| 131 // Called when |url_fetcher_| has finised 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); | |
| 115 | 146 |
| 116 // Validates that ranker model loader tasks are all performed on the same | 147 // Validates that ranker model loader tasks are all performed on the same |
| 117 // sequence. | 148 // sequence. |
| 118 base::SequenceChecker sequence_checker_; | 149 base::SequenceChecker sequence_checker_; |
| 119 | 150 |
| 120 // The task runner on which backend tasks are performed. | 151 // The task runner on which background tasks are performed. |
| 121 const scoped_refptr<base::SequencedTaskRunner> backend_task_runner_; | 152 const scoped_refptr<base::SequencedTaskRunner> background_task_runner_; |
| 122 | 153 |
| 123 // The model loader backend to which the actual loading functionality is | 154 // Validates a ranker model on behalf of the model loader client. This may |
| 124 // delegated. |backend_| may outlive its RankerModelLoader. When the | 155 // be called on any sequence and must, therefore, be thread-safe. |
| 125 // RankerModelLoader is destroyed, ownership of |backend_| is transferred | 156 const ValidateModelCallback validate_model_cb_; |
| 126 // to a delete task posted |backend_task_runner_|. | 157 |
| 127 std::unique_ptr<Backend> backend_; | 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 form file | |
| 181 // of from URL. Used for UMA reporting of load duration. | |
| 182 base::TimeTicks load_start_time_; | |
| 128 | 183 |
| 129 // The current state of the loader. | 184 // The current state of the loader. |
| 130 LoaderState state_ = LoaderState::NOT_STARTED; | 185 LoaderState state_ = LoaderState::NOT_STARTED; |
| 131 | 186 |
| 132 base::WeakPtrFactory<RankerModelLoader> weak_ptr_factory_; | 187 base::WeakPtrFactory<RankerModelLoader> weak_ptr_factory_; |
| 133 | 188 |
| 134 DISALLOW_COPY_AND_ASSIGN(RankerModelLoader); | 189 DISALLOW_COPY_AND_ASSIGN(RankerModelLoader); |
| 135 }; | 190 }; |
| 136 | 191 |
| 137 } // namespace translate | 192 } // namespace translate |
| 138 | 193 |
| 139 #endif // COMPONENTS_TRANSLATE_CORE_BROWSER_RANKER_MODEL_LOADER_H_ | 194 #endif // COMPONENTS_TRANSLATE_CORE_BROWSER_RANKER_MODEL_LOADER_H_ |
| OLD | NEW |