• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 
16 #include "hilog_wrapper.h"
17 #include "form_cache_mgr.h"
18 
19 namespace OHOS {
20 namespace AppExecFwk {
FormCacheMgr()21 FormCacheMgr::FormCacheMgr()
22 {
23     HILOG_INFO("create form cache manager instance");
24 }
~FormCacheMgr()25 FormCacheMgr::~FormCacheMgr()
26 {
27     HILOG_INFO("destroy form cache manager instance");
28 }
29 
GetData(const int64_t formId,std::string & data) const30 bool FormCacheMgr::GetData(const int64_t formId, std::string &data) const
31 {
32     HILOG_INFO("get cache data");
33     std::lock_guard<std::mutex> lock(cacheMutex_);
34     if (cacheData_.empty()) {
35         HILOG_ERROR("form cache is empty");
36         return false;
37     }
38     auto formData = cacheData_.find(formId);
39     if (formData != cacheData_.end()) {
40         data = formData->second;
41     }
42 
43     if (data.empty()) {
44         HILOG_ERROR("form cache not find");
45         return false;
46     } else {
47         return true;
48     }
49 }
50 
51 /**
52  * @brief Add form data.
53  * @param formId Form id.
54  * @param data Cache data.
55  * @return Returns true if this function is successfully called; returns false otherwise.
56  */
AddData(const int64_t formId,const std::string & data)57 bool FormCacheMgr::AddData(const int64_t formId, const std::string &data)
58 {
59     std::lock_guard<std::mutex> lock(cacheMutex_);
60     auto formData = cacheData_.find(formId);
61     if (formData == cacheData_.end()) {
62         HILOG_INFO("add new cache data.");
63         auto result = cacheData_.emplace(formId, data);
64         if (!result.second) {
65             HILOG_ERROR("Failed to emplace cache data.");
66             return false;
67         }
68     } else {
69         HILOG_INFO("update cache data.");
70         formData->second = data;
71     }
72     return true;
73 }
74 
75 /**
76  * @brief Delete form data.
77  * @param formId, Form id.
78  * @return Returns true if this function is successfully called; returns false otherwise.
79  */
DeleteData(const int64_t formId)80 bool FormCacheMgr::DeleteData(const int64_t formId)
81 {
82     HILOG_INFO("delete cache data");
83     std::lock_guard<std::mutex> lock(cacheMutex_);
84     auto formData = cacheData_.find(formId);
85     if (formData != cacheData_.end()) {
86         cacheData_.erase(formId);
87     } else {
88         HILOG_WARN("cache data is not exist");
89     }
90     return true;
91 }
92 
93 /**
94  * @brief Update form data.
95  * @param formId, Form id.
96  * @param data, Cache data.
97  * @return Returns true if this function is successfully called; returns false otherwise.
98  */
UpdateData(const int64_t formId,const std::string & data)99 bool FormCacheMgr::UpdateData(const int64_t formId, const std::string &data)
100 {
101     HILOG_INFO("update cache data");
102     std::lock_guard<std::mutex> lock(cacheMutex_);
103     auto formData = cacheData_.find(formId);
104     if (formData == cacheData_.end()) {
105         HILOG_ERROR("cache data is not exist");
106         return false;
107     }
108 
109     formData->second = data;
110     return true;
111 }
112 
113 /**
114  * @brief Check if form data is exist or not.
115  * @param formId, Form id.
116  * @return Returns true if this function is successfully called; returns false otherwise.
117  */
IsExist(const int64_t formId) const118 bool FormCacheMgr::IsExist(const int64_t formId) const
119 {
120     HILOG_INFO("get cache data");
121     std::lock_guard<std::mutex> lock(cacheMutex_);
122     if (cacheData_.empty()) {
123         HILOG_ERROR("form cache is empty");
124         return false;
125     }
126     auto formData = cacheData_.find(formId);
127     if (formData == cacheData_.end()) {
128         HILOG_ERROR("cache data not find");
129         return false;
130     }
131 
132     return true;
133 }
134 }  // namespace AppExecFwk
135 }  // namespace OHOS
136