1 /* 2 * Copyright (c) 2022 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 #ifndef ASSET_CHANGE_TIMER_H 16 #define ASSET_CHANGE_TIMER_H 17 #include <unordered_map> 18 19 #include "executor_pool.h" 20 #include "flat_object_store.h" 21 #include "object_types.h" 22 23 namespace OHOS::ObjectStore { 24 class AssetChangeTimer { 25 public: 26 static AssetChangeTimer *GetInstance(FlatObjectStore *flatObjectStore); 27 void OnAssetChanged( 28 const std::string &sessionId, const std::string &assetKey, std::shared_ptr<ObjectWatcher> watcher); 29 30 private: 31 AssetChangeTimer() = default; 32 ~AssetChangeTimer() = default; 33 AssetChangeTimer(const AssetChangeTimer &) = delete; 34 AssetChangeTimer &operator=(const AssetChangeTimer &) = delete; 35 AssetChangeTimer(FlatObjectStore *flatObjectStore); 36 void StartTimer(const std::string &sessionId, const std::string &assetKey, std::shared_ptr<ObjectWatcher> watcher); 37 void StopTimer(const std::string &sessionId, const std::string &assetKey); 38 std::function<void()> ProcessTask( 39 const std::string &sessionId, const std::string &assetKey, std::shared_ptr<ObjectWatcher> watcher); 40 uint32_t HandleAssetChanges(const std::string &sessionId, const std::string &assetKey); 41 bool GetAssetValue(const std::string &sessionId, const std::string &assetKey, Asset &assetValue); 42 43 std::mutex mutex_; 44 std::unordered_map<std::string, Executor::TaskId> assetChangeTasks_; 45 FlatObjectStore *flatObjectStore_ = nullptr; 46 std::shared_ptr<ExecutorPool> executor_; 47 48 static std::mutex instanceMutex; 49 static AssetChangeTimer *instance; 50 static constexpr size_t MAX_THREADS = 3; 51 static constexpr size_t MIN_THREADS = 0; 52 static constexpr uint32_t WAIT_INTERVAL = 100; 53 static constexpr char ASSET_SEPARATOR = '#'; 54 }; 55 } // namespace OHOS::ObjectStore 56 #endif // ASSET_CHANGE_TIMER_H