daaf9e4b创建于 2025年12月13日历史提交
/*
 * 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 <map>
#include <list>
#include <string>
#include <mutex>

#include "ability_config.h"
#include "ability_info.h"
#include "base_extension_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 BaseExtensionRecord if one is eliminated, otherwise nullptr.
     */
    std::shared_ptr<BaseExtensionRecord> Put(std::shared_ptr<BaseExtensionRecord> 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<BaseExtensionRecord> abilityRecord);

    /**
     * Get a single ability by abilityRequest record from ability cache manager,
     * this will remove the BaseExtensionRecord by default
     * @param abilityRequest the ability request to be searched in cache manager.
     * @return BaseExtensionRecord if one is matched, otherwise nullptr.
     */
    std::shared_ptr<BaseExtensionRecord> 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 BaseExtensionRecord if one is matched, otherwise nullptr.
     */
    std::shared_ptr<BaseExtensionRecord> FindRecordByToken(const sptr<IRemoteObject> &token);

    /**
     * Get all the abilities of current ability cache manager.
     * @return BaseExtensionRecord list.
     */
    std::list<std::shared_ptr<BaseExtensionRecord>> GetAbilityList();

    /**
     * Get a single ability by sessionId from ability cache manager.
     * @param assertSessionId the ability assertSessionId to be searched in cache manager.
     * @return BaseExtensionRecord if one is matched, otherwise nullptr.
     */
    std::shared_ptr<BaseExtensionRecord> 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 BaseExtensionRecord if one is matched, otherwise nullptr.
     */
    std::shared_ptr<BaseExtensionRecord> 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.
     * @param instanceKey the instance key of the process.
     */
    void SignRestartAppFlag(int32_t uid, const std::string &instanceKey);
    void SignRestartProcess(int32_t pid);

    /**
     * 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<std::shared_ptr<BaseExtensionRecord>> recList;
            uint32_t cnt;
        };
        uint32_t devLruCapacity_ = 0;
        uint32_t procLruCapacity_ = 0;
        uint32_t devLruCnt_ = 0;
        std::map<uint32_t, ProcRecordsInfo> procLruMap_;
        std::list<std::shared_ptr<BaseExtensionRecord>> devRecLru_;
        std::shared_ptr<BaseExtensionRecord> AddToProcLru(std::shared_ptr<BaseExtensionRecord> abilityRecord);
        std::shared_ptr<BaseExtensionRecord> AddToDevLru(std::shared_ptr<BaseExtensionRecord> abilityRecord,
            std::shared_ptr<BaseExtensionRecord> rec);
        std::mutex mutex_;
        void RemoveAbilityRecInDevList(std::shared_ptr<BaseExtensionRecord> abilityRecord);
        void RemoveAbilityRecInProcList(std::shared_ptr<BaseExtensionRecord> abilityRecord);
        std::shared_ptr<BaseExtensionRecord> GetAbilityRecInProcList(const AbilityRequest &abilityRequest);
        bool IsRecInfoSame(const AbilityRequest& abilityRequest, std::shared_ptr<BaseExtensionRecord> abilityRecord);
        DISALLOW_COPY_AND_MOVE(AbilityCacheManager);
};
} // namespace AAFwk
} // namespace OHOS
#endif  // OHOS_ABILITY_RUNTIME__ABILITY_CACHE_MANAGER_H