OLD | NEW |
---|---|
(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/files/file_path.h" | |
12 #include "base/memory/ref_counted.h" | |
13 #include "base/memory/weak_ptr.h" | |
14 #include "base/sequence_checker.h" | |
15 #include "base/threading/thread.h" | |
fdoray
2017/02/23 16:41:25
Not needed
Roger McFarlane (Chromium)
2017/02/23 21:17:56
Done.
| |
16 #include "url/gurl.h" | |
17 | |
18 namespace chrome_intelligence { | |
19 class RankerModel; | |
20 } // namespace chrome_intelligence | |
21 | |
22 namespace translate { | |
23 | |
24 // Enumeration denoting the outcome of an attempt to download the model. This | |
25 // must be kept in sync with the RankerModelStatus enum in histograms.xml | |
26 enum class RankerModelStatus { | |
27 OK = 0, | |
28 DOWNLOAD_THROTTLED = 1, | |
29 DOWNLOAD_FAILED = 2, | |
30 PARSE_FAILED = 3, | |
31 VALIDATION_FAILED = 4, | |
32 INCOMPATIBLE = 5, | |
33 | |
34 // Insert new values above this line. | |
35 MAX | |
36 }; | |
37 | |
38 // If enabled, downloads a translate ranker model and uses it to determine | |
39 // whether the user should be given a translation prompt or not. | |
40 class RankerModelLoader { | |
41 public: | |
42 // Callback to validate a ranker model on behalf of the model loader client. | |
43 // For example, the callback might validate that the model is compatible with | |
44 // the features generated when ranking translation offerings. This callback | |
45 // may be called on any sequence and must, therefore, be thread-safe. | |
fdoray
2017/02/23 16:41:25
ping https://codereview.chromium.org/2565873002/di
Roger McFarlane (Chromium)
2017/02/23 21:17:56
Done.
| |
46 using ValidateModelCallback = base::Callback<RankerModelStatus( | |
47 const chrome_intelligence::RankerModel& model)>; | |
48 | |
49 // Called to transfer ownership of a loaded model back to the model loader | |
50 // client. This will be called on the sequence on which the model loader was | |
51 // constructed. | |
52 using OnModelAvailableCallback = base::Callback<void( | |
53 std::unique_ptr<chrome_intelligence::RankerModel> model)>; | |
fdoray
2017/02/23 16:41:25
// This callback may be invoked after the RankerMo
Roger McFarlane (Chromium)
2017/02/23 21:17:56
Done.
| |
54 | |
55 // |validate_model_callback| may be called on any sequence; it must be thread | |
56 // safe. | |
57 // | |
58 // |on_model_available_callback| will be called on the sequence on which the | |
59 // ranker model loader is constructed. | |
60 // | |
61 // |model_path| denotes the file path at which the model is cached. The loader | |
62 // will attempt to load the model from this path first, falling back to the | |
63 // |model_url| if the model cannot be loaded or has expired. Upon downloading | |
64 // a fresh model from |model_url| the model will be persisted to |model_path| | |
65 // for subsequent caching. | |
66 // | |
67 // |model_url| denotes the URL from which the model should be loaded, if it | |
68 // has not already been cached at |model_path|. | |
69 // | |
70 // |uma_prefix| will be used as a prefix for the names of all UMA metrics | |
71 // generated by this loader. | |
72 RankerModelLoader(const ValidateModelCallback& validate_model_callback, | |
73 const OnModelAvailableCallback& on_model_available_callback, | |
74 const base::FilePath& model_path, | |
75 const GURL& model_url, | |
76 const std::string& uma_prefix); | |
77 | |
78 ~RankerModelLoader(); | |
79 | |
80 // Asynchronously initiates loading the model from the cache file path and URL | |
81 // previously configured. | |
82 void Start(); | |
83 | |
84 // Call this method periodically to notify the model loader the ranker is | |
85 // actively in use. The user's engagement with the feature is used as a proxy | |
86 // for network activity. If a model download is pending, this will trigger | |
87 // (subject to retry and frequency limits) a model download attempt. | |
88 void NotifyOfRankerActivity(); | |
89 | |
90 private: | |
91 // The model loader backend to which the actual loading functionality is | |
92 // delegated. | |
93 class Backend; | |
94 friend class Backend; | |
95 | |
96 // A enum to track the current loader state. | |
97 enum class LoaderState { NOT_STARTED, RUNNING, FINISHED }; | |
98 | |
99 // Helper method to forward OnModelAvailable callbacks while noting whether | |
100 // or not the loader has finished its work. | |
101 void InternalOnModelAvailable( | |
102 const OnModelAvailableCallback& callback, | |
103 std::unique_ptr<chrome_intelligence::RankerModel> model, | |
104 bool finished); | |
105 | |
106 // Validates that ranker model loader tasks are all performed on the correct | |
fdoray
2017/02/23 16:41:25
on the *same* sequence?
Roger McFarlane (Chromium)
2017/02/23 21:17:57
Done.
| |
107 // sequence. | |
108 base::SequenceChecker sequence_checker_; | |
109 | |
110 // The task runner on which backend tasks are performed. | |
111 const scoped_refptr<base::SequencedTaskRunner> backend_task_runner_; | |
112 | |
113 // The model loader backend to which the actual loading functionality is | |
114 // delegated. |backend_| may outlive its RankerModelLoader. When the | |
115 // RankerModelLoader is destroyed, ownership of |backend_| is transferred | |
116 // to a delete task posted |backend_task_runner_|. | |
117 std::unique_ptr<Backend> backend_; | |
118 | |
119 // The current state of the loader. | |
120 LoaderState state_ = LoaderState::NOT_STARTED; | |
121 | |
122 base::WeakPtrFactory<RankerModelLoader> weak_ptr_factory_; | |
123 | |
124 DISALLOW_COPY_AND_ASSIGN(RankerModelLoader); | |
125 }; | |
126 | |
127 } // namespace translate | |
128 | |
129 #endif // COMPONENTS_TRANSLATE_CORE_BROWSER_RANKER_MODEL_LOADER_H_ | |
OLD | NEW |