• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 ABILITY_RUNTIME_CONTEXT_IMPL_H
17 #define ABILITY_RUNTIME_CONTEXT_IMPL_H
18 
19 #include "context.h"
20 
21 #include "configuration.h"
22 #include "bundle_mgr_interface.h"
23 
24 namespace OHOS {
25 namespace AbilityRuntime {
26 class ContextImpl : public Context, public std::enable_shared_from_this<ContextImpl> {
27 public:
28     ContextImpl() = default;
29     virtual ~ContextImpl() = default;
30 
31     /**
32      * @brief Obtains the bundle name of the current ability.
33      *
34      * @return Returns the bundle name of the current ability.
35      */
36     std::string GetBundleName() const override;
37 
38     /**
39      * @brief Obtains the path of the package containing the current ability. The returned path contains the resources,
40      *  source code, and configuration files of a module.
41      *
42      * @return Returns the path of the package file.
43      */
44     std::string GetBundleCodeDir() override;
45 
46     /**
47      * @brief Obtains the application-specific cache directory on the device's internal storage. The system
48      * automatically deletes files from the cache directory if disk space is required elsewhere on the device.
49      * Older files are always deleted first.
50      *
51      * @return Returns the application-specific cache directory.
52      */
53     std::string GetCacheDir() override;
54 
55     /**
56      * @brief Checks whether the configuration of this ability is changing.
57      *
58      * @return Returns true if the configuration of this ability is changing and false otherwise.
59      */
60     bool IsUpdatingConfigurations() override;
61 
62     /**
63      * @brief Informs the system of the time required for drawing this Page ability.
64      *
65      * @return Returns the notification is successful or fail
66      */
67     bool PrintDrawnCompleted() override;
68 
69     /**
70      * @brief Obtains the temporary directory.
71      *
72      * @return Returns the application temporary directory.
73      */
74     std::string GetTempDir() override;
75 
76     /**
77      * @brief Obtains the directory for storing files for the application on the device's internal storage.
78      *
79      * @return Returns the application file directory.
80      */
81     std::string GetFilesDir() override;
82 
83     /**
84      * @brief Obtains the local database path.
85      * If the local database path does not exist, the system creates one and returns the created path.
86      *
87      * @return Returns the local database file.
88      */
89     std::string GetDatabaseDir() override;
90 
91     /**
92      * @brief Obtains the path storing the storage file of the application.
93      *
94      * @return Returns the local storage file.
95      */
96     std::string GetStorageDir() override;
97 
98     /**
99      * @brief Obtains the path distributed file of the application
100      *
101      * @return Returns the distributed file.
102      */
103     std::string GetDistributedFilesDir() override;
104 
105     /**
106      * @brief Switch file area
107      *
108      * @param mode file area.
109      */
110     void SwitchArea(int mode) override;
111 
112     /**
113      * @brief set the ResourceManager.
114      *
115      * @param the ResourceManager has been inited.
116      *
117      */
118     void SetResourceManager(const std::shared_ptr<Global::Resource::ResourceManager> &resourceManager);
119 
120     /**
121     * @brief Obtains a resource manager.
122     *
123     * @return Returns a ResourceManager object.
124     */
125     std::shared_ptr<Global::Resource::ResourceManager> GetResourceManager() const override;
126 
127     /**
128      * @brief Creates a Context object for an application with the given bundle name.
129      *
130      * @param bundleName Indicates the bundle name of the application.
131      *
132      * @return Returns a Context object created for the specified application.
133      */
134     std::shared_ptr<Context> CreateBundleContext(const std::string &bundleName) override;
135 
136     /**
137     * @brief Obtains an IBundleMgr instance.
138     * You can use this instance to obtain information about the application bundle.
139     *
140     * @return Returns an IBundleMgr instance.
141     */
142     sptr<AppExecFwk::IBundleMgr> GetBundleManager() const;
143 
144     /**
145      * @brief Set ApplicationInfo
146      *
147      * @param info ApplicationInfo instance.
148      */
149     void SetApplicationInfo(const std::shared_ptr<AppExecFwk::ApplicationInfo> &info);
150 
151     /**
152      * @brief Obtains information about the current application. The returned application information includes basic
153      * information such as the application name and application permissions.
154      *
155      * @return Returns the ApplicationInfo for the current application.
156      */
157     std::shared_ptr<AppExecFwk::ApplicationInfo> GetApplicationInfo() const override;
158 
159     /**
160      * @brief Set ApplicationInfo
161      *
162      * @param info ApplicationInfo instance.
163      */
164     void SetParentContext(const std::shared_ptr<Context> &context);
165 
166     /**
167      * @brief Obtains the path of the package containing the current ability. The returned path contains the resources,
168      *  source code, and configuration files of a module.
169      *
170      * @return Returns the path of the package file.
171      */
172     std::string GetBundleCodePath() const override;
173 
174     /**
175      * @brief Obtains the HapModuleInfo object of the application.
176      *
177      * @return Returns the HapModuleInfo object of the application.
178      */
179     std::shared_ptr<AppExecFwk::HapModuleInfo> GetHapModuleInfo() const override;
180 
181     /**
182      * @brief Set HapModuleInfo
183      *
184      * @param hapModuleInfo HapModuleInfo instance.
185      */
186     void InitHapModuleInfo(const std::shared_ptr<AppExecFwk::AbilityInfo> &abilityInfo);
187 
188     /**
189      * @brief Set HapModuleInfo
190      *
191      * @param hapModuleInfo HapModuleInfo instance.
192      */
193     void InitHapModuleInfo(const AppExecFwk::HapModuleInfo &hapModuleInfo);
194 
195     /**
196      * @brief Set application context
197      */
198     void InitAppContext();
199 
200     /**
201      * @brief Set the token witch the app launched.
202      *
203      * @param token The token which the is launched by app.
204      */
205     void SetToken(const sptr<IRemoteObject> &token) override;
206 
207     /**
208      * @brief Get the token witch the app launched.
209      *
210      * @return token The token which the is launched by app.
211      */
212     sptr<IRemoteObject> GetToken() override;
213 
214     /**
215      * @brief Get the token witch the app launched.
216      *
217      * @return token The token which the is launched by app.
218      */
219     void SetConfiguration(const std::shared_ptr<AppExecFwk::Configuration> &config);
220 
221     /**
222      * @brief Get the token witch the app launched.
223      *
224      * @return token The token which the is launched by app.
225      */
226     std::shared_ptr<AppExecFwk::Configuration> GetConfiguration() const override;
227 
228     /**
229      * @brief Obtains the application base directory on the device's internal storage.
230      *
231      * @return Returns the application base directory.
232      */
233     std::string GetBaseDir() const override;
234 
235 protected:
236     sptr<IRemoteObject> token_;
237 
238 private:
239     static const int64_t CONTEXT_CREATE_BY_SYSTEM_APP;
240     static const std::string CONTEXT_DATA_APP;
241     static const std::string CONTEXT_BUNDLE;
242     static const std::string CONTEXT_DISTRIBUTEDFILES_BASE_BEFORE;
243     static const std::string CONTEXT_DISTRIBUTEDFILES_BASE_MIDDLE;
244     static const std::string CONTEXT_DISTRIBUTEDFILES;
245     static const std::string CONTEXT_FILE_SEPARATOR;
246     static const std::string CONTEXT_DATA;
247     static const std::string CONTEXT_DATA_STORAGE;
248     static const std::string CONTEXT_BASE;
249     static const std::string CONTEXT_CACHE;
250     static const std::string CONTEXT_STORAGE;
251     static const std::string CONTEXT_DATABASE;
252     static const std::string CONTEXT_TEMP;
253     static const std::string CONTEXT_FILES;
254     static const std::string CONTEXT_HAPS;
255     static const std::string CONTEXT_ELS[];
256     static const int EL_DEFAULT = 1;
257     int flags_ = 0x00000000;
258 
259     void InitResourceManager(
260         const AppExecFwk::BundleInfo &bundleInfo, const std::shared_ptr<ContextImpl> &appContext) const;
261     bool IsCreateBySystemApp() const;
262     int GetCurrentAccountId() const;
263     void SetFlags(int64_t flags);
264     int GetCurrentActiveAccountId() const;
265     void CreateDirIfNotExist(const std::string& dirPath) const;
266 
267     std::shared_ptr<AppExecFwk::ApplicationInfo> applicationInfo_ = nullptr;
268     std::shared_ptr<Context> parentContext_ = nullptr;
269     std::shared_ptr<Global::Resource::ResourceManager> resourceManager_ = nullptr;
270     std::shared_ptr<AppExecFwk::HapModuleInfo> hapModuleInfo_ = nullptr;
271     std::shared_ptr<AppExecFwk::Configuration> config_ = nullptr;
272     std::string currArea_ = CONTEXT_ELS[EL_DEFAULT];
273 };
274 }  // namespace AbilityRuntime
275 }  // namespace OHOS
276 #endif  // ABILITY_RUNTIME_CONTEXT_IMPL_H
277