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/sequence_checker.h" | |
14 #include "base/threading/thread.h" | |
15 #include "url/gurl.h" | |
16 | |
17 namespace chrome_intelligence { | |
18 class RankerModel; | |
19 } // namespace chrome_intelligence | |
20 | |
21 namespace translate { | |
22 | |
23 // Enumeration denoting the outcome of an attempt to download the model. This | |
24 // must be kept in sync with the TranslateRankerModelStatus enum in | |
25 // histograms.xml | |
26 // TODO(rogerm): rename the enum in histograms.xml to be more generic | |
groby-ooo-7-16
2017/02/14 20:56:48
Can you do that as part of this CL?
Roger McFarlane (Chromium)
2017/02/21 22:16:51
Done.
| |
27 enum class RankerModelStatus : int { | |
28 OK = 0, | |
29 DOWNLOAD_THROTTLED = 1, | |
30 DOWNLOAD_FAILED = 2, | |
31 PARSE_FAILED = 3, | |
32 VALIDATION_FAILED = 4, | |
33 INCOMPATIBLE = 5, | |
34 | |
35 // Insert new values above this line. | |
36 MAX | |
37 }; | |
38 | |
39 // If enabled, downloads a translate ranker model and uses it to determine | |
40 // whether the user should be given a translation prompt or not. | |
41 class RankerModelLoader { | |
42 public: | |
43 // Callback to validate a ranker model on behalf of the model loader client. | |
44 // For example, the callback might validate that the model is compatible with | |
45 // the features generated when ranking translation offerings. This callback | |
46 // may be called on any sequence and must, therefore, be thread-safe. | |
fdoray
2017/02/15 20:07:40
+ // This callback may be invoked after RankerMode
Roger McFarlane (Chromium)
2017/02/21 22:16:50
Done.
| |
47 using ValidateCallback = base::Callback<RankerModelStatus( | |
hamelphi
2017/02/13 19:18:58
s\ValidateCallback\ValidateModelCallback ?
I thin
Roger McFarlane (Chromium)
2017/02/21 22:16:50
Done.
| |
48 const chrome_intelligence::RankerModel& model)>; | |
49 | |
50 // Called to transfer ownership of a loaded model back to the model loader | |
51 // client. This will be called on the sequence on which the model loader was | |
groby-ooo-7-16
2017/02/14 20:56:48
Since we're having a set of callbacks here, does i
fdoray
2017/02/15 20:07:40
Since ValidateCallback can be invoked after the Ra
Roger McFarlane (Chromium)
2017/02/21 22:16:50
Acknowledged.
Roger McFarlane (Chromium)
2017/02/21 22:16:51
fdoray@ suggested to revert to callbacks (instead
| |
52 // constructed. | |
53 using OnModelAvailableCallback = base::Callback<void( | |
54 std::unique_ptr<chrome_intelligence::RankerModel> model)>; | |
55 | |
56 // |valildate_callback| may be called on any sequence; it must, therefore, | |
groby-ooo-7-16
2017/02/14 20:56:48
s/valildate/validate
Roger McFarlane (Chromium)
2017/02/21 22:16:50
Done.
| |
57 // be thread-safe. | |
58 // | |
59 // |on_model_available_callback| will be called on the sequence on which the | |
60 // ranker model loader is constructed. | |
61 // | |
62 // |model_path| denotes the file path | |
groby-ooo-7-16
2017/02/14 20:56:48
nit: end with period.
More importantly: This prob
Roger McFarlane (Chromium)
2017/02/21 22:16:50
Done.
| |
63 // | |
64 // |uma_prefix| will be used to start all UMA metrics genereted by this | |
hamelphi
2017/02/13 19:18:58
generated
hamelphi
2017/02/13 19:18:58
Nit.
I was confused a bit by "used to start..."
Ma
Roger McFarlane (Chromium)
2017/02/21 22:16:50
Done.
Roger McFarlane (Chromium)
2017/02/21 22:16:51
Done.
| |
65 // loader. | |
66 RankerModelLoader(const ValidateCallback& validate_callback, | |
67 const OnModelAvailableCallback& on_model_available_callback, | |
68 const base::FilePath& model_path, | |
69 const GURL& model_url, | |
70 const std::string& uma_prefix); | |
71 | |
72 ~RankerModelLoader(); | |
73 | |
74 // Asynchronously initiates loading the model from the cache file path and URL | |
75 // previously configured. | |
groby-ooo-7-16
2017/02/14 20:56:48
Why not pass path and URL here, then?
Roger McFarlane (Chromium)
2017/02/21 22:16:50
The loader is stateful. Passing the path and url h
| |
76 void Start(); | |
77 | |
78 // Call this method periodically to notify the model loader the network might | |
79 // be available. If a model download is pending, this will trigger (subject to | |
groby-ooo-7-16
2017/02/14 20:56:48
That's probably not the right name if we call this
Roger McFarlane (Chromium)
2017/02/21 22:16:50
Renamed to NotifyOfRankerActivity
| |
80 // retry and frequency limits) a download attempt. | |
81 void NotifyOfNetworkAvailability(); | |
82 | |
83 private: | |
84 // The model loader backend to which the actual loading functionality is | |
85 // delegated. | |
86 class Backend; | |
groby-ooo-7-16
2017/02/14 20:56:48
Why do we have a separate backend?
fdoray
2017/02/15 20:07:40
The Backend performs asynchronous operations and c
Roger McFarlane (Chromium)
2017/02/21 22:16:50
Done.
Roger McFarlane (Chromium)
2017/02/21 22:16:51
The threading and task-scheduling folks recommende
| |
87 | |
88 // The task runner on which backend tasks are performed. | |
89 const scoped_refptr<base::SequencedTaskRunner> backend_task_runner_; | |
90 | |
91 // The model loader backend to which the actual loading functionality is | |
92 // delegated. | |
fdoray
2017/02/15 20:07:40
+ // When the RankerModelLoader is destroyed, owne
Roger McFarlane (Chromium)
2017/02/21 22:16:50
Done.
| |
93 std::unique_ptr<Backend> backend_; | |
94 | |
95 DISALLOW_COPY_AND_ASSIGN(RankerModelLoader); | |
96 }; | |
97 | |
98 } // namespace translate | |
99 | |
100 #endif // COMPONENTS_TRANSLATE_CORE_BROWSER_RANKER_MODEL_LOADER_H_ | |
OLD | NEW |