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_GLOBAL_SWITCHN_ON_SETTING_H
16 #define INTERFACES_ACCESSTOKEN_KITS_NAPI_REQUEST_GLOBAL_SWITCHN_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 typedef enum {
27     CAMERA = 0,
28     MICROPHONE = 1,
29     LOCATION = 2,
30 } SwitchType;
31 
32 struct RequestGlobalSwitchAsyncContext : public AtManagerAsyncWorkData {
RequestGlobalSwitchAsyncContextRequestGlobalSwitchAsyncContext33     explicit RequestGlobalSwitchAsyncContext(napi_env env) : AtManagerAsyncWorkData(env)
34     {
35         this->env = env;
36     }
37 
38     AccessTokenID tokenId = 0;
39     int32_t result = RET_SUCCESS;
40     PermissionGrantInfo info;
41     int32_t resultCode = -1;
42     int32_t switchType = -1;
43     napi_value requestResult = nullptr;
44     int32_t errorCode = -1;
45     bool switchStatus = false;
46     std::shared_ptr<AbilityRuntime::AbilityContext> abilityContext;
47     std::shared_ptr<AbilityRuntime::UIExtensionContext> uiExtensionContext;
48     bool uiAbilityFlag = false;
49     bool releaseFlag = false;
50 };
51 
52 struct RequestGlobalSwitchAsyncContextHandle {
RequestGlobalSwitchAsyncContextHandleRequestGlobalSwitchAsyncContextHandle53     explicit RequestGlobalSwitchAsyncContextHandle(
54         std::shared_ptr<RequestGlobalSwitchAsyncContext>& requestAsyncContext)
55     {
56         asyncContextPtr = requestAsyncContext;
57     }
58 
59     std::shared_ptr<RequestGlobalSwitchAsyncContext> asyncContextPtr;
60 };
61 
62 class SwitchOnSettingUICallback {
63 public:
64     explicit SwitchOnSettingUICallback(const std::shared_ptr<RequestGlobalSwitchAsyncContext>& reqContext);
65     ~SwitchOnSettingUICallback();
66     void SetSessionId(int32_t sessionId);
67     void OnRelease(int32_t releaseCode);
68     void OnResult(int32_t resultCode, const OHOS::AAFwk::Want& result);
69     void OnReceive(const OHOS::AAFwk::WantParams& request);
70     void OnError(int32_t code, const std::string& name, const std::string& message);
71     void OnRemoteReady(const std::shared_ptr<OHOS::Ace::ModalUIExtensionProxy>& uiProxy);
72     void OnDestroy();
73     void ReleaseHandler(int32_t code);
74 
75 private:
76     int32_t sessionId_ = 0;
77     std::shared_ptr<RequestGlobalSwitchAsyncContext> reqContext_ = nullptr;
78 };
79 
80 struct SwitchOnSettingResultCallback {
81     int32_t jsCode;
82     bool switchStatus;
83     std::shared_ptr<RequestGlobalSwitchAsyncContext> data = nullptr;
84 };
85 
86 class NapiRequestGlobalSwitch {
87 public:
88     static napi_value RequestGlobalSwitch(napi_env env, napi_callback_info info);
89 
90 private:
91     static bool ParseRequestGlobalSwitch(const napi_env& env, const napi_callback_info& cbInfo,
92         std::shared_ptr<RequestGlobalSwitchAsyncContext>& asyncContext);
93     static void RequestGlobalSwitchExecute(napi_env env, void* data);
94     static void RequestGlobalSwitchComplete(napi_env env, napi_status status, void* data);
95 };
96 } // namespace AccessToken
97 } // namespace Security
98 } // namespace OHOS
99 #endif /* INTERFACES_ACCESSTOKEN_KITS_NAPI_REQUEST_GLOBAL_SWITCHN_ON_SETTING_H */
100