• 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 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 ContextContainer : public Context {
24 public:
25     ContextContainer() = default;
26     virtual ~ContextContainer() = default;
27 
28     /**
29      * Attaches a Context object to the current ability.
30      * Generally, this method is called after Ability is loaded to provide the application context for the current
31      * ability.
32      *
33      * @param base Indicates a Context object.
34      */
35     void AttachBaseContext(const std::shared_ptr<Context> &base);
36 
37     /**
38      * @brief Detach a attatched context.
39      *
40      */
41     void DetachBaseContext();
42 
43     /**
44      * Called when getting the ProcessInfo
45      *
46      * @return ProcessInfo
47      */
48     std::shared_ptr<ProcessInfo> GetProcessInfo() const override;
49 
50     /**
51      * @brief Obtains information about the current application. The returned application information includes basic
52      * information such as the application name and application permissions.
53      *
54      * @return Returns the ApplicationInfo for the current application.
55      */
56     std::shared_ptr<ApplicationInfo> GetApplicationInfo() const override;
57 
58     /**
59      * @brief Obtains the Context object of the application.
60      *
61      * @return Returns the Context object of the application.
62      */
63     std::shared_ptr<Context> GetApplicationContext() const override;
64 
65     /**
66      * @brief Obtains the path of the package containing the current ability. The returned path contains the resources,
67      *  source code, and configuration files of a module.
68      *
69      * @return Returns the path of the package file.
70      */
71     virtual std::string GetBundleCodePath() override;
72 
73     /**
74      * @brief Obtains information about the current ability.
75      * The returned information includes the class name, bundle name, and other information about the current ability.
76      *
77      * @return Returns the AbilityInfo object for the current ability.
78      */
79     virtual const std::shared_ptr<AbilityInfo> GetAbilityInfo() override;
80 
81     /**
82      * @brief Checks whether the configuration of this ability is changing.
83      *
84      * @return Returns true if the configuration of this ability is changing and false otherwise.
85      */
86     virtual bool IsUpdatingConfigurations() override;
87 
88     /**
89      * @brief Informs the system of the time required for drawing this Page ability.
90      *
91      * @return Returns the notification is successful or fail
92      */
93     virtual bool PrintDrawnCompleted() override;
94 
95     /**
96      * @brief Obtains the Context object of the application.
97      *
98      * @return Returns the Context object of the application.
99      */
100     std::shared_ptr<Context> GetContext() override;
101 
102     /**
103      * @brief Obtains an IBundleMgr instance.
104      * You can use this instance to obtain information about the application bundle.
105      *
106      * @return Returns an IBundleMgr instance.
107      */
108     sptr<IBundleMgr> GetBundleManager() const override;
109 
110     /**
111      * @brief Obtains a resource manager.
112      *
113      * @return Returns a ResourceManager object.
114      */
115     std::shared_ptr<Global::Resource::ResourceManager> GetResourceManager() const override;
116 
117     /**
118      * @brief Deletes the specified private file associated with the application.
119      *
120      * @param fileName Indicates the name of the file to delete. The file name cannot contain path separators.
121      *
122      * @return Returns true if the file is deleted successfully; returns false otherwise.
123      */
124     bool DeleteFile(const std::string &fileName) override;
125 
126     /**
127      * @brief Obtains the application-specific cache directory on the device's internal storage. The system
128      * automatically deletes files from the cache directory if disk space is required elsewhere on the device.
129      * Older files are always deleted first.
130      *
131      * @return Returns the application-specific cache directory.
132      */
133     std::string GetCacheDir() override;
134 
135     /**
136      * @brief Obtains the application-specific code-cache directory on the device's internal storage.
137      * The system will delete any files stored in this location both when your specific application is upgraded,
138      * and when the entire platform is upgraded.
139      *
140      * @return Returns the application-specific code-cache directory.
141      */
142     std::string GetCodeCacheDir() override;
143 
144     /**
145      * @brief Obtains the local database path.
146      * If the local database path does not exist, the system creates one and returns the created path.
147      *
148      * @return Returns the local database file.
149      */
150     std::string GetDatabaseDir() override;
151 
152     /**
153      * @brief Obtains the absolute path where all private data files of this application are stored.
154      *
155      * @return Returns the absolute path storing all private data files of this application.
156      */
157     std::string GetDataDir() override;
158 
159     /**
160      * @brief Obtains the directory for storing custom data files of the application.
161      * You can use the returned File object to create and access files in this directory. The files
162      * can be accessible only by the current application.
163      *
164      * @param name Indicates the name of the directory to retrieve. This directory is created as part
165      * of your application data.
166      * @param mode Indicates the file operating mode. The value can be 0 or a combination of MODE_PRIVATE.
167      *
168      * @return Returns a File object for the requested directory.
169      */
170     std::string GetDir(const std::string &name, int mode) override;
171 
172     /**
173      * @brief Obtains the absolute path to the application-specific cache directory
174      * on the primary external or shared storage device.
175      *
176      * @return Returns the absolute path to the application-specific cache directory on the external or
177      * shared storage device; returns null if the external or shared storage device is temporarily unavailable.
178      */
179     std::string GetExternalCacheDir() override;
180 
181     /**
182      * @brief Obtains the absolute path to the directory for storing files for the application on the
183      * primary external or shared storage device.
184      *
185      * @param type Indicates the type of the file directory to return
186      *
187      * @return Returns the absolute path to the application file directory on the external or shared storage
188      * device; returns null if the external or shared storage device is temporarily unavailable.
189      */
190     std::string GetExternalFilesDir(std::string &type) override;
191 
192     /**
193      * @brief Obtains the directory for storing files for the application on the device's internal storage.
194      *
195      * @return Returns the application file directory.
196      */
197     std::string GetFilesDir() override;
198 
199     /**
200      * @brief Obtains the absolute path which app created and will be excluded from automatic backup to remote storage.
201      * The returned path maybe changed if the application is moved to an adopted storage device.
202      *
203      * @return The path of the directory holding application files that will not be automatically backed up to remote
204      * storage.
205      */
206     std::string GetNoBackupFilesDir() override;
207 
208     /**
209      * @brief Checks whether the current process has the given permission.
210      * You need to call requestPermissionsFromUser(java.lang.std::string[],int) to request a permission only
211      * if the current process does not have the specific permission.
212      *
213      * @param permission Indicates the permission to check. This parameter cannot be null.
214      *
215      * @return Returns 0 (IBundleManager.PERMISSION_GRANTED) if the current process has the permission;
216      * returns -1 (IBundleManager.PERMISSION_DENIED) otherwise.
217      */
218     int VerifySelfPermission(const std::string &permission) override;
219 
220     /**
221      * @brief Obtains the bundle name of the current ability.
222      *
223      * @return Returns the bundle name of the current ability.
224      */
225     std::string GetBundleName() const override;
226 
227     /**
228      * @brief Obtains the path of the OHOS Ability Package (HAP} containing this ability.
229      *
230      * @return Returns the path of the HAP containing this ability.
231      */
232     std::string GetBundleResourcePath() override;
233 
234     /**
235      * @brief Remove permissions for all users who have access to specific permissions
236      *
237      * @param permission Indicates the permission to unauth. This parameter cannot be null.
238      * @param uri Indicates the URI to unauth. This parameter cannot be null.
239      * @param uid Indicates the UID of the unauth to check.
240      *
241      */
242     void UnauthUriPermission(const std::string &permission, const Uri &uri, int uid) override;
243 
244     /**
245      * @brief Obtains an ability manager.
246      * The ability manager provides information about running processes and memory usage of an application.
247      *
248      * @return Returns an IAbilityManager instance.
249      */
250     sptr<AAFwk::IAbilityManager> GetAbilityManager() override;
251 
252     /**
253      * @brief Obtains the type of this application.
254      *
255      * @return Returns system if this application is a system application;
256      * returns normal if it is released in OHOS AppGallery;
257      * returns other if it is released by a third-party vendor;
258      * returns an empty string if the query fails.
259      */
260     std::string GetAppType() override;
261 
262     /**
263      * @brief Query whether the application of the specified PID and UID has been granted a certain permission
264      *
265      * @param permissions Indicates the list of permissions to be requested. This parameter cannot be null.
266      * @param pid Process id
267      * @param uid
268      * @return Returns 0 (IBundleManager.PERMISSION_GRANTED) if the current process has the permission;
269      * returns -1 (IBundleManager.PERMISSION_DENIED) otherwise.
270      */
271     int VerifyPermission(const std::string &permission, int pid, int uid) override;
272 
273     /**
274      * @brief Obtains the distributed file path.
275      * If the distributed file path does not exist, the system creates one and returns the created path. This method is
276      * applicable only to the context of an ability rather than that of an application.
277      *
278      * @return Returns the distributed file.
279      */
280     std::string GetDistributedDir() override;
281 
282     /**
283      * @brief Sets the pattern of this Context based on the specified pattern ID.
284      *
285      * @param patternId Indicates the resource ID of the pattern to set.
286      */
287     void SetPattern(int patternId) override;
288 
289     /**
290      * @brief Obtains the Context object of this ability.
291      *
292      * @return Returns the Context object of this ability.
293      */
294     std::shared_ptr<Context> GetAbilityPackageContext() override;
295 
296     /**
297      * @brief Obtains the HapModuleInfo object of the application.
298      *
299      * @return Returns the HapModuleInfo object of the application.
300      */
301     std::shared_ptr<HapModuleInfo> GetHapModuleInfo() override;
302 
303     /**
304      * @brief Obtains the name of the current process.
305      *
306      * @return Returns the current process name.
307      */
308     std::string GetProcessName() override;
309 
310     /**
311      * @brief Requests certain permissions from the system.
312      * This method is called for permission request. This is an asynchronous method. When it is executed,
313      * the task will be called back.
314      *
315      * @param permissions Indicates the list of permissions to be requested. This parameter cannot be null.
316      * @param permissionsState Indicates the list of permissions' state to be requested. This parameter cannot be null.
317      * @param task The callback or promise fo js interface.
318      */
319     void RequestPermissionsFromUser(std::vector<std::string> &permissions, std::vector<int> &permissionsState,
320         PermissionRequestTask &&task) override;
321 
322     /**
323      * @brief Creates a Context object for an application with the given bundle name.
324      *
325      * @param bundleName Indicates the bundle name of the application.
326      * @param flag  Indicates the flag for creating a Context object. It can be 0, any of
327      * the following values, or any combination of the following values: CONTEXT_IGNORE_SECURITY,
328      * CONTEXT_INCLUDE_CODE, and CONTEXT_RESTRICTED. The value 0 indicates that there is no restriction
329      * on creating contexts for applications.
330      * @param accountId Indicates the account id.
331      *
332      * @return Returns a Context object created for the specified application.
333      */
334     std::shared_ptr<Context> CreateBundleContext(std::string bundleName, int flag, int accountId = DEFAULT_ACCOUNT_ID);
335 
336     /**
337      * @brief Obtains information about the caller of this ability.
338      *
339      * @return Returns the caller information.
340      */
341     Uri GetCaller() override;
342 
343     /**
344      * @brief InitResourceManager
345      *
346      * @param bundleInfo  BundleInfo
347      */
348     void InitResourceManager(BundleInfo &bundleInfo, std::shared_ptr<ContextDeal> &deal);
349 
350     /**
351      * @brief Get the string of this Context based on the specified resource ID.
352      *
353      * @param resId Indicates the resource ID of the string to get.
354      *
355      * @return Returns the string of this Context.
356      */
357     std::string GetString(int resId) override;
358 
359     /**
360      * @brief Get the string array of this Context based on the specified resource ID.
361      *
362      * @param resId Indicates the resource ID of the string array to get.
363      *
364      * @return Returns the string array of this Context.
365      */
366     std::vector<std::string> GetStringArray(int resId) override;
367 
368     /**
369      * @brief Get the integer array of this Context based on the specified resource ID.
370      *
371      * @param resId Indicates the resource ID of the integer array to get.
372      *
373      * @return Returns the integer array of this Context.
374      */
375     std::vector<int> GetIntArray(int resId) override;
376 
377     /**
378      * @brief Obtains the theme of this Context.
379      *
380      * @return theme Returns the theme of this Context.
381      */
382     std::map<std::string, std::string> GetTheme() override;
383 
384     /**
385      * @brief Sets the theme of this Context based on the specified theme ID.
386      *
387      * @param themeId Indicates the resource ID of the theme to set.
388      */
389     void SetTheme(int themeId) override;
390 
391     /**
392      * @brief Obtains the pattern of this Context.
393      *
394      * @return getPattern in interface Context
395      */
396     std::map<std::string, std::string> GetPattern() override;
397 
398     /**
399      * @brief Get the color of this Context based on the specified resource ID.
400      *
401      * @param resId Indicates the resource ID of the color to get.
402      *
403      * @return Returns the color value of this Context.
404      */
405     int GetColor(int resId) override;
406 
407     /**
408      * @brief Obtains the theme id of this Context.
409      *
410      * @return int Returns the theme id of this Context.
411      */
412     int GetThemeId() override;
413 
414     /**
415      * @brief Obtains the current display orientation of this ability.
416      *
417      * @return Returns the current display orientation.
418      */
419     int GetDisplayOrientation() override;
420 
421     /**
422      * @brief Obtains the path storing the preference file of the application.
423      *        If the preference file path does not exist, the system creates one and returns the created path.
424      *
425      * @return Returns the preference file path .
426      */
427     std::string GetPreferencesDir() override;
428 
429     /**
430      * @brief Set color mode
431      *
432      * @param the value of color mode.
433      */
434     void SetColorMode(int mode) override;
435 
436     /**
437      * @brief Obtains color mode.
438      *
439      * @return Returns the color mode value.
440      */
441     int GetColorMode() override;
442 
443     /**
444      * @brief Obtains the unique ID of the mission containing this ability.
445      *
446      * @return Returns the unique mission ID.
447      */
448     int GetMissionId() override;
449 
450 private:
451     std::shared_ptr<Context> baseContext_;
452 };
453 }  // namespace AppExecFwk
454 }  // namespace OHOS
455 #endif  // OHOS_ABILITY_RUNTIME_CONTEXT_CONTAINER_H
456