• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
18 #include "executor_pool.h"
19 #include "flat_object_store.h"
20 
21 namespace OHOS::ObjectStore {
22 class AssetChangeTimer {
23 public:
24     static AssetChangeTimer *GetInstance(FlatObjectStore *flatObjectStore);
25     void OnAssetChanged(
26         const std::string &sessionId, const std::string &assetKey, std::shared_ptr<ObjectWatcher> watcher);
27 
28 private:
29     AssetChangeTimer() = default;
30     ~AssetChangeTimer() = default;
31     AssetChangeTimer(const AssetChangeTimer &) = delete;
32     AssetChangeTimer &operator=(const AssetChangeTimer &) = delete;
33     explicit AssetChangeTimer(FlatObjectStore *flatObjectStore);
34     void StartTimer(const std::string &sessionId, const std::string &assetKey, std::shared_ptr<ObjectWatcher> watcher);
35     void StopTimer(const std::string &sessionId, const std::string &assetKey);
36     std::function<void()> ProcessTask(
37         const std::string &sessionId, const std::string &assetKey, std::shared_ptr<ObjectWatcher> watcher);
38     uint32_t HandleAssetChanges(const std::string &sessionId, const std::string &assetKey);
39     bool GetAssetValue(const std::string &sessionId, const std::string &assetKey, Asset &assetValue);
40 
41     std::mutex mutex_;
42     std::unordered_map<std::string, Executor::TaskId> assetChangeTasks_;
43     FlatObjectStore *flatObjectStore_ = nullptr;
44     std::shared_ptr<ExecutorPool> executor_;
45 
46     static std::mutex instanceMutex;
47     static AssetChangeTimer *instance;
48 };
49 } // namespace OHOS::ObjectStore
50 #endif // ASSET_CHANGE_TIMER_H