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_APP_CONTEXT_H
17 #define OHOS_ACELITE_JS_APP_CONTEXT_H
18 #include "acelite_config.h"
19 #include "lazy_load_manager.h"
20 #include "stylemgr/app_style_manager.h"
21 
22 namespace OHOS {
23 namespace ACELite {
24 
25 enum class TopAbilityState {
26     ABILITY_UNINITIALIZED = -1,
27     ABILITY_INITIALIZED,
28     ABILITY_LAUNCHING,
29     ABILITY_LAUNCHDONE,
30     ABILITY_SHOWING,
31     ABILITY_SHOWN,
32     ABILITY_HIDING,
33     ABILITY_HIDDEN,
34     ABILITY_DESTROYING,
35     ABILITY_DESTROYED
36 };
37 
38 class JSAbilityImpl;
39 /**
40  * @brief Global App context.
41  */
42 class JsAppContext final : public MemoryHeap {
43 public:
GetInstance()44     static JsAppContext *GetInstance()
45     {
46         static JsAppContext instance;
47         return &instance;
48     }
49 
50     /**
51      * @brief trigger terminate request
52      */
53     void TerminateAbility() const;
54     /**
55      * @brief eval user's JS Code and return FeatureAbility object
56      *
57      * @param: fullPath js file full path
58      * @param: fullPathLength the given file name length
59      * @param: the flag for app eval or page eval.True is eval flag.
60      */
61     jerry_value_t Eval(char *fullPath, size_t fullPathLength, bool isAppEval) const;
62     /**
63      * @brief call FeatureAbility's render function
64      *
65      * @param: page instance, viewModel to call render on
66      */
67     jerry_value_t Render(jerry_value_t viewModel) const;
68     /**
69      * @brief set the ability path, bundle name and token info for current ability
70      *
71      * @param: ability's path, bundle name and token info
72      */
73     void SetCurrentAbilityInfo(const char * const abilityPath, const char * const bundleName, uint16_t token);
74     /**
75      * @brief return ability path
76      */
GetCurrentAbilityPath()77     const char *GetCurrentAbilityPath() const
78     {
79         return currentAbilityPath_;
80     }
81     /**
82      * @brief set the js path and uuid info for current ability
83      *
84      * @param: js's path
85      */
86     void SetCurrentJsPath(const char * const jsPath);
87     /**
88      * @brief return js path
89      */
GetCurrentJsPath()90     const char *GetCurrentJsPath() const
91     {
92         return currentJsPath_;
93     }
94 #ifdef _MINI_MULTI_TASKS_
95     /**
96      * @brief set the js path and uuid info for current ability
97      *
98      * @param: current router uri
99      */
100     void SetCurrentUri(const char* const uri);
101      /**
102      * @brief return current router uri
103      */
GetCurrentUri()104     const char* GetCurrentUri() const
105     {
106         return currentUri_;
107     }
108 #endif
109     /**
110      * @brief return current bundle name
111      */
GetCurrentBundleName()112     const char *GetCurrentBundleName() const
113     {
114         return currentBundleName_;
115     }
GetTopJSAbilityImpl()116     const JSAbilityImpl *GetTopJSAbilityImpl() const
117     {
118         return topJSAbilityImpl_;
119     }
120     /**
121      * @brief return current ability implementation
122      */
SetTopJSAbilityImpl(JSAbilityImpl * object)123     void SetTopJSAbilityImpl(JSAbilityImpl *object)
124     {
125         topJSAbilityImpl_ = object;
126     }
127     void LoadApiVersion();
128     int32_t GetCompatibleApi() const;
129     void SetCompatibleApi(int32_t compatibleApi);
130     int32_t GetTargetApi() const;
131     void SetTargetApi(int32_t targetApi);
132 
GetStyleManager()133     const AppStyleManager *GetStyleManager()
134     {
135         if (styleManage_ == nullptr) {
136             styleManage_ = new AppStyleManager();
137             styleManage_->Prepare();
138         }
139         return styleManage_;
140     }
141 
ReleaseStyles()142     void ReleaseStyles()
143     {
144         if (styleManage_) {
145             delete styleManage_;
146             styleManage_ = nullptr;
147         }
148     }
149 
150     /*
151      * @brief: clear app env.
152      */
153     void ClearContext();
154 
GetLazyLoadManager()155     const LazyLoadManager *GetLazyLoadManager()
156     {
157         if (lazyLoadManager_ == nullptr) {
158             lazyLoadManager_ = new LazyLoadManager();
159         }
160         return lazyLoadManager_;
161     }
162 
ReleaseLazyLoadManager()163     void ReleaseLazyLoadManager()
164     {
165         if (lazyLoadManager_) {
166             delete lazyLoadManager_;
167             lazyLoadManager_ = nullptr;
168         }
169     }
170     char *GetResourcePath(const char *uri) const;
171 
GetCurrentAbilityToken()172     uint16_t GetCurrentAbilityToken() const
173     {
174         return currentToken_;
175     }
176 
177     TopAbilityState GetAbilityState() const;
178     void SetAbilityState(TopAbilityState state);
179 private:
180     /**
181      * @brief: release the ability info saved
182      */
183     void ReleaseAbilityInfo();
184 
185     void SetGlobalNamedProperty(bool isAppEval, jerry_value_t viewModel) const;
186     /**
187      * @brief try read the target mode file content, if failed, change to read another mode
188      *
189      * @param: isSnapshotMode target mode, can be adjusted to the proper mode
190      * @param: outLength the reading content length
191      * @param: fullPath js file full path
192      * @param: fullPathLength the given file name length
193      *
194      * @return the target mode file content or nullptr for reading failure
195      */
196     char *EvaluateFile(bool &isSnapshotMode, uint32_t &outLength, char *fullPath, size_t fullPathLength) const;
197     void CheckSnapshotVersion(const char *bcFileContent, uint32_t contentLength) const;
198     char *ProcessResourcePathByConfiguration(size_t origUriLength, const char *slicedFilePath) const;
199     char *currentBundleName_ = nullptr;
200     char *currentAbilityPath_ = nullptr;
201     char *currentJsPath_ = nullptr;
202 #ifdef _MINI_MULTI_TASKS_
203     char *currentUri_ = nullptr;
204 #endif
205     JSAbilityImpl *topJSAbilityImpl_ = nullptr;
206     AppStyleManager *styleManage_ = nullptr;
207     LazyLoadManager *lazyLoadManager_ = nullptr;
208     // record current running ability's uuid && ability path, will be release during app-cleanup
209     uint16_t currentToken_ = 0;
210     int32_t compatibleApi_ = 0;
211     int32_t targetApi_ = 0;
212     TopAbilityState abilityState_ = TopAbilityState::ABILITY_UNINITIALIZED;
213 };
214 } // namespace ACELite
215 } // namespace OHOS
216 
217 #endif // OHOS_ACELITE_JS_APP_CONTEXT_H
218