1 /*
2 * Copyright (c) 2024 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 "interceptor/extension_control_interceptor.h"
17
18 #include "ability_manager_constants.h"
19 #include "ability_util.h"
20 #include "app_scheduler.h"
21 #include "extension_config.h"
22 #include "start_ability_utils.h"
23
24 namespace OHOS {
25 namespace AAFwk {
26
DoProcess(AbilityInterceptorParam param)27 ErrCode ExtensionControlInterceptor::DoProcess(AbilityInterceptorParam param)
28 {
29 TAG_LOGD(AAFwkTag::ABILITYMGR, "call.");
30 if (param.callerToken == nullptr) {
31 TAG_LOGD(AAFwkTag::ABILITYMGR, "callerToken is nullptr.");
32 return ERR_OK;
33 }
34 // get caller ability info
35 AppExecFwk::AbilityInfo callerAbilityInfo;
36 if (GetCallerAbilityInfo(param, callerAbilityInfo)) {
37 TAG_LOGD(AAFwkTag::ABILITYMGR, "caller enable.");
38 return ERR_OK;
39 }
40 // get target ability info
41 AppExecFwk::AbilityInfo targetAbilityInfo;
42 if (GetTargetAbilityInfo(param, targetAbilityInfo)) {
43 TAG_LOGD(AAFwkTag::ABILITYMGR, "target enable.");
44 return ERR_OK;
45 }
46
47 // check blocked list
48 if (!targetAbilityInfo.applicationInfo.isSystemApp &&
49 !DelayedSingleton<ExtensionConfig>::GetInstance()->IsExtensionStartThirdPartyAppEnable(
50 callerAbilityInfo.extensionTypeName)) {
51 TAG_LOGE(AAFwkTag::ABILITYMGR, "third party app block extension call, bundleName: %{public}s",
52 callerAbilityInfo.bundleName.c_str());
53 return EXTENSION_BLOCKED_BY_THIRD_PARTY_APP_FLAG;
54 }
55 if ((targetAbilityInfo.extensionAbilityType == AppExecFwk::ExtensionAbilityType::SERVICE ||
56 targetAbilityInfo.extensionAbilityType == AppExecFwk::ExtensionAbilityType::DATASHARE) &&
57 !DelayedSingleton<ExtensionConfig>::GetInstance()->IsExtensionStartServiceEnable(
58 callerAbilityInfo.extensionTypeName, param.want.GetElement().GetURI())) {
59 TAG_LOGE(AAFwkTag::ABILITYMGR, "service list block extension call, bundleName: %{public}s",
60 callerAbilityInfo.bundleName.c_str());
61 return EXTENSION_BLOCKED_BY_SERVICE_LIST;
62 }
63
64 TAG_LOGD(AAFwkTag::ABILITYMGR, "other ok");
65 return ERR_OK;
66 }
67
GetCallerAbilityInfo(const AbilityInterceptorParam & param,AppExecFwk::AbilityInfo & callerAbilityInfo)68 bool ExtensionControlInterceptor::GetCallerAbilityInfo(const AbilityInterceptorParam& param,
69 AppExecFwk::AbilityInfo& callerAbilityInfo)
70 {
71 if (StartAbilityUtils::GetCallerAbilityInfo(param.callerToken, callerAbilityInfo)) {
72 if (callerAbilityInfo.type != AppExecFwk::AbilityType::EXTENSION ||
73 callerAbilityInfo.extensionAbilityType == AppExecFwk::ExtensionAbilityType::SERVICE ||
74 callerAbilityInfo.bundleName == param.want.GetElement().GetBundleName()) {
75 TAG_LOGD(AAFwkTag::ABILITYMGR, "not other extension.");
76 return true;
77 }
78 auto appScheduler = DelayedSingleton<AppScheduler>::GetInstance();
79 AppExecFwk::RunningProcessInfo processInfo;
80 if (appScheduler != nullptr) {
81 appScheduler->GetRunningProcessInfoByToken(param.callerToken, processInfo);
82 if (!processInfo.isStrictMode && !param.want.GetBoolParam(STRICT_MODE, false)) {
83 TAG_LOGD(AAFwkTag::ABILITYMGR, "caller and want not strict mode");
84 return true;
85 }
86 }
87 }
88 return false;
89 }
90
GetTargetAbilityInfo(const AbilityInterceptorParam & param,AppExecFwk::AbilityInfo & targetAbilityInfo)91 bool ExtensionControlInterceptor::GetTargetAbilityInfo(const AbilityInterceptorParam& param,
92 AppExecFwk::AbilityInfo& targetAbilityInfo)
93 {
94 if (StartAbilityUtils::startAbilityInfo != nullptr &&
95 StartAbilityUtils::startAbilityInfo->abilityInfo.bundleName == param.want.GetBundle() &&
96 StartAbilityUtils::startAbilityInfo->abilityInfo.name == param.want.GetElement().GetAbilityName()) {
97 TAG_LOGD(AAFwkTag::ABILITYMGR, "targetAbilityInfo get from startAbiiltyInfo");
98 targetAbilityInfo = StartAbilityUtils::startAbilityInfo->abilityInfo;
99 } else {
100 auto bundleMgrHelper = AbilityUtil::GetBundleManagerHelper();
101 if (bundleMgrHelper == nullptr) {
102 TAG_LOGE(AAFwkTag::ABILITYMGR, "null bundleMgrHelper");
103 return true;
104 }
105 IN_PROCESS_CALL_WITHOUT_RET(bundleMgrHelper->QueryAbilityInfo(param.want,
106 AppExecFwk::AbilityInfoFlag::GET_ABILITY_INFO_WITH_APPLICATION, param.userId, targetAbilityInfo));
107 }
108 return false;
109 }
110 } // namespace AAFwk
111 } // namespace OHOS