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 // uniform error code for error throwing 54 constexpr uint32_t ERR_CODE_URL_NOTEXIST = 200002; 55 56 #if (defined(JS_PAGE_SPECIFIC) && (JS_PAGE_SPECIFIC == 1)) 57 extern JSPageSpecific jsPageSpecific; 58 #endif 59 60 class State; 61 class StateMachine final : public MemoryHeap { 62 public: 63 ACE_DISALLOW_COPY_AND_MOVE(StateMachine); 64 StateMachine(); 65 ~StateMachine(); 66 bool Init(jerry_value_t object, jerry_value_t &jsRes); 67 void ChangeState(int newState); 68 void BindParameters(); 69 bool BindUri(jerry_value_t &jsRes); GetCurrentState()70 int8_t GetCurrentState() const 71 { 72 return currentState_; 73 } 74 void EvalPage(); 75 void RenderPage(); 76 void ShowPage() const; 77 void HidePage() const; 78 void SetCurrentState(int8_t newState); 79 void InvokePageLifeCycleCallback(const char * const name) const; 80 void ReleaseHistoryPageResource(); 81 void SetHiddenFlag(bool flag); 82 #ifdef TDD_ASSERTIONS 83 // this function is just for unittest's view modle hooking purpose, should not be used in real environment 84 void SetViewModel(jerry_value_t viewModel); 85 #endif // TDD_ASSERTIONS 86 87 private: 88 void RegisterUriAndParamsToPage(const char * const uri, jerry_value_t params); 89 int GenerateJsPagePath(const char * const uri); 90 void DeleteViewModelProperties() const; 91 void ReleaseRootObject() const; 92 bool CheckJSSourceFile() const; 93 94 private: 95 int8_t currentState_; 96 State *stateMap_[PAGE_STATE_SIZE]; 97 char *jsPagePath_; 98 char *appRootPath_; 99 char *uri_; 100 Component *rootComponent_; 101 JsAppContext *appContext_; 102 jerry_value_t viewModel_; // the object evaled from user JS code 103 jerry_value_t object_; // object transferred from one page to another page 104 bool hasParams_; // the flag representation for whether having params of object_ 105 bool isEntireHidden_; // representing if the whole app is in background 106 Watcher *watchersHead_; // head of watchers list 107 ScrollLayer *scrollLayer_; 108 }; 109 } // namespace ACELite 110 } // namespace OHOS 111 112 #endif // OHOS_ACELITE_JS_PAGE_STATE_MACHINE_H 113