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 "uninstall_plugin.h"
17 
18 #include <system_ability_definition.h>
19 
20 #include "bundle_mgr_interface.h"
21 #include "bundle_mgr_proxy.h"
22 #include "edm_ipc_interface_code.h"
23 #include "edm_sys_manager.h"
24 #include "installer_callback.h"
25 #include "plugin_manager.h"
26 
27 namespace OHOS {
28 namespace EDM {
29 const bool REGISTER_RESULT = PluginManager::GetInstance()->AddPlugin(UninstallPlugin::GetPlugin());
30 
InitPlugin(std::shared_ptr<IPluginTemplate<UninstallPlugin,UninstallParam>> ptr)31 void UninstallPlugin::InitPlugin(std::shared_ptr<IPluginTemplate<UninstallPlugin, UninstallParam>> ptr)
32 {
33     EDMLOGI("UninstallPlugin InitPlugin...");
34     ptr->InitAttribute(EdmInterfaceCode::UNINSTALL, "uninstall", "ohos.permission.ENTERPRISE_INSTALL_BUNDLE",
35         IPlugin::PermissionType::SUPER_DEVICE_ADMIN, false);
36     ptr->SetSerializer(UninstallParamSerializer::GetInstance());
37     ptr->SetOnHandlePolicyListener(&UninstallPlugin::OnSetPolicy, FuncOperateType::SET);
38 }
39 
OnSetPolicy(UninstallParam & param,MessageParcel & reply)40 ErrCode UninstallPlugin::OnSetPolicy(UninstallParam &param, MessageParcel &reply)
41 {
42     EDMLOGI("UninstallPlugin OnSetPolicy bundleName : %{public}s, userId : %{public}d, isKeepData : %{public}d",
43         param.bundleName.c_str(), param.userId, param.isKeepData);
44 
45     const std::string bundleName = param.bundleName;
46     auto remoteObject = EdmSysManager::GetRemoteObjectOfSystemAbility(OHOS::BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
47     auto iBundleMgr = iface_cast<AppExecFwk::IBundleMgr>(remoteObject);
48     if (iBundleMgr == nullptr) {
49         EDMLOGE("can not get iBundleMgr");
50         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
51     }
52     auto iBundleInstaller = iBundleMgr->GetBundleInstaller();
53     if ((iBundleInstaller == nullptr) || (iBundleInstaller->AsObject() == nullptr)) {
54         EDMLOGE("can not get iBundleInstaller");
55         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
56     }
57     sptr<InstallerCallback> callback = new (std::nothrow) InstallerCallback();
58     if (callback == nullptr) {
59         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
60     }
61     AppExecFwk::InstallParam installParam;
62     installParam.userId = param.userId;
63     installParam.isKeepData = param.isKeepData;
64     iBundleInstaller->Uninstall(bundleName, installParam, callback);
65     ErrCode ret = callback->GetResultCode();
66     std::string errorMessage = callback->GetResultMsg();
67     EDMLOGE("InnerRecover resultCode %{public}d resultMsg %{public}s.", ret, errorMessage.c_str());
68     if (FAILED(ret)) {
69         reply.WriteInt32(EdmReturnErrCode::PARAM_ERROR);
70         reply.WriteString(errorMessage);
71     }
72     return ERR_OK;
73 }
74 } // namespace EDM
75 } // namespace OHOS
76