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_TIMEOUT_MSG: {
44 ProcessLoadTimeOut(event);
45 break;
46 }
47 case AbilityManagerService::ACTIVE_TIMEOUT_MSG: {
48 ProcessActiveTimeOut(event.GetParam());
49 break;
50 }
51 case AbilityManagerService::INACTIVE_TIMEOUT_MSG: {
52 TAG_LOGD(AAFwkTag::ABILITYMGR, "Inactive timeout.");
53 // inactivate pre ability immediately in case blocking next ability start
54 ProcessInactiveTimeOut(event.GetParam());
55 break;
56 }
57 case AbilityManagerService::FOREGROUND_TIMEOUT_MSG: {
58 ProcessForegroundTimeOut(event);
59 break;
60 }
61 case AbilityManagerService::SHAREDATA_TIMEOUT_MSG: {
62 ProcessShareDataTimeOut(event.GetParam());
63 break;
64 }
65 default: {
66 TAG_LOGW(AAFwkTag::ABILITYMGR, "Unsupported timeout message.");
67 break;
68 }
69 }
70 }
71
ProcessLoadTimeOut(const EventWrap & event)72 void AbilityEventHandler::ProcessLoadTimeOut(const EventWrap &event)
73 {
74 TAG_LOGD(AAFwkTag::ABILITYMGR, "called");
75 auto server = server_.lock();
76 CHECK_POINTER(server);
77 if (event.GetRunCount() == 0) {
78 uint32_t timeout = event.GetTimeout();
79 if (timeout == 0) {
80 timeout = 3000; // 3000 : default timeout
81 }
82 auto eventWrap = EventWrap(AbilityManagerService::LOAD_TIMEOUT_MSG, event.GetParam(), event.IsExtension());
83 eventWrap.SetRunCount(event.GetRunCount() + 1);
84 eventWrap.SetTimeout(timeout);
85 SendEvent(eventWrap, timeout);
86 }
87 server->HandleLoadTimeOut(event.GetParam(), event.GetRunCount() == 0, event.IsExtension());
88 }
89
ProcessActiveTimeOut(int64_t abilityRecordId)90 void AbilityEventHandler::ProcessActiveTimeOut(int64_t abilityRecordId)
91 {
92 TAG_LOGD(AAFwkTag::ABILITYMGR, "called");
93 auto server = server_.lock();
94 CHECK_POINTER(server);
95 server->HandleActiveTimeOut(abilityRecordId);
96 }
97
ProcessInactiveTimeOut(int64_t abilityRecordId)98 void AbilityEventHandler::ProcessInactiveTimeOut(int64_t abilityRecordId)
99 {
100 TAG_LOGD(AAFwkTag::ABILITYMGR, "called");
101 auto server = server_.lock();
102 CHECK_POINTER(server);
103 server->HandleInactiveTimeOut(abilityRecordId);
104 }
105
ProcessForegroundTimeOut(const EventWrap & event)106 void AbilityEventHandler::ProcessForegroundTimeOut(const EventWrap &event)
107 {
108 TAG_LOGI(AAFwkTag::ABILITYMGR, "Foreground timeout.");
109 auto server = server_.lock();
110 CHECK_POINTER(server);
111 if (event.GetRunCount() == 0) {
112 uint32_t timeout = event.GetTimeout();
113 if (timeout == 0) {
114 timeout = 3000; // 3000 : default timeout
115 }
116 auto eventWrap = EventWrap(AbilityManagerService::FOREGROUND_TIMEOUT_MSG, event.GetParam(),
117 event.IsExtension());
118 eventWrap.SetRunCount(event.GetRunCount() + 1);
119 eventWrap.SetTimeout(timeout);
120 SendEvent(eventWrap, timeout);
121 }
122 server->HandleForegroundTimeOut(event.GetParam(), event.GetRunCount() == 0, event.IsExtension());
123 }
124
ProcessShareDataTimeOut(int64_t uniqueId)125 void AbilityEventHandler::ProcessShareDataTimeOut(int64_t uniqueId)
126 {
127 TAG_LOGI(AAFwkTag::ABILITYMGR, "ShareData timeout.");
128 auto server = server_.lock();
129 CHECK_POINTER(server);
130 server->HandleShareDataTimeOut(uniqueId);
131 }
132
133 } // namespace AAFwk
134 } // namespace OHOS
135