1 /*
2  * Copyright (c) 2021-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_CONTEXT_CONTAINER_H
17 #define OHOS_ABILITY_RUNTIME_CONTEXT_CONTAINER_H
18 
19 #include "context_deal.h"
20 
21 namespace OHOS {
22 namespace AppExecFwk {
23 class BundleMgrHelper;
24 class ContextContainer : public Context {
25 public:
26     ContextContainer() = default;
27     virtual ~ContextContainer() = default;
28 
29     /**
30      * Attaches a Context object to the current ability.
31      * Generally, this method is called after Ability is loaded to provide the application context for the current
32      * ability.
33      *
34      * @param base Indicates a Context object.
35      */
36     void AttachBaseContext(const std::shared_ptr<ContextDeal> &base);
37 
38     /**
39      * @brief Detach a attatched context.
40      *
41      */
42     void DetachBaseContext();
43 
44     /**
45      * Called when getting the ProcessInfo
46      *
47      * @return ProcessInfo
48      */
49     std::shared_ptr<ProcessInfo> GetProcessInfo() const override;
50 
51     /**
52      * Called when setting the ProcessInfo
53      *
54      * @param info ProcessInfo instance
55      */
56     void SetProcessInfo(const std::shared_ptr<ProcessInfo> &info);
57 
58     /**
59      * @brief Obtains information about the current application. The returned application information includes basic
60      * information such as the application name and application permissions.
61      *
62      * @return Returns the ApplicationInfo for the current application.
63      */
64     std::shared_ptr<ApplicationInfo> GetApplicationInfo() const override;
65 
66     /**
67      * @brief Obtains the Context object of the application.
68      *
69      * @return Returns the Context object of the application.
70      */
71     std::shared_ptr<Context> GetApplicationContext() const override;
72 
73     /**
74      * @brief Obtains the path of the package containing the current ability. The returned path contains the resources,
75      *  source code, and configuration files of a module.
76      *
77      * @return Returns the path of the package file.
78      */
79     virtual std::string GetBundleCodePath() override;
80 
81     /**
82      * @brief Obtains information about the current ability.
83      * The returned information includes the class name, bundle name, and other information about the current ability.
84      *
85      * @return Returns the AbilityInfo object for the current ability.
86      */
87     virtual const std::shared_ptr<AbilityInfo> GetAbilityInfo() override;
88 
89     /**
90      * @brief Obtains the Context object of the application.
91      *
92      * @return Returns the Context object of the application.
93      */
94     std::shared_ptr<Context> GetContext() override;
95 
96     /**
97      * @brief Obtains an BundleMgrHelper instance.
98      * You can use this instance to obtain information about the application bundle.
99      *
100      * @return Returns an BundleMgrHelper instance.
101      */
102     std::shared_ptr<BundleMgrHelper> GetBundleManager() const override;
103 
104     /**
105      * @brief Obtains a resource manager.
106      *
107      * @return Returns a ResourceManager object.
108      */
109     std::shared_ptr<Global::Resource::ResourceManager> GetResourceManager() const override;
110 
111     /**
112      * @brief Obtains the local database path.
113      * If the local database path does not exist, the system creates one and returns the created path.
114      *
115      * @return Returns the local database file.
116      */
117     std::string GetDatabaseDir() override;
118 
119     /**
120      * @brief Obtains the absolute path where all private data files of this application are stored.
121      *
122      * @return Returns the absolute path storing all private data files of this application.
123      */
124     std::string GetDataDir() override;
125 
126     /**
127      * @brief Obtains the directory for storing custom data files of the application.
128      * You can use the returned File object to create and access files in this directory. The files
129      * can be accessible only by the current application.
130      *
131      * @param name Indicates the name of the directory to retrieve. This directory is created as part
132      * of your application data.
133      * @param mode Indicates the file operating mode. The value can be 0 or a combination of MODE_PRIVATE.
134      *
135      * @return Returns a File object for the requested directory.
136      */
137     std::string GetDir(const std::string &name, int mode) override;
138 
139     /**
140      * @brief Obtains the directory for storing files for the application on the device's internal storage.
141      *
142      * @return Returns the application file directory.
143      */
144     std::string GetFilesDir() override;
145 
146     /**
147      * @brief Obtains the bundle name of the current ability.
148      *
149      * @return Returns the bundle name of the current ability.
150      */
151     std::string GetBundleName() const override;
152 
153     /**
154      * @brief Obtains the path of the OHOS Ability Package (HAP} containing this ability.
155      *
156      * @return Returns the path of the HAP containing this ability.
157      */
158     std::string GetBundleResourcePath() override;
159 
160     /**
161      * @brief Obtains an ability manager.
162      * The ability manager provides information about running processes and memory usage of an application.
163      *
164      * @return Returns an IAbilityManager instance.
165      */
166     sptr<AAFwk::IAbilityManager> GetAbilityManager() override;
167 
168     /**
169      * @brief Obtains the type of this application.
170      *
171      * @return Returns system if this application is a system application;
172      * returns normal if it is released in OHOS AppGallery;
173      * returns other if it is released by a third-party vendor;
174      * returns an empty string if the query fails.
175      */
176     std::string GetAppType() override;
177 
178     /**
179      * @brief Sets the pattern of this Context based on the specified pattern ID.
180      *
181      * @param patternId Indicates the resource ID of the pattern to set.
182      */
183     void SetPattern(int patternId) override;
184 
185     /**
186      * @brief Obtains the HapModuleInfo object of the application.
187      *
188      * @return Returns the HapModuleInfo object of the application.
189      */
190     std::shared_ptr<HapModuleInfo> GetHapModuleInfo() override;
191 
192     /**
193      * @brief Obtains the name of the current process.
194      *
195      * @return Returns the current process name.
196      */
197     std::string GetProcessName() override;
198 
199     /**
200      * @brief Creates a Context object for an application with the given bundle name.
201      *
202      * @param bundleName Indicates the bundle name of the application.
203      * @param flag  Indicates the flag for creating a Context object. It can be 0, any of
204      * the following values, or any combination of the following values: CONTEXT_IGNORE_SECURITY,
205      * CONTEXT_INCLUDE_CODE, and CONTEXT_RESTRICTED. The value 0 indicates that there is no restriction
206      * on creating contexts for applications.
207      * @param accountId Indicates the account id.
208      *
209      * @return Returns a Context object created for the specified application.
210      */
211     std::shared_ptr<Context> CreateBundleContext(std::string bundleName, int flag, int accountId = DEFAULT_ACCOUNT_ID);
212 
213     /**
214      * @brief Obtains information about the caller of this ability.
215      *
216      * @return Returns the caller information.
217      */
218     Uri GetCaller() override;
219 
220     /**
221      * @brief SetUriString
222      *
223      * @param uri the uri to set.
224      */
225     void SetUriString(const std::string &uri);
226 
227     /**
228      * @brief InitResourceManager
229      *
230      * @param bundleInfo  BundleInfo
231      */
232     void InitResourceManager(BundleInfo &bundleInfo, std::shared_ptr<ContextDeal> &deal);
233 
234     void LoadResources(BundleInfo &bundleInfo, std::shared_ptr<Global::Resource::ResourceManager> &resourceManager,
235         std::unique_ptr<Global::Resource::ResConfig> &resConfig, std::shared_ptr<ContextDeal> &deal);
236 
237     /**
238      * @brief Get the string of this Context based on the specified resource ID.
239      *
240      * @param resId Indicates the resource ID of the string to get.
241      *
242      * @return Returns the string of this Context.
243      */
244     std::string GetString(int resId) override;
245 
246     /**
247      * @brief Get the string array of this Context based on the specified resource ID.
248      *
249      * @param resId Indicates the resource ID of the string array to get.
250      *
251      * @return Returns the string array of this Context.
252      */
253     std::vector<std::string> GetStringArray(int resId) override;
254 
255     /**
256      * @brief Get the integer array of this Context based on the specified resource ID.
257      *
258      * @param resId Indicates the resource ID of the integer array to get.
259      *
260      * @return Returns the integer array of this Context.
261      */
262     std::vector<int> GetIntArray(int resId) override;
263 
264     /**
265      * @brief Obtains the theme of this Context.
266      *
267      * @return theme Returns the theme of this Context.
268      */
269     std::map<std::string, std::string> GetTheme() override;
270 
271     /**
272      * @brief Sets the theme of this Context based on the specified theme ID.
273      *
274      * @param themeId Indicates the resource ID of the theme to set.
275      */
276     void SetTheme(int themeId) override;
277 
278     /**
279      * @brief Obtains the pattern of this Context.
280      *
281      * @return getPattern in interface Context
282      */
283     std::map<std::string, std::string> GetPattern() override;
284 
285     /**
286      * @brief Get the color of this Context based on the specified resource ID.
287      *
288      * @param resId Indicates the resource ID of the color to get.
289      *
290      * @return Returns the color value of this Context.
291      */
292     int GetColor(int resId) override;
293 
294     /**
295      * @brief Obtains the theme id of this Context.
296      *
297      * @return int Returns the theme id of this Context.
298      */
299     int GetThemeId() override;
300 
301     /**
302      * @brief Obtains the current display orientation of this ability.
303      *
304      * @return Returns the current display orientation.
305      */
306     int GetDisplayOrientation() override;
307 
308     /**
309      * @brief Obtains the path storing the preference file of the application.
310      *        If the preference file path does not exist, the system creates one and returns the created path.
311      *
312      * @return Returns the preference file path .
313      */
314     std::string GetPreferencesDir() override;
315 
316     /**
317      * @brief Set color mode
318      *
319      * @param the value of color mode.
320      */
321     void SetColorMode(int mode) override;
322 
323     /**
324      * @brief Obtains color mode.
325      *
326      * @return Returns the color mode value.
327      */
328     int GetColorMode() override;
329 
330     /**
331      * @brief Obtains the unique ID of the mission containing this ability.
332      *
333      * @return Returns the unique mission ID.
334      */
335     int GetMissionId() override;
336 
337     /**
338      * @brief Obtains the lifecycle state info.
339      *
340      * @return Returns the lifecycle state info.
341      */
342     AAFwk::LifeCycleStateInfo GetLifeCycleStateInfo() const;
343 
344     /**
345      * @brief Set the LifeCycleStateInfo to the deal.
346      *
347      * @param info the info to set.
348      */
349     void SetLifeCycleStateInfo(const AAFwk::LifeCycleStateInfo &info);
350 private:
351     std::shared_ptr<ContextDeal> baseContext_ = nullptr;
352     std::shared_ptr<ProcessInfo> processInfo_ = nullptr;
353     AAFwk::LifeCycleStateInfo lifeCycleStateInfo_;
354     std::string uriString_ = "";
355 };
356 }  // namespace AppExecFwk
357 }  // namespace OHOS
358 #endif  // OHOS_ABILITY_RUNTIME_CONTEXT_CONTAINER_H
359