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 ErrCode res = GetAppControlProxy()->AddAppInstallControlRule(data, controlRuleType_, userId);
49 if (res != ERR_OK) {
50 EDMLOGE("BundleInstallPlugin OnSetPolicyDone Faild %{public}d:", res);
51 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
52 }
53 currentData = mergeData;
54 return ERR_OK;
55 }
56
GetBundlePolicy(std::string & policyData,MessageParcel & data,MessageParcel & reply,int32_t userId)57 ErrCode BundleInstallPlugin::GetBundlePolicy(std::string &policyData, MessageParcel &data, MessageParcel &reply,
58 int32_t userId)
59 {
60 std::vector<std::string> bundles;
61 ArrayStringSerializer::GetInstance()->Deserialize(policyData, bundles);
62 reply.WriteInt32(ERR_OK);
63 reply.WriteInt32(bundles.size());
64 reply.WriteStringVector(bundles);
65 return ERR_OK;
66 }
67
OnRemovePolicy(std::vector<std::string> & data,std::vector<std::string> & currentData,int32_t userId)68 ErrCode BundleInstallPlugin::OnRemovePolicy(std::vector<std::string> &data, std::vector<std::string> ¤tData,
69 int32_t userId)
70 {
71 if (data.empty()) {
72 EDMLOGW("BundleInstallPlugin OnRemovePolicy data is empty.");
73 return ERR_OK;
74 }
75
76 std::vector<std::string> mergeData =
77 ArrayStringSerializer::GetInstance()->SetDifferencePolicyData(data, currentData);
78 ErrCode res = GetAppControlProxy()->DeleteAppInstallControlRule(controlRuleType_, data, userId);
79 if (res != ERR_OK) {
80 EDMLOGE("BundleInstallPlugin DeleteAppInstallControlRule OnRemovePolicy faild %{public}d:", res);
81 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
82 }
83 currentData = mergeData;
84 return ERR_OK;
85 }
86
OnAdminRemoveDone(const std::string & adminName,std::vector<std::string> & data,int32_t userId)87 void BundleInstallPlugin::OnAdminRemoveDone(const std::string &adminName, std::vector<std::string> &data,
88 int32_t userId)
89 {
90 EDMLOGI("AllowedInstallBundlesPlugin OnAdminRemoveDone adminName : %{public}s userId : %{public}d",
91 adminName.c_str(), userId);
92 ErrCode res = GetAppControlProxy()->DeleteAppInstallControlRule(controlRuleType_, data, userId);
93 EDMLOGD("BundleInstallPlugin OnAdminRemoveDone result %{public}d:", res);
94 }
95
GetAppControlProxy()96 sptr<AppExecFwk::IAppControlMgr> BundleInstallPlugin::GetAppControlProxy()
97 {
98 auto remoteObject = EdmSysManager::GetRemoteObjectOfSystemAbility(OHOS::BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
99 sptr<AppExecFwk::IBundleMgr> proxy = iface_cast<AppExecFwk::IBundleMgr>(remoteObject);
100 return proxy->GetAppControlProxy();
101 }
102 } // namespace EDM
103 } // namespace OHOS
104