1 // Copyright 2020 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 CHROME_BROWSER_CHROMEOS_POWER_ML_SMART_DIM_ML_AGENT_H_ 6 #define CHROME_BROWSER_CHROMEOS_POWER_ML_SMART_DIM_ML_AGENT_H_ 7 8 #include <string> 9 10 #include "base/cancelable_callback.h" 11 #include "base/no_destructor.h" 12 #include "chrome/browser/chromeos/power/ml/smart_dim/builtin_worker.h" 13 #include "chrome/browser/chromeos/power/ml/smart_dim/download_worker.h" 14 #include "chrome/browser/chromeos/power/ml/smart_dim/smart_dim_worker.h" 15 #include "chrome/browser/chromeos/power/ml/user_activity_event.pb.h" 16 17 namespace chromeos { 18 namespace power { 19 namespace ml { 20 21 using DimDecisionCallback = 22 base::OnceCallback<void(UserActivityEvent::ModelPrediction)>; 23 24 // SmartDimMlAgent is responsible for preprocessing the features and requesting 25 // the inference from machine learning service. 26 // Usage: 27 // 28 // SmartDimMlAgent::GetInstance()->RequestDimDecision( 29 // features_, dim_decision_callback); 30 class SmartDimMlAgent { 31 public: 32 static SmartDimMlAgent* GetInstance(); 33 34 // Post a request to determine whether an upcoming dim should go ahead based 35 // on input |features|. When a decision is arrived at, runs the callback. If 36 // this method is called again before it calls the previous callback, the 37 // previous callback will be canceled. 38 void RequestDimDecision(const UserActivityEvent::Features& features, 39 DimDecisionCallback callback); 40 void CancelPreviousRequest(); 41 42 // Called by CUS(component update service). When new version of the component 43 // downloaded, CUS first uses IsDownloadWorkerReady to see if download worker 44 // is ready. If it's not, CUS then uses OnComponentReady to update the 45 // download metainfo, preprocessor and model. 46 bool IsDownloadWorkerReady(); 47 void OnComponentReady(const ComponentFileContents& contents); 48 49 // Called by ml_agent_unittest.cc to reset the builtin and download worker. 50 void ResetForTesting(); 51 52 protected: 53 SmartDimMlAgent(); 54 virtual ~SmartDimMlAgent(); 55 56 private: 57 friend base::NoDestructor<SmartDimMlAgent>; 58 59 // Return download_worker_ if it's ready, otherwise builtin_worker_. 60 SmartDimWorker* GetWorker(); 61 62 BuiltinWorker builtin_worker_; 63 DownloadWorker download_worker_; 64 65 base::CancelableOnceCallback<void(UserActivityEvent::ModelPrediction)> 66 dim_decision_callback_; 67 68 SEQUENCE_CHECKER(sequence_checker_); 69 70 DISALLOW_COPY_AND_ASSIGN(SmartDimMlAgent); 71 }; 72 73 } // namespace ml 74 } // namespace power 75 } // namespace chromeos 76 77 #endif // CHROME_BROWSER_CHROMEOS_POWER_ML_SMART_DIM_ML_AGENT_H_ 78