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_CACHE_PROCESS_MANAGER_H
17 #define OHOS_ABILITY_RUNTIME_CACHE_PROCESS_MANAGER_H
18 
19 #include <memory>
20 #include <deque>
21 #include <mutex>
22 #include <set>
23 #include <unordered_set>
24 #include "singleton.h"
25 #include "app_running_record.h"
26 #include "cpp/mutex.h"
27 
28 namespace OHOS {
29 namespace AppExecFwk {
30 
31 class CacheProcessManager {
32     DECLARE_DELAYED_SINGLETON(CacheProcessManager);
33 public:
34     bool QueryEnableProcessCache();
35     void SetAppMgr(const std::weak_ptr<AppMgrServiceInner> &appMgr);
36     bool PenddingCacheProcess(const std::shared_ptr<AppRunningRecord> &appRecord);
37     bool CheckAndCacheProcess(const std::shared_ptr<AppRunningRecord> &appRecord);
38     bool IsCachedProcess(const std::shared_ptr<AppRunningRecord> &appRecord);
39     void OnProcessKilled(const std::shared_ptr<AppRunningRecord> &appRecord);
40     void ReuseCachedProcess(const std::shared_ptr<AppRunningRecord> &appRecord);
41     bool IsAppSupportProcessCache(const std::shared_ptr<AppRunningRecord> &appRecord);
42     bool IsAppShouldCache(const std::shared_ptr<AppRunningRecord> &appRecord);
43     void RefreshCacheNum();
44     std::string PrintCacheQueue();
45     void PrepareActivateCache(const std::shared_ptr<AppRunningRecord> &appRecord);
46     void OnAppProcessCacheBlocked(const std::shared_ptr<AppRunningRecord> &appRecord);
47     void CheckAndSetProcessCacheEnable(const std::shared_ptr<AppRunningRecord> &appRecord);
48 private:
49     bool IsAppAbilitiesEmpty(const std::shared_ptr<AppRunningRecord> &appRecord);
50     int GetCurrentCachedProcNum();
51     void RemoveCacheRecord(const std::shared_ptr<AppRunningRecord> &appRecord);
52     void ShrinkAndKillCache();
53     bool KillProcessByRecord(const std::shared_ptr<AppRunningRecord> &appRecord);
54     void AddToApplicationSet(const std::shared_ptr<AppRunningRecord> &appRecord);
55     void RemoveFromApplicationSet(const std::shared_ptr<AppRunningRecord> &appRecord);
56     bool CheckAndNotifyCachedState(const std::shared_ptr<AppRunningRecord> &appRecord);
57     bool IsAppContainsSrvExt(const std::shared_ptr<AppRunningRecord> &appRecord);
58     bool IsAppSupportProcessCacheInnerFirst(const std::shared_ptr<AppRunningRecord> &appRecord);
59     bool IsProcessSupportHotStart(const std::shared_ptr<AppRunningRecord> &appRecord);
60     bool warmStartProcesEnable_ = false;
61     int32_t maxProcCacheNum_ = 0;
62     int32_t allowedCacheNum_ = 0;
63     std::deque<std::shared_ptr<AppRunningRecord>> cachedAppRecordQueue_;
64     ffrt::recursive_mutex cacheQueueMtx;
65     std::weak_ptr<AppMgrServiceInner> appMgr_;
66     bool shouldCheckApi = true;
67     // whether the feature should check setSupportedProcessCache value or not
68     bool shouldCheckSupport = true;
69     // bundleName->uid->record
70     std::map<std::string, std::map<int32_t, std::set<std::shared_ptr<AppRunningRecord>>>> sameAppSet;
71     // stores records that are servcie extension
72     std::set<std::shared_ptr<AppRunningRecord>> srvExtRecords;
73     // stores records that has been checked service extension
74     std::unordered_set<std::shared_ptr<AppRunningRecord>> srvExtCheckedFlag;
75 };
76 } // namespace OHOS
77 } // namespace AppExecFwk
78 
79 #endif