1 /*
2 * Copyright (c) 2023 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 #define LOG_TAG "LifeCyclePolicy"
16
17 #include "lifecycle_policy.h"
18
19 #include "log_print.h"
20 #include "preprocess/preprocess_utils.h"
21
22 namespace OHOS {
23 namespace UDMF {
24 using namespace std::chrono;
25
OnGot(const UnifiedKey & key)26 Status LifeCyclePolicy::OnGot(const UnifiedKey &key)
27 {
28 auto store = StoreCache::GetInstance().GetStore(key.intention);
29 if (store == nullptr) {
30 ZLOGE("Get store failed:%{public}s", key.intention.c_str());
31 return E_DB_ERROR;
32 }
33 if (store->Delete(UnifiedKey(key.key).GetKeyCommonPrefix()) != E_OK) {
34 ZLOGE("Remove data failed:%{public}s", key.intention.c_str());
35 return E_DB_ERROR;
36 }
37 return E_OK;
38 }
39
OnStart(const std::string & intention)40 Status LifeCyclePolicy::OnStart(const std::string &intention)
41 {
42 auto store = StoreCache::GetInstance().GetStore(intention);
43 if (store == nullptr) {
44 ZLOGE("Store get failed:%{public}s", intention.c_str());
45 return E_DB_ERROR;
46 }
47 if (store->Clear() != E_OK) {
48 ZLOGE("Data removal failed:%{public}s", intention.c_str());
49 return E_DB_ERROR;
50 }
51 return E_OK;
52 }
53
OnTimeout(const std::string & intention)54 Status LifeCyclePolicy::OnTimeout(const std::string &intention)
55 {
56 auto store = StoreCache::GetInstance().GetStore(intention);
57 if (store == nullptr) {
58 ZLOGE("Store get failed:%{public}s", intention.c_str());
59 return E_DB_ERROR;
60 }
61 std::vector<std::string> timeoutKeys;
62 Duration interval = INTERVAL;
63 CheckFileMangerIntention(intention, interval);
64 auto status = GetTimeoutKeys(store, INTERVAL, timeoutKeys);
65 if (status != E_OK) {
66 ZLOGE("Timeout keys get failed");
67 return E_DB_ERROR;
68 }
69 if (store->DeleteBatch(timeoutKeys) != E_OK) {
70 ZLOGE("Data removal failed:%{public}s", intention.c_str());
71 return E_DB_ERROR;
72 }
73 return E_OK;
74 }
75
GetTimeoutKeys(const std::shared_ptr<Store> & store,Duration interval,std::vector<std::string> & timeoutKeys)76 Status LifeCyclePolicy::GetTimeoutKeys(
77 const std::shared_ptr<Store> &store, Duration interval, std::vector<std::string> &timeoutKeys)
78 {
79 std::vector<UnifiedData> datas;
80 auto status = store->GetBatchData(DATA_PREFIX, datas);
81 if (status != E_OK) {
82 ZLOGE("Get data failed.");
83 return E_DB_ERROR;
84 }
85 if (datas.empty()) {
86 ZLOGD("entries is empty.");
87 return E_OK;
88 }
89 auto curTime = PreProcessUtils::GetTimestamp();
90 for (const auto &data : datas) {
91 if (data.GetRuntime() == nullptr) {
92 ZLOGD("Runtime is null");
93 return E_DB_ERROR;
94 }
95 if (curTime > data.GetRuntime()->createTime + duration_cast<milliseconds>(interval).count()
96 || curTime < data.GetRuntime()->createTime) {
97 timeoutKeys.push_back(UnifiedKey(data.GetRuntime()->key.key).GetKeyCommonPrefix());
98 }
99 }
100 return E_OK;
101 }
102
CheckFileMangerIntention(const std::string & intention,Duration & interval)103 Status LifeCyclePolicy::CheckFileMangerIntention(const std::string &intention, Duration &interval)
104 {
105 if (intention == UD_INTENTION_MAP.at(UD_INTENTION_SYSTEM_SHARE) ||
106 intention == UD_INTENTION_MAP.at(UD_INTENTION_PICKER) ||
107 intention == UD_INTENTION_MAP.at(UD_INTENTION_MENU)) {
108 interval = SYSTEM_SHARE_INTERVAL;
109 }
110 return E_OK;
111 }
112 } // namespace UDMF
113 } // namespace OHOS