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 */ 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 #include <mutex> 21 22 #include "ability_lifecycle_callback.h" 23 #include "application_context.h" 24 #include "application_state_change_callback.h" 25 #include "native_engine/native_engine.h" 26 #include "running_process_info.h" 27 28 namespace OHOS { 29 namespace AbilityRuntime { 30 namespace { 31 enum JsAppProcessState { 32 STATE_CREATE, 33 STATE_FOREGROUND, 34 STATE_ACTIVE, 35 STATE_BACKGROUND, 36 STATE_DESTROY 37 }; 38 } 39 class JsApplicationContextUtils { 40 public: JsApplicationContextUtils(std::weak_ptr<ApplicationContext> && applicationContext)41 explicit JsApplicationContextUtils(std::weak_ptr<ApplicationContext> &&applicationContext) 42 : applicationContext_(std::move(applicationContext)) 43 { 44 } 45 virtual ~JsApplicationContextUtils() = default; 46 static void Finalizer(NativeEngine *engine, void *data, void *hint); 47 static NativeValue* RegisterAbilityLifecycleCallback(NativeEngine *engine, NativeCallbackInfo *info); 48 static NativeValue* UnregisterAbilityLifecycleCallback(NativeEngine *engine, NativeCallbackInfo *info); 49 static NativeValue* RegisterEnvironmentCallback(NativeEngine *engine, NativeCallbackInfo *info); 50 static NativeValue* UnregisterEnvironmentCallback(NativeEngine *engine, NativeCallbackInfo *info); 51 static NativeValue* On(NativeEngine *engine, NativeCallbackInfo *info); 52 static NativeValue* Off(NativeEngine *engine, NativeCallbackInfo *info); 53 static NativeValue* CreateBundleContext(NativeEngine *engine, NativeCallbackInfo *info); 54 static NativeValue* SwitchArea(NativeEngine *engine, NativeCallbackInfo *info); 55 static NativeValue* GetArea(NativeEngine* engine, NativeCallbackInfo* info); 56 static NativeValue* CreateModuleContext(NativeEngine* engine, NativeCallbackInfo* info); 57 58 NativeValue* OnRegisterAbilityLifecycleCallback(NativeEngine &engine, NativeCallbackInfo &info); 59 NativeValue* OnUnregisterAbilityLifecycleCallback(NativeEngine &engine, NativeCallbackInfo &info); 60 61 NativeValue* OnRegisterEnvironmentCallback(NativeEngine &engine, NativeCallbackInfo &info); 62 NativeValue* OnUnregisterEnvironmentCallback(NativeEngine &engine, NativeCallbackInfo &info); 63 64 NativeValue* OnOn(NativeEngine &engine, NativeCallbackInfo &info); 65 NativeValue* OnOff(NativeEngine &engine, const NativeCallbackInfo &info); 66 NativeValue* OnOnAbilityLifecycle(NativeEngine &engine, NativeCallbackInfo &info, bool isSync); 67 NativeValue* OnOffAbilityLifecycle(NativeEngine &engine, const NativeCallbackInfo &info, int32_t callbackId); 68 NativeValue* OnOffAbilityLifecycleEventSync(NativeEngine &engine, 69 const NativeCallbackInfo &info, int32_t callbackId); 70 NativeValue* OnOnEnvironment(NativeEngine &engine, NativeCallbackInfo &info, bool isSync); 71 NativeValue* OnOffEnvironment(NativeEngine &engine, const NativeCallbackInfo &info, int32_t callbackId); 72 NativeValue* OnOffEnvironmentEventSync( 73 NativeEngine &engine, const NativeCallbackInfo &info, int32_t callbackId); 74 NativeValue* OnOnApplicationStateChange(NativeEngine &engine, NativeCallbackInfo &info); 75 NativeValue* OnOffApplicationStateChange(NativeEngine &engine, const NativeCallbackInfo &info); 76 77 NativeValue* OnGetCacheDir(NativeEngine &engine, NativeCallbackInfo &info); 78 NativeValue* OnGetTempDir(NativeEngine &engine, NativeCallbackInfo &info); 79 NativeValue* OnGetFilesDir(NativeEngine &engine, NativeCallbackInfo &info); 80 NativeValue* OnGetDistributedFilesDir(NativeEngine &engine, NativeCallbackInfo &info); 81 NativeValue* OnGetDatabaseDir(NativeEngine &engine, NativeCallbackInfo &info); 82 NativeValue* OnGetPreferencesDir(NativeEngine &engine, NativeCallbackInfo &info); 83 NativeValue* OnGetGroupDir(NativeEngine &engine, NativeCallbackInfo &info); 84 NativeValue* OnGetBundleCodeDir(NativeEngine &engine, NativeCallbackInfo &info); 85 NativeValue* OnKillProcessBySelf(NativeEngine &engine, NativeCallbackInfo &info); 86 NativeValue* OnGetRunningProcessInformation(NativeEngine &engine, NativeCallbackInfo &info); 87 88 static NativeValue* GetCacheDir(NativeEngine *engine, NativeCallbackInfo *info); 89 static NativeValue* GetTempDir(NativeEngine *engine, NativeCallbackInfo *info); 90 static NativeValue* GetFilesDir(NativeEngine *engine, NativeCallbackInfo *info); 91 static NativeValue* GetDistributedFilesDir(NativeEngine *engine, NativeCallbackInfo *info); 92 static NativeValue* GetDatabaseDir(NativeEngine *engine, NativeCallbackInfo *info); 93 static NativeValue* GetPreferencesDir(NativeEngine *engine, NativeCallbackInfo *info); 94 static NativeValue* GetGroupDir(NativeEngine *engine, NativeCallbackInfo *info); 95 static NativeValue* GetBundleCodeDir(NativeEngine *engine, NativeCallbackInfo *info); 96 static NativeValue* GetApplicationContext(NativeEngine *engine, NativeCallbackInfo *info); 97 static NativeValue* KillProcessBySelf(NativeEngine *engine, NativeCallbackInfo *info); 98 static NativeValue* GetRunningProcessInformation(NativeEngine *engine, NativeCallbackInfo *info); 99 static NativeValue* CreateJsApplicationContext(NativeEngine &engine); 100 101 protected: 102 std::weak_ptr<ApplicationContext> applicationContext_; 103 104 private: 105 NativeValue* OnCreateBundleContext(NativeEngine &engine, NativeCallbackInfo &info); 106 NativeValue* OnSwitchArea(NativeEngine &engine, NativeCallbackInfo &info); 107 NativeValue* OnGetArea(NativeEngine& engine, NativeCallbackInfo& info); 108 NativeValue* OnCreateModuleContext(NativeEngine& engine, NativeCallbackInfo& info); 109 NativeValue* OnGetApplicationContext(NativeEngine& engine, NativeCallbackInfo& info); 110 bool CheckCallerIsSystemApp(); 111 static void BindNativeApplicationContext(NativeEngine &engine, NativeObject* object); 112 static JsAppProcessState ConvertToJsAppProcessState( 113 const AppExecFwk::AppProcessState &appProcessState, const bool &isFocused); 114 std::shared_ptr<JsAbilityLifecycleCallback> callback_; 115 std::shared_ptr<JsEnvironmentCallback> envCallback_; 116 std::shared_ptr<JsApplicationStateChangeCallback> applicationStateCallback_; 117 std::mutex applicationStateCallbackLock_; 118 }; 119 } // namespace AbilityRuntime 120 } // namespace OHOS 121 #endif // OHOS_ABILITY_RUNTIME_JS_APPLICATION_CONTEXT_UTILS_H 122