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> ¤tData,
34 int32_t userId)
35 {
36 if (data.empty()) {
37 EDMLOGW("BundleInstallPlugin OnSetPolicy data is empty.");
38 return ERR_OK;
39 }
40
41 std::vector<std::string> mergeData = ArrayStringSerializer::GetInstance()->SetUnionPolicyData(data, currentData);
42
43 if (mergeData.size() > EdmConstants::APPID_MAX_SIZE) {
44 EDMLOGE("BundleInstallPlugin OnSetPolicy data is too large.");
45 return EdmReturnErrCode::PARAM_ERROR;
46 }
47
48 auto appControlProxy = GetAppControlProxy();
49 if (!appControlProxy) {
50 EDMLOGE("BundleInstallPlugin OnSetPolicy GetAppControlProxy failed.");
51 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
52 }
53 ErrCode res = appControlProxy->AddAppInstallControlRule(data, controlRuleType_, userId);
54 if (res != ERR_OK) {
55 EDMLOGE("BundleInstallPlugin OnSetPolicyDone Faild %{public}d:", res);
56 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
57 }
58 currentData = mergeData;
59 return ERR_OK;
60 }
61
GetBundlePolicy(std::string & policyData,MessageParcel & data,MessageParcel & reply,int32_t userId)62 ErrCode BundleInstallPlugin::GetBundlePolicy(std::string &policyData, MessageParcel &data, MessageParcel &reply,
63 int32_t userId)
64 {
65 std::vector<std::string> bundles;
66 ArrayStringSerializer::GetInstance()->Deserialize(policyData, bundles);
67 reply.WriteInt32(ERR_OK);
68 reply.WriteInt32(bundles.size());
69 reply.WriteStringVector(bundles);
70 return ERR_OK;
71 }
72
OnRemovePolicy(std::vector<std::string> & data,std::vector<std::string> & currentData,int32_t userId)73 ErrCode BundleInstallPlugin::OnRemovePolicy(std::vector<std::string> &data, std::vector<std::string> ¤tData,
74 int32_t userId)
75 {
76 if (data.empty()) {
77 EDMLOGW("BundleInstallPlugin OnRemovePolicy data is empty.");
78 return ERR_OK;
79 }
80
81 std::vector<std::string> mergeData =
82 ArrayStringSerializer::GetInstance()->SetDifferencePolicyData(data, currentData);
83 auto appControlProxy = GetAppControlProxy();
84 if (!appControlProxy) {
85 EDMLOGE("BundleInstallPlugin OnRemovePolicy GetAppControlProxy failed.");
86 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
87 }
88 ErrCode res = appControlProxy->DeleteAppInstallControlRule(controlRuleType_, data, userId);
89 if (res != ERR_OK) {
90 EDMLOGE("BundleInstallPlugin DeleteAppInstallControlRule OnRemovePolicy faild %{public}d:", res);
91 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
92 }
93 currentData = mergeData;
94 return ERR_OK;
95 }
96
OnAdminRemoveDone(const std::string & adminName,std::vector<std::string> & data,int32_t userId)97 void BundleInstallPlugin::OnAdminRemoveDone(const std::string &adminName, std::vector<std::string> &data,
98 int32_t userId)
99 {
100 EDMLOGI("AllowedInstallBundlesPlugin OnAdminRemoveDone adminName : %{public}s userId : %{public}d",
101 adminName.c_str(), userId);
102 auto appControlProxy = GetAppControlProxy();
103 if (!appControlProxy) {
104 EDMLOGE("BundleInstallPlugin OnAdminRemoveDone GetAppControlProxy failed.");
105 return;
106 }
107 ErrCode res = appControlProxy->DeleteAppInstallControlRule(controlRuleType_, data, userId);
108 EDMLOGD("BundleInstallPlugin OnAdminRemoveDone result %{public}d:", res);
109 }
110
GetAppControlProxy()111 sptr<AppExecFwk::IAppControlMgr> BundleInstallPlugin::GetAppControlProxy()
112 {
113 auto remoteObject = EdmSysManager::GetRemoteObjectOfSystemAbility(OHOS::BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
114 sptr<AppExecFwk::IBundleMgr> proxy = iface_cast<AppExecFwk::IBundleMgr>(remoteObject);
115 return proxy->GetAppControlProxy();
116 }
117 } // namespace EDM
118 } // namespace OHOS
119