Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(87)

Side by Side Diff: components/ranker/ranker_model_loader.h

Issue 2925733002: Move ranker_model_loader to a new component. (Closed)
Patch Set: Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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_RANKER_RANKER_MODEL_LOADER_H_
6 #define COMPONENTS_TRANSLATE_CORE_BROWSER_RANKER_MODEL_LOADER_H_ 6 #define COMPONENTS_RANKER_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/files/file_path.h"
13 #include "base/memory/ref_counted.h" 13 #include "base/memory/ref_counted.h"
14 #include "base/memory/weak_ptr.h" 14 #include "base/memory/weak_ptr.h"
15 #include "base/sequence_checker.h" 15 #include "base/sequence_checker.h"
16 #include "base/time/time.h" 16 #include "base/time/time.h"
17 #include "url/gurl.h" 17 #include "url/gurl.h"
18 18
19 namespace base { 19 namespace base {
20 class SequencedTaskRunner; 20 class SequencedTaskRunner;
21 } // namespace base 21 } // namespace base
22 22
23 namespace chrome_intelligence { 23 namespace ranker {
24
25 class RankerURLFetcher;
24 class RankerModel; 26 class RankerModel;
25 } // namespace chrome_intelligence
26
27 namespace translate {
28
29 class TranslateURLFetcher;
30 27
31 // Enumeration denoting the outcome of an attempt to download the model. This 28 // 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 29 // must be kept in sync with the RankerModelStatus enum in histograms.xml
33 enum class RankerModelStatus { 30 enum class RankerModelStatus {
34 OK = 0, 31 OK = 0,
35 DOWNLOAD_THROTTLED = 1, 32 DOWNLOAD_THROTTLED = 1,
36 DOWNLOAD_FAILED = 2, 33 DOWNLOAD_FAILED = 2,
37 PARSE_FAILED = 3, 34 PARSE_FAILED = 3,
38 VALIDATION_FAILED = 4, 35 VALIDATION_FAILED = 4,
39 INCOMPATIBLE = 5, 36 INCOMPATIBLE = 5,
40 LOAD_FROM_CACHE_FAILED = 6, 37 LOAD_FROM_CACHE_FAILED = 6,
41 MODEL_LOADING_ABANDONED = 7, 38 MODEL_LOADING_ABANDONED = 7,
42 39
43 // Insert new values above this line. 40 // Insert new values above this line.
44 MAX 41 MAX
45 }; 42 };
46 43
47 // If enabled, downloads a translate ranker model and uses it to determine 44 // FIXME update comments.
48 // whether the user should be given a translation prompt or not. 45
46 // If enabled, downloads a ranker model.
49 class RankerModelLoader { 47 class RankerModelLoader {
50 public: 48 public:
51 // Callback to validate a ranker model on behalf of the model loader client. 49 // 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 50 // For example, the callback might validate that the model is compatible with
53 // the features generated when ranking translation offerings. This will be 51 // the features generated when ranking translation offerings. This will be
54 // called on the sequence on which the model loader was constructed. 52 // called on the sequence on which the model loader was constructed.
55 using ValidateModelCallback = base::RepeatingCallback<RankerModelStatus( 53 using ValidateModelCallback =
56 const chrome_intelligence::RankerModel&)>; 54 base::RepeatingCallback<RankerModelStatus(const ranker::RankerModel&)>;
57 55
58 // Called to transfer ownership of a loaded model back to the model loader 56 // 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 57 // client. This will be called on the sequence on which the model loader was
60 // constructed. 58 // constructed.
61 using OnModelAvailableCallback = base::RepeatingCallback<void( 59 using OnModelAvailableCallback =
62 std::unique_ptr<chrome_intelligence::RankerModel>)>; 60 base::RepeatingCallback<void(std::unique_ptr<ranker::RankerModel>)>;
63 61
64 // |validate_model_callback| may be called on any sequence; it must be thread 62 // |validate_model_callback| may be called on any sequence; it must be thread
65 // safe. 63 // safe.
66 // 64 //
67 // |on_model_available_callback| will be called on the sequence on which the 65 // |on_model_available_callback| will be called on the sequence on which the
68 // ranker model loader is constructed. 66 // ranker model loader is constructed.
69 // 67 //
70 // |model_path| denotes the file path at which the model is cached. The loader 68 // |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 69 // 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 70 // |model_url| if the model cannot be loaded or has expired. Upon downloading
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
123 121
124 // Called when the background worker has finished loading |data| from 122 // Called when the background worker has finished loading |data| from
125 // |model_path_|. If |data| is empty, the load from |model_path_| failed. 123 // |model_path_|. If |data| is empty, the load from |model_path_| failed.
126 void OnFileLoaded(const std::string& data); 124 void OnFileLoaded(const std::string& data);
127 125
128 // Asynchronously initiates loading the model from |model_url_|. 126 // Asynchronously initiates loading the model from |model_url_|.
129 void StartLoadFromURL(); 127 void StartLoadFromURL();
130 128
131 // Called when |url_fetcher_| has finished loading |data| from |model_url_|. 129 // Called when |url_fetcher_| has finished loading |data| from |model_url_|.
132 // 130 //
133 // This call signature is mandated by TranslateURLFetcher. 131 // This call signature is mandated by RankerURLFetcher.
134 // 132 //
135 // id - the id given to the TranslateURLFetcher on creation
136 // success - true of the download was successful 133 // success - true of the download was successful
137 // data - the body of the downloads response 134 // data - the body of the downloads response
138 void OnURLFetched(int id, bool success, const std::string& data); 135 void OnURLFetched(bool success, const std::string& data);
139 136
140 // Parse |data| and return a validated model. Returns nullptr on failure. 137 // Parse |data| and return a validated model. Returns nullptr on failure.
141 std::unique_ptr<chrome_intelligence::RankerModel> CreateAndValidateModel( 138 std::unique_ptr<ranker::RankerModel> CreateAndValidateModel(
142 const std::string& data); 139 const std::string& data);
143 140
144 // Helper function to log |model_status| to UMA and return it. 141 // Helper function to log |model_status| to UMA and return it.
145 RankerModelStatus ReportModelStatus(RankerModelStatus model_status); 142 RankerModelStatus ReportModelStatus(RankerModelStatus model_status);
146 143
147 // Validates that ranker model loader tasks are all performed on the same 144 // Validates that ranker model loader tasks are all performed on the same
148 // sequence. 145 // sequence.
149 base::SequenceChecker sequence_checker_; 146 base::SequenceChecker sequence_checker_;
150 147
151 // The task runner on which background tasks are performed. 148 // The task runner on which background tasks are performed.
(...skipping 12 matching lines...) Expand all
164 const base::FilePath model_path_; 161 const base::FilePath model_path_;
165 162
166 // The URL from which to download the model if the model is not in the cache 163 // The URL from which to download the model if the model is not in the cache
167 // or the cached model is invalid/expired. 164 // or the cached model is invalid/expired.
168 const GURL model_url_; 165 const GURL model_url_;
169 166
170 // This will prefix all UMA metrics generated by the model loader. 167 // This will prefix all UMA metrics generated by the model loader.
171 const std::string uma_prefix_; 168 const std::string uma_prefix_;
172 169
173 // Used to download model data from |model_url_|. 170 // Used to download model data from |model_url_|.
174 std::unique_ptr<TranslateURLFetcher> url_fetcher_; 171 std::unique_ptr<RankerURLFetcher> url_fetcher_;
175 172
176 // The next time before which no new attempts to download the model should be 173 // The next time before which no new attempts to download the model should be
177 // attempted. 174 // attempted.
178 base::TimeTicks next_earliest_download_time_; 175 base::TimeTicks next_earliest_download_time_;
179 176
180 // Tracks the last time of the last attempt to load a model, either from file 177 // 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. 178 // of from URL. Used for UMA reporting of load durations.
182 base::TimeTicks load_start_time_; 179 base::TimeTicks load_start_time_;
183 180
184 // The current state of the loader. 181 // The current state of the loader.
185 LoaderState state_ = LoaderState::NOT_STARTED; 182 LoaderState state_ = LoaderState::NOT_STARTED;
186 183
187 // Creates weak pointer references to the loader. 184 // Creates weak pointer references to the loader.
188 base::WeakPtrFactory<RankerModelLoader> weak_ptr_factory_; 185 base::WeakPtrFactory<RankerModelLoader> weak_ptr_factory_;
189 186
190 DISALLOW_COPY_AND_ASSIGN(RankerModelLoader); 187 DISALLOW_COPY_AND_ASSIGN(RankerModelLoader);
191 }; 188 };
192 189
193 } // namespace translate 190 } // namespace ranker
194 191
195 #endif // COMPONENTS_TRANSLATE_CORE_BROWSER_RANKER_MODEL_LOADER_H_ 192 #endif // COMPONENTS_RANKER_RANKER_MODEL_LOADER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698