• 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 #include "sa_interceptor_manager.h"
16 
17 #include <random>
18 
19 #include "ability_manager_errors.h"
20 #include "ability_manager_service.h"
21 #include "hilog_tag_wrapper.h"
22 #include "hitrace_meter.h"
23 
24 namespace OHOS {
25 namespace AbilityRuntime {
26 constexpr int32_t BASE_USER_RANGE = 200000;
27 
SAInterceptorManager()28 SAInterceptorManager::SAInterceptorManager()
29 {}
30 
~SAInterceptorManager()31 SAInterceptorManager::~SAInterceptorManager()
32 {}
33 
GetInstance()34 SAInterceptorManager &SAInterceptorManager::GetInstance()
35 {
36     static SAInterceptorManager manager;
37     return manager;
38 }
39 
AddSAInterceptor(sptr<ISAInterceptor> interceptor)40 int32_t SAInterceptorManager::AddSAInterceptor(sptr<ISAInterceptor> interceptor)
41 {
42     if (interceptor == nullptr) {
43         TAG_LOGE(AAFwkTag::SA_INTERCEPTOR, "null interceptor");
44         return AAFwk::ERR_NULL_SA_INTERCEPTOR_EXECUTER;
45     }
46 
47     if (ObserverExist(interceptor)) {
48         TAG_LOGI(AAFwkTag::ABILITYMGR, "interceptor exist");
49         return ERR_OK;
50     }
51 
52     {
53         std::lock_guard<std::mutex> lock(saInterceptorLock_);
54         saInterceptors_.emplace_back(interceptor);
55     }
56 
57     if (!deathRecipient_) {
58         // add death recipient
59         deathRecipient_ = new SAInterceptorRecipient([](const wptr<IRemoteObject> &remote) {
60             SAInterceptorManager::GetInstance().OnObserverDied(remote);
61         });
62     }
63 
64     auto observerObj = interceptor->AsObject();
65     if (!observerObj || !observerObj->AddDeathRecipient(deathRecipient_)) {
66         TAG_LOGE(AAFwkTag::SA_INTERCEPTOR, "AddDeathRecipient failed");
67     }
68 
69     return ERR_OK;
70 }
71 
SAInterceptorListIsEmpty()72 bool SAInterceptorManager::SAInterceptorListIsEmpty()
73 {
74     return saInterceptors_.empty();
75 }
76 
ExecuteSAInterceptor(const std::string & params,Rule & rule)77 int32_t SAInterceptorManager::ExecuteSAInterceptor(const std::string &params, Rule &rule)
78 {
79     TAG_LOGI(AAFwkTag::SA_INTERCEPTOR, "call ExecuteSAInterceptor");
80     std::lock_guard<std::mutex> lock(saInterceptorLock_);
81     for (auto &interceptor : saInterceptors_) {
82         if (interceptor == nullptr) {
83             continue;
84         }
85         HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
86         auto result = interceptor->OnCheckStarting(params, rule);
87         if (result != ERR_OK || rule.type != RuleType::ALLOW) {
88             TAG_LOGW(AAFwkTag::SA_INTERCEPTOR, "OnCheckStarting error: %{public}d", result);
89             return result;
90         }
91     }
92     return ERR_OK;
93 }
94 
OnObserverDied(const wptr<IRemoteObject> & remote)95 void SAInterceptorManager::OnObserverDied(const wptr<IRemoteObject> &remote)
96 {
97     TAG_LOGI(AAFwkTag::SA_INTERCEPTOR, "call OnObserverDied");
98     auto remoteObj = remote.promote();
99     if (remoteObj == nullptr) {
100         TAG_LOGE(AAFwkTag::SA_INTERCEPTOR, "null remote");
101         return;
102     }
103     std::lock_guard<std::mutex> lock(saInterceptorLock_);
104     for (auto interceptorIter = saInterceptors_.begin(); interceptorIter != saInterceptors_.end(); interceptorIter++) {
105         if (*interceptorIter && (*interceptorIter)->AsObject() == remoteObj) {
106             saInterceptors_.erase(interceptorIter);
107             return;
108         }
109     }
110 }
111 
ObserverExist(sptr<IRemoteBroker> observer)112 bool SAInterceptorManager::ObserverExist(sptr<IRemoteBroker> observer)
113 {
114     if (observer == nullptr) {
115         TAG_LOGE(AAFwkTag::ABILITYMGR, "null observer");
116         return false;
117     }
118     std::lock_guard<std::mutex> lockRegister(saInterceptorLock_);
119     for (auto it = saInterceptors_.begin(); it != saInterceptors_.end(); ++it) {
120         if ((*it)->AsObject() == observer->AsObject()) {
121             return true;
122         }
123     }
124     return false;
125 }
126 
GenerateSAInterceptorParams(const AAFwk::Want & want,sptr<IRemoteObject> callerToken,const AppExecFwk::AbilityInfo & abilityInfo,const std::string & dialogSessionId)127 std::string SAInterceptorManager::GenerateSAInterceptorParams(const AAFwk::Want &want, sptr<IRemoteObject> callerToken,
128     const AppExecFwk::AbilityInfo &abilityInfo, const std::string &dialogSessionId)
129 {
130     nlohmann::json jsonObj;
131     auto abilityRecord = AAFwk::Token::GetAbilityRecordByToken(callerToken);
132     if (abilityRecord) {
133         const auto &callerAbilityInfo = abilityRecord->GetAbilityInfo();
134         jsonObj["callerBundleName"] = callerAbilityInfo.bundleName;
135         jsonObj["callerModuleName"] = callerAbilityInfo.moduleName;
136         jsonObj["callerAbilityName"] = callerAbilityInfo.name;
137     } else {
138         jsonObj["callerBundleName"] = "";
139         jsonObj["callerModuleName"] = "";
140         jsonObj["callerAbilityName"] = "";
141     }
142     jsonObj["callerUid"] = IPCSkeleton::GetCallingUid();
143     jsonObj["callerAppState"] = abilityRecord ? static_cast<int32_t>(abilityRecord->GetAppState()) : 0;
144     jsonObj["callerUserId"] = IPCSkeleton::GetCallingUid() / BASE_USER_RANGE;
145 
146     jsonObj["targetBundleName"] = abilityInfo.bundleName;
147     jsonObj["targetModuleName"] = abilityInfo.moduleName;
148     jsonObj["targetAbilityName"] = abilityInfo.name;
149     jsonObj["targetUid"] = abilityInfo.applicationInfo.uid;
150     jsonObj["targetAbilityType"] = abilityInfo.type;
151     jsonObj["targetAppIndex"] = abilityInfo.appIndex;
152     jsonObj["targetExtensionAbilityType"] = abilityInfo.extensionAbilityType;
153     jsonObj["linking"] = want.GetUriString();
154     jsonObj["dialogSessionId"] = dialogSessionId;
155     return jsonObj.dump();
156 }
157 
SAInterceptorRecipient(RemoteDiedHandler handler)158 SAInterceptorRecipient::SAInterceptorRecipient(RemoteDiedHandler handler) : handler_(handler)
159 {}
160 
~SAInterceptorRecipient()161 SAInterceptorRecipient::~SAInterceptorRecipient()
162 {}
163 
OnRemoteDied(const wptr<IRemoteObject> & remote)164 void SAInterceptorRecipient::OnRemoteDied(const wptr<IRemoteObject> &__attribute__((unused)) remote)
165 {
166     TAG_LOGI(AAFwkTag::SA_INTERCEPTOR, "call OnRemoteDied");
167     if (handler_) {
168         handler_(remote);
169     }
170 }
171 } // namespace AbilityRuntime
172 } // namespace OHOS