• 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_PAGE_STATE_MACHINE_H
17 #define OHOS_ACELITE_JS_PAGE_STATE_MACHINE_H
18 
19 #include "component.h"
20 #include "js_app_context.h"
21 #include "non_copyable.h"
22 #include "scroll_layer.h"
23 
24 #define JS_PAGE_RETURN_IF_ERROR(error, pagePath)                                           \
25     do {                                                                                   \
26         if (error != 0) {                                                                  \
27             HILOG_ERROR(HILOG_MODULE_ACE, "use secure function error(%{public}d)", error); \
28             ace_free(pagePath);                                                            \
29             pagePath = nullptr;                                                            \
30             return ERROR_SECURE_USE;                                                       \
31         }                                                                                  \
32     } while (0)
33 
34 namespace OHOS {
35 namespace ACELite {
36 enum {
37     UNDEFINED_STATE = -1,
38     INIT_STATE, // 0
39     READY_STATE,
40     SHOW_STATE,
41     BACKGROUND_STATE,
42     DESTROY_STATE,
43     END_STATE
44 };
45 #define PAGE_STATE_SIZE END_STATE
46 
47 constexpr char PAGE_LIFECYCLE_ON_INIT[] = "onInit";
48 constexpr char PAGE_LIFECYCLE_CALLBACK_ON_READY[] = "onReady";
49 constexpr char PAGE_LIFECYCLE_CALLBACK_ON_SHOW[] = "onShow";
50 constexpr char PAGE_LIFECYCLE_CALLBACK_ON_HIDE[] = "onHide";
51 constexpr char PAGE_LIFECYCLE_CALLBACK_ON_DESTROY[] = "onDestroy";
52 
53 #if JS_PAGE_SPECIFIC
54 extern JSPageSpecific jsPageSpecific;
55 #endif
56 
57 class State;
58 class StateMachine final : public MemoryHeap {
59 public:
60     ACE_DISALLOW_COPY_AND_MOVE(StateMachine);
61     StateMachine();
62     ~StateMachine();
63     bool Init(jerry_value_t object, jerry_value_t &jsRes);
64     void ChangeState(int newState);
65     void BindParameters();
66     bool BindUri(jerry_value_t &jsRes);
GetCurrentState()67     int8_t GetCurrentState() const
68     {
69         return currentState_;
70     }
71     void EvalPage();
72     void RenderPage();
73     void ShowPage() const;
74     void HidePage() const;
75     void SetCurrentState(int8_t newState);
76     void InvokePageLifeCycleCallback(const char * const name) const;
77     void ReleaseHistoryPageResource();
78     void SetHiddenFlag(bool flag);
79 #ifdef TDD_ASSERTIONS
80     // this function is just for unittest's view modle hooking purpose, should not be used in real environment
81     void SetViewModel(jerry_value_t viewModel);
82 #endif // TDD_ASSERTIONS
83 
84 private:
85     void RegisterUriAndParamsToPage(const char * const uri, jerry_value_t params);
86     int GenerateJsPagePath(const char * const uri);
87     void DeleteViewModelProperties() const;
88     void ReleaseRootObject() const;
89     bool CheckJSSourceFile() const;
90 
91 private:
92     int8_t currentState_;
93     State *stateMap_[PAGE_STATE_SIZE];
94     char *jsPagePath_;
95     char *appRootPath_;
96     char *uri_;
97     Component *rootComponent_;
98     JsAppContext *appContext_;
99     jerry_value_t viewModel_;     // the object evaled from user JS code
100     jerry_value_t object_;        // object transferred from one page to another page
101     bool hasParams_;              // the flag representation for whether having params of object_
102     bool isEntireHidden_;            // representing if the whole app is in background
103     Watcher *watchersHead_;       // head of watchers list
104     ScrollLayer *scrollLayer_;
105 };
106 } // namespace ACELite
107 } // namespace OHOS
108 
109 #endif // OHOS_ACELITE_JS_PAGE_STATE_MACHINE_H
110