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

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

Issue 2925733002: Move ranker_model_loader to a new component. (Closed)
Patch Set: Adressing sdefresne's comments. Created 3 years, 5 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_MACHINE_INTELLIGENCE_RANKER_MODEL_LOADER_H_
6 #define COMPONENTS_TRANSLATE_CORE_BROWSER_RANKER_MODEL_LOADER_H_ 6 #define COMPONENTS_MACHINE_INTELLIGENCE_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 "net/url_request/url_request_context_getter.h"
17 #include "url/gurl.h" 18 #include "url/gurl.h"
18 19
19 namespace base { 20 namespace base {
20 class SequencedTaskRunner; 21 class SequencedTaskRunner;
21 } // namespace base 22 } // namespace base
22 23
23 namespace chrome_intelligence { 24 namespace machine_intelligence {
25
26 class RankerURLFetcher;
24 class RankerModel; 27 class RankerModel;
25 } // namespace chrome_intelligence
26
27 namespace translate {
28
29 class TranslateURLFetcher;
30 28
31 // Enumeration denoting the outcome of an attempt to download the model. This 29 // 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 30 // must be kept in sync with the RankerModelStatus enum in histograms.xml
33 enum class RankerModelStatus { 31 enum class RankerModelStatus {
34 OK = 0, 32 OK = 0,
35 DOWNLOAD_THROTTLED = 1, 33 DOWNLOAD_THROTTLED = 1,
36 DOWNLOAD_FAILED = 2, 34 DOWNLOAD_FAILED = 2,
37 PARSE_FAILED = 3, 35 PARSE_FAILED = 3,
38 VALIDATION_FAILED = 4, 36 VALIDATION_FAILED = 4,
39 INCOMPATIBLE = 5, 37 INCOMPATIBLE = 5,
40 LOAD_FROM_CACHE_FAILED = 6, 38 LOAD_FROM_CACHE_FAILED = 6,
41 MODEL_LOADING_ABANDONED = 7, 39 MODEL_LOADING_ABANDONED = 7,
42 40
43 // Insert new values above this line. 41 // Insert new values above this line.
44 MAX 42 MAX
45 }; 43 };
46 44
47 // If enabled, downloads a translate ranker model and uses it to determine 45 // Loads a ranker model. Will attempt to load the model from disk cache. If it
48 // whether the user should be given a translation prompt or not. 46 // fails, will attempt to download from the given URL.
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 = base::RepeatingCallback<RankerModelStatus(
56 const chrome_intelligence::RankerModel&)>; 54 const machine_intelligence::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 = base::RepeatingCallback<void(
62 std::unique_ptr<chrome_intelligence::RankerModel>)>; 60 std::unique_ptr<machine_intelligence::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
73 // a fresh model from |model_url| the model will be persisted to |model_path| 71 // a fresh model from |model_url| the model will be persisted to |model_path|
74 // for subsequent caching. 72 // for subsequent caching.
75 // 73 //
76 // |model_url| denotes the URL from which the model should be loaded, if it 74 // |model_url| denotes the URL from which the model should be loaded, if it
77 // has not already been cached at |model_path|. 75 // has not already been cached at |model_path|.
78 // 76 //
79 // |uma_prefix| will be used as a prefix for the names of all UMA metrics 77 // |uma_prefix| will be used as a prefix for the names of all UMA metrics
80 // generated by this loader. 78 // generated by this loader.
81 RankerModelLoader(ValidateModelCallback validate_model_callback, 79 RankerModelLoader(ValidateModelCallback validate_model_callback,
82 OnModelAvailableCallback on_model_available_callback, 80 OnModelAvailableCallback on_model_available_callback,
81 net::URLRequestContextGetter* request_context_getter,
83 base::FilePath model_path, 82 base::FilePath model_path,
84 GURL model_url, 83 GURL model_url,
85 std::string uma_prefix); 84 std::string uma_prefix);
86 85
87 ~RankerModelLoader(); 86 ~RankerModelLoader();
88 87
89 // Call this method periodically to notify the model loader the ranker is 88 // 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 89 // 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 90 // 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 91 // is pending, this will trigger (subject to retry and frequency limits) a
(...skipping 30 matching lines...) Expand all
123 122
124 // Called when the background worker has finished loading |data| from 123 // Called when the background worker has finished loading |data| from
125 // |model_path_|. If |data| is empty, the load from |model_path_| failed. 124 // |model_path_|. If |data| is empty, the load from |model_path_| failed.
126 void OnFileLoaded(const std::string& data); 125 void OnFileLoaded(const std::string& data);
127 126
128 // Asynchronously initiates loading the model from |model_url_|. 127 // Asynchronously initiates loading the model from |model_url_|.
129 void StartLoadFromURL(); 128 void StartLoadFromURL();
130 129
131 // Called when |url_fetcher_| has finished loading |data| from |model_url_|. 130 // Called when |url_fetcher_| has finished loading |data| from |model_url_|.
132 // 131 //
133 // This call signature is mandated by TranslateURLFetcher. 132 // This call signature is mandated by RankerURLFetcher.
134 // 133 //
135 // id - the id given to the TranslateURLFetcher on creation
136 // success - true of the download was successful 134 // success - true of the download was successful
137 // data - the body of the downloads response 135 // data - the body of the downloads response
138 void OnURLFetched(int id, bool success, const std::string& data); 136 void OnURLFetched(bool success, const std::string& data);
139 137
140 // Parse |data| and return a validated model. Returns nullptr on failure. 138 // Parse |data| and return a validated model. Returns nullptr on failure.
141 std::unique_ptr<chrome_intelligence::RankerModel> CreateAndValidateModel( 139 std::unique_ptr<machine_intelligence::RankerModel> CreateAndValidateModel(
142 const std::string& data); 140 const std::string& data);
143 141
144 // Helper function to log |model_status| to UMA and return it. 142 // Helper function to log |model_status| to UMA and return it.
145 RankerModelStatus ReportModelStatus(RankerModelStatus model_status); 143 RankerModelStatus ReportModelStatus(RankerModelStatus model_status);
146 144
147 // Validates that ranker model loader tasks are all performed on the same 145 // Validates that ranker model loader tasks are all performed on the same
148 // sequence. 146 // sequence.
149 base::SequenceChecker sequence_checker_; 147 SEQUENCE_CHECKER(sequence_checker_);
150 148
151 // The task runner on which background tasks are performed. 149 // The task runner on which background tasks are performed.
152 const scoped_refptr<base::SequencedTaskRunner> background_task_runner_; 150 const scoped_refptr<base::SequencedTaskRunner> background_task_runner_;
153 151
154 // Validates a ranker model on behalf of the model loader client. This will be 152 // 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. 153 // called on the sequence on which the model leader was constructed.
156 const ValidateModelCallback validate_model_cb_; 154 const ValidateModelCallback validate_model_cb_;
157 155
158 // Transfers ownership of a loaded model back to the model loader client. 156 // 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 157 // This will be called on the sequence on which the model loader was
160 // constructed. 158 // constructed.
161 const OnModelAvailableCallback on_model_available_cb_; 159 const OnModelAvailableCallback on_model_available_cb_;
162 160
161 // Request Context Getter used for RankerURLFetcher.
162 scoped_refptr<net::URLRequestContextGetter> request_context_getter_;
163
163 // The path at which the model is (or should be) cached. 164 // The path at which the model is (or should be) cached.
164 const base::FilePath model_path_; 165 const base::FilePath model_path_;
165 166
166 // The URL from which to download the model if the model is not in the cache 167 // 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 // or the cached model is invalid/expired.
168 const GURL model_url_; 169 const GURL model_url_;
169 170
170 // This will prefix all UMA metrics generated by the model loader. 171 // This will prefix all UMA metrics generated by the model loader.
171 const std::string uma_prefix_; 172 const std::string uma_prefix_;
172 173
173 // Used to download model data from |model_url_|. 174 // Used to download model data from |model_url_|.
174 std::unique_ptr<TranslateURLFetcher> url_fetcher_; 175 std::unique_ptr<RankerURLFetcher> url_fetcher_;
175 176
176 // The next time before which no new attempts to download the model should be 177 // The next time before which no new attempts to download the model should be
177 // attempted. 178 // attempted.
178 base::TimeTicks next_earliest_download_time_; 179 base::TimeTicks next_earliest_download_time_;
179 180
180 // Tracks the last time of the last attempt to load a model, either from file 181 // 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 // of from URL. Used for UMA reporting of load durations.
182 base::TimeTicks load_start_time_; 183 base::TimeTicks load_start_time_;
183 184
184 // The current state of the loader. 185 // The current state of the loader.
185 LoaderState state_ = LoaderState::NOT_STARTED; 186 LoaderState state_ = LoaderState::NOT_STARTED;
186 187
187 // Creates weak pointer references to the loader. 188 // Creates weak pointer references to the loader.
188 base::WeakPtrFactory<RankerModelLoader> weak_ptr_factory_; 189 base::WeakPtrFactory<RankerModelLoader> weak_ptr_factory_;
189 190
190 DISALLOW_COPY_AND_ASSIGN(RankerModelLoader); 191 DISALLOW_COPY_AND_ASSIGN(RankerModelLoader);
191 }; 192 };
192 193
193 } // namespace translate 194 } // namespace machine_intelligence
194 195
195 #endif // COMPONENTS_TRANSLATE_CORE_BROWSER_RANKER_MODEL_LOADER_H_ 196 #endif // COMPONENTS_MACHINE_INTELLIGENCE_RANKER_MODEL_LOADER_H_
OLDNEW
« no previous file with comments | « components/machine_intelligence/ranker_model.cc ('k') | components/machine_intelligence/ranker_model_loader.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698