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