• 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 "insight_intent_db_cache.h"
17 
18 namespace OHOS {
19 namespace AbilityRuntime {
InsightIntentDbCache()20 InsightIntentDbCache::InsightIntentDbCache()
21 {}
22 
InitInsightIntentCache(const int32_t userId)23 void InsightIntentDbCache::InitInsightIntentCache(const int32_t userId)
24 {
25     std::lock_guard<std::mutex> lock(genericInfosMutex_);
26     if (userId_ == userId) {
27         TAG_LOGD(AAFwkTag::INTENT, "no need init, userId %{public}d.", userId_);
28         return;
29     }
30     std::vector<ExtractInsightIntentInfo> totalInfos;
31     totalInfos.clear();
32     intentGenericInfos_.clear();
33     if (DelayedSingleton<InsightRdbStorageMgr>::GetInstance()->LoadInsightIntentInfos(userId, totalInfos) != ERR_OK) {
34         TAG_LOGE(AAFwkTag::INTENT, "Load All IntentData failed");
35         return;
36     }
37     for (size_t i = 0; i < totalInfos.size(); i++) {
38         ExtractInsightIntentInfo info = totalInfos.at(i);
39         std::string bundleName = info.genericInfo.bundleName;
40         intentGenericInfos_[bundleName].push_back(info.genericInfo);
41     }
42     userId_ = userId;
43 }
44 
~InsightIntentDbCache()45 InsightIntentDbCache::~InsightIntentDbCache()
46 {}
47 
SaveInsightIntentTotalInfo(const std::string & bundleName,const std::string & moduleName,const int32_t userId,ExtractInsightIntentProfileInfoVec profileInfos)48 int32_t InsightIntentDbCache::SaveInsightIntentTotalInfo(const std::string &bundleName, const std::string &moduleName,
49     const int32_t userId, ExtractInsightIntentProfileInfoVec profileInfos)
50 {
51     std::lock_guard<std::mutex> lock(genericInfosMutex_);
52     if (userId != userId_) {
53         TAG_LOGE(AAFwkTag::INTENT, "The userId %{public}d. is not the cache userId %{public}d.", userId, userId_);
54         return ERR_INVALID_VALUE;
55     }
56     std::vector<ExtractInsightIntentGenericInfo> genericInfos;
57     for (auto profileInfo : profileInfos.insightIntents) {
58         ExtractInsightIntentInfo info;
59         ExtractInsightIntentProfile::ProfileInfoFormat(profileInfo, info);
60         ExtractInsightIntentGenericInfo genericInfo = info.genericInfo;
61         genericInfos.emplace_back(genericInfo);
62     }
63     auto it = intentGenericInfos_.find(bundleName);
64     if (it != intentGenericInfos_.end()) {
65         TAG_LOGW(AAFwkTag::INTENT, "need update, bundleName %{public}s", bundleName.c_str());
66         for (auto iter = intentGenericInfos_[bundleName].begin(); iter != intentGenericInfos_[bundleName].end();) {
67             if (iter->moduleName == moduleName) {
68                 iter = intentGenericInfos_[bundleName].erase(iter);
69             } else {
70                 iter++;
71             }
72         }
73         it->second.insert(it->second.end(), genericInfos.begin(), genericInfos.end());
74     } else {
75         intentGenericInfos_[bundleName] = genericInfos;
76     }
77     int32_t res = DelayedSingleton<InsightRdbStorageMgr>::GetInstance()->DeleteStorageInsightIntentData(bundleName,
78         moduleName, userId);
79     if (res != ERR_OK) {
80         TAG_LOGW(AAFwkTag::INTENT, "Save before delete key error");
81         return res;
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     std::lock_guard<std::mutex> lock(genericInfosMutex_);
123     for (auto iter = intentGenericInfos_.begin(); iter != intentGenericInfos_.end(); ++iter) {
124         genericInfos.insert(genericInfos.end(), iter->second.begin(), iter->second.end());
125     }
126 }
127 
GetInsightIntentGenericInfoByName(const std::string & bundleName,std::vector<ExtractInsightIntentGenericInfo> & genericInfos)128 void InsightIntentDbCache::GetInsightIntentGenericInfoByName(const std::string &bundleName,
129     std::vector<ExtractInsightIntentGenericInfo> &genericInfos)
130 {
131     std::lock_guard<std::mutex> lock(genericInfosMutex_);
132     genericInfos = intentGenericInfos_[bundleName];
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     std::lock_guard<std::mutex> lock(genericInfosMutex_);
149     if (userId != userId_) {
150         TAG_LOGE(AAFwkTag::INTENT, "The userId %{public}d. is not the cache userId %{public}d.", userId, userId_);
151         return;
152     }
153     if (DelayedSingleton<InsightRdbStorageMgr>::GetInstance()->LoadInsightIntentInfos(userId, infos) != ERR_OK) {
154         TAG_LOGE(AAFwkTag::INTENT, "LoadIntentData failed");
155         return;
156     }
157 }
158 
GetInsightIntentInfoByName(const std::string & bundleName,const int32_t userId,std::vector<ExtractInsightIntentInfo> & infos)159 void InsightIntentDbCache::GetInsightIntentInfoByName(const std::string &bundleName, const int32_t userId,
160     std::vector<ExtractInsightIntentInfo> &infos)
161 {
162     std::lock_guard<std::mutex> lock(genericInfosMutex_);
163     if (userId != userId_) {
164         TAG_LOGE(AAFwkTag::INTENT, "The userId %{public}d. is not the cache userId %{public}d.", userId, userId_);
165         return;
166     }
167     if (DelayedSingleton<InsightRdbStorageMgr>::GetInstance()->
168         LoadInsightIntentInfoByName(bundleName, userId, infos) != ERR_OK) {
169         TAG_LOGE(AAFwkTag::INTENT, "GetInsightIntentInfoByName failed");
170         return;
171     }
172 }
173 
GetInsightIntentInfo(const std::string & bundleName,const std::string & moduleName,const std::string & intentName,const int32_t userId,ExtractInsightIntentInfo & info)174 void InsightIntentDbCache::GetInsightIntentInfo(const std::string &bundleName, const std::string &moduleName,
175     const std::string &intentName, const int32_t userId, ExtractInsightIntentInfo &info)
176 {
177     std::lock_guard<std::mutex> lock(genericInfosMutex_);
178     if (userId != userId_) {
179         TAG_LOGE(AAFwkTag::INTENT, "The userId %{public}d. is not the cache userId %{public}d.", userId, userId_);
180         return;
181     }
182     if (DelayedSingleton<InsightRdbStorageMgr>::GetInstance()->
183         LoadInsightIntentInfo(bundleName, moduleName, intentName, userId, info) != ERR_OK) {
184         TAG_LOGE(AAFwkTag::INTENT, "GetInsightIntentInfo failed");
185         return;
186     }
187 }
188 } // namespace AbilityRuntime
189 } // namespace OHOS
190