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_IMPL_H
17 #define OHOS_ACELITE_JS_ABILITY_IMPL_H
18 
19 #ifdef _MINI_MULTI_TASKS_
20 #include "ability_saved_data.h"
21 #endif
22 
23 #include "js_app_context.h"
24 #include "js_debugger_config.h"
25 #include "js_router.h"
26 #include "js_timer_list.h"
27 #if IS_ENABLED(JS_PROFILER)
28 #include "js_profiler.h"
29 #endif
30 #include "non_copyable.h"
31 
32 namespace OHOS {
33 namespace ACELite {
34 class JSAbilityImpl final : public MemoryHeap {
35 public:
36     ACE_DISALLOW_COPY_AND_MOVE(JSAbilityImpl);
37     /**
38      * @fn JSAbilityImpl::JSAbilityImpl()
39      *
40      * @brief constructor with the ability path parameter.
41      */
JSAbilityImpl()42     JSAbilityImpl()
43         : appContext_(nullptr),
44           abilityModel_(0),
45           nativeElement_(0),
46           rendered_(false),
47           isEnvInit_(false),
48 #ifdef _MINI_MULTI_TASKS_
49           pageInfo_(nullptr),
50 #endif
51           router_(nullptr)
52     {
53 #if IS_ENABLED(JS_PROFILER)
54         // Call GetInstance() to prepare data
55         JSProfiler::GetInstance()->PrepareDataBuffer();
56 #endif
57     }
58 
59     /**
60      * @fn virtual JSAbilityImpl::~JSAbilityImpl()
61      *
62      * @brief Destructor.
63      *
64      *        Destructor.
65      */
~JSAbilityImpl()66     virtual ~JSAbilityImpl()
67     {
68 #if IS_ENABLED(JS_PROFILER)
69         // Call release() to free all performance data
70         JSProfiler::GetInstance()->Release();
71 #endif
72     }
73 
74     /**
75      * @fn JSAbilityRuntime::Init(char* path, uint16_t token)
76      *
77      * @brief initialize JS executing environment, including JS engine and JS fwk
78      */
79     void InitEnvironment(const char * const abilityPath, const char * const bundleName, uint16_t token);
80 
81     /**
82      * @fn JSAbilityRuntime::CleanUp()
83      *
84      * @brief destroy JS executing environment
85      */
86     void CleanUp();
87 
88     /**
89      * @fn JSAbilityRuntime::DeliverCreate();
90      * @param param the JS object to init the app start
91      * @brief call this function when transfer lifecycle into init
92      */
93     void DeliverCreate(const char *param = nullptr);
94 
95 #ifdef _MINI_MULTI_TASKS_
96     /**
97      * @fn JSAbilityRuntime::OnRestoreData()
98      *
99      * @brief Called when the user data need to be restored.
100     */
101     void OnRestoreData(AbilitySlite::AbilitySavedData* abilitySavedData);
102 #endif
103 
104     /**
105      * @fn JSAbilityRuntime::Show();
106      *
107      * @brief move JS application to active state
108      */
109     void Show();
110 
111     /**
112      * @fn JSAbilityRuntime::Hide();
113      *
114      * @brief move the JS application to background state
115      */
116     void Hide() const;
117 
118 #ifdef _MINI_MULTI_TASKS_
119     /**
120      * @fn JSAbilityRuntime::OnSaveData()
121      *
122      * @brief Called when the user data need to be saved.
123     */
124     void OnSaveData(AbilitySlite::AbilitySavedData* abilitySavedData);
125 #endif
126 
127     /**
128      * @fn JSAbilityRuntime::NotifyBackPressed()
129      *
130      * @brief call this function when back key pressed
131      */
132     void NotifyBackPressed() const;
133 
134     /**
135      * @fn JSAbilityRuntime::GetRouter();
136      *
137      * @brief call this function when replace new page
138      */
GetRouter()139     const Router *GetRouter() const
140     {
141         return router_;
142     }
143 
GetAppVM()144     jerry_value_t GetAppVM() const
145     {
146         return abilityModel_;
147     }
148 
149 private:
150     void InvokeOnCreate() const;
151 #ifdef _MINI_MULTI_TASKS_
152     bool InvokeOnRestoreData(const char* userData, AbilitySlite::SavedResultCode retCode) const;
153     void GotoPage(const char* uri);
154 #endif
155     void InvokeOnDestroy() const;
156     void InvokeOnBackPressed() const;
157     void InvokeMethodWithoutParameter(const char * const name) const;
158     void MarkAppViewModelEvaling(bool evaling) const;
159 
160     JsAppContext *appContext_;
161     jerry_value_t abilityModel_;  // the object evaled from user JS code
162     jerry_value_t nativeElement_; // the object returned from render function
163     bool rendered_;
164     bool isEnvInit_;
165 #ifdef _MINI_MULTI_TASKS_
166     char *pageInfo_;
167 #endif
168     Router *router_;
169 };
170 } // namespace ACELite
171 } // namespace OHOS
172 #endif // OHOS_ACELITE_JS_ABILITY_IMPL_H
173