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/translate/core/browser/ranker_model_loader.h

Issue 2565873002: [translate] Add translate ranker model loader. (Closed)
Patch Set: comments from sdefresne Created 3 years, 9 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
(Empty)
1 // Copyright 2016 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/memory/ref_counted.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/sequence_checker.h"
15
16 class GURL;
17
18 namespace base {
19 class FilePath;
20 class SequencedTaskRunner;
21 } // namespace base
22
23 namespace chrome_intelligence {
24 class RankerModel;
25 } // namespace chrome_intelligence
26
27 namespace translate {
28
29 // 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
31 enum class RankerModelStatus {
32 OK = 0,
33 DOWNLOAD_THROTTLED = 1,
34 DOWNLOAD_FAILED = 2,
35 PARSE_FAILED = 3,
36 VALIDATION_FAILED = 4,
37 INCOMPATIBLE = 5,
38
39 // Insert new values above this line.
40 MAX
41 };
42
43 // If enabled, downloads a translate ranker model and uses it to determine
44 // whether the user should be given a translation prompt or not.
45 class RankerModelLoader {
46 public:
47 // 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
49 // the features generated when ranking translation offerings. This callback
50 // may be called on any sequence and must, therefore, be thread-safe. This
51 // callback may be invoked after the RankerModelLoader has been destroyed
52 // and must, therefore, only access memory that it can guarantee to be
53 // valid.
54 using ValidateModelCallback = base::Callback<RankerModelStatus(
55 const chrome_intelligence::RankerModel& model)>;
56
57 // 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 // constructed. This callback may be invoked after the RankerModelLoader has
60 // been destroyed and must, therefore, only access memory that it can
61 // guarantee to be valid.
62 using OnModelAvailableCallback = base::Callback<void(
63 std::unique_ptr<chrome_intelligence::RankerModel> model)>;
64
65 // |validate_model_callback| may be called on any sequence; it must be thread
66 // safe.
67 //
68 // |on_model_available_callback| will be called on the sequence on which the
69 // ranker model loader is constructed.
70 //
71 // |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
73 // |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|
75 // for subsequent caching.
76 //
77 // |model_url| denotes the URL from which the model should be loaded, if it
78 // has not already been cached at |model_path|.
79 //
80 // |uma_prefix| will be used as a prefix for the names of all UMA metrics
81 // generated by this loader.
82 RankerModelLoader(const ValidateModelCallback& validate_model_callback,
83 const OnModelAvailableCallback& on_model_available_callback,
84 const base::FilePath& model_path,
85 const GURL& model_url,
86 const std::string& uma_prefix);
87
88 ~RankerModelLoader();
89
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
95 // actively in use. The user's engagement with the feature is used as a proxy
96 // for network activity. If a model download is pending, this will trigger
97 // (subject to retry and frequency limits) a model download attempt.
98 void NotifyOfRankerActivity();
99
100 private:
101 // The model loader backend to which the actual loading functionality is
102 // delegated.
103 class Backend;
104 friend class Backend;
105
106 // A enum to track the current loader state.
107 enum class LoaderState { NOT_STARTED, RUNNING, FINISHED };
108
109 // Helper method to forward OnModelAvailable callbacks while noting whether
110 // or not the loader has finished its work.
111 void InternalOnModelAvailable(
112 const OnModelAvailableCallback& callback,
113 std::unique_ptr<chrome_intelligence::RankerModel> model,
114 bool finished);
115
116 // Validates that ranker model loader tasks are all performed on the same
117 // sequence.
118 base::SequenceChecker sequence_checker_;
119
120 // The task runner on which backend tasks are performed.
121 const scoped_refptr<base::SequencedTaskRunner> backend_task_runner_;
122
123 // The model loader backend to which the actual loading functionality is
124 // delegated. |backend_| may outlive its RankerModelLoader. When the
125 // RankerModelLoader is destroyed, ownership of |backend_| is transferred
126 // to a delete task posted |backend_task_runner_|.
127 std::unique_ptr<Backend> backend_;
128
129 // The current state of the loader.
130 LoaderState state_ = LoaderState::NOT_STARTED;
131
132 base::WeakPtrFactory<RankerModelLoader> weak_ptr_factory_;
133
134 DISALLOW_COPY_AND_ASSIGN(RankerModelLoader);
135 };
136
137 } // namespace translate
138
139 #endif // COMPONENTS_TRANSLATE_CORE_BROWSER_RANKER_MODEL_LOADER_H_
OLDNEW
« no previous file with comments | « components/translate/core/browser/ranker_model.cc ('k') | components/translate/core/browser/ranker_model_loader.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698