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 #include "napi_context_common.h"
19 #include "permission_grant_info.h"
20 #include "ui_content.h"
21 #include "ui_extension_context.h"
22 
23 namespace OHOS {
24 namespace Security {
25 namespace AccessToken {
26 struct RequestPermOnSettingAsyncContext : public AtManagerAsyncWorkData {
RequestPermOnSettingAsyncContextRequestPermOnSettingAsyncContext27     explicit RequestPermOnSettingAsyncContext(napi_env env) : AtManagerAsyncWorkData(env)
28     {
29         this->env = env;
30     }
31 
32     AccessTokenID tokenId = 0;
33     int32_t result = RET_SUCCESS;
34     PermissionGrantInfo info;
35     int32_t resultCode = -1;
36 
37     std::vector<std::string> permissionList;
38     napi_value requestResult = nullptr;
39     int32_t errorCode = -1;
40     std::vector<int32_t> stateList;
41 
42     std::shared_ptr<AbilityRuntime::AbilityContext> abilityContext;
43     std::shared_ptr<AbilityRuntime::UIExtensionContext> uiExtensionContext;
44     bool uiAbilityFlag = false;
45     bool releaseFlag = false;
46 };
47 
48 struct RequestOnSettingAsyncContextHandle {
RequestOnSettingAsyncContextHandleRequestOnSettingAsyncContextHandle49     explicit RequestOnSettingAsyncContextHandle(std::shared_ptr<RequestPermOnSettingAsyncContext>& requestAsyncContext)
50     {
51         asyncContextPtr = requestAsyncContext;
52     }
53 
54     std::shared_ptr<RequestPermOnSettingAsyncContext> asyncContextPtr;
55 };
56 
57 class PermissonOnSettingUICallback {
58 public:
59     explicit PermissonOnSettingUICallback(const std::shared_ptr<RequestPermOnSettingAsyncContext>& reqContext);
60     ~PermissonOnSettingUICallback();
61     void SetSessionId(int32_t sessionId);
62     void OnRelease(int32_t releaseCode);
63     void OnResult(int32_t resultCode, const OHOS::AAFwk::Want& result);
64     void OnReceive(const OHOS::AAFwk::WantParams& request);
65     void OnError(int32_t code, const std::string& name, const std::string& message);
66     void OnRemoteReady(const std::shared_ptr<OHOS::Ace::ModalUIExtensionProxy>& uiProxy);
67     void OnDestroy();
68     void ReleaseHandler(int32_t code);
69 
70 private:
71     int32_t sessionId_ = 0;
72     std::shared_ptr<RequestPermOnSettingAsyncContext> reqContext_ = nullptr;
73 };
74 
75 struct PermissonOnSettingResultCallback {
76     int32_t jsCode;
77     std::vector<int32_t> stateList;
78     std::shared_ptr<RequestPermOnSettingAsyncContext> data = nullptr;
79 };
80 
81 class NapiRequestPermissionOnSetting {
82 public:
83     static napi_value RequestPermissionOnSetting(napi_env env, napi_callback_info info);
84 
85 private:
86     static bool ParseRequestPermissionOnSetting(const napi_env& env, const napi_callback_info& cbInfo,
87         std::shared_ptr<RequestPermOnSettingAsyncContext>& asyncContext);
88     static void RequestPermissionOnSettingExecute(napi_env env, void* data);
89     static void RequestPermissionOnSettingComplete(napi_env env, napi_status status, void* data);
90 };
91 } // namespace AccessToken
92 } // namespace Security
93 } // namespace OHOS
94 #endif /* INTERFACES_ACCESSTOKEN_KITS_NAPI_REQUEST_PERMISSION_ON_SETTING_H */
95