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_errors.h"
23 #include "edm_sys_manager.h"
24
25 namespace OHOS {
26 namespace EDM {
SetAppInstallControlRuleType(AppExecFwk::AppInstallControlRuleType controlRuleType)27 void BundleInstallPlugin::SetAppInstallControlRuleType(AppExecFwk::AppInstallControlRuleType controlRuleType)
28 {
29 controlRuleType_ = controlRuleType;
30 }
31
SetOtherModulePolicy(const std::vector<std::string> & data,int32_t userId,std::vector<std::string> & failedData)32 ErrCode BundleInstallPlugin::SetOtherModulePolicy(const std::vector<std::string> &data, int32_t userId,
33 std::vector<std::string> &failedData)
34 {
35 auto appControlProxy = GetAppControlProxy();
36 if (!appControlProxy) {
37 EDMLOGE("BundleInstallPlugin OnSetPolicy GetAppControlProxy failed.");
38 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
39 }
40 ErrCode res = appControlProxy->AddAppInstallControlRule(data, controlRuleType_, userId);
41 if (res != ERR_OK) {
42 EDMLOGE("BundleInstallPlugin OnSetPolicyDone Faild %{public}d:", res);
43 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
44 }
45 return ERR_OK;
46 }
47
RemoveOtherModulePolicy(const std::vector<std::string> & data,int32_t userId,std::vector<std::string> & failedData)48 ErrCode BundleInstallPlugin::RemoveOtherModulePolicy(const std::vector<std::string> &data, int32_t userId,
49 std::vector<std::string> &failedData)
50 {
51 auto appControlProxy = GetAppControlProxy();
52 if (!appControlProxy) {
53 EDMLOGE("BundleInstallPlugin OnRemovePolicy GetAppControlProxy failed.");
54 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
55 }
56 ErrCode res = appControlProxy->DeleteAppInstallControlRule(controlRuleType_, data, userId);
57 if (res != ERR_OK) {
58 EDMLOGE("BundleInstallPlugin DeleteAppInstallControlRule OnRemovePolicy faild %{public}d:", res);
59 return EdmReturnErrCode::SYSTEM_ABNORMALLY;
60 }
61 return ERR_OK;
62 }
63
GetAppControlProxy()64 sptr<AppExecFwk::IAppControlMgr> BundleInstallPlugin::GetAppControlProxy()
65 {
66 auto remoteObject = EdmSysManager::GetRemoteObjectOfSystemAbility(OHOS::BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
67 sptr<AppExecFwk::IBundleMgr> proxy = iface_cast<AppExecFwk::IBundleMgr>(remoteObject);
68 if (proxy == nullptr) {
69 EDMLOGE("BundleInstallPlugin GetAppControlProxy failed.");
70 return nullptr;
71 }
72 return proxy->GetAppControlProxy();
73 }
74 } // namespace EDM
75 } // namespace OHOS
76