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 /** 95 * @brief return current bundle name 96 */ GetCurrentBundleName()97 const char *GetCurrentBundleName() const 98 { 99 return currentBundleName_; 100 } 101 GetTopJSAbilityImpl()102 const JSAbilityImpl *GetTopJSAbilityImpl() const 103 { 104 return topJSAbilityImpl_; 105 } 106 /** 107 * @brief return current ability implementation 108 */ SetTopJSAbilityImpl(JSAbilityImpl * object)109 void SetTopJSAbilityImpl(JSAbilityImpl *object) 110 { 111 topJSAbilityImpl_ = object; 112 } 113 void LoadApiVersion(); 114 int32_t GetCompatibleApi() const; 115 void SetCompatibleApi(int32_t compatibleApi); 116 int32_t GetTargetApi() const; 117 void SetTargetApi(int32_t targetApi); 118 GetStyleManager()119 const AppStyleManager *GetStyleManager() 120 { 121 if (styleManage_ == nullptr) { 122 styleManage_ = new AppStyleManager(); 123 styleManage_->Prepare(); 124 } 125 return styleManage_; 126 } 127 ReleaseStyles()128 void ReleaseStyles() 129 { 130 if (styleManage_) { 131 delete styleManage_; 132 styleManage_ = nullptr; 133 } 134 } 135 136 /* 137 * @brief: clear app env. 138 */ 139 void ClearContext(); 140 GetLazyLoadManager()141 const LazyLoadManager *GetLazyLoadManager() 142 { 143 if (lazyLoadManager_ == nullptr) { 144 lazyLoadManager_ = new LazyLoadManager(); 145 } 146 return lazyLoadManager_; 147 } 148 ReleaseLazyLoadManager()149 void ReleaseLazyLoadManager() 150 { 151 if (lazyLoadManager_) { 152 delete lazyLoadManager_; 153 lazyLoadManager_ = nullptr; 154 } 155 } 156 char *GetResourcePath(const char *uri) const; 157 GetCurrentAbilityToken()158 uint16_t GetCurrentAbilityToken() const 159 { 160 return currentToken_; 161 } 162 163 TopAbilityState GetAbilityState() const; 164 void SetAbilityState(TopAbilityState state); 165 private: 166 /** 167 * @brief: release the ability info saved 168 */ 169 void ReleaseAbilityInfo(); 170 171 void SetGlobalNamedProperty(bool isAppEval, jerry_value_t viewModel) const; 172 /** 173 * @brief try read the target mode file content, if failed, change to read another mode 174 * 175 * @param: isSnapshotMode target mode, can be adjusted to the proper mode 176 * @param: outLength the reading content length 177 * @param: fullPath js file full path 178 * @param: fullPathLength the given file name length 179 * 180 * @return the target mode file content or nullptr for reading failure 181 */ 182 char *EvaluateFile(bool &isSnapshotMode, uint32_t &outLength, char *fullPath, size_t fullPathLength) const; 183 void CheckSnapshotVersion(const char *bcFileContent, uint32_t contentLength) const; 184 char *ProcessResourcePathByConfiguration(size_t origUriLength, const char *slicedFilePath) const; 185 char *currentBundleName_ = nullptr; 186 char *currentAbilityPath_ = nullptr; 187 char *currentJsPath_ = nullptr; 188 JSAbilityImpl *topJSAbilityImpl_ = nullptr; 189 AppStyleManager *styleManage_ = nullptr; 190 LazyLoadManager *lazyLoadManager_ = nullptr; 191 // record current running ability's uuid && ability path, will be release during app-cleanup 192 uint16_t currentToken_ = 0; 193 int32_t compatibleApi_ = 0; 194 int32_t targetApi_ = 0; 195 TopAbilityState abilityState_ = TopAbilityState::ABILITY_UNINITIALIZED; 196 }; 197 } // namespace ACELite 198 } // namespace OHOS 199 200 #endif // OHOS_ACELITE_JS_APP_CONTEXT_H 201