Index: components/translate/core/browser/ranker_model_loader.h |
diff --git a/components/translate/core/browser/ranker_model_loader.h b/components/translate/core/browser/ranker_model_loader.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..f20e26573efb185b8ef1f8326e67dac9bb4cc03e |
--- /dev/null |
+++ b/components/translate/core/browser/ranker_model_loader.h |
@@ -0,0 +1,100 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#ifndef COMPONENTS_TRANSLATE_CORE_BROWSER_RANKER_MODEL_LOADER_H_ |
+#define COMPONENTS_TRANSLATE_CORE_BROWSER_RANKER_MODEL_LOADER_H_ |
+ |
+#include <memory> |
+#include <string> |
+ |
+#include "base/files/file_path.h" |
+#include "base/memory/ref_counted.h" |
+#include "base/sequence_checker.h" |
+#include "base/threading/thread.h" |
+#include "url/gurl.h" |
+ |
+namespace chrome_intelligence { |
+class RankerModel; |
+} // namespace chrome_intelligence |
+ |
+namespace translate { |
+ |
+// Enumeration denoting the outcome of an attempt to download the model. This |
+// must be kept in sync with the TranslateRankerModelStatus enum in |
+// histograms.xml |
+// 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.
|
+enum class RankerModelStatus : int { |
+ OK = 0, |
+ DOWNLOAD_THROTTLED = 1, |
+ DOWNLOAD_FAILED = 2, |
+ PARSE_FAILED = 3, |
+ VALIDATION_FAILED = 4, |
+ INCOMPATIBLE = 5, |
+ |
+ // Insert new values above this line. |
+ MAX |
+}; |
+ |
+// If enabled, downloads a translate ranker model and uses it to determine |
+// whether the user should be given a translation prompt or not. |
+class RankerModelLoader { |
+ public: |
+ // Callback to validate a ranker model on behalf of the model loader client. |
+ // For example, the callback might validate that the model is compatible with |
+ // the features generated when ranking translation offerings. This callback |
+ // 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.
|
+ 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.
|
+ const chrome_intelligence::RankerModel& model)>; |
+ |
+ // Called to transfer ownership of a loaded model back to the model loader |
+ // 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
|
+ // constructed. |
+ using OnModelAvailableCallback = base::Callback<void( |
+ std::unique_ptr<chrome_intelligence::RankerModel> model)>; |
+ |
+ // |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.
|
+ // be thread-safe. |
+ // |
+ // |on_model_available_callback| will be called on the sequence on which the |
+ // ranker model loader is constructed. |
+ // |
+ // |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.
|
+ // |
+ // |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.
|
+ // loader. |
+ RankerModelLoader(const ValidateCallback& validate_callback, |
+ const OnModelAvailableCallback& on_model_available_callback, |
+ const base::FilePath& model_path, |
+ const GURL& model_url, |
+ const std::string& uma_prefix); |
+ |
+ ~RankerModelLoader(); |
+ |
+ // Asynchronously initiates loading the model from the cache file path and URL |
+ // 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
|
+ void Start(); |
+ |
+ // Call this method periodically to notify the model loader the network might |
+ // 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
|
+ // retry and frequency limits) a download attempt. |
+ void NotifyOfNetworkAvailability(); |
+ |
+ private: |
+ // The model loader backend to which the actual loading functionality is |
+ // delegated. |
+ 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
|
+ |
+ // The task runner on which backend tasks are performed. |
+ const scoped_refptr<base::SequencedTaskRunner> backend_task_runner_; |
+ |
+ // The model loader backend to which the actual loading functionality is |
+ // delegated. |
fdoray
2017/02/15 20:07:40
+ // When the RankerModelLoader is destroyed, owne
Roger McFarlane (Chromium)
2017/02/21 22:16:50
Done.
|
+ std::unique_ptr<Backend> backend_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(RankerModelLoader); |
+}; |
+ |
+} // namespace translate |
+ |
+#endif // COMPONENTS_TRANSLATE_CORE_BROWSER_RANKER_MODEL_LOADER_H_ |