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