1 /* 2 * Copyright (c) 2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef HGM_UPDATE_CALLBACK_H 17 #define HGM_UPDATE_CALLBACK_H 18 19 #include <functional> 20 #include <mutex> 21 #include <vector> 22 23 namespace OHOS { 24 namespace Rosen { 25 class HgmUpdateCallback { 26 public: 27 using UpdateCallback = std::function<void(HgmUpdateCallback&)>; 28 HgmUpdateCallback() = default; 29 virtual ~HgmUpdateCallback() = default; 30 RegisterUpdateCallback(const UpdateCallback & callback)31 void RegisterUpdateCallback(const UpdateCallback& callback) 32 { 33 if (callback == nullptr) { 34 return; 35 } 36 std::lock_guard<std::mutex> lock(mutex_); 37 updateCallback_.emplace_back(callback); 38 } 39 OnUpdate()40 virtual void OnUpdate() 41 { 42 std::lock_guard<std::mutex> lock(mutex_); 43 for (auto& callback : updateCallback_) { 44 if (callback != nullptr) { 45 callback(*this); 46 } 47 } 48 } 49 protected: 50 mutable std::mutex mutex_; 51 std::vector<UpdateCallback> updateCallback_; 52 }; 53 } // namespace Rosen 54 } // namespace OHOS 55 #endif // HGM_UPDATE_CALLBACK_H