• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2024 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 namespace OHOS {
28 namespace AAFwk {
29 class Want;
30 struct ExitReason;
31 }
32 namespace AbilityRuntime {
33 using AppConfigUpdateCallback = std::function<void(const AppExecFwk::Configuration &config)>;
34 using AppProcessExitCallback = std::function<void(const AAFwk::ExitReason &exitReason)>;
35 using AppGetSpecifiedRuntimeCallback = std::function<const std::unique_ptr<Runtime>&(const std::string &)>;
36 class ApplicationContext : public Context {
37 public:
38     ApplicationContext() = default;
39     ~ApplicationContext() = default;
40     void RegisterAbilityLifecycleCallback(const std::shared_ptr<AbilityLifecycleCallback> &abilityLifecycleCallback);
41     void UnregisterAbilityLifecycleCallback(const std::shared_ptr<AbilityLifecycleCallback> &abilityLifecycleCallback);
42     bool IsAbilityLifecycleCallbackEmpty();
43     void RegisterEnvironmentCallback(const std::shared_ptr<EnvironmentCallback> &environmentCallback);
44     void UnregisterEnvironmentCallback(const std::shared_ptr<EnvironmentCallback> &environmentCallback);
45     void RegisterApplicationStateChangeCallback(
46         const std::weak_ptr<ApplicationStateChangeCallback> &applicationStateChangeCallback);
47     void DispatchOnAbilityCreate(const std::shared_ptr<NativeReference> &ability);
48     void DispatchOnWindowStageCreate(const std::shared_ptr<NativeReference> &ability,
49         const std::shared_ptr<NativeReference> &windowStage);
50     void DispatchOnWindowStageDestroy(const std::shared_ptr<NativeReference> &ability,
51         const std::shared_ptr<NativeReference> &windowStage);
52     void DispatchWindowStageFocus(const std::shared_ptr<NativeReference> &ability,
53         const std::shared_ptr<NativeReference> &windowStage);
54     void DispatchWindowStageUnfocus(const std::shared_ptr<NativeReference> &ability,
55         const std::shared_ptr<NativeReference> &windowStage);
56     void DispatchOnAbilityDestroy(const std::shared_ptr<NativeReference> &ability);
57     void DispatchOnAbilityForeground(const std::shared_ptr<NativeReference> &ability);
58     void DispatchOnAbilityBackground(const std::shared_ptr<NativeReference> &ability);
59     void DispatchOnAbilityContinue(const std::shared_ptr<NativeReference> &ability);
60     void DispatchOnAbilityWillContinue(const std::shared_ptr<NativeReference> &ability);
61     void DispatchOnWindowStageWillRestore(const std::shared_ptr<NativeReference> &ability,
62         const std::shared_ptr<NativeReference> &windowStage);
63     void DispatchOnWindowStageRestore(const std::shared_ptr<NativeReference> &ability,
64         const std::shared_ptr<NativeReference> &windowStage);
65     void DispatchOnAbilityWillSaveState(const std::shared_ptr<NativeReference> &ability);
66     void DispatchOnAbilitySaveState(const std::shared_ptr<NativeReference> &ability);
67     void DispatchConfigurationUpdated(const AppExecFwk::Configuration &config);
68     void DispatchMemoryLevel(const int level);
69     void NotifyApplicationForeground();
70     void NotifyApplicationBackground();
71     void DispatchOnWillNewWant(const std::shared_ptr<NativeReference> &ability);
72     void DispatchOnNewWant(const std::shared_ptr<NativeReference> &ability);
73     void DispatchOnAbilityWillCreate(const std::shared_ptr<NativeReference> &ability);
74     void DispatchOnWindowStageWillCreate(const std::shared_ptr<NativeReference> &ability,
75         const std::shared_ptr<NativeReference> &windowStage);
76     void DispatchOnWindowStageWillDestroy(const std::shared_ptr<NativeReference> &ability,
77         const std::shared_ptr<NativeReference> &windowStage);
78     void DispatchOnAbilityWillDestroy(const std::shared_ptr<NativeReference> &ability);
79     void DispatchOnAbilityWillForeground(const std::shared_ptr<NativeReference> &ability);
80     void DispatchOnAbilityWillBackground(const std::shared_ptr<NativeReference> &ability);
81 
82     std::string GetBundleName() const override;
83     std::shared_ptr<Context> CreateBundleContext(const std::string &bundleName) override;
84     std::shared_ptr<Context> CreateModuleContext(const std::string &moduleName) override;
85     std::shared_ptr<Context> CreateModuleContext(const std::string &bundleName, const std::string &moduleName) override;
86     std::shared_ptr<Global::Resource::ResourceManager> CreateModuleResourceManager(
87         const std::string &bundleName, const std::string &moduleName) override;
88     int32_t CreateSystemHspModuleResourceManager(const std::string &bundleName,
89         const std::string &moduleName, std::shared_ptr<Global::Resource::ResourceManager> &resourceManager) override;
90     std::shared_ptr<Context> CreateAreaModeContext(int areaMode) override;
91 #ifdef SUPPORT_GRAPHICS
92     std::shared_ptr<Context> CreateDisplayContext(uint64_t displayId) override;
93 #endif
94     std::shared_ptr<AppExecFwk::ApplicationInfo> GetApplicationInfo() const override;
95     void SetApplicationInfo(const std::shared_ptr<AppExecFwk::ApplicationInfo> &info);
96     std::shared_ptr<Global::Resource::ResourceManager> GetResourceManager() const override;
97     std::string GetBundleCodePath() const override;
98     std::string GetBundleCodeDir() override;
99     std::string GetCacheDir() override;
100     std::string GetTempDir() override;
101     std::string GetResourceDir(const std::string &moduleName = "") override;
102     bool IsModuleExist(const std::string &moduleName);
103     void GetAllTempDir(std::vector<std::string> &tempPaths);
104     std::string GetFilesDir() override;
105     bool IsUpdatingConfigurations() override;
106     bool PrintDrawnCompleted() override;
107     std::string GetDatabaseDir() override;
108     std::string GetPreferencesDir() override;
109     int32_t GetSystemDatabaseDir(const std::string &groupId, bool checkExist, std::string &databaseDir) override;
110     int32_t GetSystemPreferencesDir(const std::string &groupId, bool checkExist, std::string &preferencesDir) override;
111     std::string GetGroupDir(std::string groupId) override;
112     std::string GetDistributedFilesDir() override;
113     std::string GetCloudFileDir() override;
114     sptr<IRemoteObject> GetToken() override;
115     void SetToken(const sptr<IRemoteObject> &token) override;
116     void SwitchArea(int mode) override;
117     void SetColorMode(int32_t colorMode);
118     void SetLanguage(const std::string &language);
119     void SetFont(const std::string &font);
120     bool SetFontSizeScale(double fontSizeScale);
121     void SetMcc(const std::string &mcc);
122     void SetMnc(const std::string &mnc);
123     void ClearUpApplicationData();
124     int GetArea() override;
125     std::string GetProcessName() override;
126     std::shared_ptr<AppExecFwk::Configuration> GetConfiguration() const override;
127     void SetConfiguration(const std::shared_ptr<AppExecFwk::Configuration> &config);
128     void AppHasDarkRes(bool &darkRes);
129     std::string GetBaseDir() const override;
130     Global::Resource::DeviceType GetDeviceType() const override;
131     void KillProcessBySelf(const bool clearPageStack = false);
132     int32_t GetProcessRunningInformation(AppExecFwk::RunningProcessInfo &info);
133     int32_t RestartApp(const AAFwk::Want& want);
134 
135     void AttachContextImpl(const std::shared_ptr<ContextImpl> &contextImpl);
136 
137     static std::shared_ptr<ApplicationContext> GetInstance();
138 
139     // unused
140     std::shared_ptr<AppExecFwk::HapModuleInfo> GetHapModuleInfo() const override;
141 
142     bool GetApplicationInfoUpdateFlag() const;
143     void SetApplicationInfoUpdateFlag(bool flag);
144     void RegisterAppConfigUpdateObserver(AppConfigUpdateCallback appConfigChangeCallback);
145     void RegisterAppFontObserver(AppConfigUpdateCallback appFontCallback);
146     void RegisterProcessSecurityExit(AppProcessExitCallback appProcessExitCallback);
147     void RegisterAppGetSpecifiedRuntime(AppGetSpecifiedRuntimeCallback appGetSpecifiedRuntimeCallback);
148 #ifdef SUPPORT_GRAPHICS
149     void RegisterGetDisplayConfig(GetDisplayConfigCallback getDisplayConfigCallback);
150 #endif
151 
152     std::string GetAppRunningUniqueId() const;
153     void SetAppRunningUniqueId(const std::string &appRunningUniqueId);
154     int32_t SetSupportedProcessCacheSelf(bool isSupport);
155     int32_t GetCurrentAppCloneIndex();
156     void SetCurrentAppCloneIndex(int32_t appIndex);
157     std::string GetCurrentInstanceKey();
158     void SetCurrentInstanceKey(const std::string& instanceKey);
159     int32_t GetAllRunningInstanceKeys(std::vector<std::string> &instanceKeys);
160     int32_t GetCurrentAppMode();
161     void SetCurrentAppMode(int32_t appIndex);
162     void ProcessSecurityExit(const AAFwk::ExitReason &exitReason);
163     napi_env GetMainNapiEnv() const;
164 
165     using SelfType = ApplicationContext;
166     static const size_t CONTEXT_TYPE_ID;
167     std::string GetDataDir();
168 
169 protected:
IsContext(size_t contextTypeId)170     bool IsContext(size_t contextTypeId) override
171     {
172         return contextTypeId == CONTEXT_TYPE_ID || Context::IsContext(contextTypeId);
173     }
174 
175 private:
176     std::shared_ptr<ContextImpl> contextImpl_;
177     static std::vector<std::shared_ptr<AbilityLifecycleCallback>> callbacks_;
178     static std::vector<std::shared_ptr<EnvironmentCallback>> envCallbacks_;
179     static std::vector<std::weak_ptr<ApplicationStateChangeCallback>> applicationStateCallback_;
180     std::recursive_mutex callbackLock_;
181     std::recursive_mutex envCallbacksLock_;
182     std::recursive_mutex applicationStateCallbackLock_;
183     bool applicationInfoUpdateFlag_ = false;
184     AppConfigUpdateCallback appConfigChangeCallback_ = nullptr;
185     AppConfigUpdateCallback appFontCallback_ = nullptr;
186     AppProcessExitCallback appProcessExitCallback_ = nullptr;
187     AppGetSpecifiedRuntimeCallback appGetSpecifiedRuntimeCallback_ = nullptr;
188     std::string appRunningUniqueId_;
189     int32_t appIndex_ = 0;
190     int32_t appMode_ = 0;
191     std::string instanceKey_;
192     std::string dataDir_;
193     std::mutex dataDirMutex_;
194 };
195 }  // namespace AbilityRuntime
196 }  // namespace OHOS
197 #endif  // OHOS_ABILITY_RUNTIME_APPLICATION_CONTEXT_H
198