Chromium Code Reviews| 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 "components/translate/core/browser/proto/ranker_model.pb.h" | |
| 16 #include "url/gurl.h" | |
| 17 | |
| 18 namespace chrome_intelligence { | |
| 19 class RankerModel; | |
| 20 } // namespace chrome_intelligence | |
| 21 | |
| 22 namespace translate { | |
| 23 | |
| 24 class TranslateURLFetcher; | |
| 25 | |
| 26 // Enumeration denoting the outcome of an attempt to download the model. This | |
| 27 // must be kept in sync with the TranslateRankerModelStatus enum in | |
| 28 // histograms.xml | |
| 29 // TODO(rogerm): rename the enum in histograms.xml to be more generic | |
| 30 enum class RankerModelStatus { | |
| 31 OK = 0, | |
| 32 DOWNLOAD_THROTTLED = 1, | |
| 33 DOWNLOAD_FAILED = 2, | |
| 34 PARSE_FAILED = 3, | |
| 35 VALIDATION_FAILED = 4, | |
| 36 INCOMPATIBLE = 5, | |
| 37 | |
| 38 // Insert new values above this line. | |
| 39 MAX | |
| 40 }; | |
| 41 | |
| 42 // Interface exposed by a Model Loader's Observer. The observer must outlive | |
| 43 // the model loader. | |
| 44 class RankerModelObserver { | |
| 45 public: | |
| 46 virtual ~RankerModelObserver() = default; | |
| 47 | |
| 48 // The type of model the observer is willing/able to accept. Must be | |
| 49 // thread-safe. | |
| 50 virtual RankerModelStatus Validate( | |
| 51 const chrome_intelligence::RankerModel& model) const = 0; | |
| 52 | |
| 53 // Called when a model having the expected model type and compatibility level | |
| 54 // is loaded the model loader. This call will be executed by the task_runner | |
| 55 // used by this observer. | |
|
groby-ooo-7-16
2017/01/23 21:41:57
Wait.. isn't that called by the task_runner associ
Roger McFarlane (Chromium)
2017/02/08 23:08:09
Sorry, a previous iteration had a task_runner() ge
| |
| 56 virtual void OnModelAvailable( | |
| 57 std::unique_ptr<chrome_intelligence::RankerModel> model) = 0; | |
| 58 | |
| 59 protected: | |
| 60 RankerModelObserver() = default; | |
| 61 | |
| 62 private: | |
| 63 DISALLOW_COPY_AND_ASSIGN(RankerModelObserver); | |
| 64 }; | |
| 65 | |
| 66 // If enabled, downloads a translate ranker model and uses it to determine | |
| 67 // whether the user should be given a translation prompt or not. | |
| 68 class RankerModelLoader { | |
| 69 public: | |
| 70 RankerModelLoader(RankerModelObserver* observer, | |
| 71 const base::FilePath& model_path, | |
| 72 const GURL& model_url, | |
| 73 const std::string& uma_prefix); | |
| 74 | |
| 75 ~RankerModelLoader(); | |
| 76 | |
| 77 // Asynchronously initiates loading the model from the cache file path and URL | |
| 78 // previously configured. | |
| 79 void Start(); | |
| 80 | |
| 81 // Call this method periodically to notify the model loader the network might | |
| 82 // be available. If a model download is pending, this will trigger (subject to | |
| 83 // retry and frequency limits) a download attempt. | |
| 84 void NotifyOfNetworkAvailability(); | |
| 85 | |
| 86 private: | |
| 87 // The maximum number of model download attempts to make. Download may fail | |
| 88 // due to server error or network availability issues. | |
| 89 const int kMaxRetryOn5xx = 8; | |
| 90 | |
| 91 // The minimum duration, in minutes, between download attempts. | |
| 92 const int kDownloadRefractoryPeriodMin = 3; | |
| 93 | |
| 94 // Log the result of loading a model to UMA. | |
| 95 void ReportModelStatus(RankerModelStatus model_status); | |
| 96 | |
| 97 // Called to construct a model from the given |data|. | |
| 98 std::unique_ptr<chrome_intelligence::RankerModel> Parse( | |
| 99 const std::string& data); | |
| 100 | |
| 101 // Attempt to read the model from the path returns by T::GerModelPath() | |
| 102 void LoadFromFile(); | |
| 103 | |
| 104 // Attempt to read the model from the URL returns by T::GetModelURL() | |
| 105 void LoadFromURL(); | |
| 106 | |
| 107 // Called when the background task to download the model from |model_url_| | |
| 108 // has completed. | |
| 109 void OnDownloadComplete(int id, bool success, const std::string& data); | |
| 110 | |
| 111 // Transfer owhersnip of |model| to the |observer_|. | |
| 112 void TransferModelToObserver( | |
| 113 std::unique_ptr<chrome_intelligence::RankerModel> model); | |
| 114 | |
| 115 base::SequenceChecker sequence_checker_; | |
| 116 | |
| 117 // The thread on which to perform background IO to read, download and | |
| 118 // cache the model. | |
| 119 base::Thread thread_; | |
| 120 | |
| 121 // Used to download model data from |model_url_|. | |
| 122 // TODO(rogerm): Use net::URLFetcher directly? | |
|
groby-ooo-7-16
2017/01/23 21:41:56
There's a bunch of useful setup code in TranslateU
Roger McFarlane (Chromium)
2017/02/08 23:08:09
I can look at creating a CookieLessURLFetcher or s
| |
| 123 std::unique_ptr<TranslateURLFetcher> url_fetcher_; | |
| 124 | |
| 125 // The next time before which no new attempts to download the model should be | |
| 126 // attempted. | |
| 127 base::TimeTicks next_earliest_download_time_; | |
| 128 | |
| 129 // Tracks the last time of the last attempt to download a model. Used for UMA | |
| 130 // reporting of download duration. | |
| 131 base::TimeTicks download_start_time_; | |
| 132 | |
| 133 // A pointer to the observer for this model loader. The observer must outlive | |
| 134 // the loader. | |
| 135 RankerModelObserver* const observer_; // Weak. | |
| 136 | |
| 137 // The path at which the model is (or should be) cached. | |
| 138 const base::FilePath model_path_; | |
| 139 | |
| 140 // The URL from which to download the model if the model is not in the cache | |
| 141 // or the cached model is invalid/expired. | |
| 142 const GURL model_url_; | |
| 143 | |
| 144 // This will prefix all UMA metrics generated by the model loader. | |
| 145 const std::string uma_prefix_; | |
| 146 | |
| 147 DISALLOW_COPY_AND_ASSIGN(RankerModelLoader); | |
| 148 }; | |
| 149 | |
| 150 } // namespace translate | |
| 151 | |
| 152 #endif // COMPONENTS_TRANSLATE_CORE_BROWSER_RANKER_MODEL_LOADER_H_ | |
| OLD | NEW |