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