• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "manage_auto_start_apps_plugin.h"
17 
18 #include <bundle_info.h>
19 #include <bundle_mgr_interface.h>
20 #include <system_ability_definition.h>
21 
22 #include "ability_auto_startup_client.h"
23 #include "array_string_serializer.h"
24 #include "edm_constants.h"
25 #include "edm_ipc_interface_code.h"
26 #include "edm_sys_manager.h"
27 #include "element_name.h"
28 #include "iplugin_manager.h"
29 
30 namespace OHOS {
31 namespace EDM {
32 const bool REGISTER_RESULT = IPluginManager::GetInstance()->AddPlugin(ManageAutoStartAppsPlugin::GetPlugin());
33 const std::string SEPARATOR = "/";
34 
InitPlugin(std::shared_ptr<IPluginTemplate<ManageAutoStartAppsPlugin,std::vector<std::string>>> ptr)35 void ManageAutoStartAppsPlugin::InitPlugin(
36     std::shared_ptr<IPluginTemplate<ManageAutoStartAppsPlugin, std::vector<std::string>>> ptr)
37 {
38     EDMLOGI("ManageAutoStartAppsPlugin InitPlugin...");
39     ptr->InitAttribute(EdmInterfaceCode::MANAGE_AUTO_START_APPS, "manage_auto_start_apps",
40         EdmPermission::PERMISSION_ENTERPRISE_MANAGE_APPLICATION, IPlugin::PermissionType::SUPER_DEVICE_ADMIN, true);
41     ptr->SetSerializer(ArrayStringSerializer::GetInstance());
42     ptr->SetOnHandlePolicyListener(&ManageAutoStartAppsPlugin::OnBasicSetPolicy, FuncOperateType::SET);
43     ptr->SetOnHandlePolicyListener(&ManageAutoStartAppsPlugin::OnBasicRemovePolicy, FuncOperateType::REMOVE);
44     ptr->SetOnAdminRemoveListener(&ManageAutoStartAppsPlugin::OnBasicAdminRemove);
45     maxListSize_ = EdmConstants::AUTO_START_APPS_MAX_SIZE;
46 }
47 
SetOtherModulePolicy(const std::vector<std::string> & data,int32_t userId,std::vector<std::string> & failedData)48 ErrCode ManageAutoStartAppsPlugin::SetOtherModulePolicy(const std::vector<std::string> &data,
49     int32_t userId, std::vector<std::string> &failedData)
50 {
51     return SetOrRemoveOtherModulePolicy(data, true, failedData);
52 }
53 
OnGetPolicy(std::string & policyData,MessageParcel & data,MessageParcel & reply,int32_t userId)54 ErrCode ManageAutoStartAppsPlugin::OnGetPolicy(std::string &policyData, MessageParcel &data, MessageParcel &reply,
55     int32_t userId)
56 {
57     EDMLOGI("ManageAutoStartAppsPlugin OnGetPolicy policyData : %{public}s, userId : %{public}d", policyData.c_str(),
58         userId);
59     return BasicGetPolicy(policyData, data, reply, userId);
60 }
61 
RemoveOtherModulePolicy(const std::vector<std::string> & data,int32_t userId,std::vector<std::string> & failedData)62 ErrCode ManageAutoStartAppsPlugin::RemoveOtherModulePolicy(const std::vector<std::string> &data, int32_t userId,
63     std::vector<std::string> &failedData)
64 {
65     return SetOrRemoveOtherModulePolicy(data, false, failedData);
66 }
67 
SetOrRemoveOtherModulePolicy(const std::vector<std::string> & data,bool isSet,std::vector<std::string> & failedData)68 ErrCode ManageAutoStartAppsPlugin::SetOrRemoveOtherModulePolicy(const std::vector<std::string> &data, bool isSet,
69     std::vector<std::string> &failedData)
70 {
71     EDMLOGI("ManageAutoStartAppsPlugin SetOrRemoveOtherModulePolicy: %{public}d.", isSet);
72     auto autoStartupClient = AAFwk::AbilityAutoStartupClient::GetInstance();
73     if (!autoStartupClient) {
74         EDMLOGE("ManageAutoStartAppsPlugin SetOrRemoveOtherModulePolicy GetAppControlProxy failed.");
75         return EdmReturnErrCode::SYSTEM_ABNORMALLY;
76     }
77     bool flag = false;
78     for (const auto &str : data) {
79         OHOS::AbilityRuntime::AutoStartupInfo autoStartupInfo;
80         if (!ParseAutoStartAppWant(str, autoStartupInfo.bundleName, autoStartupInfo.abilityName)) {
81             EDMLOGW("ParseAutoStartAppWant failed bundleName: %{public}s, abilityName: %{public}s",
82                 autoStartupInfo.bundleName.c_str(), autoStartupInfo.abilityName.c_str());
83             failedData.push_back(str);
84             continue;
85         }
86         if (!CheckBundleAndAbilityExited(autoStartupInfo.bundleName, autoStartupInfo.abilityName)) {
87             EDMLOGW("CheckBundleAndAbilityExited failed bundleName: %{public}s, abilityName: %{public}s",
88                 autoStartupInfo.bundleName.c_str(), autoStartupInfo.abilityName.c_str());
89             if (!isSet) {
90                 flag = true;
91             }
92             failedData.push_back(str);
93             continue;
94         }
95         ErrCode res;
96         if (isSet) {
97             res = autoStartupClient->SetApplicationAutoStartupByEDM(autoStartupInfo, true);
98         } else {
99             res = autoStartupClient->CancelApplicationAutoStartupByEDM(autoStartupInfo, true);
100         }
101         if (res != ERR_OK) {
102             EDMLOGW("OnRemovePolicy SetOrCancelApplicationAutoStartupByEDM err res: %{public}d bundleName: %{public}s "
103                 "abilityName:%{public}s", res, autoStartupInfo.bundleName.c_str(), autoStartupInfo.abilityName.c_str());
104             failedData.push_back(str);
105             continue;
106         }
107         flag = true;
108     }
109     return flag ? ERR_OK : EdmReturnErrCode::PARAM_ERROR;
110 }
111 
ParseAutoStartAppWant(std::string appWant,std::string & bundleName,std::string & abilityName)112 bool ManageAutoStartAppsPlugin::ParseAutoStartAppWant(std::string appWant, std::string &bundleName,
113     std::string &abilityName)
114 {
115     size_t index = appWant.find(SEPARATOR);
116     if (index != appWant.npos) {
117         bundleName = appWant.substr(0, index);
118         abilityName = appWant.substr(index + 1);
119     } else {
120         EDMLOGE("ManageAutoStartAppsPlugin ParseAutoStartAppWant parse auto start app want failed");
121         return false;
122     }
123     return true;
124 }
125 
CheckBundleAndAbilityExited(const std::string & bundleName,const std::string & abilityName)126 bool ManageAutoStartAppsPlugin::CheckBundleAndAbilityExited(const std::string &bundleName,
127     const std::string &abilityName)
128 {
129     auto remoteObject = EdmSysManager::GetRemoteObjectOfSystemAbility(OHOS::BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
130     sptr<AppExecFwk::IBundleMgr> proxy = iface_cast<AppExecFwk::IBundleMgr>(remoteObject);
131     if (!proxy) {
132         EDMLOGE("EnterpriseDeviceMgrAbility::GetAllPermissionsByAdmin GetBundleMgr failed.");
133         return false;
134     }
135 
136     std::vector<AppExecFwk::AbilityInfo> abilityInfos;
137     AppExecFwk::ElementName element;
138     element.SetBundleName(bundleName);
139     element.SetAbilityName(abilityName);
140     OHOS::AppExecFwk::IBundleMgr::Want want;
141     want.SetElement(element);
142     std::vector<AppExecFwk::ExtensionAbilityInfo> extensionAbilityInfo;
143     return proxy->QueryAbilityInfos(want, AppExecFwk::AbilityInfoFlag::GET_ABILITY_INFO_DEFAULT,
144         DEFAULT_USER_ID, abilityInfos) ||
145         proxy->QueryExtensionAbilityInfos(want, AppExecFwk::ExtensionAbilityType::SERVICE,
146             AppExecFwk::ExtensionAbilityInfoFlag::GET_EXTENSION_INFO_DEFAULT, DEFAULT_USER_ID,
147             extensionAbilityInfo);
148 }
149 } // namespace EDM
150 } // namespace OHOS
151