• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 ANI_REQUEST_BASE_H
16 #define ANI_REQUEST_BASE_H
17 
18 #include "ability_context.h"
19 #include "access_token.h"
20 #include "ani.h"
21 #include "ani_error.h"
22 #include "ani_utils.h"
23 #ifdef EVENTHANDLER_ENABLE
24 #include "event_handler.h"
25 #include "event_queue.h"
26 #endif
27 #include "permission_grant_info.h"
28 #include "ui_content.h"
29 #include "ui_extension_context.h"
30 
31 namespace OHOS {
32 namespace Security {
33 namespace AccessToken {
34 enum AniRequestType {
35     REQUEST_PERMISSIONS_FROM_USER,
36     REQUEST_PERMISSION_ON_SETTING,
37     REQUEST_GLOBAL_SWITCH_ON_SETTING
38 };
39 
40 struct RequestAsyncContextBase {
41     AccessTokenID tokenId = 0;
42     std::string bundleName;
43     AtmResult result;
44     PermissionGrantInfo info;
45     int32_t instanceId = -1;
46     std::shared_ptr<OHOS::AbilityRuntime::Context> stageContext_ = nullptr;
47     bool uiExtensionFlag = false;
48     bool releaseFlag = false;
49     bool uiContentFlag = false;
50     std::mutex lockReleaseFlag;
51 
52 #ifdef EVENTHANDLER_ENABLE
53     std::shared_ptr<AppExecFwk::EventHandler> handler_ = nullptr;
54 #endif
55     std::thread::id threadId_;
56     ani_vm* vm_ = nullptr;
57     ani_env* env_ = nullptr;
58     ani_ref callbackRef = nullptr;
59     AniRequestType contextType_;
60 
61     RequestAsyncContextBase(ani_vm* vm, ani_env* env, AniRequestType type);
62     virtual ~RequestAsyncContextBase() = default;
63     bool FillInfoFromContext(const ani_object& aniContext);
64     void FinishCallback();
65     void GetInstanceId();
66     void Clear();
67     AniRequestType GetType();
68 
69     virtual bool IsSameRequest(const std::shared_ptr<RequestAsyncContextBase> other);
70     virtual void CopyResult(const std::shared_ptr<RequestAsyncContextBase> other);
71     virtual void ProcessFailResult(int32_t code);
72 
73     virtual bool NoNeedUpdate() = 0;
74     virtual int32_t ConvertErrorCode(int32_t code) = 0;
75     virtual void ProcessUIExtensionCallback(const OHOS::AAFwk::Want& result) = 0;
76     virtual ani_object WrapResult(ani_env* env) = 0;
77     virtual void StartExtensionAbility(std::shared_ptr<RequestAsyncContextBase> asyncContext) = 0;
78     virtual bool CheckDynamicRequest() = 0;
79 };
80 
81 class RequestInstanceControl {
82 public:
83     void AddCallbackByInstanceId(std::shared_ptr<RequestAsyncContextBase> asyncContext);
84     void ExecCallback(int32_t id);
85     void UpdateQueueData(const std::shared_ptr<RequestAsyncContextBase> asyncContext);
86 private:
87     std::map<int32_t, std::vector<std::shared_ptr<RequestAsyncContextBase>>> instanceIdMap_;
88     std::mutex instanceIdMutex_;
89 };
90 
91 class UIExtensionCallback {
92 public:
93     explicit UIExtensionCallback(const std::shared_ptr<RequestAsyncContextBase> reqContext);
94     ~UIExtensionCallback();
95 
96     void SetSessionId(int32_t sessionId);
97     void SetRequestInstanceControl(std::shared_ptr<RequestInstanceControl> controllerInstance);
98     void ReleaseHandler(int32_t code);
99     void OnRelease(int32_t releaseCode);
100     void OnReceive(const OHOS::AAFwk::WantParams& request);
101     void OnError(int32_t code, const std::string& name, const std::string& message);
102     void OnRemoteReady(const std::shared_ptr<OHOS::Ace::ModalUIExtensionProxy>& uiProxy);
103     void OnDestroy();
104     void OnResult(int32_t resultCode, const OHOS::AAFwk::Want& result);
105 
106 protected:
107     std::shared_ptr<RequestInstanceControl> controllerInstance_ = nullptr;
108     std::shared_ptr<RequestAsyncContextBase> reqContext_ = nullptr;
109 
110 private:
111     int32_t sessionId_ = 0;
112 };
113 
114 } // namespace AccessToken
115 } // namespace Security
116 } // namespace OHOS
117 #endif /* ANI_REQUEST_BASE_H */
118