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 #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 typedef enum { 31 CAMERA = 0, 32 MICROPHONE = 1, 33 LOCATION = 2, 34 } SwitchType; 35 36 struct RequestGlobalSwitchAsyncContext : public AtManagerAsyncWorkData { RequestGlobalSwitchAsyncContextRequestGlobalSwitchAsyncContext37 explicit RequestGlobalSwitchAsyncContext(napi_env env) : AtManagerAsyncWorkData(env) 38 { 39 this->env = env; 40 } 41 42 AccessTokenID tokenId = 0; 43 AtmResult result; 44 PermissionGrantInfo info; 45 int32_t resultCode = -1; 46 int32_t switchType = -1; 47 napi_value requestResult = nullptr; 48 int32_t errorCode = -1; 49 bool switchStatus = false; 50 int32_t instanceId = -1; 51 bool isDynamic = true; 52 std::shared_ptr<AbilityRuntime::AbilityContext> abilityContext; 53 std::shared_ptr<AbilityRuntime::UIExtensionContext> uiExtensionContext; 54 bool uiAbilityFlag = false; 55 bool releaseFlag = false; 56 #ifdef EVENTHANDLER_ENABLE 57 std::shared_ptr<AppExecFwk::EventHandler> handler_ = nullptr; 58 #endif 59 }; 60 61 struct RequestGlobalSwitchAsyncContextHandle { RequestGlobalSwitchAsyncContextHandleRequestGlobalSwitchAsyncContextHandle62 explicit RequestGlobalSwitchAsyncContextHandle( 63 std::shared_ptr<RequestGlobalSwitchAsyncContext>& requestAsyncContext) 64 { 65 asyncContextPtr = requestAsyncContext; 66 } 67 68 std::shared_ptr<RequestGlobalSwitchAsyncContext> asyncContextPtr; 69 }; 70 71 class RequestGlobalSwitchAsyncInstanceControl { 72 public: 73 static void AddCallbackByInstanceId(std::shared_ptr<RequestGlobalSwitchAsyncContext>& asyncContext); 74 static void ExecCallback(int32_t id); 75 static void CheckDynamicRequest( 76 std::shared_ptr<RequestGlobalSwitchAsyncContext>& asyncContext, bool& isDynamic); 77 static void UpdateQueueData(const std::shared_ptr<RequestGlobalSwitchAsyncContext>& asyncContext); 78 private: 79 static std::map<int32_t, std::vector<std::shared_ptr<RequestGlobalSwitchAsyncContext>>> instanceIdMap_; 80 static std::mutex instanceIdMutex_; 81 }; 82 83 class SwitchOnSettingUICallback { 84 public: 85 explicit SwitchOnSettingUICallback(const std::shared_ptr<RequestGlobalSwitchAsyncContext>& reqContext); 86 ~SwitchOnSettingUICallback(); 87 void SetSessionId(int32_t sessionId); 88 void OnRelease(int32_t releaseCode); 89 void OnResult(int32_t resultCode, const OHOS::AAFwk::Want& result); 90 void OnReceive(const OHOS::AAFwk::WantParams& request); 91 void OnError(int32_t code, const std::string& name, const std::string& message); 92 void OnRemoteReady(const std::shared_ptr<OHOS::Ace::ModalUIExtensionProxy>& uiProxy); 93 void OnDestroy(); 94 void ReleaseHandler(int32_t code); 95 96 private: 97 int32_t sessionId_ = 0; 98 std::shared_ptr<RequestGlobalSwitchAsyncContext> reqContext_ = nullptr; 99 }; 100 101 struct SwitchOnSettingResultCallback { 102 int32_t errorCode; 103 bool switchStatus; 104 std::shared_ptr<RequestGlobalSwitchAsyncContext> data = nullptr; 105 }; 106 107 class NapiRequestGlobalSwitch { 108 public: 109 static napi_value RequestGlobalSwitch(napi_env env, napi_callback_info info); 110 111 private: 112 static bool ParseRequestGlobalSwitch(const napi_env& env, const napi_callback_info& cbInfo, 113 std::shared_ptr<RequestGlobalSwitchAsyncContext>& asyncContext); 114 static void RequestGlobalSwitchExecute(napi_env env, void* data); 115 static void RequestGlobalSwitchComplete(napi_env env, napi_status status, void* data); 116 }; 117 } // namespace AccessToken 118 } // namespace Security 119 } // namespace OHOS 120 #endif /* INTERFACES_ACCESSTOKEN_KITS_NAPI_REQUEST_GLOBAL_SWITCHN_ON_SETTING_H */ 121