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