• 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 
16 #ifndef OHOS_ABILITY_ACCESS_AT_MANAGER_IMPL_H
17 #define OHOS_ABILITY_ACCESS_AT_MANAGER_IMPL_H
18 
19 #include <cstdint>
20 #include <cstdio>
21 #include <cstring>
22 #include <pthread.h>
23 #include <unistd.h>
24 #include <thread>
25 
26 #include "accesstoken_kit.h"
27 #include "cj_common_ffi.h"
28 #include "cj_lambda.h"
29 #include "ffi_remote_data.h"
30 #include "permission_grant_info.h"
31 #include "token_callback_stub.h"
32 #include "ui_content.h"
33 #include "ui_extension_context.h"
34 
35 struct CArrBool {
36     void* head;
37     int64_t size;
38 };
39 
40 struct CPermissionRequestResult {
41     CArrString permissions;
42     CArrI32 authResults;
43     CArrBool dialogShownResults;
44 };
45 
46 struct RetDataCPermissionRequestResult {
47     int32_t code;
48     CPermissionRequestResult data;
49 };
50 
51 namespace OHOS {
52 namespace CJSystemapi {
53 
54 using namespace OHOS::Security::AccessToken;
55 
56 const int AT_PERM_OPERA_FAIL = -1;
57 const int AT_PERM_OPERA_SUCC = 0;
58 const int32_t PARAM_DEFAULT_VALUE = -1;
59 
60 struct GrantStatusCache {
61     int32_t status;
62     std::string paramValue;
63 };
64 
65 struct PermissionParamCache {
66     long long sysCommitIdCache = PARAM_DEFAULT_VALUE;
67     int32_t commitIdCache = PARAM_DEFAULT_VALUE;
68     int32_t handle = PARAM_DEFAULT_VALUE;
69     std::string sysParamCache;
70 };
71 
72 struct CPermStateChangeInfo {
73     int32_t permStateChangeType;
74     AccessTokenID tokenID;
75     char* permissionName;
76 };
77 
78 struct RequestAsyncContext {
79     AccessTokenID tokenId = 0;
80     bool needDynamicRequest = true;
81     int32_t result = AT_PERM_OPERA_SUCC;
82     std::vector<std::string> permissionList;
83     std::vector<int32_t> permissionsState;
84     std::vector<bool> dialogShownResults;
85     PermissionGrantInfo info;
86     std::shared_ptr<AbilityRuntime::AbilityContext> abilityContext;
87     std::shared_ptr<AbilityRuntime::UIExtensionContext> uiExtensionContext;
88     bool uiAbilityFlag = false;
89     std::function<void(RetDataCPermissionRequestResult)> callbackRef =  nullptr;
90 };
91 
92 class UIExtensionCallback {
93 public:
94     explicit UIExtensionCallback(const std::shared_ptr<RequestAsyncContext>& reqContext);
95     ~UIExtensionCallback();
96     void SetSessionId(int32_t sessionId);
97     void OnRelease(int32_t releaseCode);
98     void OnResult(int32_t resultCode, const OHOS::AAFwk::Want& result);
99     void OnReceive(const OHOS::AAFwk::WantParams& request);
100     void OnError(int32_t code, const std::string& name, const std::string& message);
101     void OnRemoteReady(const std::shared_ptr<OHOS::Ace::ModalUIExtensionProxy>& uiProxy);
102     void OnDestroy();
103     void ReleaseOrErrorHandle(int32_t code);
104 
105 private:
106     int32_t sessionId_ = 0;
107     std::shared_ptr<RequestAsyncContext> reqContext_ = nullptr;
108 };
109 
110 class AuthorizationResult : public Security::AccessToken::TokenCallbackStub {
111 public:
AuthorizationResult(std::shared_ptr<RequestAsyncContext> & data)112     explicit AuthorizationResult(std::shared_ptr<RequestAsyncContext>& data) : data_(data) {}
113     ~AuthorizationResult() override = default;
114     void GrantResultsCallback(const std::vector<std::string>& permissions,
115         const std::vector<int>& grantResults) override;
116 
117 private:
118     std::shared_ptr<RequestAsyncContext> data_ = nullptr;
119 };
120 
121 class RegisterPermStateChangeScopePtr : public std::enable_shared_from_this<RegisterPermStateChangeScopePtr>,
122     public PermStateChangeCallbackCustomize {
123 public:
124     explicit RegisterPermStateChangeScopePtr(const PermStateChangeScope& subscribeInfo);
125     ~RegisterPermStateChangeScopePtr() override;
126     void PermStateChangeCallback(PermStateChangeInfo& result) override;
127     void SetCallbackRef(const std::function<void(CPermStateChangeInfo)>& ref);
128     void SetValid(bool valid);
129 
130 private:
131     std::function<void(CPermStateChangeInfo)> ref_;
132     bool valid_ = true;
133     std::mutex validMutex_;
134 };
135 
136 struct PermStateChangeContext {
137     virtual ~PermStateChangeContext();
138     int64_t callbackRef =  0;
139     int32_t errCode = 0;
140     std::string permStateChangeType;
141     AccessTokenKit* accessTokenKit = nullptr;
142     std::thread::id threadId;
143     std::shared_ptr<RegisterPermStateChangeScopePtr> subscriber = nullptr;
144 };
145 
146 typedef PermStateChangeContext RegisterPermStateChangeInfo;
147 
148 struct UnregisterPermStateChangeInfo : public PermStateChangeContext {
149     PermStateChangeScope scopeInfo;
150 };
151 
152 class AtManagerImpl {
153 public:
154     static int32_t VerifyAccessTokenSync(unsigned int tokenID, const char* cPermissionName);
155     static int32_t GrantUserGrantedPermission(unsigned int tokenID, const char* cPermissionName,
156     unsigned int permissionFlags);
157     static int32_t RevokeUserGrantedPermission(unsigned int tokenID, const char* cPermissionName,
158     unsigned int permissionFlags);
159     static int32_t RegisterPermStateChangeCallback(
160         const char* cType,
161         CArrUI32 cTokenIDList,
162         CArrString cPermissionList,
163         int64_t callbackRef);
164     static int32_t UnregisterPermStateChangeCallback(
165         const char* cType, CArrUI32 cTokenIDList,
166         CArrString cPermissionList,
167         int64_t callbackRef);
168     static void RequestPermissionsFromUser(OHOS::AbilityRuntime::Context* context, CArrString cPermissionList,
169         const std::function<void(RetDataCPermissionRequestResult)>& callbackRef);
170 private:
171     static std::string GetPermParamValue();
172     static int32_t FillPermStateChangeInfo(
173         const std::string& type,
174         CArrUI32 cTokenIDList,
175         CArrString cPermissionList,
176         int64_t callback,
177         RegisterPermStateChangeInfo& registerPermStateChangeInfo);
178     static int32_t FillUnregisterPermStateChangeInfo(
179         const std::string& type,
180         CArrUI32 cTokenIDList,
181         CArrString cPermissionList,
182         int64_t callback,
183         UnregisterPermStateChangeInfo& unregisterPermStateChangeInfo);
184     static bool IsExistRegister(const RegisterPermStateChangeInfo* registerPermStateChangeInfo);
185     static bool IsDynamicRequest(const std::vector<std::string>& permissions,
186         std::vector<int32_t>& permissionsState, std::vector<bool>& dialogShownResults, PermissionGrantInfo& info);
187     static bool FindAndGetSubscriberInVector(UnregisterPermStateChangeInfo* unregisterPermStateChangeInfo,
188         std::vector<RegisterPermStateChangeInfo*>& batchPermStateChangeRegisters);
189     static void DeleteRegisterFromVector(const PermStateChangeScope& scopeInfo, int64_t subscriberRef);
190     static bool ParseRequestPermissionFromUser(OHOS::AbilityRuntime::Context* context, CArrString cPermissionList,
191         const std::function<void(RetDataCPermissionRequestResult)>& callbackRef,
192         const std::shared_ptr<RequestAsyncContext>& asyncContext);
193 };
194 }
195 }
196 
197 #endif