1eace7efcSopenharmony_ci/*
2eace7efcSopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
3eace7efcSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4eace7efcSopenharmony_ci * you may not use this file except in compliance with the License.
5eace7efcSopenharmony_ci * You may obtain a copy of the License at
6eace7efcSopenharmony_ci *
7eace7efcSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8eace7efcSopenharmony_ci *
9eace7efcSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10eace7efcSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11eace7efcSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12eace7efcSopenharmony_ci * See the License for the specific language governing permissions and
13eace7efcSopenharmony_ci * limitations under the License.
14eace7efcSopenharmony_ci */
15eace7efcSopenharmony_ci
16eace7efcSopenharmony_ci#ifndef OHOS_ABILITY_RUNTIME_CACHE_PROCESS_MANAGER_H
17eace7efcSopenharmony_ci#define OHOS_ABILITY_RUNTIME_CACHE_PROCESS_MANAGER_H
18eace7efcSopenharmony_ci
19eace7efcSopenharmony_ci#include <memory>
20eace7efcSopenharmony_ci#include <deque>
21eace7efcSopenharmony_ci#include <mutex>
22eace7efcSopenharmony_ci#include <set>
23eace7efcSopenharmony_ci#include <unordered_set>
24eace7efcSopenharmony_ci#include "singleton.h"
25eace7efcSopenharmony_ci#include "app_running_record.h"
26eace7efcSopenharmony_ci#include "cpp/mutex.h"
27eace7efcSopenharmony_ci
28eace7efcSopenharmony_cinamespace OHOS {
29eace7efcSopenharmony_cinamespace AppExecFwk {
30eace7efcSopenharmony_ci
31eace7efcSopenharmony_ciclass CacheProcessManager {
32eace7efcSopenharmony_ci    DECLARE_DELAYED_SINGLETON(CacheProcessManager);
33eace7efcSopenharmony_cipublic:
34eace7efcSopenharmony_ci    bool QueryEnableProcessCache();
35eace7efcSopenharmony_ci    void SetAppMgr(const std::weak_ptr<AppMgrServiceInner> &appMgr);
36eace7efcSopenharmony_ci    bool PenddingCacheProcess(const std::shared_ptr<AppRunningRecord> &appRecord);
37eace7efcSopenharmony_ci    bool CheckAndCacheProcess(const std::shared_ptr<AppRunningRecord> &appRecord);
38eace7efcSopenharmony_ci    bool IsCachedProcess(const std::shared_ptr<AppRunningRecord> &appRecord);
39eace7efcSopenharmony_ci    void OnProcessKilled(const std::shared_ptr<AppRunningRecord> &appRecord);
40eace7efcSopenharmony_ci    bool ReuseCachedProcess(const std::shared_ptr<AppRunningRecord> &appRecord);
41eace7efcSopenharmony_ci    bool IsAppSupportProcessCache(const std::shared_ptr<AppRunningRecord> &appRecord);
42eace7efcSopenharmony_ci    bool IsAppShouldCache(const std::shared_ptr<AppRunningRecord> &appRecord);
43eace7efcSopenharmony_ci    void RefreshCacheNum();
44eace7efcSopenharmony_ci    std::string PrintCacheQueue();
45eace7efcSopenharmony_ci    void PrepareActivateCache(const std::shared_ptr<AppRunningRecord> &appRecord);
46eace7efcSopenharmony_ci    void OnAppProcessCacheBlocked(const std::shared_ptr<AppRunningRecord> &appRecord);
47eace7efcSopenharmony_ci    void CheckAndSetProcessCacheEnable(const std::shared_ptr<AppRunningRecord> &appRecord);
48eace7efcSopenharmony_ciprivate:
49eace7efcSopenharmony_ci    bool IsAppAbilitiesEmpty(const std::shared_ptr<AppRunningRecord> &appRecord);
50eace7efcSopenharmony_ci    int GetCurrentCachedProcNum();
51eace7efcSopenharmony_ci    void RemoveCacheRecord(const std::shared_ptr<AppRunningRecord> &appRecord);
52eace7efcSopenharmony_ci    void ShrinkAndKillCache();
53eace7efcSopenharmony_ci    bool KillProcessByRecord(const std::shared_ptr<AppRunningRecord> &appRecord);
54eace7efcSopenharmony_ci    void AddToApplicationSet(const std::shared_ptr<AppRunningRecord> &appRecord);
55eace7efcSopenharmony_ci    void RemoveFromApplicationSet(const std::shared_ptr<AppRunningRecord> &appRecord);
56eace7efcSopenharmony_ci    bool CheckAndNotifyCachedState(const std::shared_ptr<AppRunningRecord> &appRecord);
57eace7efcSopenharmony_ci    bool IsAppContainsSrvExt(const std::shared_ptr<AppRunningRecord> &appRecord);
58eace7efcSopenharmony_ci    bool IsAppSupportProcessCacheInnerFirst(const std::shared_ptr<AppRunningRecord> &appRecord);
59eace7efcSopenharmony_ci    bool IsProcessSupportHotStart(const std::shared_ptr<AppRunningRecord> &appRecord);
60eace7efcSopenharmony_ci    bool warmStartProcesEnable_ = false;
61eace7efcSopenharmony_ci    int32_t maxProcCacheNum_ = 0;
62eace7efcSopenharmony_ci    std::deque<std::shared_ptr<AppRunningRecord>> cachedAppRecordQueue_;
63eace7efcSopenharmony_ci    ffrt::recursive_mutex cacheQueueMtx;
64eace7efcSopenharmony_ci    std::weak_ptr<AppMgrServiceInner> appMgr_;
65eace7efcSopenharmony_ci    bool shouldCheckApi = true;
66eace7efcSopenharmony_ci    // whether the feature should check setSupportedProcessCache value or not
67eace7efcSopenharmony_ci    bool shouldCheckSupport = true;
68eace7efcSopenharmony_ci    // bundleName->uid->record
69eace7efcSopenharmony_ci    std::map<std::string, std::map<int32_t, std::set<std::shared_ptr<AppRunningRecord>>>> sameAppSet;
70eace7efcSopenharmony_ci    // stores records that are servcie extension
71eace7efcSopenharmony_ci    std::set<std::shared_ptr<AppRunningRecord>> srvExtRecords;
72eace7efcSopenharmony_ci    // stores records that has been checked service extension
73eace7efcSopenharmony_ci    std::unordered_set<std::shared_ptr<AppRunningRecord>> srvExtCheckedFlag;
74eace7efcSopenharmony_ci};
75eace7efcSopenharmony_ci} // namespace OHOS
76eace7efcSopenharmony_ci} // namespace AppExecFwk
77eace7efcSopenharmony_ci
78eace7efcSopenharmony_ci#endif