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