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