• 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_H
17 #define OHOS_ABILITY_RUNTIME_CONTEXT_H
18 
19 #include <memory>
20 
21 #include "bundle_mgr_interface.h"
22 #include "ability_manager_interface.h"
23 #include "ability_info.h"
24 #include "process_info.h"
25 #include "resource_manager.h"
26 #include "dummy_hap_module_info.h"
27 #include "hap_module_info.h"
28 #include "task/task_priority.h"
29 
30 namespace OHOS {
31 namespace DataShare {
32 class DataShareHelper;
33 }
34 namespace AppExecFwk {
35 using Want = OHOS::AAFwk::Want;
36 using AbilityStartSetting = AAFwk::AbilityStartSetting;
37 using PermissionRequestTask = std::function<void(const std::vector<std::string>&, const std::vector<int>&)>;
38 // Request permissions for user
39 #define OHOS_REQUEST_PERMISSION_BUNDLENAME "com.ohos.systemui"
40 #define OHOS_REQUEST_PERMISSION_ABILITY_NAME "com.ohos.systemui.systemdialog.MainAbility"
41 
42 #define OHOS_REQUEST_PERMISSION_KEY "OHOS_REQUEST_PERMISSION_KEY"
43 #define OHOS_REQUEST_PERMISSIONS_LIST "OHOS_REQUEST_PERMISSIONS_LIST"
44 #define OHOS_REQUEST_PERMISSIONS_DES_LIST "OHOS_REQUEST_PERMISSIONS_DES_LIST"
45 #define OHOS_REQUEST_CALLER_BUNDLERNAME "OHOS_REQUEST_CALLER_BUNDLERNAME"
46 
47 #define OHOS_RESULT_PERMISSION_KEY "OHOS_RESULT_PERMISSION_KEY"
48 #define OHOS_RESULT_PERMISSIONS_LIST "OHOS_RESULT_PERMISSIONS_LIST"
49 #define OHOS_RESULT_PERMISSIONS_LIST_YES "OHOS_RESULT_PERMISSIONS_LIST_YES"
50 #define OHOS_RESULT_PERMISSIONS_LIST_NO "OHOS_RESULT_PERMISSIONS_LIST_NO"
51 #define OHOS_RESULT_CALLER_BUNDLERNAME "OHOS_RESULT_CALLER_BUNDLERNAME"
52 
53 #define OHOS_REQUEST_PERMISSION_VALUE 1
54 
55 constexpr int INVALID_RESOURCE_VALUE = -1;  // GetColor() Failed return Value
56 constexpr int DEFAULT_ACCOUNT_ID = -1;
57 
58 class DataAbilityHelperImpl;
59 class ContinuationConnector;
60 class IAbilityManager;
61 class Context {
62 public:
63     Context() = default;
64     virtual ~Context() = default;
65 
66     /**
67      * Called when getting the ProcessInfo
68      *
69      * @return ProcessInfo
70      */
71     virtual std::shared_ptr<ProcessInfo> GetProcessInfo() const = 0;
72 
73     /**
74      * @brief Obtains information about the current application. The returned application information includes basic
75      * information such as the application name and application permissions.
76      *
77      * @return Returns the ApplicationInfo for the current application.
78      */
79     virtual std::shared_ptr<ApplicationInfo> GetApplicationInfo() const = 0;
80 
81     /**
82      * @brief Obtains the Context object of the application.
83      *
84      * @return Returns the Context object of the application.
85      */
86     virtual std::shared_ptr<Context> GetApplicationContext() const = 0;
87 
88     /**
89      * @brief Obtains the path of the package containing the current ability. The returned path contains the resources,
90      *  source code, and configuration files of a module.
91      *
92      * @return Returns the path of the package file.
93      */
94     virtual std::string GetBundleCodePath() = 0;
95 
96     /**
97      * @brief Obtains information about the current ability.
98      * The returned information includes the class name, bundle name, and other information about the current ability.
99      *
100      * @return Returns the AbilityInfo object for the current ability.
101      */
102     virtual const std::shared_ptr<AbilityInfo> GetAbilityInfo() = 0;
103 
104     /**
105      * @brief Obtains the Context object of the application.
106      *
107      * @return Returns the Context object of the application.
108      */
109     virtual std::shared_ptr<Context> GetContext() = 0;
110 
111     /**
112      * @brief Obtains an IBundleMgr instance.
113      * You can use this instance to obtain information about the application bundle.
114      *
115      * @return Returns an IBundleMgr instance.
116      */
117     virtual sptr<IBundleMgr> GetBundleManager() const = 0;
118 
119     /**
120      * @brief Obtains a resource manager.
121      *
122      * @return Returns a ResourceManager object.
123      */
124     virtual std::shared_ptr<Global::Resource::ResourceManager> GetResourceManager() const = 0;
125 
126     /**
127      * @brief Destroys another ability that uses the AbilityInfo.AbilityType.SERVICE template.
128      * The current ability using either the AbilityInfo.AbilityType.SERVICE or AbilityInfo.AbilityType.PAGE
129      * template can call this method to destroy another ability that uses the AbilityInfo.AbilityType.SERVICE
130      * template. The current ability itself can be destroyed by calling the terminateAbility() method.
131      *
132      * @param want Indicates the Want containing information about the ability to destroy.
133      *
134      * @return Returns true if the ability is destroyed successfully; returns false otherwise.
135      */
136     virtual bool StopAbility(const AAFwk::Want &want) = 0;
137 
138     /**
139      * @brief Obtains the local database path.
140      * If the local database path does not exist, the system creates one and returns the created path.
141      *
142      * @return Returns the local database file.
143      */
144     virtual std::string GetDatabaseDir() = 0;
145 
146     /**
147      * @brief Obtains the absolute path where all private data files of this application are stored.
148      *
149      * @return Returns the absolute path storing all private data files of this application.
150      */
151     virtual std::string GetDataDir() = 0;
152 
153     /**
154      * @brief Obtains the directory for storing custom data files of the application.
155      * You can use the returned File object to create and access files in this directory. The files
156      * can be accessible only by the current application.
157      *
158      * @param name Indicates the name of the directory to retrieve. This directory is created as part
159      * of your application data.
160      * @param mode Indicates the file operating mode. The value can be 0 or a combination of MODE_PRIVATE.
161      *
162      * @return Returns a File object for the requested directory.
163      */
164     virtual std::string GetDir(const std::string &name, int mode) = 0;
165 
166     /**
167      * @brief Obtains the absolute path to the application-specific cache directory
168      * on the primary external or shared storage device.
169      *
170      * @return Returns the absolute path to the application-specific cache directory on the external or
171      * shared storage device; returns null if the external or shared storage device is temporarily unavailable.
172      */
GetExternalCacheDir()173     virtual std::string GetExternalCacheDir()
174     {
175         return "";
176     }
177 
178     /**
179      * @brief Obtains the absolute path to the directory for storing files for the application on the
180      * primary external or shared storage device.
181      *
182      * @param type Indicates the type of the file directory to return
183      *
184      * @return Returns the absolute path to the application file directory on the external or shared storage
185      * device; returns null if the external or shared storage device is temporarily unavailable.
186      */
GetExternalFilesDir(std::string & type)187     virtual std::string GetExternalFilesDir(std::string &type)
188     {
189         return "";
190     }
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     virtual std::string GetFilesDir() = 0;
198 
199     /**
200      * @brief Checks whether the current process has the given permission.
201      * You need to call requestPermissionsFromUser(std::vector<std::string>,std::vector<int>, int) to request
202      * a permission only if the current process does not have the specific permission.
203      *
204      * @param permission Indicates the permission to check. This parameter cannot be null.
205      *
206      * @return Returns 0 (IBundleManager.PERMISSION_GRANTED) if the current process has the permission;
207      * returns -1 (IBundleManager.PERMISSION_DENIED) otherwise.
208      */
VerifySelfPermission(const std::string & permission)209     virtual int VerifySelfPermission(const std::string &permission)
210     {
211         return 0;
212     }
213 
214     /**
215      * @brief Obtains the bundle name of the current ability.
216      *
217      * @return Returns the bundle name of the current ability.
218      */
219     virtual std::string GetBundleName() const = 0;
220 
221     /**
222      * @brief Obtains the path of the OHOS Ability Package (HAP} containing this ability.
223      *
224      * @return Returns the path of the HAP containing this ability.
225      */
226     virtual std::string GetBundleResourcePath() = 0;
227 
228     /**
229      * @brief Starts a new ability.
230      * An ability using the AbilityInfo.AbilityType.SERVICE or AbilityInfo.AbilityType.PAGE template uses this method
231      * to start a specific ability. The system locates the target ability from installed abilities based on the value
232      * of the want parameter and then starts it. You can specify the ability to start using the want parameter.
233      *
234      * @param want Indicates the Want containing information about the target ability to start.
235      *
236      * @param requestCode Indicates the request code returned after the ability using the AbilityInfo.AbilityType.PAGE
237      * template is started. You can define the request code to identify the results returned by abilities. The value
238      * ranges from 0 to 65535. This parameter takes effect only on abilities using the AbilityInfo.AbilityType.PAGE
239      * template.
240      *
241      * @return errCode ERR_OK on success, others on failure.
242      */
243     virtual ErrCode StartAbility(const AAFwk::Want &want, int requestCode) = 0;
244 
245     /**
246      * @brief Remove permissions for all users who have access to specific permissions
247      *
248      * @param permission Indicates the permission to unauth. This parameter cannot be null.
249      * @param uri Indicates the URI to unauth. This parameter cannot be null.
250      * @param uid Indicates the UID of the unauth to check.
251      *
252      */
UnauthUriPermission(const std::string & permission,const Uri & uri,int uid)253     virtual void UnauthUriPermission(const std::string &permission, const Uri &uri, int uid)
254     {}
255 
256     /**
257      * @brief Obtains an ability manager.
258      * The ability manager provides information about running processes and memory usage of an application.
259      *
260      * @return Returns an IAbilityManager instance.
261      */
262     virtual sptr<AAFwk::IAbilityManager> GetAbilityManager() = 0;
263 
264     /**
265      * @brief Obtains the type of this application.
266      *
267      * @return Returns system if this application is a system application;
268      * returns normal if it is released in OHOS AppGallery;
269      * returns other if it is released by a third-party vendor;
270      * returns an empty string if the query fails.
271      */
272     virtual std::string GetAppType() = 0;
273 
274     /**
275      * @brief Destroys the current ability.
276      *
277      * @return errCode ERR_OK on success, others on failure.
278      */
279     virtual ErrCode TerminateAbility() = 0;
280 
281     /**
282      * @brief Query whether the application of the specified PID and UID has been granted a certain permission
283      *
284      * @param permissions Indicates the list of permissions to be requested. This parameter cannot be null.
285      * @param pid Process id
286      * @param uid
287      * @return Returns 0 (IBundleManager.PERMISSION_GRANTED) if the current process has the permission;
288      * returns -1 (IBundleManager.PERMISSION_DENIED) otherwise.
289      */
VerifyPermission(const std::string & permission,int pid,int uid)290     virtual int VerifyPermission(const std::string &permission, int pid, int uid)
291     {
292         return 0;
293     }
294 
295     /**
296      * @brief Sets the pattern of this Context based on the specified pattern ID.
297      *
298      * @param patternId Indicates the resource ID of the pattern to set.
299      */
300     virtual void SetPattern(int patternId) = 0;
301 
302     /**
303      * @brief Obtains the Context object of this ability.
304      *
305      * @return Returns the Context object of this ability.
306      */
GetAbilityPackageContext()307     virtual std::shared_ptr<Context> GetAbilityPackageContext()
308     {
309         return nullptr;
310     }
311 
312     /**
313      * @brief Obtains the HapModuleInfo object of the application.
314      *
315      * @return Returns the HapModuleInfo object of the application.
316      */
317     virtual std::shared_ptr<HapModuleInfo> GetHapModuleInfo() = 0;
318 
319     /**
320      * @brief Obtains the name of the current process.
321      *
322      * @return Returns the current process name.
323      */
324     virtual std::string GetProcessName() = 0;
325 
326     /**
327      * @brief Obtains the bundle name of the ability that called the current ability.
328      * You can use the obtained bundle name to check whether the calling ability is allowed to receive the data you will
329      * send. If you did not use Ability.startAbilityForResult(ohos.aafwk.content.Want, int,
330      * ohos.aafwk.ability.startsetting.AbilityStartSetting) to start the calling ability, null is returned.
331      *
332      * @return Returns the bundle name of the calling ability; returns null if no calling ability is available.
333      */
334     virtual std::string GetCallingBundle() = 0;
335 
336     /**
337      * @brief Requests certain permissions from the system.
338      * This method is called for permission request. This is an asynchronous method. When it is executed,
339      * the task will be called back.
340      *
341      * @param permissions Indicates the list of permissions to be requested. This parameter cannot be null.
342      * @param permissionsState Indicates the list of permissions' state to be requested. This parameter cannot be null.
343      * @param task The callback or promise fo js interface.
344      */
RequestPermissionsFromUser(std::vector<std::string> & permissions,std::vector<int> & permissionsState,PermissionRequestTask && task)345     virtual void RequestPermissionsFromUser(std::vector<std::string> &permissions, std::vector<int> &permissionsState,
346         PermissionRequestTask &&task)
347     {}
348 
349     /**
350      * @brief Starts a new ability with special ability start setting.
351      *
352      * @param want Indicates the Want containing information about the target ability to start.
353      * @param requestCode Indicates the request code returned after the ability is started. You can define the request
354      * code to identify the results returned by abilities. The value ranges from 0 to 65535.
355      * @param abilityStartSetting Indicates the special start setting used in starting ability.
356      *
357      * @return errCode ERR_OK on success, others on failure.
358      */
359     virtual ErrCode StartAbility(const Want &want, int requestCode, const AbilityStartSetting &abilityStartSetting) = 0;
360 
361     /**
362      * @brief Connects the current ability to an ability using the AbilityInfo.AbilityType.SERVICE template.
363      *
364      * @param want Indicates the want containing information about the ability to connect
365      *
366      * @param conn Indicates the callback object when the target ability is connected.
367      *
368      * @return True means success and false means failure
369      */
370     virtual bool ConnectAbility(const Want &want, const sptr<AAFwk::IAbilityConnection> &conn) = 0;
371 
372     /**
373      * @brief Disconnects the current ability from an ability
374      *
375      * @param conn Indicates the IAbilityConnection callback object passed by connectAbility after the connection
376      *              is set up. The IAbilityConnection object uniquely identifies a connection between two abilities.
377      *
378      * @return errCode ERR_OK on success, others on failure.
379      */
380     virtual ErrCode DisconnectAbility(const sptr<AAFwk::IAbilityConnection> &conn) = 0;
381 
382     /**
383      * @brief Obtains information about the caller of this ability.
384      *
385      * @return Returns the caller information.
386      */
387     virtual Uri GetCaller() = 0;
388 
389     /**
390      * @brief Get the string of this Context based on the specified resource ID.
391      *
392      * @param resId Indicates the resource ID of the string to get.
393      *
394      * @return Returns the string of this Context.
395      */
396     virtual std::string GetString(int resId) = 0;
397 
398     /**
399      * @brief Get the string array of this Context based on the specified resource ID.
400      *
401      * @param resId Indicates the resource ID of the string array to get.
402      *
403      * @return Returns the string array of this Context.
404      */
405     virtual std::vector<std::string> GetStringArray(int resId) = 0;
406 
407     /**
408      * @brief Get the integer array of this Context based on the specified resource ID.
409      *
410      * @param resId Indicates the resource ID of the integer array to get.
411      *
412      * @return Returns the integer array of this Context.
413      */
414     virtual std::vector<int> GetIntArray(int resId) = 0;
415 
416     /**
417      * @brief Obtains the theme of this Context.
418      *
419      * @return theme Returns the theme of this Context.
420      */
421     virtual std::map<std::string, std::string> GetTheme() = 0;
422 
423     /**
424      * @brief Sets the theme of this Context based on the specified theme ID.
425      *
426      * @param themeId Indicates the resource ID of the theme to set.
427      */
428     virtual void SetTheme(int themeId) = 0;
429 
430     /**
431      * @brief Obtains the pattern of this Context.
432      *
433      * @return getPattern in interface Context
434      */
435     virtual std::map<std::string, std::string> GetPattern() = 0;
436 
437     /**
438      * @brief Get the color of this Context based on the specified resource ID.
439      *
440      * @param resId Indicates the resource ID of the color to get.
441      *
442      * @return Returns the color value of this Context.
443      */
444     virtual int GetColor(int resId) = 0;
445 
446     /**
447      * @brief Obtains the theme id of this Context.
448      *
449      * @return int Returns the theme id of this Context.
450      */
451     virtual int GetThemeId() = 0;
452 
453     /**
454      * @brief Obtains the current display orientation of this ability.
455      *
456      * @return Returns the current display orientation.
457      */
458     virtual int GetDisplayOrientation() = 0;
459 
460     /**
461      * @brief Obtains the path storing the preference file of the application.
462      *        If the preference file path does not exist, the system creates one and returns the created path.
463      *
464      * @return Returns the preference file path .
465      */
466     virtual std::string GetPreferencesDir() = 0;
467 
468     /**
469      * @brief Set color mode
470      *
471      * @param the value of color mode.
472      */
473     virtual void SetColorMode(int mode) = 0;
474 
475     /**
476      * @brief Obtains color mode.
477      *
478      * @return Returns the color mode value.
479      */
480     virtual int GetColorMode() = 0;
481 
482     /**
483      * @brief Obtains the unique ID of the mission containing this ability.
484      *
485      * @return Returns the unique mission ID.
486      */
487     virtual int GetMissionId() = 0;
488 
489     /**
490      * @brief Starts multiple abilities.
491      *
492      * @param wants Indicates the Want containing information array about the target ability to start.
493      */
494     virtual void StartAbilities(const std::vector<AAFwk::Want> &wants) = 0;
495 
496     /**
497      * @brief Checks whether the configuration of this ability is changing.
498      *
499      * @return Returns true if the configuration of this ability is changing and false otherwise.
500      */
IsUpdatingConfigurations()501     virtual bool IsUpdatingConfigurations()
502     {
503         return false;
504     }
505 
506     /**
507      * @brief Informs the system of the time required for drawing this Page ability.
508      *
509      * @return Returns the notification is successful or fail
510      */
PrintDrawnCompleted()511     virtual bool PrintDrawnCompleted()
512     {
513         return false;
514     }
515 
516     virtual sptr<IRemoteObject> GetToken() = 0;
517 };
518 }  // namespace AppExecFwk
519 }  // namespace OHOS
520 #endif  // OHOS_ABILITY_RUNTIME_CONTEXT_H
521