1/* 2 * Copyright (c) 2022-2023 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 */ 15class BusinessError extends Error { 16 constructor(code) { 17 let msg = ''; 18 if (errMap.has(code)) { 19 msg = errMap.get(code); 20 } else { 21 msg = ERROR_MSG_INNER_ERROR; 22 } 23 super(msg); 24 this.code = code; 25 } 26} 27 28class EventHub { 29 constructor() { 30 this.eventMap = {}; 31 } 32 33 on(event, callback) { 34 if ((typeof (event) !== 'string') || (typeof (callback) !== 'function')) { 35 throw new BusinessError(ERROR_CODE_INVALID_PARAM); 36 return; 37 } 38 if (!this.eventMap[event]) { 39 this.eventMap[event] = []; 40 } 41 if (this.eventMap[event].indexOf(callback) === -1) { 42 this.eventMap[event].push(callback); 43 } 44 } 45 46 off(event, callback) { 47 if (typeof (event) !== 'string') { 48 throw new BusinessError(ERROR_CODE_INVALID_PARAM); 49 return; 50 } 51 if (this.eventMap[event]) { 52 if (callback) { 53 let cbArray = this.eventMap[event]; 54 let index = cbArray.indexOf(callback); 55 if (index > -1) { 56 for (; index + 1 < cbArray.length; index++) { 57 cbArray[index] = cbArray[index + 1]; 58 } 59 cbArray.pop(); 60 } 61 } else { 62 delete this.eventMap[event]; 63 } 64 } 65 } 66 67 emit(event, ...args) { 68 if (typeof (event) !== 'string') { 69 throw new BusinessError(ERROR_CODE_INVALID_PARAM); 70 return; 71 } 72 if (this.eventMap[event]) { 73 const cloneArray = [...this.eventMap[event]]; 74 const len = cloneArray.length; 75 for (let i = 0; i < len; ++i) { 76 cloneArray[i].apply(this, args); 77 } 78 } 79 } 80} 81 82class ApplicationContext { 83 constructor(obj) { 84 this.__context_impl__ = obj; 85 this.__context_impl__.eventHub = new EventHub(); 86 } 87 88 registerAbilityLifecycleCallback(abilityLifecycleCallback) { 89 return this.__context_impl__.registerAbilityLifecycleCallback(abilityLifecycleCallback); 90 } 91 92 unregisterAbilityLifecycleCallback(callbackId, callback) { 93 return this.__context_impl__.unregisterAbilityLifecycleCallback(callbackId, callback); 94 } 95 96 registerEnvironmentCallback(environmentCallback) { 97 return this.__context_impl__.registerEnvironmentCallback(environmentCallback); 98 } 99 100 unregisterEnvironmentCallback(callbackId, envcallback) { 101 return this.__context_impl__.unregisterEnvironmentCallback(callbackId, envcallback); 102 } 103 104 on(type, callback) { 105 return this.__context_impl__.on(type, callback); 106 } 107 108 off(type, callbackId, callback) { 109 return this.__context_impl__.off(type, callbackId, callback); 110 } 111 112 createBundleContext(bundleName) { 113 return this.__context_impl__.createBundleContext(bundleName); 114 } 115 116 createModuleContext(moduleName) { 117 return this.__context_impl__.createModuleContext(moduleName); 118 } 119 120 createModuleContext(bundleName, moduleName) { 121 return this.__context_impl__.createModuleContext(bundleName, moduleName); 122 } 123 124 createSystemHspModuleResourceManager(bundleName, moduleName) { 125 return this.__context_impl__.createSystemHspModuleResourceManager(bundleName, moduleName); 126 } 127 128 createModuleResourceManager(bundleName, moduleName) { 129 return this.__context_impl__.createModuleResourceManager(bundleName, moduleName); 130 } 131 132 getGroupDir(groupId, callback) { 133 return this.__context_impl__.getGroupDir(groupId, callback); 134 } 135 136 getApplicationContext() { 137 return this.__context_impl__.getApplicationContext(); 138 } 139 140 killAllProcesses(callback) { 141 return this.__context_impl__.killAllProcesses(callback); 142 } 143 144 getProcessRunningInformation(callback) { 145 return this.__context_impl__.getProcessRunningInformation(callback); 146 } 147 148 getRunningProcessInformation(callback) { 149 return this.__context_impl__.getProcessRunningInformation(callback); 150 } 151 152 setColorMode(colorMode) { 153 return this.__context_impl__.setColorMode(colorMode); 154 } 155 156 setLanguage(language) { 157 return this.__context_impl__.setLanguage(language); 158 } 159 160 setFont(font) { 161 return this.__context_impl__.setFont(font); 162 } 163 164 setFontSizeScale(fontSizeScale) { 165 return this.__context_impl__.setFontSizeScale(fontSizeScale); 166 } 167 168 setAutoStartup(info, callback) { 169 return this.__context_impl__.setAutoStartup(info, callback); 170 } 171 172 cancelAutoStartup(info, callback) { 173 return this.__context_impl__.cancelAutoStartup(info, callback); 174 } 175 176 isAutoStartup(info, callback) { 177 return this.__context_impl__.isAutoStartup(info, callback); 178 } 179 180 clearUpApplicationData() { 181 return this.__context_impl__.clearUpApplicationData(); 182 } 183 184 clearUpApplicationData(callback) { 185 return this.__context_impl__.clearUpApplicationData(callback); 186 } 187 188 preloadUIExtensionAbility(want) { 189 return this.__context_impl__.preloadUIExtensionAbility(want); 190 } 191 192 restartApp(want) { 193 return this.__context_impl__.restartApp(want); 194 } 195 196 setSupportedProcessCache(isSupport) { 197 return this.__context_impl__.setSupportedProcessCache(isSupport); 198 } 199 200 getCurrentAppCloneIndex() { 201 return this.__context_impl__.getCurrentAppCloneIndex(); 202 } 203 204 getCurrentInstanceKey() { 205 return this.__context_impl__.getCurrentInstanceKey(); 206 } 207 208 getAllRunningInstanceKeys() { 209 return this.__context_impl__.getAllRunningInstanceKeys(); 210 } 211 212 set area(mode) { 213 return this.__context_impl__.switchArea(mode); 214 } 215 216 get area() { 217 return this.__context_impl__.getArea(); 218 } 219 220 get resourceManager() { 221 return this.__context_impl__.resourceManager; 222 } 223 224 get applicationInfo() { 225 return this.__context_impl__.applicationInfo; 226 } 227 228 get cacheDir() { 229 return this.__context_impl__.cacheDir; 230 } 231 232 get tempDir() { 233 return this.__context_impl__.tempDir; 234 } 235 236 get resourceDir() { 237 return this.__context_impl__.resourceDir; 238 } 239 240 get filesDir() { 241 return this.__context_impl__.filesDir; 242 } 243 244 get distributedFilesDir() { 245 return this.__context_impl__.distributedFilesDir; 246 } 247 248 get databaseDir() { 249 return this.__context_impl__.databaseDir; 250 } 251 252 get preferencesDir() { 253 return this.__context_impl__.preferencesDir; 254 } 255 256 get bundleCodeDir() { 257 return this.__context_impl__.bundleCodeDir; 258 } 259 260 get cloudFileDir() { 261 return this.__context_impl__.cloudFileDir; 262 } 263 264 get eventHub() { 265 return this.__context_impl__.eventHub; 266 } 267 268 get stageMode() { 269 return true; 270 } 271} 272 273export default ApplicationContext; 274