1 /*
2  * Copyright (c) 2021-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 "ability_event_handler.h"
17 
18 #include "ability_manager_service.h"
19 #include "ability_util.h"
20 
21 namespace OHOS {
22 namespace AAFwk {
AbilityEventHandler( const std::shared_ptr<TaskHandlerWrap> &taskHandler, const std::weak_ptr<AbilityManagerService> &server)23 AbilityEventHandler::AbilityEventHandler(
24     const std::shared_ptr<TaskHandlerWrap> &taskHandler, const std::weak_ptr<AbilityManagerService> &server)
25     : EventHandlerWrap(taskHandler), server_(server)
26 {
27     TAG_LOGI(AAFwkTag::ABILITYMGR, "constructors");
28 }
29 
ProcessEvent(const EventWrap &event)30 void AbilityEventHandler::ProcessEvent(const EventWrap &event)
31 {
32     TAG_LOGD(AAFwkTag::ABILITYMGR, "Event id obtained: %{public}u.", event.GetEventId());
33     // check libc.hook_mode
34     const int bufferLen = 128;
35     char paramOutBuf[bufferLen] = {0};
36     const char *hook_mode = "startup:";
37     int ret = GetParameter("libc.hook_mode", "", paramOutBuf, bufferLen);
38     if (ret > 0 && strncmp(paramOutBuf, hook_mode, strlen(hook_mode)) == 0) {
39         TAG_LOGD(AAFwkTag::ABILITYMGR, "Hook_mode: no process time out");
40         return;
41     }
42     switch (event.GetEventId()) {
43         case AbilityManagerService::LOAD_HALF_TIMEOUT_MSG: {
44             ProcessLoadTimeOut(event, true);
45             break;
46         }
47         case AbilityManagerService::LOAD_TIMEOUT_MSG: {
48             ProcessLoadTimeOut(event, false);
49             break;
50         }
51         case AbilityManagerService::ACTIVE_TIMEOUT_MSG: {
52             ProcessActiveTimeOut(event.GetParam());
53             break;
54         }
55         case AbilityManagerService::INACTIVE_TIMEOUT_MSG: {
56             TAG_LOGD(AAFwkTag::ABILITYMGR, "Inactive timeout.");
57             // inactivate pre ability immediately in case blocking next ability start
58             ProcessInactiveTimeOut(event.GetParam());
59             break;
60         }
61         case AbilityManagerService::FOREGROUND_HALF_TIMEOUT_MSG: {
62             ProcessForegroundTimeOut(event, true);
63             break;
64         }
65         case AbilityManagerService::FOREGROUND_TIMEOUT_MSG: {
66             ProcessForegroundTimeOut(event, false);
67             break;
68         }
69         case AbilityManagerService::SHAREDATA_TIMEOUT_MSG: {
70             ProcessShareDataTimeOut(event.GetParam());
71             break;
72         }
73         default: {
74             TAG_LOGW(AAFwkTag::ABILITYMGR, "unsupported timeout message");
75             break;
76         }
77     }
78 }
79 
ProcessLoadTimeOut(const EventWrap &event, bool isHalf)80 void AbilityEventHandler::ProcessLoadTimeOut(const EventWrap &event, bool isHalf)
81 {
82     TAG_LOGD(AAFwkTag::ABILITYMGR, "called");
83     auto server = server_.lock();
84     CHECK_POINTER(server);
85     server->HandleLoadTimeOut(event.GetParam(), isHalf, event.IsExtension());
86 }
87 
ProcessActiveTimeOut(int64_t abilityRecordId)88 void AbilityEventHandler::ProcessActiveTimeOut(int64_t abilityRecordId)
89 {
90     TAG_LOGD(AAFwkTag::ABILITYMGR, "called");
91     auto server = server_.lock();
92     CHECK_POINTER(server);
93     server->HandleActiveTimeOut(abilityRecordId);
94 }
95 
ProcessInactiveTimeOut(int64_t abilityRecordId)96 void AbilityEventHandler::ProcessInactiveTimeOut(int64_t abilityRecordId)
97 {
98     TAG_LOGD(AAFwkTag::ABILITYMGR, "called");
99     auto server = server_.lock();
100     CHECK_POINTER(server);
101     server->HandleInactiveTimeOut(abilityRecordId);
102 }
103 
ProcessForegroundTimeOut(const EventWrap &event, bool isHalf)104 void AbilityEventHandler::ProcessForegroundTimeOut(const EventWrap &event, bool isHalf)
105 {
106     TAG_LOGI(AAFwkTag::ABILITYMGR, "foreground timeout");
107     auto server = server_.lock();
108     CHECK_POINTER(server);
109     server->HandleForegroundTimeOut(event.GetParam(), isHalf, event.IsExtension());
110 }
111 
ProcessShareDataTimeOut(int64_t uniqueId)112 void AbilityEventHandler::ProcessShareDataTimeOut(int64_t uniqueId)
113 {
114     TAG_LOGI(AAFwkTag::ABILITYMGR, "shareData timeout");
115     auto server = server_.lock();
116     CHECK_POINTER(server);
117     server->HandleShareDataTimeOut(uniqueId);
118 }
119 
120 }  // namespace AAFwk
121 }  // namespace OHOS
122