• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 #ifndef INTERFACES_ACCESSTOKEN_KITS_NAPI_REQUEST_PERMISSION_ON_SETTING_H
16 #define INTERFACES_ACCESSTOKEN_KITS_NAPI_REQUEST_PERMISSION_ON_SETTING_H
17 
18 #ifdef EVENTHANDLER_ENABLE
19 #include "event_handler.h"
20 #include "event_queue.h"
21 #endif
22 #include "napi_context_common.h"
23 #include "permission_grant_info.h"
24 #include "ui_content.h"
25 #include "ui_extension_context.h"
26 
27 namespace OHOS {
28 namespace Security {
29 namespace AccessToken {
30 struct RequestPermOnSettingAsyncContext : public AtManagerAsyncWorkData {
RequestPermOnSettingAsyncContextRequestPermOnSettingAsyncContext31     explicit RequestPermOnSettingAsyncContext(napi_env env) : AtManagerAsyncWorkData(env)
32     {
33         this->env = env;
34     }
35 
36     AccessTokenID tokenId = 0;
37     AtmResult result;
38     PermissionGrantInfo info;
39     int32_t resultCode = -1;
40 
41     int32_t instanceId = -1;
42     bool isDynamic = true;
43     std::vector<std::string> permissionList;
44     napi_value requestResult = nullptr;
45     int32_t errorCode = -1;
46     std::vector<int32_t> stateList;
47 
48     std::shared_ptr<AbilityRuntime::AbilityContext> abilityContext;
49     std::shared_ptr<AbilityRuntime::UIExtensionContext> uiExtensionContext;
50     bool uiAbilityFlag = false;
51     bool releaseFlag = false;
52 #ifdef EVENTHANDLER_ENABLE
53     std::shared_ptr<AppExecFwk::EventHandler> handler_ = nullptr;
54 #endif
55 };
56 
57 struct RequestOnSettingAsyncContextHandle {
RequestOnSettingAsyncContextHandleRequestOnSettingAsyncContextHandle58     explicit RequestOnSettingAsyncContextHandle(std::shared_ptr<RequestPermOnSettingAsyncContext>& requestAsyncContext)
59     {
60         asyncContextPtr = requestAsyncContext;
61     }
62 
63     std::shared_ptr<RequestPermOnSettingAsyncContext> asyncContextPtr;
64 };
65 
66 class RequestOnSettingAsyncInstanceControl {
67     public:
68         static void AddCallbackByInstanceId(std::shared_ptr<RequestPermOnSettingAsyncContext>& asyncContext);
69         static void ExecCallback(int32_t id);
70         static void CheckDynamicRequest(
71             std::shared_ptr<RequestPermOnSettingAsyncContext>& asyncContext, bool& isDynamic);
72         static void UpdateQueueData(const std::shared_ptr<RequestPermOnSettingAsyncContext>& asyncContext);
73     private:
74         static std::map<int32_t, std::vector<std::shared_ptr<RequestPermOnSettingAsyncContext>>> instanceIdMap_;
75         static std::mutex instanceIdMutex_;
76 };
77 
78 class PermissonOnSettingUICallback {
79 public:
80     explicit PermissonOnSettingUICallback(const std::shared_ptr<RequestPermOnSettingAsyncContext>& reqContext);
81     ~PermissonOnSettingUICallback();
82     void SetSessionId(int32_t sessionId);
83     void OnRelease(int32_t releaseCode);
84     void OnResult(int32_t resultCode, const OHOS::AAFwk::Want& result);
85     void OnReceive(const OHOS::AAFwk::WantParams& request);
86     void OnError(int32_t code, const std::string& name, const std::string& message);
87     void OnRemoteReady(const std::shared_ptr<OHOS::Ace::ModalUIExtensionProxy>& uiProxy);
88     void OnDestroy();
89     void ReleaseHandler(int32_t code);
90 
91 private:
92     int32_t sessionId_ = 0;
93     std::shared_ptr<RequestPermOnSettingAsyncContext> reqContext_ = nullptr;
94 };
95 
96 struct PermissonOnSettingResultCallback {
97     int32_t errorCode;
98     std::vector<int32_t> stateList;
99     std::shared_ptr<RequestPermOnSettingAsyncContext> data = nullptr;
100 };
101 
102 class NapiRequestPermissionOnSetting {
103 public:
104     static napi_value RequestPermissionOnSetting(napi_env env, napi_callback_info info);
105 
106 private:
107     static bool ParseRequestPermissionOnSetting(const napi_env& env, const napi_callback_info& cbInfo,
108         std::shared_ptr<RequestPermOnSettingAsyncContext>& asyncContext);
109     static void RequestPermissionOnSettingExecute(napi_env env, void* data);
110     static void RequestPermissionOnSettingComplete(napi_env env, napi_status status, void* data);
111 };
112 } // namespace AccessToken
113 } // namespace Security
114 } // namespace OHOS
115 #endif /* INTERFACES_ACCESSTOKEN_KITS_NAPI_REQUEST_PERMISSION_ON_SETTING_H */
116