/* * Copyright (c) 2024 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef OHOS_ABILITY_RUNTIME__ABILITY_CACHE_MANAGER_H #define OHOS_ABILITY_RUNTIME__ABILITY_CACHE_MANAGER_H #include #include #include #include #include "ability_config.h" #include "ability_info.h" #include "ability_record.h" namespace OHOS { namespace AAFwk { /** * @class AbilityCacheManager * AbilityCacheManager provides a lru cache for managing ability record. */ class AbilityCacheManager { public: using AbilityInfo = OHOS::AppExecFwk::AbilityInfo; using AbilityType = OHOS::AppExecFwk::AbilityType; /** * Get ability cache manager. * @return AbilityCacheManager */ static AbilityCacheManager &GetInstance(void); /** * Init the ability cache manager with capacity for the device (devCapacity) * and the capacity for a single process(procCapacity) */ void Init(uint32_t devCapacity, uint32_t procCapacity); /** * Put a single ability record into ability cache manager. * @param abilityRecord the ability record to be putted into cache manager. * @return AbilityRecord if one is eliminated, otherwise nullptr. */ std::shared_ptr Put(std::shared_ptr abilityRecord); /** * Remove a single ability record from ability cache manager. * @param abilityRecord, the ability record to be removed into cache manager. */ void Remove(std::shared_ptr abilityRecord); /** * Get a single ability by abilityRequest record from ability cache manager, * this will remove the AbilityRecord by default * @param abilityRequest the ability request to be searched in cache manager. * @return AbilityRecord if one is matched, otherwise nullptr. */ std::shared_ptr Get(const AbilityRequest &abilityRequest); /** * Get a single ability by token from ability cache manager. * @param token the ability token to be searched in cache manager. * @return AbilityRecord if one is matched, otherwise nullptr. */ std::shared_ptr FindRecordByToken(const sptr &token); /** * Get all the abilities of current ability cache manager. * @return AbilityRecord list. */ std::list> GetAbilityList(); /** * Get a single ability by sessionId from ability cache manager. * @param assertSessionId the ability assertSessionId to be searched in cache manager. * @return AbilityRecord if one is matched, otherwise nullptr. */ std::shared_ptr FindRecordBySessionId(const std::string &assertSessionId); /** * Get a single ability by serviceKey from ability cache manager. * @param serviceKey the ability serviceKey to be searched in cache manager. * @return AbilityRecord if one is matched, otherwise nullptr. */ std::shared_ptr FindRecordByServiceKey(const std::string &serviceKey); /** * Remove the launcher death recipient from ability cache manager. */ void RemoveLauncherDeathRecipient(); /** * Sign the restart flag by uid of ability from ability cache manager. * @param uid the ability uid to be searched in cache manager. */ void SignRestartAppFlag(int32_t uid); /** * Delete the invalid ability by bundleName from ability cache manager. * @param bundleName the ability bundleName to be searched in cache manager. */ void DeleteInvalidServiceRecord(const std::string &bundleName); private: AbilityCacheManager(); ~AbilityCacheManager(); struct ProcRecordsInfo { std::list> recList; uint32_t cnt; }; uint32_t devLruCapacity_ = 0; uint32_t procLruCapacity_ = 0; uint32_t devLruCnt_ = 0; std::mutex mutex_; std::map procLruMap_; std::list> devRecLru_; std::shared_ptr AddToProcLru(std::shared_ptr abilityRecord); std::shared_ptr AddToDevLru(std::shared_ptr abilityRecord, std::shared_ptr rec); void RemoveAbilityRecInDevList(std::shared_ptr abilityRecord); void RemoveAbilityRecInProcList(std::shared_ptr abilityRecord); std::shared_ptr GetAbilityRecInProcList(const AbilityRequest &abilityRequest); bool IsRecInfoSame(const AbilityRequest& abilityRequest, std::shared_ptr abilityRecord); DISALLOW_COPY_AND_MOVE(AbilityCacheManager); }; } // namespace AAFwk } // namespace OHOS #endif // OHOS_ABILITY_RUNTIME__ABILITY_CACHE_MANAGER_H