199552fe9Sopenharmony_ci/*
299552fe9Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
399552fe9Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
499552fe9Sopenharmony_ci * you may not use this file except in compliance with the License.
599552fe9Sopenharmony_ci * You may obtain a copy of the License at
699552fe9Sopenharmony_ci *
799552fe9Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
899552fe9Sopenharmony_ci *
999552fe9Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1099552fe9Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1199552fe9Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1299552fe9Sopenharmony_ci * See the License for the specific language governing permissions and
1399552fe9Sopenharmony_ci * limitations under the License.
1499552fe9Sopenharmony_ci */
1599552fe9Sopenharmony_ci#ifndef FOUNDATION_RESOURCESCHEDULE_STANDBY_SERVICE_STRATEGY_INCLUDE_RUNNINGLOCK_STRATEGY_H
1699552fe9Sopenharmony_ci#define FOUNDATION_RESOURCESCHEDULE_STANDBY_SERVICE_STRATEGY_INCLUDE_RUNNINGLOCK_STRATEGY_H
1799552fe9Sopenharmony_ci#include "ibase_strategy.h"
1899552fe9Sopenharmony_ci
1999552fe9Sopenharmony_ci#include <unordered_map>
2099552fe9Sopenharmony_ci#include <set>
2199552fe9Sopenharmony_ci#include <string>
2299552fe9Sopenharmony_ci
2399552fe9Sopenharmony_cinamespace OHOS {
2499552fe9Sopenharmony_cinamespace DevStandbyMgr {
2599552fe9Sopenharmony_cistruct ProxiedAppInfo {
2699552fe9Sopenharmony_ci    std::string name_ {""};
2799552fe9Sopenharmony_ci    int32_t uid_ {-1};
2899552fe9Sopenharmony_ci    int32_t pid_ {-1};
2999552fe9Sopenharmony_ci    uint8_t appExemptionFlag_ {0};
3099552fe9Sopenharmony_ci};
3199552fe9Sopenharmony_ci
3299552fe9Sopenharmony_cistruct ProxiedProcInfo {
3399552fe9Sopenharmony_ci    std::string name_ {""};
3499552fe9Sopenharmony_ci    int32_t uid_ {-1};
3599552fe9Sopenharmony_ci    std::set<int32_t> pids_ {};
3699552fe9Sopenharmony_ci    uint8_t appExemptionFlag_ {0};
3799552fe9Sopenharmony_ci};
3899552fe9Sopenharmony_ci
3999552fe9Sopenharmony_ciclass RunningLockStrategy : public IBaseStrategy {
4099552fe9Sopenharmony_cipublic:
4199552fe9Sopenharmony_ci    /**
4299552fe9Sopenharmony_ci     * @brief RunningLockStrategy HandleEvent by StandbyMessage.
4399552fe9Sopenharmony_ci     */
4499552fe9Sopenharmony_ci    void HandleEvent(const StandbyMessage& message) override;
4599552fe9Sopenharmony_ci
4699552fe9Sopenharmony_ci    /**
4799552fe9Sopenharmony_ci     * @brief RunningLockStrategy OnCreated.
4899552fe9Sopenharmony_ci     *
4999552fe9Sopenharmony_ci     * @return ERR_OK if OnCreated success, failed with other code.
5099552fe9Sopenharmony_ci     */
5199552fe9Sopenharmony_ci    ErrCode OnCreated() override;
5299552fe9Sopenharmony_ci
5399552fe9Sopenharmony_ci    /**
5499552fe9Sopenharmony_ci     * @brief RunningLockStrategy OnDestroy.
5599552fe9Sopenharmony_ci     *
5699552fe9Sopenharmony_ci     * @return ERR_OK if OnDestroy success, failed with other code.
5799552fe9Sopenharmony_ci     */
5899552fe9Sopenharmony_ci    ErrCode OnDestroy() override;
5999552fe9Sopenharmony_ci    void ShellDump(const std::vector<std::string>& argsInStr, std::string& result) override;
6099552fe9Sopenharmony_ciprotected:
6199552fe9Sopenharmony_ci    // native processes will not be proxied, except in restriction list
6299552fe9Sopenharmony_ci    virtual ErrCode InitNativeProcInfo();
6399552fe9Sopenharmony_ci    // add exemption flag according to message
6499552fe9Sopenharmony_ci    virtual void AddExemptionFlag(int32_t uid, const std::string& name, uint8_t flag);
6599552fe9Sopenharmony_ci    // remove exemption flag according to message
6699552fe9Sopenharmony_ci    virtual void RemoveExemptionFlag(int32_t uid, const std::string& name, uint8_t flag);
6799552fe9Sopenharmony_ci    // proxy app and native process
6899552fe9Sopenharmony_ci    virtual ErrCode ProxyAppAndProcess(bool isProxied);
6999552fe9Sopenharmony_ci    // clear record of proxied app and process
7099552fe9Sopenharmony_ci    virtual void ClearProxyRecord();
7199552fe9Sopenharmony_ci
7299552fe9Sopenharmony_ci    void SetProxiedAppList(std::vector<std::pair<int32_t, int32_t>>& proxiedAppList,
7399552fe9Sopenharmony_ci        const ProxiedProcInfo& info);
7499552fe9Sopenharmony_ci    void ProxyRunningLockList(bool isProxied, const std::vector<std::pair<int32_t, int32_t>>& proxiedAppList);
7599552fe9Sopenharmony_ciprivate:
7699552fe9Sopenharmony_ci    // update exemtion list when received exemtion list changed event
7799552fe9Sopenharmony_ci    ErrCode UpdateExemptionList(const StandbyMessage& message);
7899552fe9Sopenharmony_ci    // update resource config when received condition changed event
7999552fe9Sopenharmony_ci    ErrCode UpdateResourceConfig(const StandbyMessage& message);
8099552fe9Sopenharmony_ci    ErrCode StartProxy(const StandbyMessage& message);
8199552fe9Sopenharmony_ci    ErrCode StartProxyInner();
8299552fe9Sopenharmony_ci    ErrCode StopProxy(const StandbyMessage& message);
8399552fe9Sopenharmony_ci    ErrCode StopProxyInner();
8499552fe9Sopenharmony_ci    // add exemption to background task and work_scheduler task, unproxy running lock
8599552fe9Sopenharmony_ci    ErrCode UpdateBgTaskAppStatus(const StandbyMessage& message);
8699552fe9Sopenharmony_ci    void ResetProxyStatus(const StandbyMessage& message);
8799552fe9Sopenharmony_ci
8899552fe9Sopenharmony_ci    // application in exemption list or with bgtask will not be proxied.
8999552fe9Sopenharmony_ci    ErrCode InitProxiedAppInfo();
9099552fe9Sopenharmony_ci    // get all apps, system apps defaultly not be restricted.
9199552fe9Sopenharmony_ci    ErrCode GetAllAppInfos();
9299552fe9Sopenharmony_ci    ErrCode GetAllRunningAppInfo();
9399552fe9Sopenharmony_ci    ErrCode GetForegroundApplications();
9499552fe9Sopenharmony_ci    // get running work scheduler task and add work_scheduler flag to relative apps.
9599552fe9Sopenharmony_ci    ErrCode GetWorkSchedulerTask();
9699552fe9Sopenharmony_ci    // get background task, including continuous task and transient, defaultly not be constricted.
9799552fe9Sopenharmony_ci    ErrCode GetBackgroundTaskApp();
9899552fe9Sopenharmony_ci    // get exemption app list from standby service
9999552fe9Sopenharmony_ci    ErrCode GetExemptionConfig();
10099552fe9Sopenharmony_ci
10199552fe9Sopenharmony_ci    // when process is created or died, update proxy app info
10299552fe9Sopenharmony_ci    void HandleProcessStatusChanged(const StandbyMessage& message);
10399552fe9Sopenharmony_ci
10499552fe9Sopenharmony_ci    void GetAndCreateAppInfo(uint32_t uid, uint32_t pid, const std::string& bundleName);
10599552fe9Sopenharmony_ci    ErrCode GetExemptionConfigForApp(ProxiedProcInfo& appInfo, const std::string& bundleName);
10699552fe9Sopenharmony_ci
10799552fe9Sopenharmony_ci    void DumpShowDetailInfo(const std::vector<std::string>& argsInStr, std::string& result);
10899552fe9Sopenharmony_ciprotected:
10999552fe9Sopenharmony_ci    bool isProxied_ {false};
11099552fe9Sopenharmony_ciprivate:
11199552fe9Sopenharmony_ci    bool isIdleMaintence_ {false};
11299552fe9Sopenharmony_ci    // proxyed app and native process info
11399552fe9Sopenharmony_ci    std::unordered_map<std::string, ProxiedProcInfo> proxiedAppInfo_;
11499552fe9Sopenharmony_ci
11599552fe9Sopenharmony_ci    std::unordered_map<std::int32_t, std::string> uidBundleNmeMap_;
11699552fe9Sopenharmony_ci};
11799552fe9Sopenharmony_ci}  // namespace DevStandbyMgr
11899552fe9Sopenharmony_ci}  // namespace OHOS
11999552fe9Sopenharmony_ci#endif  // FOUNDATION_RESOURCESCHEDULE_STANDBY_SERVICE_STRATEGY_INCLUDE_RUNNINGLOCK_STRATEGY_H