1 /* 2 * Copyright (C) 2019 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef IORAP_MANAGER_EVENT_MANAGER_H_ 18 #define IORAP_MANAGER_EVENT_MANAGER_H_ 19 20 #include "binder/app_launch_event.h" 21 #include "binder/dexopt_event.h" 22 #include "binder/job_scheduled_event.h" 23 #include "binder/request_id.h" 24 #include "binder/task_result.h" 25 26 #include <android/content/pm/PackageChangeEvent.h> 27 28 #include <memory> 29 30 namespace android { 31 class Printer; 32 } // namespace android 33 34 namespace iorap::perfetto { 35 struct RxProducerFactory; 36 } // namespace iorap::perfetto 37 38 namespace iorap::manager { 39 40 // These callbacks are invoked by the EventManager to provide asynchronous notification for the status 41 // of an event handler. 42 // 43 // Calling 'On_Event' in EventManager should be considered merely to start the task. 44 // Calling 'OnComplete' here is considered to terminate the request (either with a success or error). 45 // OnProgress is optional, but if it is called it must be called prior to 'OnComplete'. 46 // 47 // All callbacks for the same request-id are sequentially consistent. 48 class TaskResultCallbacks { 49 public: OnProgress(iorap::binder::RequestId request_id,iorap::binder::TaskResult task_result)50 virtual void OnProgress(iorap::binder::RequestId request_id, iorap::binder::TaskResult task_result) {} OnComplete(iorap::binder::RequestId request_id,iorap::binder::TaskResult task_result)51 virtual void OnComplete(iorap::binder::RequestId request_id, iorap::binder::TaskResult task_result) {} 52 ~TaskResultCallbacks()53 virtual ~TaskResultCallbacks() {} 54 }; 55 56 class EventManager { 57 public: 58 static std::shared_ptr<EventManager> Create(); 59 static std::shared_ptr<EventManager> Create( 60 /*borrow*/perfetto::RxProducerFactory& perfetto_factory); 61 void SetTaskResultCallbacks(std::shared_ptr<TaskResultCallbacks> callbacks); 62 63 // Joins any background threads created by EventManager. 64 void Join(); 65 66 // Handles an AppLaunchEvent: 67 // 68 // * Intent starts and app launch starts are treated critically 69 // and will be handled immediately. This means the caller 70 // (e.g. the binder pool thread) could be starved in the name 71 // of low latency. 72 // 73 // * Other types are handled in a separate thread. 74 bool OnAppLaunchEvent(binder::RequestId request_id, 75 const binder::AppLaunchEvent& event); 76 77 // Handles a DexOptEvent: 78 // 79 // Clean up the invalidate traces after package is updated by dexopt. 80 bool OnDexOptEvent(binder::RequestId request_id, 81 const binder::DexOptEvent& event); 82 83 84 // Handles a JobScheduledEvent: 85 // 86 // * Start/stop background jobs (typically for idle maintenance). 87 // * For example, this could kick off a background compiler. 88 bool OnJobScheduledEvent(binder::RequestId request_id, 89 const binder::JobScheduledEvent& event); 90 91 // Handles a PackageChangeEvent: 92 // 93 // * The package manager service send this event for package install 94 // update or delete. 95 bool OnPackageChanged(const android::content::pm::PackageChangeEvent& event); 96 97 // Print to adb shell dumpsys (for bugreport info). 98 void Dump(/*borrow*/::android::Printer& printer); 99 100 // A dumpsys --refresh-properties command signaling that we should 101 // refresh our system properties. 102 void RefreshSystemProperties(::android::Printer& printer); 103 104 // A dumpsys --purge-package <name> command signaling 105 // that all db rows and files associated with a package should be deleted. 106 bool PurgePackage(::android::Printer& printer, const std::string& package_name); 107 108 // A dumpsys --compile-package <name> command signaling 109 // that a package should be recompiled. 110 bool CompilePackage(::android::Printer& printer, const std::string& package_name); 111 112 class Impl; 113 private: 114 std::unique_ptr<Impl> impl_; 115 116 EventManager(perfetto::RxProducerFactory& perfetto_factory); 117 }; 118 119 } // namespace iorap::manager 120 121 #endif // IORAP_MANAGER_EVENT_MANAGER_H_ 122