1 /*
2  * Copyright (c) 2020-2021 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 #ifndef OHOS_ACELITE_JS_ABILITY_H
17 #define OHOS_ACELITE_JS_ABILITY_H
18 
19 #include <cstdint>
20 #include <cstdlib>
21 #include <cstring>
22 
23 #ifdef _MINI_MULTI_TASKS_
24 #include "ability_saved_data.h"
25 #endif
26 
27 #include "memory_heap.h"
28 
29 namespace OHOS {
30 namespace ACELite {
31 class JSAbility final : public MemoryHeap {
32 public:
33     JSAbility(const JSAbility &) = delete;
34     JSAbility &operator=(const JSAbility &) = delete;
35     JSAbility(JSAbility &&) = delete;
36     JSAbility &operator=(JSAbility &&) = delete;
37 
38     /**
39      * @fn JSAbility::JSAbility()
40      *
41      * @brief Default constructor.
42      */
JSAbility()43     JSAbility() : jsAbilityImpl_(nullptr) {}
44 
45     /**
46      * @fn virtual JSAbility::~JSAbility()
47      *
48      * @brief Destructor.
49      */
50     virtual ~JSAbility();
51 
52     /**
53      * @fn JSAbility::Launch()
54      *
55      * @brief Call this function to create the JS runtime environment, and it will init
56      *        the JS framework and eval abilityPath/src/index.js as well
57      * @param [in] the ability path to bundle name and token of this ability
58      */
59     void Launch(const char *abilityPath, const char *bundleName, uint16_t token, const char *pageInfo = nullptr);
60 
61 #ifdef _MINI_MULTI_TASKS_
62     /**
63      * @fn JSAbility::OnRestoreData()
64      *
65      * @brief Called when the user data need to be restored.
66      */
67     void OnRestoreData(AbilitySlite::AbilitySavedData* data);
68 #endif
69 
70     /**
71      * @fn JSAbility::Show()
72      *
73      * @brief Call this function to active the application UI
74      */
75     void Show();
76 
77     /**
78      * @fn JSAbility::Hide()
79      *
80      * @brief Call this function to move the current JS application to background
81      */
82     void Hide();
83 
84 #ifdef _MINI_MULTI_TASKS_
85     /**
86      * @fn JSAbility::OnSaveData()
87      *
88      * @brief Called when the user data need to be saved.
89      */
90     void OnSaveData(AbilitySlite::AbilitySavedData* data);
91 #endif
92 
93     /**
94      * @fn JSAbility::TransferToDestroy()
95      *
96      * @brief Tear down JS runtime, release JS engine, and invoke user's onDestroy callback
97      */
98     void TransferToDestroy();
99 
100     /**
101      * @fn JSAbility::BackPressed()
102      *
103      * @brief Called by AbilityHost to notify back key has been pressed, JS ability will tear down
104      */
105     void BackPressed();
106 
107     /**
108      * @fn JSAbility::GetPackageName()
109      *
110      * @brief Return current package name
111      */
112     static const char *GetPackageName();
113 
114     /**
115      * @fn JSAbility::ForceDestroy()
116      *
117      * @brief Force to clean up the whole application
118      */
119     void ForceDestroy();
120 
121     /**
122      * @fn JSAbility::IsRecycled()
123      *
124      * @brief Used for checking if the current ability is already teardown
125      */
126     bool IsRecycled();
127 
128     /**
129      * @fn JSAbility::HandleRenderTick()
130      *
131      * @brief Call the render tick if it's set
132      */
133     void HandleRenderTick();
134 
135     /**
136      * @fn JSAbility::LazyLoadHandleRenderTick()
137      *
138      * @brief Call the lazy load tick if it's set
139      */
140     static void LazyLoadHandleRenderTick(void *data);
141 
142 private:
143     // the holder of JS runtime and user' JS related environment
144     void *jsAbilityImpl_ = nullptr;
145     // tracking the error render tick count
146     uint32_t errorTickCount_ = 0;
147     // to avoid tracing too frequently, output every 5 ticks
148     static constexpr uint8_t ERR_TICK_COUNT_TRACE_CTRL = 5;
149     bool isActived_ = false;
150 }; // class JSAbility
151 } // namespace ACELite
152 } // namespace OHOS
153 #endif // OHOS_ACELITE_JS_ABILITY_H
154