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