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