1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef OHOS_ABILITY_RUNTIME_EXIT_RESIDENT_PROCESS_MANAGER_H
17 #define OHOS_ABILITY_RUNTIME_EXIT_RESIDENT_PROCESS_MANAGER_H
18 
19 #include <string>
20 #include <vector>
21 
22 #include "bundle_info.h"
23 #include "cpp/mutex.h"
24 #include "nocopyable.h"
25 #include "refbase.h"
26 
27 
28 namespace OHOS {
29 namespace AppExecFwk {
30 enum class MemorySizeState {
31     MEMORY_SIZE_SUFFICIENT = 0,
32     MEMORY_SIZE_INSUFFICIENT = 1
33 };
34 
35 struct ExitResidentProcessInfo {
36     ExitResidentProcessInfo() = default;
ExitResidentProcessInfoExitResidentProcessInfo37     ExitResidentProcessInfo(const std::string &bundleName, int32_t uid)
38         : bundleName(bundleName), uid(uid) {}
39     std::string bundleName;
40     int32_t uid = 0;
41 };
42 
43 class ExitResidentProcessManager {
44 public:
45     static ExitResidentProcessManager &GetInstance();
46     ~ExitResidentProcessManager();
47     bool IsMemorySizeSufficent() const;
48     bool RecordExitResidentBundleName(const std::string &bundleName, int32_t uid);
49     void RecordExitResidentBundleDependedOnWeb(const std::string &bundleName, int32_t uid);
50     int32_t HandleMemorySizeInSufficent();
51     int32_t HandleMemorySizeSufficient(std::vector<ExitResidentProcessInfo>& bundleNames);
52     void HandleExitResidentBundleDependedOnWeb(std::vector<ExitResidentProcessInfo>& bundleNames);
53     void QueryExitBundleInfos(const std::vector<ExitResidentProcessInfo>& exitBundleNames,
54         std::vector<AppExecFwk::BundleInfo>& exitBundleInfos);
55     bool IsKilledForUpgradeWeb(const std::string &bundleName) const;
56 
57 private:
58     ExitResidentProcessManager();
59     MemorySizeState currentMemorySizeState_ = MemorySizeState::MEMORY_SIZE_SUFFICIENT;
60     std::vector<ExitResidentProcessInfo> exitResidentInfos_;
61     std::vector<ExitResidentProcessInfo> exitResidentBundlesDependedOnWeb_;
62     mutable ffrt::mutex mutexLock_;
63     mutable ffrt::mutex webMutexLock_;
64     DISALLOW_COPY_AND_MOVE(ExitResidentProcessManager);
65 };
66 }  // namespace AppExecFwk
67 }  // namespace OHOS
68 #endif  // OHOS_ABILITY_RUNTIME_EXIT_RESIDENT_PROCESS_MANAGER_H
69