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/control_interceptor.h"
17 
18 #include "ability_util.h"
19 #include "appexecfwk_errors.h"
20 #include "hitrace_meter.h"
21 
22 namespace OHOS {
23 namespace AAFwk {
24 namespace {
25 constexpr const char* INTERCEPT_PARAMETERS = "intercept_parammeters";
26 constexpr const char* INTERCEPT_BUNDLE_NAME = "intercept_bundleName";
27 constexpr const char* INTERCEPT_ABILITY_NAME = "intercept_abilityName";
28 constexpr const char* INTERCEPT_MODULE_NAME = "intercept_moduleName";
29 constexpr const char* IS_FROM_PARENTCONTROL = "ohos.ability.isFromParentControl";
30 }
31 
DoProcess(AbilityInterceptorParam param)32 ErrCode ControlInterceptor::DoProcess(AbilityInterceptorParam param)
33 {
34     AppExecFwk::AppRunningControlRuleResult controlRule;
35     if (CheckControl(param.want, param.userId, controlRule)) {
36         TAG_LOGI(AAFwkTag::ABILITYMGR,
37             "The target application is intercpted. %{public}s", controlRule.controlMessage.c_str());
38 #ifdef SUPPORT_GRAPHICS
39         if (!param.isWithUI || controlRule.controlWant == nullptr) {
40             TAG_LOGE(AAFwkTag::ABILITYMGR, "Can not start control want");
41             return AbilityUtil::EdmErrorType(controlRule.isEdm);
42         }
43         if (controlRule.controlWant->GetBoolParam(IS_FROM_PARENTCONTROL, false)) {
44             auto controlWant = controlRule.controlWant;
45             auto controlParam = controlWant->GetParams();
46             sptr<AAFwk::IWantParams> interceptParam = WantParamWrapper::Box(param.want.GetParams());
47             if (interceptParam != nullptr) {
48                 controlParam.SetParam(INTERCEPT_PARAMETERS, interceptParam);
49             }
50             controlWant->SetParams(controlParam);
51             controlWant->SetParam(INTERCEPT_BUNDLE_NAME, param.want.GetElement().GetBundleName());
52             controlWant->SetParam(INTERCEPT_ABILITY_NAME, param.want.GetElement().GetAbilityName());
53             controlWant->SetParam(INTERCEPT_MODULE_NAME, param.want.GetElement().GetModuleName());
54             controlRule.controlWant = controlWant;
55         }
56         int ret = IN_PROCESS_CALL(AbilityManagerClient::GetInstance()->StartAbility(*controlRule.controlWant,
57             param.requestCode, param.userId));
58         if (ret != ERR_OK) {
59             TAG_LOGE(AAFwkTag::ABILITYMGR, "Control implicit start appgallery failed.");
60             return ret;
61         }
62 #endif
63         return AbilityUtil::EdmErrorType(controlRule.isEdm);
64     }
65     return ERR_OK;
66 }
67 
CheckControl(const Want & want,int32_t userId,AppExecFwk::AppRunningControlRuleResult & controlRule)68 bool ControlInterceptor::CheckControl(const Want &want, int32_t userId,
69     AppExecFwk::AppRunningControlRuleResult &controlRule)
70 {
71     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
72     // get bms
73     auto bundleMgrHelper = AbilityUtil::GetBundleManagerHelper();
74     if (bundleMgrHelper == nullptr) {
75         TAG_LOGE(AAFwkTag::ABILITYMGR, "The bundleMgrHelper is nullptr.");
76         return false;
77     }
78 
79     // get disposed status
80     std::string bundleName = want.GetBundle();
81     auto appControlMgr = bundleMgrHelper->GetAppControlProxy();
82     if (appControlMgr == nullptr) {
83         TAG_LOGE(AAFwkTag::ABILITYMGR, "The appControlMgr is nullptr.");
84         return false;
85     }
86 
87     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, "GetAppRunningControlRule");
88     std::string identity = IPCSkeleton::ResetCallingIdentity();
89     if (identity == "") {
90         TAG_LOGE(AAFwkTag::ABILITYMGR, "get identity failed");
91     }
92     auto ret = appControlMgr->GetAppRunningControlRule(bundleName, userId, controlRule);
93     if (ret == ERR_BUNDLE_MANAGER_PERMISSION_DENIED) {
94         IPCSkeleton::SetCallingIdentity(identity, true);
95     } else {
96         IPCSkeleton::SetCallingIdentity(identity);
97     }
98     if (ret != ERR_OK) {
99         TAG_LOGD(AAFwkTag::ABILITYMGR, "Get No AppRunningControlRule.");
100         return false;
101     }
102     return true;
103 }
104 } // namespace AAFwk
105 } // namespace OHOS