1 /* 2 * Copyright (c) 2022 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/ability_interceptor_executer.h" 17 #include "hitrace_meter.h" 18 19 namespace OHOS { 20 namespace AAFwk { AddInterceptor(std::string interceptorName,const std::shared_ptr<IAbilityInterceptor> & interceptor)21void AbilityInterceptorExecuter::AddInterceptor(std::string interceptorName, 22 const std::shared_ptr<IAbilityInterceptor> &interceptor) 23 { 24 std::lock_guard lock(interceptorMapLock_); 25 if (interceptor != nullptr) { 26 interceptorMap_[interceptorName] = interceptor; 27 } 28 } 29 RemoveInterceptor(std::string interceptorName)30void AbilityInterceptorExecuter::RemoveInterceptor(std::string interceptorName) 31 { 32 std::lock_guard lock(interceptorMapLock_); 33 auto iter = interceptorMap_.find(interceptorName); 34 if (iter != interceptorMap_.end()) { 35 interceptorMap_.erase(interceptorName); 36 } 37 } 38 DoProcess(AbilityInterceptorParam param)39ErrCode AbilityInterceptorExecuter::DoProcess(AbilityInterceptorParam param) 40 { 41 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__); 42 int32_t result = ERR_OK; 43 auto interceptorMap = GetInterceptorMapCopy(); 44 auto item = interceptorMap.begin(); 45 while (item != interceptorMap.end()) { 46 if ((*item).second == nullptr) { 47 item++; 48 continue; 49 } 50 result = (*item).second->DoProcess(param); 51 if (result != ERR_OK) { 52 break; 53 } else { 54 item++; 55 } 56 } 57 return result; 58 } 59 SetTaskHandler(std::shared_ptr<AAFwk::TaskHandlerWrap> taskHandler)60void AbilityInterceptorExecuter::SetTaskHandler(std::shared_ptr<AAFwk::TaskHandlerWrap> taskHandler) 61 { 62 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__); 63 64 std::lock_guard lock(interceptorMapLock_); 65 for (auto &item : interceptorMap_) { 66 if (item.second == nullptr) { 67 continue; 68 } 69 (item.second)->SetTaskHandler(taskHandler); 70 } 71 } 72 GetInterceptorMapCopy()73InterceptorMap AbilityInterceptorExecuter::GetInterceptorMapCopy() 74 { 75 std::lock_guard lock(interceptorMapLock_); 76 return interceptorMap_; 77 } 78 } // namespace AAFwk 79 } // namespace OHOS