1 /*
2  * Copyright (c) 2023 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 #include "bundle_install_plugin.h"
17 
18 #include <system_ability_definition.h>
19 
20 #include "app_control/app_control_proxy.h"
21 #include "array_string_serializer.h"
22 #include "edm_constants.h"
23 #include "edm_errors.h"
24 #include "edm_sys_manager.h"
25 
26 namespace OHOS {
27 namespace EDM {
SetAppInstallControlRuleType(AppExecFwk::AppInstallControlRuleType controlRuleType)28 void BundleInstallPlugin::SetAppInstallControlRuleType(AppExecFwk::AppInstallControlRuleType controlRuleType)
29 {
30     controlRuleType_ = controlRuleType;
31 }
32 
OnSetPolicy(std::vector<std::string> & data,std::vector<std::string> & currentData,int32_t userId)33 ErrCode BundleInstallPlugin::OnSetPolicy(std::vector<std::string> &data, std::vector<std::string> &currentData,
34     int32_t userId)
35 {
36     if (data.empty()) {
37         EDMLOGW("BundleInstallPlugin OnSetPolicy data is empty.");
38         return ERR_OK;
39     }
40     if (data.size() > EdmConstants::APPID_MAX_SIZE) {
41         EDMLOGE("BundleInstallPlugin OnSetPolicy input data is too large.");
42         return EdmReturnErrCode::PARAM_ERROR;
43     }
44 
45     std::vector<std::string> mergeData = ArrayStringSerializer::GetInstance()->SetUnionPolicyData(data, currentData);
46 
47     if (mergeData.size() > EdmConstants::APPID_MAX_SIZE) {
48         EDMLOGE("BundleInstallPlugin OnSetPolicy merge data is too large.");
49         return EdmReturnErrCode::PARAM_ERROR;
50     }
51 
52     auto appControlProxy = GetAppControlProxy();
53     if (!appControlProxy) {
54         EDMLOGE("BundleInstallPlugin OnSetPolicy GetAppControlProxy failed.");
55         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
56     }
57     ErrCode res = appControlProxy->AddAppInstallControlRule(data, controlRuleType_, userId);
58     if (res != ERR_OK) {
59         EDMLOGE("BundleInstallPlugin OnSetPolicyDone Faild %{public}d:", res);
60         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
61     }
62     currentData = mergeData;
63     return ERR_OK;
64 }
65 
GetBundlePolicy(std::string & policyData,MessageParcel & data,MessageParcel & reply,int32_t userId)66 ErrCode BundleInstallPlugin::GetBundlePolicy(std::string &policyData, MessageParcel &data, MessageParcel &reply,
67     int32_t userId)
68 {
69     std::vector<std::string> bundles;
70     ArrayStringSerializer::GetInstance()->Deserialize(policyData, bundles);
71     reply.WriteInt32(ERR_OK);
72     reply.WriteInt32(bundles.size());
73     reply.WriteStringVector(bundles);
74     return ERR_OK;
75 }
76 
OnRemovePolicy(std::vector<std::string> & data,std::vector<std::string> & currentData,int32_t userId)77 ErrCode BundleInstallPlugin::OnRemovePolicy(std::vector<std::string> &data, std::vector<std::string> &currentData,
78     int32_t userId)
79 {
80     if (data.empty()) {
81         EDMLOGW("BundleInstallPlugin OnRemovePolicy data is empty.");
82         return ERR_OK;
83     }
84     if (data.size() > EdmConstants::APPID_MAX_SIZE) {
85         EDMLOGE("BundleInstallPlugin OnRemovePolicy input data is too large.");
86         return EdmReturnErrCode::PARAM_ERROR;
87     }
88 
89     std::vector<std::string> mergeData =
90         ArrayStringSerializer::GetInstance()->SetDifferencePolicyData(data, currentData);
91     auto appControlProxy = GetAppControlProxy();
92     if (!appControlProxy) {
93         EDMLOGE("BundleInstallPlugin OnRemovePolicy GetAppControlProxy failed.");
94         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
95     }
96     ErrCode res = appControlProxy->DeleteAppInstallControlRule(controlRuleType_, data, userId);
97     if (res != ERR_OK) {
98         EDMLOGE("BundleInstallPlugin DeleteAppInstallControlRule OnRemovePolicy faild %{public}d:", res);
99         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
100     }
101     currentData = mergeData;
102     return ERR_OK;
103 }
104 
OnAdminRemoveDone(const std::string & adminName,std::vector<std::string> & data,int32_t userId)105 void BundleInstallPlugin::OnAdminRemoveDone(const std::string &adminName, std::vector<std::string> &data,
106     int32_t userId)
107 {
108     EDMLOGI("AllowedInstallBundlesPlugin OnAdminRemoveDone adminName : %{public}s userId : %{public}d",
109         adminName.c_str(), userId);
110     auto appControlProxy = GetAppControlProxy();
111     if (!appControlProxy) {
112         EDMLOGE("BundleInstallPlugin OnAdminRemoveDone GetAppControlProxy failed.");
113         return;
114     }
115     ErrCode res = appControlProxy->DeleteAppInstallControlRule(controlRuleType_, data, userId);
116     EDMLOGD("BundleInstallPlugin OnAdminRemoveDone result %{public}d:", res);
117 }
118 
GetAppControlProxy()119 sptr<AppExecFwk::IAppControlMgr> BundleInstallPlugin::GetAppControlProxy()
120 {
121     auto remoteObject = EdmSysManager::GetRemoteObjectOfSystemAbility(OHOS::BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
122     sptr<AppExecFwk::IBundleMgr> proxy = iface_cast<AppExecFwk::IBundleMgr>(remoteObject);
123     return proxy->GetAppControlProxy();
124 }
125 } // namespace EDM
126 } // namespace OHOS
127