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_APPLICATION_CONTEXT_H 17 #define OHOS_ABILITY_RUNTIME_APPLICATION_CONTEXT_H 18 19 #include <vector> 20 #include <shared_mutex> 21 22 #include "ability_lifecycle_callback.h" 23 #include "application_state_change_callback.h" 24 #include "context.h" 25 #include "context_impl.h" 26 #include "environment_callback.h" 27 28 namespace OHOS { 29 namespace AbilityRuntime { 30 class ApplicationContext : public Context { 31 public: 32 ApplicationContext() = default; 33 ~ApplicationContext() = default; 34 void RegisterAbilityLifecycleCallback(const std::shared_ptr<AbilityLifecycleCallback> &abilityLifecycleCallback); 35 void UnregisterAbilityLifecycleCallback(const std::shared_ptr<AbilityLifecycleCallback> &abilityLifecycleCallback); 36 bool IsAbilityLifecycleCallbackEmpty(); 37 void RegisterEnvironmentCallback(const std::shared_ptr<EnvironmentCallback> &environmentCallback); 38 void UnregisterEnvironmentCallback(const std::shared_ptr<EnvironmentCallback> &environmentCallback); 39 void RegisterApplicationStateChangeCallback( 40 const std::weak_ptr<ApplicationStateChangeCallback> &applicationStateChangeCallback); 41 void DispatchOnAbilityCreate(const std::shared_ptr<NativeReference> &ability); 42 void DispatchOnWindowStageCreate(const std::shared_ptr<NativeReference> &ability, 43 const std::shared_ptr<NativeReference> &windowStage); 44 void DispatchOnWindowStageDestroy(const std::shared_ptr<NativeReference> &ability, 45 const std::shared_ptr<NativeReference> &windowStage); 46 void DispatchWindowStageFocus(const std::shared_ptr<NativeReference> &ability, 47 const std::shared_ptr<NativeReference> &windowStage); 48 void DispatchWindowStageUnfocus(const std::shared_ptr<NativeReference> &ability, 49 const std::shared_ptr<NativeReference> &windowStage); 50 void DispatchOnAbilityDestroy(const std::shared_ptr<NativeReference> &ability); 51 void DispatchOnAbilityForeground(const std::shared_ptr<NativeReference> &ability); 52 void DispatchOnAbilityBackground(const std::shared_ptr<NativeReference> &ability); 53 void DispatchOnAbilityContinue(const std::shared_ptr<NativeReference> &ability); 54 void DispatchConfigurationUpdated(const AppExecFwk::Configuration &config); 55 void DispatchMemoryLevel(const int level); 56 void NotifyApplicationForeground(); 57 void NotifyApplicationBackground(); 58 59 std::string GetBundleName() const override; 60 std::shared_ptr<Context> CreateBundleContext(const std::string &bundleName) override; 61 std::shared_ptr<Context> CreateModuleContext(const std::string &moduleName) override; 62 std::shared_ptr<Context> CreateModuleContext(const std::string &bundleName, const std::string &moduleName) override; 63 std::shared_ptr<AppExecFwk::ApplicationInfo> GetApplicationInfo() const override; 64 void SetApplicationInfo(const std::shared_ptr<AppExecFwk::ApplicationInfo> &info); 65 std::shared_ptr<Global::Resource::ResourceManager> GetResourceManager() const override; 66 std::string GetBundleCodePath() const override; 67 std::string GetBundleCodeDir() override; 68 std::string GetCacheDir() override; 69 std::string GetTempDir() override; 70 std::string GetFilesDir() override; 71 bool IsUpdatingConfigurations() override; 72 bool PrintDrawnCompleted() override; 73 std::string GetDatabaseDir() override; 74 std::string GetPreferencesDir() override; 75 int32_t GetSystemDatabaseDir(const std::string &groupId, bool checkExist, std::string &databaseDir) override; 76 int32_t GetSystemPreferencesDir(const std::string &groupId, bool checkExist, std::string &preferencesDir) override; 77 std::string GetGroupDir(std::string groupId) override; 78 std::string GetDistributedFilesDir() override; 79 sptr<IRemoteObject> GetToken() override; 80 void SetToken(const sptr<IRemoteObject> &token) override; 81 void SwitchArea(int mode) override; 82 int GetArea() override; 83 std::shared_ptr<AppExecFwk::Configuration> GetConfiguration() const override; 84 std::string GetBaseDir() const override; 85 Global::Resource::DeviceType GetDeviceType() const override; 86 void KillProcessBySelf(); 87 int32_t GetProcessRunningInformation(AppExecFwk::RunningProcessInfo &info); 88 89 void AttachContextImpl(const std::shared_ptr<ContextImpl> &contextImpl); 90 91 static std::shared_ptr<ApplicationContext> GetInstance(); 92 93 // unused 94 std::shared_ptr<AppExecFwk::HapModuleInfo> GetHapModuleInfo() const override; 95 96 private: 97 std::shared_ptr<ContextImpl> contextImpl_; 98 static std::vector<std::shared_ptr<AbilityLifecycleCallback>> callbacks_; 99 static std::vector<std::shared_ptr<EnvironmentCallback>> envCallbacks_; 100 static std::weak_ptr<ApplicationStateChangeCallback> applicationStateCallback_; 101 std::mutex callbackLock_; 102 }; 103 } // namespace AbilityRuntime 104 } // namespace OHOS 105 #endif // OHOS_ABILITY_RUNTIME_APPLICATION_CONTEXT_H 106