/*
 * Copyright (c) 2023 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_APP_RECOVERY_MGR_H
#define OHOS_ABILITY_RUNTIME_APP_RECOVERY_MGR_H
 
#include "cpp/mutex.h"
#include <map>
#include "iremote_object.h"
#include "refbase.h"
#include "event_handler.h"
 
namespace OHOS {
namespace AppRecoveryMgr {
 
class AppRecoveryMgr {
public:
    static AppRecoveryMgr& GetInstance();
    void SetOnRemoteDieCallback(const sptr<IRemoteObject>& abilityToken,
        const std::function<void(const sptr<IRemoteObject>&)>& callback);
    
    void RemoveOnRemoteDieCallback(const sptr<IRemoteObject>& abilityToken);
    
    void HandleAppDied(const sptr<IRemoteObject>& remote);
    
    void Init(const std::shared_ptr<AppExecFwk::EventHandler>& handler);
    
    void Clear();
 
private:
    AppRecoveryMgr();
    ~AppRecoveryMgr();
    std::function<void(const sptr<IRemoteObject>&)> FindCallback(const sptr<IRemoteObject>& remote);
    
private:
    std::mutex mutex_;
    std::shared_ptr<AppExecFwk::EventHandler> handler_;
    std::map<sptr<IRemoteObject>, std::function<void(const sptr<IRemoteObject>&)>> dieCallbacks_;
};
 
} // namespace AppRecoveryMgr
} // namespace OHOS
 
#endif // OHOS_ABILITY_RUNTIME_APP_RECOVERY_MGR_H