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<AppExecFwk::EventRunner> & runner,const std::weak_ptr<AbilityManagerService> & server)25 AbilityEventHandler::AbilityEventHandler(
26 const std::shared_ptr<AppExecFwk::EventRunner> &runner, const std::weak_ptr<AbilityManagerService> &server)
27 : AppExecFwk::EventHandler(runner), server_(server)
28 {
29 HILOG_INFO("Constructors.");
30 }
31
ProcessEvent(const AppExecFwk::InnerEvent::Pointer & event)32 void AbilityEventHandler::ProcessEvent(const AppExecFwk::InnerEvent::Pointer &event)
33 {
34 CHECK_POINTER(event);
35 HILOG_DEBUG("Event id obtained: %{public}u.", event->GetInnerEventId());
36 // check libc.hook_mode
37 const int bufferLen = 128;
38 char paramOutBuf[bufferLen] = {0};
39 const char *hook_mode = "startup:";
40 int ret = GetParameter("libc.hook_mode", "", paramOutBuf, bufferLen);
41 if (ret > 0 && strncmp(paramOutBuf, hook_mode, strlen(hook_mode)) == 0) {
42 HILOG_DEBUG("Hook_mode: no process time out");
43 return;
44 }
45 switch (event->GetInnerEventId()) {
46 case AbilityManagerService::LOAD_TIMEOUT_MSG: {
47 ProcessLoadTimeOut(event->GetParam());
48 break;
49 }
50 case AbilityManagerService::ACTIVE_TIMEOUT_MSG: {
51 ProcessActiveTimeOut(event->GetParam());
52 break;
53 }
54 case AbilityManagerService::INACTIVE_TIMEOUT_MSG: {
55 HILOG_INFO("Inactive timeout.");
56 // inactivate pre ability immediately in case blocking next ability start
57 ProcessInactiveTimeOut(event->GetParam());
58 break;
59 }
60 case AbilityManagerService::FOREGROUND_TIMEOUT_MSG: {
61 ProcessForegroundTimeOut(event->GetParam());
62 break;
63 }
64 case AbilityManagerService::BACKGROUND_TIMEOUT_MSG: {
65 ProcessBackgroundTimeOut(event->GetParam());
66 break;
67 }
68 default: {
69 HILOG_WARN("Unsupported timeout message.");
70 break;
71 }
72 }
73 }
74
ProcessLoadTimeOut(int64_t eventId)75 void AbilityEventHandler::ProcessLoadTimeOut(int64_t eventId)
76 {
77 HILOG_INFO("Attach timeout.");
78 auto server = server_.lock();
79 CHECK_POINTER(server);
80 server->HandleLoadTimeOut(eventId);
81 }
82
ProcessActiveTimeOut(int64_t eventId)83 void AbilityEventHandler::ProcessActiveTimeOut(int64_t eventId)
84 {
85 HILOG_INFO("Active timeout.");
86 auto server = server_.lock();
87 CHECK_POINTER(server);
88 server->HandleActiveTimeOut(eventId);
89 }
90
ProcessInactiveTimeOut(int64_t eventId)91 void AbilityEventHandler::ProcessInactiveTimeOut(int64_t eventId)
92 {
93 HILOG_INFO("Inactive timeout.");
94 auto server = server_.lock();
95 CHECK_POINTER(server);
96 server->HandleInactiveTimeOut(eventId);
97 }
98
ProcessForegroundTimeOut(int64_t eventId)99 void AbilityEventHandler::ProcessForegroundTimeOut(int64_t eventId)
100 {
101 HILOG_INFO("Foreground timeout.");
102 auto server = server_.lock();
103 CHECK_POINTER(server);
104 server->HandleForegroundTimeOut(eventId);
105 }
106
ProcessBackgroundTimeOut(int64_t eventId)107 void AbilityEventHandler::ProcessBackgroundTimeOut(int64_t eventId)
108 {
109 HILOG_INFO("Background timeout.");
110 auto server = server_.lock();
111 CHECK_POINTER(server);
112 server->HandleBackgroundTimeOut(eventId);
113 }
114 } // namespace AAFwk
115 } // namespace OHOS
116