• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 FOUNDATION_ABILITY_RUNTIME_SIMULATOR_COMMON_CONTEXT_H
17 #define FOUNDATION_ABILITY_RUNTIME_SIMULATOR_COMMON_CONTEXT_H
18 
19 #include <memory>
20 #include <mutex>
21 
22 #include "application_info.h"
23 #include "bindable.h"
24 #include "configuration.h"
25 #include "hap_module_info.h"
26 #include "options.h"
27 
28 namespace OHOS {
29 namespace Global {
30 namespace Resource {
31 class ResourceManager;
32 enum DeviceType : int32_t;
33 }
34 }
35 namespace AbilityRuntime {
36 class Context : public Bindable, public std::enable_shared_from_this<Context> {
37 public:
38     Context() = default;
39     ~Context() = default;
40 
41     virtual std::string GetBundleName() const = 0;
42 
43     virtual std::string GetBundleCodePath() = 0;
44 
45     virtual std::string GetBundleCodeDir() = 0;
46 
47     virtual std::string GetCacheDir() = 0;
48 
49     virtual std::string GetTempDir() = 0;
50 
51     virtual std::string GetResourceDir() = 0;
52 
53     virtual std::string GetFilesDir() = 0;
54 
55     virtual std::string GetDatabaseDir() = 0;
56 
57     virtual std::string GetPreferencesDir() = 0;
58 
59     virtual std::string GetDistributedFilesDir() = 0;
60 
61     virtual std::string GetCloudFileDir() = 0;
62 
63     virtual void SwitchArea(int mode) = 0;
64 
65     virtual int GetArea() = 0;
66 
67     virtual std::string GetBaseDir() = 0;
68 
69     virtual std::shared_ptr<AppExecFwk::Configuration> GetConfiguration() = 0;
70 
71     virtual Options GetOptions() = 0;
72 
73     virtual void SetOptions(const Options &options) = 0;
74 
75     virtual std::shared_ptr<AppExecFwk::ApplicationInfo> GetApplicationInfo() const = 0;
76 
77     virtual std::shared_ptr<AppExecFwk::HapModuleInfo> GetHapModuleInfo() const = 0;
78 
79     virtual std::shared_ptr<Global::Resource::ResourceManager> GetResourceManager() const = 0;
80 
81     virtual std::shared_ptr<Context> CreateModuleContext(const std::string &moduleName) = 0;
82 
83     virtual std::shared_ptr<Context> CreateModuleContext(
84         const std::string &bundleName, const std::string &moduleName) = 0;
85 
86     /**
87      * @brief Getting derived class
88      *
89      * @tparam T template
90      * @param context the context object
91      * @return std::shared_ptr<T> derived class
92      */
93     template<class T>
ConvertTo(const std::shared_ptr<Context> & context)94     static std::shared_ptr<T> ConvertTo(const std::shared_ptr<Context> &context)
95     {
96         if constexpr (!std::is_same_v<T, typename T::SelfType>) {
97             return nullptr;
98         }
99 
100         if (context && context->IsContext(T::CONTEXT_TYPE_ID)) {
101             return std::static_pointer_cast<T>(context);
102         }
103 
104         return nullptr;
105     }
106 
107     using SelfType = Context;
108     static const size_t CONTEXT_TYPE_ID;
109 
110 protected:
IsContext(size_t contextTypeId)111     virtual bool IsContext(size_t contextTypeId)
112     {
113         return contextTypeId == CONTEXT_TYPE_ID;
114     }
115 };
116 } // namespace AbilityRuntime
117 } // namespace OHOS
118 #endif // FOUNDATION_ABILITY_RUNTIME_SIMULATOR_COMMON_CONTEXT_H
119