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