• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 "clear_up_application_data_plugin.h"
17 
18 #include "app_mgr_proxy.h"
19 #include "clear_up_application_data_param_serializer.h"
20 #include "edm_ipc_interface_code.h"
21 #include "external_manager_factory.h"
22 #include "iplugin_manager.h"
23 #include "iservice_registry.h"
24 #include "system_ability_definition.h"
25 
26 namespace OHOS {
27 namespace EDM {
28 
29 const bool REGISTER_RESULT = IPluginManager::GetInstance()->AddPlugin(ClearUpApplicationDataPlugin::GetPlugin());
30 
InitPlugin(std::shared_ptr<IPluginTemplate<ClearUpApplicationDataPlugin,ClearUpApplicationDataParam>> ptr)31 void ClearUpApplicationDataPlugin::InitPlugin(
32     std::shared_ptr<IPluginTemplate<ClearUpApplicationDataPlugin, ClearUpApplicationDataParam>> ptr)
33 {
34     EDMLOGI("ClearUpApplicationDataPlugin InitPlugin...");
35     ptr->InitAttribute(EdmInterfaceCode::CLEAR_UP_APPLICATION_DATA, PolicyName::POLICY_CLEAR_UP_APPLICATION_DATA,
36         EdmPermission::PERMISSION_ENTERPRISE_MANAGE_APPLICATION, IPlugin::PermissionType::SUPER_DEVICE_ADMIN, false);
37     ptr->SetSerializer(ClearUpApplicationDataParamSerializer::GetInstance());
38     ptr->SetOnHandlePolicyListener(&ClearUpApplicationDataPlugin::OnSetPolicy, FuncOperateType::SET);
39 }
40 
OnSetPolicy(ClearUpApplicationDataParam & param)41 ErrCode ClearUpApplicationDataPlugin::OnSetPolicy(ClearUpApplicationDataParam &param)
42 {
43     EDMLOGD("ClearUpApplicationDataPlugin::OnSetPolicy, bundleName = %{public}s", param.bundleName.c_str());
44     if (param.bundleName.empty() || !IsBundleInstalled(param)) {
45         EDMLOGE("Parameter bundle name wrong");
46         return EdmReturnErrCode::PARAM_ERROR;
47     }
48     sptr<ISystemAbilityManager> abilityMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
49     if (abilityMgr == nullptr) {
50         EDMLOGE("Failed to get ISystemAbilityManager");
51         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
52     }
53     sptr<IRemoteObject> remoteObject = abilityMgr->CheckSystemAbility(APP_MGR_SERVICE_ID);
54     if (remoteObject == nullptr) {
55         EDMLOGE("Failed to get app manager service");
56         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
57     }
58     sptr<AppExecFwk::IAppMgr> appMgrProxy = iface_cast<AppExecFwk::IAppMgr>(remoteObject);
59     if (appMgrProxy == nullptr || !appMgrProxy->AsObject()) {
60         EDMLOGE("Failed to get app mgr proxy");
61         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
62     }
63     int ret = appMgrProxy->ClearUpApplicationData(param.bundleName, param.appIndex, param.userId);
64     EDMLOGI("ClearUpApplicationData ret: %{public}d", ret);
65     if (ret != 0) {
66         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
67     }
68     return ERR_OK;
69 }
70 
IsBundleInstalled(const ClearUpApplicationDataParam & param)71 bool ClearUpApplicationDataPlugin::IsBundleInstalled(const ClearUpApplicationDataParam &param)
72 {
73     std::shared_ptr<IExternalManagerFactory> factory = std::make_shared<ExternalManagerFactory>();
74     return factory->CreateBundleManager()->IsBundleInstalled(param.bundleName, param.userId, param.appIndex);
75 }
76 } // namespace EDM
77 } // namespace OHOS
78