1 /* 2 * Copyright (c) 2022 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_ABILITY_RUNTIME_JS_APPLICATION_CONTEXT_UTILS_H 17 #define OHOS_ABILITY_RUNTIME_JS_APPLICATION_CONTEXT_UTILS_H 18 19 #include <memory> 20 21 #include "ability_lifecycle_callback.h" 22 #include "application_context.h" 23 #include "native_engine/native_engine.h" 24 25 namespace OHOS { 26 namespace AbilityRuntime { 27 class JsApplicationContextUtils { 28 public: JsApplicationContextUtils(std::weak_ptr<ApplicationContext> && applicationContext)29 explicit JsApplicationContextUtils(std::weak_ptr<ApplicationContext> &&applicationContext) 30 : applicationContext_(std::move(applicationContext)) 31 { 32 } 33 virtual ~JsApplicationContextUtils() = default; 34 static void Finalizer(NativeEngine *engine, void *data, void *hint); 35 static NativeValue* RegisterAbilityLifecycleCallback(NativeEngine *engine, NativeCallbackInfo *info); 36 static NativeValue* UnregisterAbilityLifecycleCallback(NativeEngine *engine, NativeCallbackInfo *info); 37 static NativeValue* RegisterEnvironmentCallback(NativeEngine *engine, NativeCallbackInfo *info); 38 static NativeValue* UnregisterEnvironmentCallback(NativeEngine *engine, NativeCallbackInfo *info); 39 static NativeValue* On(NativeEngine *engine, NativeCallbackInfo *info); 40 static NativeValue* Off(NativeEngine *engine, NativeCallbackInfo *info); 41 static NativeValue* CreateBundleContext(NativeEngine *engine, NativeCallbackInfo *info); 42 static NativeValue* SwitchArea(NativeEngine *engine, NativeCallbackInfo *info); 43 static NativeValue* GetArea(NativeEngine* engine, NativeCallbackInfo* info); 44 static NativeValue* CreateModuleContext(NativeEngine* engine, NativeCallbackInfo* info); 45 46 NativeValue* OnRegisterAbilityLifecycleCallback(NativeEngine &engine, NativeCallbackInfo &info); 47 NativeValue* OnUnregisterAbilityLifecycleCallback(NativeEngine &engine, NativeCallbackInfo &info); 48 49 NativeValue* OnRegisterEnvironmentCallback(NativeEngine &engine, NativeCallbackInfo &info); 50 NativeValue* OnUnregisterEnvironmentCallback(NativeEngine &engine, NativeCallbackInfo &info); 51 52 NativeValue* OnOn(NativeEngine &engine, NativeCallbackInfo &info); 53 NativeValue* OnOff(NativeEngine &engine, const NativeCallbackInfo &info); 54 NativeValue* OnOnAbilityLifecycle(NativeEngine &engine, NativeCallbackInfo &info); 55 NativeValue* OnOffAbilityLifecycle(NativeEngine &engine, const NativeCallbackInfo &info, int32_t callbackId); 56 NativeValue* OnOnEnvironment(NativeEngine &engine, NativeCallbackInfo &info); 57 NativeValue* OnOffEnvironment(NativeEngine &engine, const NativeCallbackInfo &info, int32_t callbackId); 58 59 NativeValue* OnGetCacheDir(NativeEngine &engine, NativeCallbackInfo &info); 60 NativeValue* OnGetTempDir(NativeEngine &engine, NativeCallbackInfo &info); 61 NativeValue* OnGetFilesDir(NativeEngine &engine, NativeCallbackInfo &info); 62 NativeValue* OnGetDistributedFilesDir(NativeEngine &engine, NativeCallbackInfo &info); 63 NativeValue* OnGetDatabaseDir(NativeEngine &engine, NativeCallbackInfo &info); 64 NativeValue* OnGetPreferencesDir(NativeEngine &engine, NativeCallbackInfo &info); 65 NativeValue* OnGetBundleCodeDir(NativeEngine &engine, NativeCallbackInfo &info); 66 NativeValue* OnKillProcessBySelf(NativeEngine &engine, NativeCallbackInfo &info); 67 NativeValue* OnGetRunningProcessInformation(NativeEngine &engine, NativeCallbackInfo &info); 68 69 static NativeValue* GetCacheDir(NativeEngine *engine, NativeCallbackInfo *info); 70 static NativeValue* GetTempDir(NativeEngine *engine, NativeCallbackInfo *info); 71 static NativeValue* GetFilesDir(NativeEngine *engine, NativeCallbackInfo *info); 72 static NativeValue* GetDistributedFilesDir(NativeEngine *engine, NativeCallbackInfo *info); 73 static NativeValue* GetDatabaseDir(NativeEngine *engine, NativeCallbackInfo *info); 74 static NativeValue* GetPreferencesDir(NativeEngine *engine, NativeCallbackInfo *info); 75 static NativeValue* GetBundleCodeDir(NativeEngine *engine, NativeCallbackInfo *info); 76 static NativeValue* GetApplicationContext(NativeEngine *engine, NativeCallbackInfo *info); 77 static NativeValue* KillProcessBySelf(NativeEngine *engine, NativeCallbackInfo *info); 78 static NativeValue* GetRunningProcessInformation(NativeEngine *engine, NativeCallbackInfo *info); 79 static NativeValue* CreateJsApplicationContext(NativeEngine &engine); 80 81 protected: 82 std::weak_ptr<ApplicationContext> applicationContext_; 83 84 private: 85 NativeValue* OnCreateBundleContext(NativeEngine &engine, NativeCallbackInfo &info); 86 NativeValue* OnSwitchArea(NativeEngine &engine, NativeCallbackInfo &info); 87 NativeValue* OnGetArea(NativeEngine& engine, NativeCallbackInfo& info); 88 NativeValue* OnCreateModuleContext(NativeEngine& engine, NativeCallbackInfo& info); 89 NativeValue* OnGetApplicationContext(NativeEngine& engine, NativeCallbackInfo& info); 90 static void BindNativeApplicationContext(NativeEngine &engine, NativeObject* object); 91 92 std::shared_ptr<JsAbilityLifecycleCallback> callback_; 93 std::shared_ptr<JsEnvironmentCallback> envCallback_; 94 }; 95 } // namespace AbilityRuntime 96 } // namespace OHOS 97 #endif // OHOS_ABILITY_RUNTIME_JS_APPLICATION_CONTEXT_UTILS_H 98