99148157创建于 2025年12月30日历史提交
/*
 * Copyright (c) 2021-2025 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_MISSION_LIST_H
#define OHOS_ABILITY_RUNTIME_MISSION_LIST_H

#include <list>
#include <memory>

#include "ability_manager_constants.h"
#include "iremote_object.h"
#include "mission.h"
#include "mission_ability_record.h"

using IRemoteObject = OHOS::IRemoteObject;

namespace OHOS {
namespace AAFwk {
enum MissionListType {
    CURRENT = 0,
    DEFAULT_STANDARD,
    DEFAULT_SINGLE,
    LAUNCHER
};

class MissionList : public std::enable_shared_from_this<MissionList> {
public:
    explicit MissionList(MissionListType type = MissionListType::CURRENT);
    explicit MissionList(const MissionList& missionList) : type_(missionList.type_), missions_(missionList.missions_) {}
    virtual ~MissionList();

    /**
     * Add mission to top of this mission list.
     *
     * @param mission target mission
     */
    void AddMissionToTop(const std::shared_ptr<Mission> &mission);

    /**
     * Remove mission from this mission list.
     *
     * @param mission target mission
     */
    void RemoveMission(const std::shared_ptr<Mission> &mission);

    /**
     * Get singleton mission by name.
     *
     * @param missionName target mission name.
     * @return founded mission.
     */
    std::shared_ptr<Mission> GetSingletonMissionByName(const std::string& missionName) const;

    /**
     * Get specified mission by name and flag.
     *
     * @param missionName target mission name.
     * @param flag target mission specified flag.
     * @return founded mission.
     */
    std::shared_ptr<Mission> GetSpecifiedMission(const std::string& missionName, const std::string& flag) const;

    /**
     * Get recent standard mission by name.
     *
     * @param missionName target mission name.
     * @return founded mission.
     */
    std::shared_ptr<Mission> GetRecentStandardMission(const std::string& missionName) const;

    /**
     * Get top mission of this mission list.
     *
     * @return founded mission.
     */
    std::shared_ptr<Mission> GetTopMission() const;

    /**
     * @brief Get the Ability Record By Token object
     *
     * @param token the ability to search
     * @return std::shared_ptr<AbilityRecord> the ability
     */
    std::shared_ptr<AbilityRecord> GetAbilityRecordByToken(const sptr<IRemoteObject> &token) const;

    /**
     * @brief remove mission by ability record
     *
     * @param abilityRecord the ability need to remove
     */
    void RemoveMissionByAbilityRecord(const std::shared_ptr<AbilityRecord> &abilityRecord);

    /**
     * whether the missionList contains mission.
     *
     * @return finded mission.
     */
    bool IsEmpty();

    /**
     * @brief Get the Top Ability object
     *
     * @return MissionAbilityRecordPtr the top ability
     */
    MissionAbilityRecordPtr GetTopAbility() const;

    /**
     * @brief Get the Mission By Id object
     *
     * @param missionId the given missionId
     * @return the mission of the given id
     */
    std::shared_ptr<Mission> GetMissionById(int missionId) const;

    /**
     * @brief Get the Mission By Id object
     *
     * @param missionId the given missionId
     * @return the mission of the given id
     */
    std::list<std::shared_ptr<Mission>>& GetAllMissions();

    /**
     * @brief Get the type of the missionList
     *
     * @return the mission list type
     */
    MissionListType GetType() const;

    /**
     * @brief Get the launcher root
     *
     * @return launcher root
     */
    MissionAbilityRecordPtr GetLauncherRoot() const;

    /**
     * @brief get ability record by id
     *
     * @param abilityRecordId ability record id
     * @return std::shared_ptr<AbilityRecord> return ability record
     */
    std::shared_ptr<AbilityRecord> GetAbilityRecordById(int64_t abilityRecordId) const;

    /**
     * @brief Get the Ability Record By Caller object
     *
     * @param caller the ability which call terminateAbility
     * @param requestCode startAbilityWithRequstCode
     * @return std::shared_ptr<AbilityRecord> the ability record which find
     */
    std::shared_ptr<AbilityRecord> GetAbilityRecordByCaller(
        const std::shared_ptr<AbilityRecord> &caller, int requestCode);

    /**
     * @brief Get the Ability Record By elementName
     *
     * @param element
     * @return std::shared_ptr<AbilityRecord> the ability record which find
     */
    std::shared_ptr<AbilityRecord> GetAbilityRecordByName(const AppExecFwk::ElementName &element);

    /**
     * @brief Get the Ability Records By elementName
     *
     * @param element conditions for search
     * @param records out of parameter, list of returned records
     * @return void
     */
    void GetAbilityRecordsByName(
        const AppExecFwk::ElementName &element, std::vector<std::shared_ptr<AbilityRecord>> &records);

    /**
     * Get ability token by target mission id.
     *
     * @param missionId target missionId.
     * @return the ability token of target mission.
     */
    sptr<IRemoteObject> GetAbilityTokenByMissionId(int32_t missionId);

    /**
     * Handle uninstall bundle.
     *
     * @param bundleName name of bundle.
     * @param uid the uid of bundle.
     */
    void HandleUnInstallApp(const std::string &bundleName, int32_t uid);

    /**
     * @brief dump mission
     *
     * @param info dump result.
     */
    void Dump(std::vector<std::string> &info);

    /**
     * @brief dump mission list info
     *
     * @param info dump result.
     */
    void DumpList(std::vector<std::string> &info, bool isClient);

    /**
     * @brief Dump mission state by ability record ID
     * @param info Output vector to store dump results
     * @param isClient Whether the dump request is from client
     * @param abilityRecordId The ID of target ability record
     * @param params Additional parameters for dump
     */
    void DumpStateByRecordId(
        std::vector<std::string> &info, bool isClient, int32_t abilityRecordId, const std::vector<std::string> &params);

    /**
     * @brief Get mission by specified flag
     * @param want The want object containing search conditions
     * @param flag The flag to identify mission
     * @return The matched mission, nullptr if not found
     */
    std::shared_ptr<Mission> GetMissionBySpecifiedFlag(const AAFwk::Want &want, const std::string &flag) const;

    /**
     * @brief Get mission count by user ID
     * @param targetUid The user ID to query
     * @return Number of missions belong to the user
     */
    int32_t GetMissionCountByUid(int32_t targetUid) const;

    /**
     * @brief Find the earliest created mission in the list
     * @param targetMission Output parameter to store the found mission
     */
    void FindEarliestMission(std::shared_ptr<Mission>& targetMission) const;

    /**
     * @brief Get total mission count in the list
     * @return Number of missions in current list
     */
    int32_t GetMissionCount() const;

    /**
     * @brief Get active ability list for specific user
     * @param uid The user ID to query
     * @param abilityList Output vector to store ability names
     * @param pid Optional process ID filter, default is NO_PID
     */
    void GetActiveAbilityList(int32_t uid, std::vector<std::string> &abilityList, int32_t pid = NO_PID);
    
    /**
     * @brief Sign restart flag for application
     * @param uid The user ID of target application
     * @param instanceKey The instance key to identify application
     */
    void SignRestartAppFlag(int32_t uid, const std::string &instanceKey);

private:
    /**
     * @brief Get the name of mission list type
     * @return String representation of the mission list type
     */
    std::string GetTypeName();

    /**
     * @brief Check if mission matches initial conditions
     * @param mission The mission to check
     * @param bundleName Target bundle name to match
     * @param uid Target user ID to match
     * @return true if mission matches the conditions, false otherwise
     */
    bool MatchedInitialMission(const std::shared_ptr<Mission>& mission, const std::string &bundleName, int32_t uid);

    MissionListType type_;
    std::list<std::shared_ptr<Mission>> missions_ {};
};
}  // namespace AAFwk
}  // namespace OHOS
#endif  // OHOS_ABILITY_RUNTIME_MISSION_LIST_H