• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 "mock_insight_intent_db_cache.h"
17 #include "insight_intent_db_cache.h"
18 
19 namespace OHOS {
20 namespace AbilityRuntime {
InsightIntentDbCache()21 InsightIntentDbCache::InsightIntentDbCache()
22 {}
23 
InitInsightIntentCache(const int32_t userId)24 void InsightIntentDbCache::InitInsightIntentCache(const int32_t userId)
25 {
26     std::lock_guard<std::mutex> lock(genericInfosMutex_);
27     if (userId_ == userId) {
28         TAG_LOGD(AAFwkTag::INTENT, "no need init, userId %{public}d.", userId_);
29         return;
30     }
31     std::vector<ExtractInsightIntentInfo> totalInfos;
32     totalInfos.clear();
33     intentGenericInfos_.clear();
34     if (DelayedSingleton<InsightRdbStorageMgr>::GetInstance()->LoadInsightIntentInfos(userId, totalInfos) != ERR_OK) {
35         TAG_LOGE(AAFwkTag::INTENT, "Load All IntentData failed");
36         return;
37     }
38     for (size_t i = 0; i < totalInfos.size(); i++) {
39         ExtractInsightIntentInfo info = totalInfos.at(i);
40         std::string bundleName = info.genericInfo.bundleName;
41         intentGenericInfos_[bundleName].push_back(info.genericInfo);
42     }
43     userId_ = userId;
44 }
45 
~InsightIntentDbCache()46 InsightIntentDbCache::~InsightIntentDbCache()
47 {}
48 
SaveInsightIntentTotalInfo(const std::string & bundleName,const std::string & moduleName,const int32_t userId,ExtractInsightIntentProfileInfoVec profileInfos)49 int32_t InsightIntentDbCache::SaveInsightIntentTotalInfo(const std::string &bundleName, const std::string &moduleName,
50     const int32_t userId, ExtractInsightIntentProfileInfoVec profileInfos)
51 {
52     std::lock_guard<std::mutex> lock(genericInfosMutex_);
53     if (userId != userId_) {
54         TAG_LOGE(AAFwkTag::INTENT, "The userId %{public}d. is not the cache userId %{public}d.", userId, userId_);
55         return ERR_INVALID_VALUE;
56     }
57     std::vector<ExtractInsightIntentGenericInfo> genericInfos;
58     for (auto profileInfo : profileInfos.insightIntents) {
59         ExtractInsightIntentInfo info;
60         ExtractInsightIntentProfile::ProfileInfoFormat(profileInfo, info);
61         ExtractInsightIntentGenericInfo genericInfo = info.genericInfo;
62         genericInfos.emplace_back(genericInfo);
63     }
64     auto it = intentGenericInfos_.find(bundleName);
65     if (it != intentGenericInfos_.end()) {
66         TAG_LOGW(AAFwkTag::INTENT, "need update, bundleName %{public}s", bundleName.c_str());
67         for (auto iter = intentGenericInfos_[bundleName].begin(); iter != intentGenericInfos_[bundleName].end();) {
68             if (iter->moduleName == moduleName) {
69                 iter = intentGenericInfos_[bundleName].erase(iter);
70             } else {
71                 iter++;
72             }
73         }
74         it->second.insert(it->second.end(), genericInfos.begin(), genericInfos.end());
75     } else {
76         intentGenericInfos_[bundleName] = genericInfos;
77     }
78     int32_t res = DelayedSingleton<InsightRdbStorageMgr>::GetInstance()->DeleteStorageInsightIntentData(bundleName,
79         moduleName, userId);
80     if (res != ERR_OK) {
81         TAG_LOGW(AAFwkTag::INTENT, "Save before delete key error");
82     }
83     return DelayedSingleton<InsightRdbStorageMgr>::GetInstance()
84             ->SaveStorageInsightIntentData(bundleName, moduleName, userId, profileInfos);
85 }
86 
DeleteInsightIntentTotalInfo(const std::string & bundleName,const std::string & moduleName,const int32_t userId)87 int32_t InsightIntentDbCache::DeleteInsightIntentTotalInfo(const std::string &bundleName,
88     const std::string &moduleName, const int32_t userId)
89 {
90     std::lock_guard<std::mutex> lock(genericInfosMutex_);
91     if (userId != userId_) {
92         TAG_LOGE(AAFwkTag::INTENT, "userId %{public}d. is not the cache userId %{public}d.", userId, userId_);
93         return ERR_INVALID_VALUE;
94     }
95     if (moduleName.empty()) {
96         intentGenericInfos_.erase(bundleName);
97     } else {
98         for (auto iter = intentGenericInfos_[bundleName].begin(); iter != intentGenericInfos_[bundleName].end();) {
99             if (iter->moduleName == moduleName) {
100                 iter = intentGenericInfos_[bundleName].erase(iter);
101             } else {
102                 iter++;
103             }
104         }
105     }
106     return DelayedSingleton<InsightRdbStorageMgr>::GetInstance()->DeleteStorageInsightIntentData(bundleName,
107         moduleName, userId);
108 }
109 
DeleteInsightIntentByUserId(const int32_t userId)110 int32_t InsightIntentDbCache::DeleteInsightIntentByUserId(const int32_t userId)
111 {
112     std::lock_guard<std::mutex> lock(genericInfosMutex_);
113     if (userId == userId_) {
114         TAG_LOGE(AAFwkTag::INTENT, "can't delete the current user, userId %{public}d.", userId_);
115         return ERR_INVALID_VALUE;
116     }
117     return DelayedSingleton<InsightRdbStorageMgr>::GetInstance()->DeleteStorageInsightIntentByUserId(userId);
118 }
119 
GetAllInsightIntentGenericInfo(std::vector<ExtractInsightIntentGenericInfo> & genericInfos)120 void InsightIntentDbCache::GetAllInsightIntentGenericInfo(std::vector<ExtractInsightIntentGenericInfo> &genericInfos)
121 {
122     ExtractInsightIntentGenericInfo info;
123     info.bundleName = "bundleName";
124     genericInfos.emplace_back(info);
125 }
126 
GetInsightIntentGenericInfoByName(const std::string & bundleName,std::vector<ExtractInsightIntentGenericInfo> & genericInfos)127 void InsightIntentDbCache::GetInsightIntentGenericInfoByName(const std::string &bundleName,
128     std::vector<ExtractInsightIntentGenericInfo> &genericInfos)
129 {
130     ExtractInsightIntentGenericInfo info;
131     info.bundleName = "bundleName";
132     genericInfos.emplace_back(info);
133 }
134 
GetInsightIntentGenericInfo(const std::string & bundleName,const std::string & moduleName,const std::string & intentName,ExtractInsightIntentGenericInfo & genericInfo)135 void InsightIntentDbCache::GetInsightIntentGenericInfo(const std::string &bundleName, const std::string &moduleName,
136     const std::string &intentName, ExtractInsightIntentGenericInfo &genericInfo)
137 {
138     std::lock_guard<std::mutex> lock(genericInfosMutex_);
139     for (auto info : intentGenericInfos_[bundleName]) {
140         if (info.moduleName == moduleName && info.intentName == intentName) {
141             genericInfo = info;
142         }
143     }
144 }
145 
GetAllInsightIntentInfo(const int32_t userId,std::vector<ExtractInsightIntentInfo> & infos)146 void InsightIntentDbCache::GetAllInsightIntentInfo(const int32_t userId, std::vector<ExtractInsightIntentInfo> &infos)
147 {
148     ExtractInsightIntentInfo info;
149     info.decoratorClass = "decoratorClass";
150     info.decoratorFile = "decoratorFile";
151     infos.emplace_back(info);
152 }
153 
GetInsightIntentInfoByName(const std::string & bundleName,const int32_t userId,std::vector<ExtractInsightIntentInfo> & infos)154 void InsightIntentDbCache::GetInsightIntentInfoByName(const std::string &bundleName, const int32_t userId,
155     std::vector<ExtractInsightIntentInfo> &infos)
156 {
157     ExtractInsightIntentInfo info;
158     info.decoratorClass = "decoratorClass";
159     info.decoratorFile = "decoratorFile";
160     infos.emplace_back(info);
161 }
162 
GetInsightIntentInfo(const std::string & bundleName,const std::string & moduleName,const std::string & intentName,const int32_t userId,ExtractInsightIntentInfo & info)163 void InsightIntentDbCache::GetInsightIntentInfo(const std::string &bundleName, const std::string &moduleName,
164     const std::string &intentName, const int32_t userId, ExtractInsightIntentInfo &info)
165 {
166     std::lock_guard<std::mutex> lock(genericInfosMutex_);
167     if (userId != userId_) {
168         TAG_LOGE(AAFwkTag::INTENT, "The userId %{public}d. is not the cache userId %{public}d.", userId, userId_);
169         return;
170     }
171     if (DelayedSingleton<InsightRdbStorageMgr>::GetInstance()->
172         LoadInsightIntentInfo(bundleName, moduleName, intentName, userId, info) != ERR_OK) {
173         TAG_LOGE(AAFwkTag::INTENT, "GetInsightIntentInfo failed");
174         return;
175     }
176 }
177 } // namespace AbilityRuntime
178 } // namespace OHOS
179