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)21 void 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)30 void 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)39 ErrCode 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         result = (*item).second->DoProcess(param);
47         if (result != ERR_OK) {
48             break;
49         } else {
50             item++;
51         }
52     }
53     return result;
54 }
55 
SetTaskHandler(std::shared_ptr<AAFwk::TaskHandlerWrap> taskHandler)56 void AbilityInterceptorExecuter::SetTaskHandler(std::shared_ptr<AAFwk::TaskHandlerWrap> taskHandler)
57 {
58     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
59 
60     std::lock_guard lock(interceptorMapLock_);
61     for (auto &item : interceptorMap_) {
62         if (item.second == nullptr) {
63             continue;
64         }
65         (item.second)->SetTaskHandler(taskHandler);
66     }
67 }
68 
GetInterceptorMapCopy()69 InterceptorMap AbilityInterceptorExecuter::GetInterceptorMapCopy()
70 {
71     std::lock_guard lock(interceptorMapLock_);
72     return interceptorMap_;
73 }
74 } // namespace AAFwk
75 } // namespace OHOS