1 /*
2 * Copyright (c) 2020 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 "js_app_host.h"
17
18 #include "ability_manager_inner.h"
19 #include "ability_message_id.h"
20 #include "ability_state.h"
21 #include "adapter.h"
22
23 using namespace OHOS::ACELite;
24
25 namespace OHOS {
JsAppHost()26 JsAppHost::JsAppHost()
27 {
28 }
29
~JsAppHost()30 JsAppHost::~JsAppHost()
31 {
32 if (jsAbility_ != nullptr) {
33 delete jsAbility_;
34 }
35 }
36
JsAppTaskHandler(uint32_t uwArg)37 void JsAppHost::JsAppTaskHandler(uint32_t uwArg)
38 {
39 auto jsappHost = reinterpret_cast<JsAppHost *>((uintptr_t)uwArg);
40 if (jsappHost == nullptr) {
41 return;
42 }
43
44 osMessageQueueId_t appQueueId = jsappHost->GetMessageQueueId();
45 if (appQueueId == nullptr) {
46 return;
47 }
48
49 for (;;) {
50 AbilityInnerMsg innerMsg;
51 uint8_t prio = 0;
52 osStatus_t ret = osMessageQueueGet(appQueueId, &innerMsg, &prio, osWaitForever);
53 if (ret != osOK) {
54 return;
55 }
56 LP_TaskBegin();
57 switch ((uint32_t)innerMsg.msgId) {
58 case ACTIVE:
59 jsappHost->OnActive(innerMsg.token, innerMsg.bundleName, innerMsg.path);
60 break;
61 case BACKGROUND:
62 jsappHost->OnBackground(innerMsg.token);
63 break;
64 case DESTORY:
65 // cleanup the message queue id to present any new async message
66 JsAsyncWork::SetAppQueueHandler(nullptr);
67 jsappHost->OnDestroy(innerMsg.token);
68 LP_TaskEnd();
69 return; // here exit the loop, and abort all messages afterwards
70 case BACKPRESSED:
71 jsappHost->BackPressed();
72 break;
73 case ASYNCWORK: {
74 AsyncWork* work = reinterpret_cast<AsyncWork *>(innerMsg.data);
75 JsAsyncWork::ExecuteAsyncWork(work);
76 break;
77 }
78 case TE_EVENT:
79 jsappHost->HandleTickEvent();
80 break;
81 default:
82 break;
83 }
84 LP_TaskEnd();
85 }
86 }
87
OnActive(uint16_t token,const char * bundleName,const char * path)88 void JsAppHost::OnActive(uint16_t token, const char *bundleName, const char *path)
89 {
90 if (jsAbility_ == nullptr) {
91 jsAbility_ = new ACELite::JSAbility();
92 if (jsAbility_ == nullptr) {
93 return;
94 }
95 jsAbility_->Launch(const_cast<char *>(path), bundleName, token);
96 }
97 jsAbility_->Show();
98 isBackground_ = false;
99 SendMsgToAbilityService(token, STATE_ACTIVE);
100 }
101
OnBackground(uint16_t token)102 void JsAppHost::OnBackground(uint16_t token)
103 {
104 if (jsAbility_ != nullptr) {
105 isBackground_ = true;
106 jsAbility_->Hide();
107 SendMsgToAbilityService(token, STATE_BACKGROUND);
108 }
109 }
110
OnDestroy(uint16_t token)111 void JsAppHost::OnDestroy(uint16_t token)
112 {
113 if (jsAbility_ != nullptr) {
114 // the TE owner will be JS application after JS application start up except for it's lying in background,
115 // call render once to make sure the last TE message is processed properly
116 if (!isBackground_) {
117 jsAbility_->HandleRenderTick();
118 }
119 jsAbility_->TransferToDestroy();
120 }
121 SendMsgToAbilityService(token, STATE_UNINITIALIZED);
122 }
123
BackPressed()124 void JsAppHost::BackPressed()
125 {
126 if (jsAbility_ != nullptr) {
127 jsAbility_->BackPressed();
128 }
129 }
130
ForceDestroy() const131 void JsAppHost::ForceDestroy() const
132 {
133 if (jsAbility_ != nullptr) {
134 jsAbility_->ForceDestroy();
135 }
136 }
137
HandleTickEvent()138 void JsAppHost::HandleTickEvent()
139 {
140 if (jsAbility_ != nullptr) {
141 jsAbility_->HandleRenderTick();
142 }
143 }
144
SendMsgToAbilityService(uint16_t token,int32_t id)145 void JsAppHost::SendMsgToAbilityService(uint16_t token, int32_t id)
146 {
147 SchedulerLifecycleDone(token, id);
148 }
149 } // namespace OHOS
150