1 /*
2  * Copyright (c) 2022 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 FOUNDATION_BUNDLE_FRAMEWORK_SERVICE_INCLUDE_APP_CONTROL_MANAGER_H
17 #define FOUNDATION_BUNDLE_FRAMEWORK_SERVICE_INCLUDE_APP_CONTROL_MANAGER_H
18 
19 #include <mutex>
20 #include <unordered_map>
21 
22 #include "app_control_manager_db_interface.h"
23 #include "app_jump_interceptor_manager_db_interface.h"
24 #include "bundle_common_event_mgr.h"
25 #include "singleton.h"
26 #include "want.h"
27 
28 namespace OHOS {
29 namespace AppExecFwk {
30 class AppControlManager : public DelayedSingleton<AppControlManager> {
31 public:
32     using Want = OHOS::AAFwk::Want;
33 
34     AppControlManager();
35     ~AppControlManager();
36 
37     ErrCode AddAppInstallControlRule(const std::string &callingName,
38         const std::vector<std::string> &appIds, const std::string &controlRuleType, int32_t userId);
39 
40     ErrCode DeleteAppInstallControlRule(const std::string &callingName, const std::string &controlRuleType,
41         const std::vector<std::string> &appIds, int32_t userId);
42 
43     ErrCode DeleteAppInstallControlRule(const std::string &callingName,
44         const std::string &controlRuleType, int32_t userId);
45 
46     ErrCode GetAppInstallControlRule(const std::string &callingName,
47         const std::string &controlRuleType, int32_t userId, std::vector<std::string> &appIds);
48 
49     ErrCode AddAppRunningControlRule(const std::string &callingName,
50         const std::vector<AppRunningControlRule> &controlRules, int32_t userId);
51     ErrCode DeleteAppRunningControlRule(const std::string &callingName,
52         const std::vector<AppRunningControlRule> &controlRules, int32_t userId);
53     ErrCode DeleteAppRunningControlRule(const std::string &callingName, int32_t userId);
54     ErrCode GetAppRunningControlRule(const std::string &callingName, int32_t userId, std::vector<std::string> &appIds);
55     ErrCode GetAppRunningControlRule(
56         const std::string &bundleName, int32_t userId, AppRunningControlRuleResult &controlRule);
57 
58     ErrCode ConfirmAppJumpControlRule(const std::string &callerBundleName, const std::string &targetBundleName,
59         int32_t userId);
60     ErrCode AddAppJumpControlRule(const std::vector<AppJumpControlRule> &controlRules, int32_t userId);
61     ErrCode DeleteAppJumpControlRule(const std::vector<AppJumpControlRule> &controlRules, int32_t userId);
62     ErrCode DeleteRuleByCallerBundleName(const std::string &callerBundleName, int32_t userId);
63     ErrCode DeleteRuleByTargetBundleName(const std::string &targetBundleName, int32_t userId);
64     ErrCode GetAppJumpControlRule(const std::string &callerBundleName, const std::string &targetBundleName,
65         int32_t userId, AppJumpControlRule &controlRule);
66 
67     ErrCode SetDisposedStatus(const std::string &appId, const Want& want, int32_t userId);
68 
69     ErrCode DeleteDisposedStatus(const std::string &appId, int32_t userId);
70 
71     ErrCode GetDisposedStatus(const std::string &appId, Want& want, int32_t userId);
72 
73     bool IsAppInstallControlEnabled() const;
74 
75     void SetAppInstallControlStatus();
76 
77     ErrCode SetDisposedRule(const std::string &callerName, const std::string &appId,
78         const DisposedRule &DisposedRule, int32_t appIndex, int32_t userId);
79 
80     ErrCode GetDisposedRule(const std::string &callerName, const std::string &appId,
81         DisposedRule &DisposedRule, int32_t appIndex, int32_t userId);
82 
83     ErrCode DeleteDisposedRule(const std::string &callerName, const std::string &appId,
84         int32_t appIndex, int32_t userId);
85 
86     ErrCode GetAbilityRunningControlRule(const std::string &bundleName, int32_t appIndex, int32_t userId,
87         std::vector<DisposedRule>& disposedRules);
88 
89     ErrCode DeleteAllDisposedRuleByBundle(const InnerBundleInfo &bundleInfo, int32_t appIndex, int32_t userId);
90 
91     void SetDisposedRuleOnlyForBms(const std::string &appId);
92 
93     void DeleteDisposedRuleOnlyForBms(const std::string &appId);
94 
95 private:
96     void KillRunningApp(const std::vector<AppRunningControlRule> &rules, int32_t userId) const;
97     void DeleteAppRunningRuleCache(std::string &key);
98     void DeleteAbilityRunningRuleCache(std::string &key);
99     void DeleteAbilityRunningRuleBmsCache(const std::string &appId);
100     bool CheckCanDispose(const std::string &appId, int32_t userId);
101 
102     bool isAppInstallControlEnabled_ = false;
103     std::shared_ptr<IAppControlManagerDb> appControlManagerDb_;
104     std::shared_ptr<IAppJumpInterceptorlManagerDb> appJumpInterceptorManagerDb_;
105     std::unordered_map<std::string, AppRunningControlRuleResult> appRunningControlRuleResult_;
106     std::shared_ptr<BundleCommonEventMgr> commonEventMgr_;
107     std::mutex appRunningControlMutex_;
108     std::unordered_map<std::string, std::vector<DisposedRule>> abilityRunningControlRuleCache_;
109     std::mutex abilityRunningControlRuleMutex_;
110     std::unordered_map<std::string, DisposedRule> abilityRunningControlRuleCacheForBms_;
111     std::vector<std::string> noControllingList_;
112 };
113 } // AppExecFwk
114 } // OHOS
115 #endif // FOUNDATION_BUNDLE_FRAMEWORK_SERVICE_INCLUDE_APP_CONTROL_MANAGER_H
116