1eace7efcSopenharmony_ci/* 2eace7efcSopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd. 3eace7efcSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4eace7efcSopenharmony_ci * you may not use this file except in compliance with the License. 5eace7efcSopenharmony_ci * You may obtain a copy of the License at 6eace7efcSopenharmony_ci * 7eace7efcSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8eace7efcSopenharmony_ci * 9eace7efcSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10eace7efcSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11eace7efcSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12eace7efcSopenharmony_ci * See the License for the specific language governing permissions and 13eace7efcSopenharmony_ci * limitations under the License. 14eace7efcSopenharmony_ci */ 15eace7efcSopenharmony_ci 16eace7efcSopenharmony_ci#ifndef OHOS_ABILITY_RUNTIME__ABILITY_CACHE_MANAGER_H 17eace7efcSopenharmony_ci#define OHOS_ABILITY_RUNTIME__ABILITY_CACHE_MANAGER_H 18eace7efcSopenharmony_ci 19eace7efcSopenharmony_ci#include <map> 20eace7efcSopenharmony_ci#include <list> 21eace7efcSopenharmony_ci#include <string> 22eace7efcSopenharmony_ci#include <mutex> 23eace7efcSopenharmony_ci 24eace7efcSopenharmony_ci#include "ability_config.h" 25eace7efcSopenharmony_ci#include "ability_info.h" 26eace7efcSopenharmony_ci#include "ability_record.h" 27eace7efcSopenharmony_cinamespace OHOS { 28eace7efcSopenharmony_cinamespace AAFwk { 29eace7efcSopenharmony_ci/** 30eace7efcSopenharmony_ci * @class AbilityCacheManager 31eace7efcSopenharmony_ci * AbilityCacheManager provides a lru cache for managing ability record. 32eace7efcSopenharmony_ci */ 33eace7efcSopenharmony_ciclass AbilityCacheManager { 34eace7efcSopenharmony_cipublic: 35eace7efcSopenharmony_ci using AbilityInfo = OHOS::AppExecFwk::AbilityInfo; 36eace7efcSopenharmony_ci using AbilityType = OHOS::AppExecFwk::AbilityType; 37eace7efcSopenharmony_ci /** 38eace7efcSopenharmony_ci * Get ability cache manager. 39eace7efcSopenharmony_ci * @return AbilityCacheManager 40eace7efcSopenharmony_ci */ 41eace7efcSopenharmony_ci static AbilityCacheManager &GetInstance(void); 42eace7efcSopenharmony_ci 43eace7efcSopenharmony_ci /** 44eace7efcSopenharmony_ci * Init the ability cache manager with capacity for the device (devCapacity) 45eace7efcSopenharmony_ci * and the capacity for a single process(procCapacity) 46eace7efcSopenharmony_ci */ 47eace7efcSopenharmony_ci void Init(uint32_t devCapacity, uint32_t procCapacity); 48eace7efcSopenharmony_ci 49eace7efcSopenharmony_ci /** 50eace7efcSopenharmony_ci * Put a single ability record into ability cache manager. 51eace7efcSopenharmony_ci * @param abilityRecord the ability record to be putted into cache manager. 52eace7efcSopenharmony_ci * @return AbilityRecord if one is eliminated, otherwise nullptr. 53eace7efcSopenharmony_ci */ 54eace7efcSopenharmony_ci std::shared_ptr<AbilityRecord> Put(std::shared_ptr<AbilityRecord> abilityRecord); 55eace7efcSopenharmony_ci 56eace7efcSopenharmony_ci /** 57eace7efcSopenharmony_ci * Remove a single ability record from ability cache manager. 58eace7efcSopenharmony_ci * @param abilityRecord, the ability record to be removed into cache manager. 59eace7efcSopenharmony_ci */ 60eace7efcSopenharmony_ci void Remove(std::shared_ptr<AbilityRecord> abilityRecord); 61eace7efcSopenharmony_ci 62eace7efcSopenharmony_ci /** 63eace7efcSopenharmony_ci * Get a single ability by abilityRequest record from ability cache manager, 64eace7efcSopenharmony_ci * this will remove the AbilityRecord by default 65eace7efcSopenharmony_ci * @param abilityRequest the ability request to be searched in cache manager. 66eace7efcSopenharmony_ci * @return AbilityRecord if one is matched, otherwise nullptr. 67eace7efcSopenharmony_ci */ 68eace7efcSopenharmony_ci std::shared_ptr<AbilityRecord> Get(const AbilityRequest &abilityRequest); 69eace7efcSopenharmony_ci 70eace7efcSopenharmony_ci /** 71eace7efcSopenharmony_ci * Get a single ability by token from ability cache manager. 72eace7efcSopenharmony_ci * @param token the ability token to be searched in cache manager. 73eace7efcSopenharmony_ci * @return AbilityRecord if one is matched, otherwise nullptr. 74eace7efcSopenharmony_ci */ 75eace7efcSopenharmony_ci std::shared_ptr<AbilityRecord> FindRecordByToken(const sptr<IRemoteObject> &token); 76eace7efcSopenharmony_ci 77eace7efcSopenharmony_ci /** 78eace7efcSopenharmony_ci * Get all the abilities of current ability cache manager. 79eace7efcSopenharmony_ci * @return AbilityRecord list. 80eace7efcSopenharmony_ci */ 81eace7efcSopenharmony_ci std::list<std::shared_ptr<AbilityRecord>> GetAbilityList(); 82eace7efcSopenharmony_ci 83eace7efcSopenharmony_ci /** 84eace7efcSopenharmony_ci * Get a single ability by sessionId from ability cache manager. 85eace7efcSopenharmony_ci * @param assertSessionId the ability assertSessionId to be searched in cache manager. 86eace7efcSopenharmony_ci * @return AbilityRecord if one is matched, otherwise nullptr. 87eace7efcSopenharmony_ci */ 88eace7efcSopenharmony_ci std::shared_ptr<AbilityRecord> FindRecordBySessionId(const std::string &assertSessionId); 89eace7efcSopenharmony_ci 90eace7efcSopenharmony_ci /** 91eace7efcSopenharmony_ci * Get a single ability by serviceKey from ability cache manager. 92eace7efcSopenharmony_ci * @param serviceKey the ability serviceKey to be searched in cache manager. 93eace7efcSopenharmony_ci * @return AbilityRecord if one is matched, otherwise nullptr. 94eace7efcSopenharmony_ci */ 95eace7efcSopenharmony_ci std::shared_ptr<AbilityRecord> FindRecordByServiceKey(const std::string &serviceKey); 96eace7efcSopenharmony_ci 97eace7efcSopenharmony_ci /** 98eace7efcSopenharmony_ci * Remove the launcher death recipient from ability cache manager. 99eace7efcSopenharmony_ci */ 100eace7efcSopenharmony_ci void RemoveLauncherDeathRecipient(); 101eace7efcSopenharmony_ci 102eace7efcSopenharmony_ci /** 103eace7efcSopenharmony_ci * Sign the restart flag by uid of ability from ability cache manager. 104eace7efcSopenharmony_ci * @param uid the ability uid to be searched in cache manager. 105eace7efcSopenharmony_ci */ 106eace7efcSopenharmony_ci void SignRestartAppFlag(int32_t uid); 107eace7efcSopenharmony_ci 108eace7efcSopenharmony_ci /** 109eace7efcSopenharmony_ci * Delete the invalid ability by bundleName from ability cache manager. 110eace7efcSopenharmony_ci * @param bundleName the ability bundleName to be searched in cache manager. 111eace7efcSopenharmony_ci */ 112eace7efcSopenharmony_ci void DeleteInvalidServiceRecord(const std::string &bundleName); 113eace7efcSopenharmony_ci private: 114eace7efcSopenharmony_ci AbilityCacheManager(); 115eace7efcSopenharmony_ci ~AbilityCacheManager(); 116eace7efcSopenharmony_ci struct ProcRecordsInfo { 117eace7efcSopenharmony_ci std::list<std::shared_ptr<AbilityRecord>> recList; 118eace7efcSopenharmony_ci uint32_t cnt; 119eace7efcSopenharmony_ci }; 120eace7efcSopenharmony_ci uint32_t devLruCapacity_ = 0; 121eace7efcSopenharmony_ci uint32_t procLruCapacity_ = 0; 122eace7efcSopenharmony_ci uint32_t devLruCnt_ = 0; 123eace7efcSopenharmony_ci std::mutex mutex_; 124eace7efcSopenharmony_ci std::map<uint32_t, ProcRecordsInfo> procLruMap_; 125eace7efcSopenharmony_ci std::list<std::shared_ptr<AbilityRecord>> devRecLru_; 126eace7efcSopenharmony_ci std::shared_ptr<AbilityRecord> AddToProcLru(std::shared_ptr<AbilityRecord> abilityRecord); 127eace7efcSopenharmony_ci std::shared_ptr<AbilityRecord> AddToDevLru(std::shared_ptr<AbilityRecord> abilityRecord, 128eace7efcSopenharmony_ci std::shared_ptr<AbilityRecord> rec); 129eace7efcSopenharmony_ci void RemoveAbilityRecInDevList(std::shared_ptr<AbilityRecord> abilityRecord); 130eace7efcSopenharmony_ci void RemoveAbilityRecInProcList(std::shared_ptr<AbilityRecord> abilityRecord); 131eace7efcSopenharmony_ci std::shared_ptr<AbilityRecord> GetAbilityRecInProcList(const AbilityRequest &abilityRequest); 132eace7efcSopenharmony_ci bool IsRecInfoSame(const AbilityRequest& abilityRequest, std::shared_ptr<AbilityRecord> abilityRecord); 133eace7efcSopenharmony_ci DISALLOW_COPY_AND_MOVE(AbilityCacheManager); 134eace7efcSopenharmony_ci}; 135eace7efcSopenharmony_ci} // namespace AAFwk 136eace7efcSopenharmony_ci} // namespace OHOS 137eace7efcSopenharmony_ci#endif // OHOS_ABILITY_RUNTIME__ABILITY_CACHE_MANAGER_H