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