1 /*
2  * Copyright (c) 2024 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 #ifndef OHOS_ABILITY_RUNTIME__ABILITY_CACHE_MANAGER_H
17 #define OHOS_ABILITY_RUNTIME__ABILITY_CACHE_MANAGER_H
18 
19 #include <map>
20 #include <list>
21 #include <string>
22 #include <mutex>
23 
24 #include "ability_config.h"
25 #include "ability_info.h"
26 #include "ability_record.h"
27 namespace OHOS {
28 namespace AAFwk {
29 /**
30  * @class AbilityCacheManager
31  * AbilityCacheManager provides a lru cache for managing ability record.
32  */
33 class AbilityCacheManager {
34 public:
35     using AbilityInfo = OHOS::AppExecFwk::AbilityInfo;
36     using AbilityType = OHOS::AppExecFwk::AbilityType;
37     /**
38      * Get ability cache manager.
39      * @return AbilityCacheManager
40      */
41     static AbilityCacheManager &GetInstance(void);
42 
43     /**
44      * Init the ability cache manager with capacity for the device (devCapacity)
45      * and the capacity for a single process(procCapacity)
46      */
47     void Init(uint32_t devCapacity, uint32_t procCapacity);
48 
49     /**
50      * Put a single ability record into ability cache manager.
51      * @param abilityRecord the ability record to be putted into cache manager.
52      * @return AbilityRecord if one is eliminated, otherwise nullptr.
53      */
54     std::shared_ptr<AbilityRecord> Put(std::shared_ptr<AbilityRecord> abilityRecord);
55 
56     /**
57      * Remove a single ability record from ability cache manager.
58      * @param abilityRecord, the ability record to be removed into cache manager.
59      */
60     void Remove(std::shared_ptr<AbilityRecord> abilityRecord);
61 
62     /**
63      * Get a single ability by abilityRequest record from ability cache manager,
64      * this will remove the AbilityRecord by default
65      * @param abilityRequest the ability request to be searched in cache manager.
66      * @return AbilityRecord if one is matched, otherwise nullptr.
67      */
68     std::shared_ptr<AbilityRecord> Get(const AbilityRequest &abilityRequest);
69 
70      /**
71      * Get a single ability by token from ability cache manager.
72      * @param token the ability token to be searched in cache manager.
73      * @return AbilityRecord if one is matched, otherwise nullptr.
74      */
75     std::shared_ptr<AbilityRecord> FindRecordByToken(const sptr<IRemoteObject> &token);
76 
77     /**
78      * Get all the abilities of current ability cache manager.
79      * @return AbilityRecord list.
80      */
81     std::list<std::shared_ptr<AbilityRecord>> GetAbilityList();
82 
83     /**
84      * Get a single ability by sessionId from ability cache manager.
85      * @param assertSessionId the ability assertSessionId to be searched in cache manager.
86      * @return AbilityRecord if one is matched, otherwise nullptr.
87      */
88     std::shared_ptr<AbilityRecord> FindRecordBySessionId(const std::string &assertSessionId);
89 
90     /**
91      * Get a single ability by serviceKey from ability cache manager.
92      * @param serviceKey the ability serviceKey to be searched in cache manager.
93      * @return AbilityRecord if one is matched, otherwise nullptr.
94      */
95     std::shared_ptr<AbilityRecord> FindRecordByServiceKey(const std::string &serviceKey);
96 
97     /**
98      * Remove the launcher death recipient from ability cache manager.
99      */
100     void RemoveLauncherDeathRecipient();
101 
102     /**
103      * Sign the restart flag by uid of ability from ability cache manager.
104      * @param uid the ability uid to be searched in cache manager.
105      */
106     void SignRestartAppFlag(int32_t uid);
107 
108     /**
109      * Delete the invalid ability by bundleName from ability cache manager.
110      * @param bundleName the ability bundleName to be searched in cache manager.
111      */
112     void DeleteInvalidServiceRecord(const std::string &bundleName);
113     private:
114         AbilityCacheManager();
115         ~AbilityCacheManager();
116         struct ProcRecordsInfo {
117             std::list<std::shared_ptr<AbilityRecord>> recList;
118             uint32_t cnt;
119         };
120         uint32_t devLruCapacity_ = 0;
121         uint32_t procLruCapacity_ = 0;
122         uint32_t devLruCnt_ = 0;
123         std::mutex mutex_;
124         std::map<uint32_t, ProcRecordsInfo> procLruMap_;
125         std::list<std::shared_ptr<AbilityRecord>> devRecLru_;
126         std::shared_ptr<AbilityRecord> AddToProcLru(std::shared_ptr<AbilityRecord> abilityRecord);
127         std::shared_ptr<AbilityRecord> AddToDevLru(std::shared_ptr<AbilityRecord> abilityRecord,
128             std::shared_ptr<AbilityRecord> rec);
129         void RemoveAbilityRecInDevList(std::shared_ptr<AbilityRecord> abilityRecord);
130         void RemoveAbilityRecInProcList(std::shared_ptr<AbilityRecord> abilityRecord);
131         std::shared_ptr<AbilityRecord> GetAbilityRecInProcList(const AbilityRequest &abilityRequest);
132         bool IsRecInfoSame(const AbilityRequest& abilityRequest, std::shared_ptr<AbilityRecord> abilityRecord);
133         DISALLOW_COPY_AND_MOVE(AbilityCacheManager);
134 };
135 } // namespace AAFwk
136 } // namespace OHOS
137 #endif  // OHOS_ABILITY_RUNTIME__ABILITY_CACHE_MANAGER_H