• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     /**
235      * @brief Get the string of this Context based on the specified resource ID.
236      *
237      * @param resId Indicates the resource ID of the string to get.
238      *
239      * @return Returns the string of this Context.
240      */
241     std::string GetString(int resId) override;
242 
243     /**
244      * @brief Get the string array of this Context based on the specified resource ID.
245      *
246      * @param resId Indicates the resource ID of the string array to get.
247      *
248      * @return Returns the string array of this Context.
249      */
250     std::vector<std::string> GetStringArray(int resId) override;
251 
252     /**
253      * @brief Get the integer array of this Context based on the specified resource ID.
254      *
255      * @param resId Indicates the resource ID of the integer array to get.
256      *
257      * @return Returns the integer array of this Context.
258      */
259     std::vector<int> GetIntArray(int resId) override;
260 
261     /**
262      * @brief Obtains the theme of this Context.
263      *
264      * @return theme Returns the theme of this Context.
265      */
266     std::map<std::string, std::string> GetTheme() override;
267 
268     /**
269      * @brief Sets the theme of this Context based on the specified theme ID.
270      *
271      * @param themeId Indicates the resource ID of the theme to set.
272      */
273     void SetTheme(int themeId) override;
274 
275     /**
276      * @brief Obtains the pattern of this Context.
277      *
278      * @return getPattern in interface Context
279      */
280     std::map<std::string, std::string> GetPattern() override;
281 
282     /**
283      * @brief Get the color of this Context based on the specified resource ID.
284      *
285      * @param resId Indicates the resource ID of the color to get.
286      *
287      * @return Returns the color value of this Context.
288      */
289     int GetColor(int resId) override;
290 
291     /**
292      * @brief Obtains the theme id of this Context.
293      *
294      * @return int Returns the theme id of this Context.
295      */
296     int GetThemeId() override;
297 
298     /**
299      * @brief Obtains the current display orientation of this ability.
300      *
301      * @return Returns the current display orientation.
302      */
303     int GetDisplayOrientation() override;
304 
305     /**
306      * @brief Obtains the path storing the preference file of the application.
307      *        If the preference file path does not exist, the system creates one and returns the created path.
308      *
309      * @return Returns the preference file path .
310      */
311     std::string GetPreferencesDir() override;
312 
313     /**
314      * @brief Set color mode
315      *
316      * @param the value of color mode.
317      */
318     void SetColorMode(int mode) override;
319 
320     /**
321      * @brief Obtains color mode.
322      *
323      * @return Returns the color mode value.
324      */
325     int GetColorMode() override;
326 
327     /**
328      * @brief Obtains the unique ID of the mission containing this ability.
329      *
330      * @return Returns the unique mission ID.
331      */
332     int GetMissionId() override;
333 
334     /**
335      * @brief Obtains the lifecycle state info.
336      *
337      * @return Returns the lifecycle state info.
338      */
339     AAFwk::LifeCycleStateInfo GetLifeCycleStateInfo() const;
340 
341     /**
342      * @brief Set the LifeCycleStateInfo to the deal.
343      *
344      * @param info the info to set.
345      */
346     void SetLifeCycleStateInfo(const AAFwk::LifeCycleStateInfo &info);
347 private:
348     std::shared_ptr<ContextDeal> baseContext_ = nullptr;
349     std::shared_ptr<ProcessInfo> processInfo_ = nullptr;
350     AAFwk::LifeCycleStateInfo lifeCycleStateInfo_;
351     std::string uriString_ = "";
352 };
353 }  // namespace AppExecFwk
354 }  // namespace OHOS
355 #endif  // OHOS_ABILITY_RUNTIME_CONTEXT_CONTAINER_H
356