1 /*
2 * Copyright (c) 2023 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_thread.h"
17
18 #include "abilityms_log.h"
19 #include "ability_errors.h"
20 #include "ability_inner_message.h"
21 #include "adapter.h"
22
23 namespace OHOS {
24 namespace AbilitySlite {
25
26 AbilityThread::AbilityThread() = default;
27
28 AbilityThread::~AbilityThread() = default;
29
HandleCreate(const Want * want)30 int32_t AbilityThread::HandleCreate(const Want *want)
31 {
32 if (ability_ == nullptr) {
33 return PARAM_NULL_ERROR;
34 }
35 if (want == nullptr) {
36 return PARAM_NULL_ERROR;
37 }
38 if (want->data != nullptr) {
39 HILOG_INFO(HILOG_MODULE_AAFWK, "start ability with data %{public}u", want->dataLength);
40 } else {
41 HILOG_INFO(HILOG_MODULE_AAFWK, "start ability with no data");
42 }
43 ability_->OnCreate(*want);
44 return ERR_OK;
45 }
46
HandleRestore(AbilitySavedData * data)47 int32_t AbilityThread::HandleRestore(AbilitySavedData *data)
48 {
49 if (ability_ == nullptr) {
50 return PARAM_NULL_ERROR;
51 }
52 if (data != nullptr) {
53 ability_->OnRestoreData(data);
54 }
55 return ERR_OK;
56 }
57
HandleForeground(const Want * want)58 int32_t AbilityThread::HandleForeground(const Want *want)
59 {
60 if (ability_ == nullptr) {
61 return PARAM_NULL_ERROR;
62 }
63 if (want == nullptr) {
64 return PARAM_NULL_ERROR;
65 }
66 if (want->data != nullptr) {
67 HILOG_INFO(HILOG_MODULE_AAFWK, "foreground ability with data %{public}u", want->dataLength);
68 } else {
69 HILOG_INFO(HILOG_MODULE_AAFWK, "foreground ability with no data");
70 }
71 ability_->OnForeground(*want);
72 return ERR_OK;
73 }
74
HandleBackground()75 int32_t AbilityThread::HandleBackground()
76 {
77 if (ability_ == nullptr) {
78 return PARAM_NULL_ERROR;
79 }
80 ability_->OnBackground();
81 return ERR_OK;
82 }
83
HandleSave(AbilitySavedData * data)84 int32_t AbilityThread::HandleSave(AbilitySavedData *data)
85 {
86 if (ability_ == nullptr) {
87 return PARAM_NULL_ERROR;
88 }
89 if (data != nullptr) {
90 ability_->OnSaveData(data);
91 }
92 return ERR_OK;
93 }
94
HandleDestroy()95 int32_t AbilityThread::HandleDestroy()
96 {
97 if (ability_ == nullptr) {
98 return PARAM_NULL_ERROR;
99 }
100 ability_->OnDestroy();
101 return ERR_OK;
102 }
103
SendScheduleMsgToAbilityThread(SliteAbilityInnerMsg & innerMsg)104 int32_t AbilityThread::SendScheduleMsgToAbilityThread(SliteAbilityInnerMsg &innerMsg)
105 {
106 constexpr uint32_t maxRetryTimes = 5;
107
108 uint32_t retryTimes = 0;
109 while (retryTimes < maxRetryTimes) {
110 osStatus_t ret = osMessageQueuePut(GetMessageQueueId(), static_cast<void *>(&innerMsg), 0, 0);
111 if (ret == osOK) {
112 return ERR_OK;
113 }
114 HILOG_WARN(HILOG_MODULE_AAFWK, "AbilityThread osMessageQueuePut failed with %{public}d", ret);
115 osDelay(200); // sleep 200ms
116 retryTimes++;
117 }
118
119 HILOG_ERROR(HILOG_MODULE_AAFWK, "AbilityThread SendScheduleMsgToAbilityThread failed.");
120 return IPC_REQUEST_ERROR;
121 }
122 } // namespace AbilitySlite
123 } // namespace OHOS
124