• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 
30 /**
31  * @brief Get form data.
32  * @param formId, Form id.
33  * @param data, Cache data.
34  * @param imageMap Image map cache.
35  * @return Returns true if this function is successfully called; returns false otherwise.
36  */
GetData(const int64_t formId,std::string & data,std::map<std::string,std::pair<sptr<FormAshmem>,int32_t>> & imageMap) const37 bool FormCacheMgr::GetData(const int64_t formId, std::string &data,
38     std::map<std::string, std::pair<sptr<FormAshmem>, int32_t>> &imageMap) const
39 {
40     HILOG_INFO("get cache data");
41     std::lock_guard<std::mutex> lock(cacheMutex_);
42     if (cacheData_.empty() && cacheImageMap_.empty()) {
43         HILOG_ERROR("form cache is empty");
44         return false;
45     }
46     auto formData = cacheData_.find(formId);
47     if (formData != cacheData_.end()) {
48         data = formData->second;
49     }
50 
51     auto imageMapIt = cacheImageMap_.find(formId);
52     if (imageMapIt != cacheImageMap_.end()) {
53         imageMap = imageMapIt->second;
54     }
55 
56     if (data.empty() && imageMap.empty()) {
57         HILOG_ERROR("form cache not find");
58         return false;
59     } else {
60         return true;
61     }
62 }
63 
64 /**
65  * @brief Add form data.
66  * @param formId, Form id.
67  * @param data, Cache data.
68  * @param imageMap Image map cache.
69  * @return Returns true if this function is successfully called; returns false otherwise.
70  */
AddData(const int64_t formId,const std::string & data,const std::map<std::string,std::pair<sptr<FormAshmem>,int32_t>> & imageMap)71 bool FormCacheMgr::AddData(const int64_t formId, const std::string &data,
72     const std::map<std::string, std::pair<sptr<FormAshmem>, int32_t>> &imageMap)
73 {
74     HILOG_INFO("add new cache data");
75     std::lock_guard<std::mutex> lock(cacheMutex_);
76     cacheData_[formId] = data;
77     cacheImageMap_[formId] = imageMap;
78     return true;
79 }
80 
81 /**
82  * @brief Delete form data.
83  * @param formId, Form id.
84  * @return Returns true if this function is successfully called; returns false otherwise.
85  */
DeleteData(const int64_t formId)86 bool FormCacheMgr::DeleteData(const int64_t formId)
87 {
88     HILOG_INFO("delete cache data");
89     std::lock_guard<std::mutex> lock(cacheMutex_);
90     auto formData = cacheData_.find(formId);
91     if (formData != cacheData_.end()) {
92         cacheData_.erase(formId);
93     } else {
94         HILOG_WARN("cache data is not exist");
95     }
96 
97     auto imageMap = cacheImageMap_.find(formId);
98     if (imageMap != cacheImageMap_.end()) {
99         cacheImageMap_.erase(formId);
100     } else {
101         HILOG_WARN("image data is not exist");
102     }
103     return true;
104 }
105 
106 /**
107  * @brief Update form data.
108  * @param formId, Form id.
109  * @param data, Cache data.
110  * @return Returns true if this function is successfully called; returns false otherwise.
111  */
UpdateData(const int64_t formId,const std::string & data)112 bool FormCacheMgr::UpdateData(const int64_t formId, const std::string &data)
113 {
114     HILOG_INFO("update cache data");
115     std::lock_guard<std::mutex> lock(cacheMutex_);
116     auto formData = cacheData_.find(formId);
117     if (formData == cacheData_.end()) {
118         HILOG_ERROR("cache data is not exist");
119         return false;
120     }
121 
122     formData->second = data;
123     return true;
124 }
125 /**
126  * @brief Check if form data is exist or not.
127  * @param formId, Form id.
128  * @return Returns true if this function is successfully called; returns false otherwise.
129  */
IsExist(const int64_t formId) const130 bool FormCacheMgr::IsExist(const int64_t formId) const
131 {
132     HILOG_INFO("get cache data");
133     std::lock_guard<std::mutex> lock(cacheMutex_);
134     if (cacheData_.empty()) {
135         HILOG_ERROR("form cache is empty");
136         return false;
137     }
138     auto formData = cacheData_.find(formId);
139     if (formData == cacheData_.end()) {
140         HILOG_ERROR("cache data not find");
141         return false;
142     }
143 
144     return true;
145 }
146 }  // namespace AppExecFwk
147 }  // namespace OHOS
148