• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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_APPEXECFWK_OHOS_CONTEXT_CONTAINER_H
17 #define FOUNDATION_APPEXECFWK_OHOS_CONTEXT_CONTAINER_H
18 
19 #include "context_deal.h"
20 
21 namespace OHOS {
22 namespace AppExecFwk {
23 
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<Context> &base);
37 
38     /**
39      * Called when getting the ProcessInfo
40      *
41      * @return ProcessInfo
42      */
43     std::shared_ptr<ProcessInfo> GetProcessInfo() const override;
44 
45     /**
46      * @brief Obtains information about the current application. The returned application information includes basic
47      * information such as the application name and application permissions.
48      *
49      * @return Returns the ApplicationInfo for the current application.
50      */
51     std::shared_ptr<ApplicationInfo> GetApplicationInfo() const override;
52 
53     /**
54      * @brief Obtains the Context object of the application.
55      *
56      * @return Returns the Context object of the application.
57      */
58     std::shared_ptr<Context> GetApplicationContext() const override;
59 
60     /**
61      * @brief Obtains the path of the package containing the current ability. The returned path contains the resources,
62      *  source code, and configuration files of a module.
63      *
64      * @return Returns the path of the package file.
65      */
66     virtual std::string GetBundleCodePath() override;
67 
68     /**
69      * @brief Obtains information about the current ability.
70      * The returned information includes the class name, bundle name, and other information about the current ability.
71      *
72      * @return Returns the AbilityInfo object for the current ability.
73      */
74     virtual const std::shared_ptr<AbilityInfo> GetAbilityInfo() override;
75 
76     /**
77      * @brief Obtains the Context object of the application.
78      *
79      * @return Returns the Context object of the application.
80      */
81     std::shared_ptr<Context> GetContext() override;
82 
83     /**
84      * @brief Obtains an IBundleMgr instance.
85      * You can use this instance to obtain information about the application bundle.
86      *
87      * @return Returns an IBundleMgr instance.
88      */
89     sptr<IBundleMgr> GetBundleManager() const override;
90 
91     /**
92      * @brief Obtains a resource manager.
93      *
94      * @return Returns a ResourceManager object.
95      */
96     std::shared_ptr<Global::Resource::ResourceManager> GetResourceManager() const override;
97 
98     /**
99      * @brief Deletes the specified private file associated with the application.
100      *
101      * @param fileName Indicates the name of the file to delete. The file name cannot contain path separators.
102      *
103      * @return Returns true if the file is deleted successfully; returns false otherwise.
104      */
105     bool DeleteFile(const std::string &fileName) override;
106 
107     /**
108      * @brief Obtains the application-specific cache directory on the device's internal storage. The system
109      * automatically deletes files from the cache directory if disk space is required elsewhere on the device.
110      * Older files are always deleted first.
111      *
112      * @return Returns the application-specific cache directory.
113      */
114     std::string GetCacheDir() override;
115 
116     /**
117      * @brief Obtains the application-specific code-cache directory on the device's internal storage.
118      * The system will delete any files stored in this location both when your specific application is upgraded,
119      * and when the entire platform is upgraded.
120      *
121      * @return Returns the application-specific code-cache directory.
122      */
123     std::string GetCodeCacheDir() override;
124 
125     /**
126      * @brief Obtains the local database path.
127      * If the local database path does not exist, the system creates one and returns the created path.
128      *
129      * @return Returns the local database file.
130      */
131     std::string GetDatabaseDir() override;
132 
133     /**
134      * @brief Obtains the absolute path where all private data files of this application are stored.
135      *
136      * @return Returns the absolute path storing all private data files of this application.
137      */
138     std::string GetDataDir() override;
139 
140     /**
141      * @brief Obtains the directory for storing custom data files of the application.
142      * You can use the returned File object to create and access files in this directory. The files
143      * can be accessible only by the current application.
144      *
145      * @param name Indicates the name of the directory to retrieve. This directory is created as part
146      * of your application data.
147      * @param mode Indicates the file operating mode. The value can be 0 or a combination of MODE_PRIVATE.
148      *
149      * @return Returns a File object for the requested directory.
150      */
151     std::string GetDir(const std::string &name, int mode) override;
152 
153     /**
154      * @brief Obtains the absolute path to the application-specific cache directory
155      * on the primary external or shared storage device.
156      *
157      * @return Returns the absolute path to the application-specific cache directory on the external or
158      * shared storage device; returns null if the external or shared storage device is temporarily unavailable.
159      */
160     std::string GetExternalCacheDir() override;
161 
162     /**
163      * @brief Obtains the absolute path to the directory for storing files for the application on the
164      * primary external or shared storage device.
165      *
166      * @param type Indicates the type of the file directory to return
167      *
168      * @return Returns the absolute path to the application file directory on the external or shared storage
169      * device; returns null if the external or shared storage device is temporarily unavailable.
170      */
171     std::string GetExternalFilesDir(std::string &type) override;
172 
173     /**
174      * @brief Obtains the directory for storing files for the application on the device's internal storage.
175      *
176      * @return Returns the application file directory.
177      */
178     std::string GetFilesDir() override;
179 
180     /**
181      * @brief Obtains the absolute path which app created and will be excluded from automatic backup to remote storage.
182      * The returned path maybe changed if the application is moved to an adopted storage device.
183      *
184      * @return The path of the directory holding application files that will not be automatically backed up to remote
185      * storage.
186      */
187     std::string GetNoBackupFilesDir() override;
188 
189     /**
190      * @brief Checks whether the calling process for inter-process communication has the given permission.
191      * The calling process is not the current process.
192      *
193      * @param permission Indicates the permission to check. This parameter cannot be null.
194      *
195      * @return Returns 0 (IBundleManager.PERMISSION_GRANTED) if the calling process has the permission;
196      * returns -1 (IBundleManager.PERMISSION_DENIED) otherwise.
197      */
198     int VerifyCallingPermission(const std::string &permission) override;
199 
200     /**
201      * @brief Checks whether the current process has the given permission.
202      * You need to call requestPermissionsFromUser(java.lang.std::string[],int) to request a permission only
203      * if the current process does not have the specific permission.
204      *
205      * @param permission Indicates the permission to check. This parameter cannot be null.
206      *
207      * @return Returns 0 (IBundleManager.PERMISSION_GRANTED) if the current process has the permission;
208      * returns -1 (IBundleManager.PERMISSION_DENIED) otherwise.
209      */
210     int VerifySelfPermission(const std::string &permission) override;
211 
212     /**
213      * @brief Obtains the bundle name of the current ability.
214      *
215      * @return Returns the bundle name of the current ability.
216      */
217     std::string GetBundleName() override;
218 
219     /**
220      * @brief Obtains the path of the OHOS Ability Package (HAP} containing this ability.
221      *
222      * @return Returns the path of the HAP containing this ability.
223      */
224     std::string GetBundleResourcePath() override;
225 
226     /**
227      * @brief Remove permissions for all users who have access to specific permissions
228      *
229      * @param permission Indicates the permission to unauth. This parameter cannot be null.
230      * @param uri Indicates the URI to unauth. This parameter cannot be null.
231      * @param uid Indicates the UID of the unauth to check.
232      *
233      */
234     void UnauthUriPermission(const std::string &permission, const Uri &uri, int uid) override;
235 
236     /**
237      * @brief Obtains an ability manager.
238      * The ability manager provides information about running processes and memory usage of an application.
239      *
240      * @return Returns an IAbilityManager instance.
241      */
242     sptr<AAFwk::IAbilityManager> GetAbilityManager() override;
243 
244     /**
245      * @brief Obtains the type of this application.
246      *
247      * @return Returns system if this application is a system application;
248      * returns normal if it is released in OHOS AppGallery;
249      * returns other if it is released by a third-party vendor;
250      * returns an empty string if the query fails.
251      */
252     std::string GetAppType() override;
253 
254     /**
255      * @brief Confirms with the permission management module to check whether a request prompt is required for granting
256      * a certain permission. You need to call the current method to check whether a prompt is required before calling
257      * requestPermissionsFromUser(java.lang.String[],int) to request a permission. If a prompt is not required,
258      * permission request will not be initiated.
259      *
260      * @param requestCode Indicates the permission to be queried. This parameter cannot be null.
261      *
262      * @return Returns true if the current application does not have the permission and the user does not turn off
263      * further requests; returns false if the current application already has the permission, the permission is rejected
264      * by the system, or the permission is denied by the user and the user has turned off further requests.
265      */
266     bool CanRequestPermission(const std::string &permission) override;
267 
268     /**
269      * @brief When there is a remote call to check whether the remote has permission, otherwise check whether it has
270      * permission
271      *
272      * @param permissions Indicates the list of permissions to be requested. This parameter cannot be null.
273      * @return Returns 0 (IBundleManager.PERMISSION_GRANTED) if the current process has the permission;
274      * returns -1 (IBundleManager.PERMISSION_DENIED) otherwise.
275      */
276     int VerifyCallingOrSelfPermission(const std::string &permission) override;
277 
278     /**
279      * @brief Query whether the application of the specified PID and UID has been granted a certain permission
280      *
281      * @param permissions Indicates the list of permissions to be requested. This parameter cannot be null.
282      * @param pid Process id
283      * @param uid
284      * @return Returns 0 (IBundleManager.PERMISSION_GRANTED) if the current process has the permission;
285      * returns -1 (IBundleManager.PERMISSION_DENIED) otherwise.
286      */
287     int VerifyPermission(const std::string &permission, int pid, int uid) override;
288 
289     /**
290      * @brief Obtains the distributed file path.
291      * If the distributed file path does not exist, the system creates one and returns the created path. This method is
292      * applicable only to the context of an ability rather than that of an application.
293      *
294      * @return Returns the distributed file.
295      */
296     std::string GetDistributedDir() override;
297 
298     /**
299      * @brief Sets the pattern of this Context based on the specified pattern ID.
300      *
301      * @param patternId Indicates the resource ID of the pattern to set.
302      */
303     void SetPattern(int patternId) override;
304 
305     /**
306      * @brief Obtains the Context object of this ability.
307      *
308      * @return Returns the Context object of this ability.
309      */
310     std::shared_ptr<Context> GetAbilityPackageContext() override;
311 
312     /**
313      * @brief Obtains the HapModuleInfo object of the application.
314      *
315      * @return Returns the HapModuleInfo object of the application.
316      */
317     std::shared_ptr<HapModuleInfo> GetHapModuleInfo() override;
318 
319     /**
320      * @brief Obtains the name of the current process.
321      *
322      * @return Returns the current process name.
323      */
324     std::string GetProcessName() override;
325 
326     /**
327      * @brief Requests certain permissions from the system.
328      * This method is called for permission request. This is an asynchronous method. When it is executed,
329      * the Ability.onRequestPermissionsFromUserResult(int, String[], int[]) method will be called back.
330      *
331      * @param permissions Indicates the list of permissions to be requested. This parameter cannot be null.
332      * @param requestCode Indicates the request code to be passed to the Ability.onRequestPermissionsFromUserResult(int,
333      * String[], int[]) callback method. This code cannot be a negative number.
334      *
335      */
336     void RequestPermissionsFromUser(std::vector<std::string> &permissions, int requestCode) override;
337 
338     /**
339      * @brief Creates a Context object for an application with the given bundle name.
340      *
341      * @param bundleName Indicates the bundle name of the application.
342      *
343      * @param flag  Indicates the flag for creating a Context object. It can be 0, any of
344      * the following values, or any combination of the following values: CONTEXT_IGNORE_SECURITY,
345      * CONTEXT_INCLUDE_CODE, and CONTEXT_RESTRICTED. The value 0 indicates that there is no restriction
346      * on creating contexts for applications.
347      *
348      * @return Returns a Context object created for the specified application.
349      */
350     std::shared_ptr<Context> CreateBundleContext(std::string bundleName, int flag);
351 
352     /**
353      * @brief Obtains information about the caller of this ability.
354      *
355      * @return Returns the caller information.
356      */
357     Uri GetCaller() override;
358 
359     /**
360      * @brief InitResourceManager
361      *
362      * @param bundleInfo  BundleInfo
363      */
364     void InitResourceManager(BundleInfo &bundleInfo, std::shared_ptr<ContextDeal> &deal);
365 
366     /**
367      * @brief Get the string of this Context based on the specified resource ID.
368      *
369      * @param resId Indicates the resource ID of the string to get.
370      *
371      * @return Returns the string of this Context.
372      */
373     std::string GetString(int resId) override;
374 
375     /**
376      * @brief Get the string array of this Context based on the specified resource ID.
377      *
378      * @param resId Indicates the resource ID of the string array to get.
379      *
380      * @return Returns the string array of this Context.
381      */
382     std::vector<std::string> GetStringArray(int resId) override;
383 
384     /**
385      * @brief Get the integer array of this Context based on the specified resource ID.
386      *
387      * @param resId Indicates the resource ID of the integer array to get.
388      *
389      * @return Returns the integer array of this Context.
390      */
391     std::vector<int> GetIntArray(int resId) override;
392 
393     /**
394      * @brief Obtains the theme of this Context.
395      *
396      * @return theme Returns the theme of this Context.
397      */
398     std::map<std::string, std::string> GetTheme() override;
399 
400     /**
401      * @brief Sets the theme of this Context based on the specified theme ID.
402      *
403      * @param themeId Indicates the resource ID of the theme to set.
404      */
405     void SetTheme(int themeId) override;
406 
407     /**
408      * @brief Obtains the pattern of this Context.
409      *
410      * @return getPattern in interface Context
411      */
412     std::map<std::string, std::string> GetPattern() override;
413 
414     /**
415      * @brief Get the color of this Context based on the specified resource ID.
416      *
417      * @param resId Indicates the resource ID of the color to get.
418      *
419      * @return Returns the color value of this Context.
420      */
421     int GetColor(int resId) override;
422 
423     /**
424      * @brief Obtains the theme id of this Context.
425      *
426      * @return int Returns the theme id of this Context.
427      */
428     int GetThemeId() override;
429 
430     /**
431      * @brief Obtains the current display orientation of this ability.
432      *
433      * @return Returns the current display orientation.
434      */
435     int GetDisplayOrientation() override;
436 
437     /**
438      * @brief Obtains the path storing the preference file of the application.
439      *        If the preference file path does not exist, the system creates one and returns the created path.
440      *
441      * @return Returns the preference file path .
442      */
443     std::string GetPreferencesDir() override;
444 
445     /**
446      * @brief Set color mode
447      *
448      * @param the value of color mode.
449      */
450     void SetColorMode(int mode) override;
451 
452     /**
453      * @brief Obtains color mode.
454      *
455      * @return Returns the color mode value.
456      */
457     int GetColorMode() override;
458 
459     /**
460      * @brief Obtains the unique ID of the mission containing this ability.
461      *
462      * @return Returns the unique mission ID.
463      */
464     int GetMissionId() override;
465 
466     /**
467      * @brief Call this when your ability should be closed and the mission should be completely removed as a part of
468      * finishing the root ability of the mission.
469      */
470     void TerminateAndRemoveMission() override;
471 
472     /**
473      * @brief Obtains a task dispatcher that is bound to the UI thread.
474      *
475      * @return Returns the task dispatcher that is bound to the UI thread.
476      */
477     std::shared_ptr<TaskDispatcher> GetUITaskDispatcher() override;
478 
479     /**
480      * @brief Obtains a task dispatcher that is bound to the application main thread.
481      *
482      * @return Returns the task dispatcher that is bound to the application main thread.
483      */
484     std::shared_ptr<TaskDispatcher> GetMainTaskDispatcher() override;
485 
486     /**
487      * @brief Creates a parallel task dispatcher with a specified priority.
488      *
489      * @param name Indicates the task dispatcher name. This parameter is used to locate problems.
490      * @param priority Indicates the priority of all tasks dispatched by the parallel task dispatcher.
491      *
492      * @return Returns a parallel task dispatcher.
493      */
494     std::shared_ptr<TaskDispatcher> CreateParallelTaskDispatcher(
495         const std::string &name, const TaskPriority &priority) override;
496 
497     /**
498      * @brief Creates a serial task dispatcher with a specified priority.
499      *
500      * @param name Indicates the task dispatcher name. This parameter is used to locate problems.
501      * @param priority Indicates the priority of all tasks dispatched by the created task dispatcher.
502      *
503      * @return Returns a serial task dispatcher.
504      */
505     std::shared_ptr<TaskDispatcher> CreateSerialTaskDispatcher(
506         const std::string &name, const TaskPriority &priority) override;
507 
508     /**
509      * @brief Obtains a global task dispatcher with a specified priority.
510      *
511      * @param priority Indicates the priority of all tasks dispatched by the global task dispatcher.
512      *
513      * @return Returns a global task dispatcher.
514      */
515     std::shared_ptr<TaskDispatcher> GetGlobalTaskDispatcher(const TaskPriority &priority) override;
516 
517     /**
518      * @brief Requires that tasks associated with a given capability token be moved to the background
519      *
520      * @param nonFirst If nonfirst is false and not the lowest ability of the mission, you cannot move mission to end
521      *
522      * @return Returns true on success, others on failure.
523      */
524     bool MoveMissionToEnd(bool nonFirst) override;
525 
526     /**
527      * @brief Sets the application to start its ability in lock mission mode.
528      */
529     void LockMission() override;
530 
531     /**
532      * @brief Unlocks this ability by exiting the lock mission mode.
533      */
534     void UnlockMission() override;
535 
536     /**
537      * @brief Sets description information about the mission containing this ability.
538      *
539      * @param MissionInformation Indicates the object containing information about the
540      *                               mission. This parameter cannot be null.
541      * @return Returns true on success, others on failure.
542      */
543     bool SetMissionInformation(const MissionInformation &missionInformation) override;
544 
545 private:
546     std::shared_ptr<Context> baseContext_;
547 };
548 
549 }  // namespace AppExecFwk
550 }  // namespace OHOS
551 #endif  // FOUNDATION_APPEXECFWK_OHOS_CONTEXT_CONTAINER_H
552