1 /*
2  * Copyright (c) 2022 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_PRIVACY_KITS_NAPI_CONTEXT_COMMON_H
16 #define INTERFACES_PRIVACY_KITS_NAPI_CONTEXT_COMMON_H
17 
18 #include <memory>
19 #include <uv.h>
20 #include "accesstoken_log.h"
21 #include "active_change_response_info.h"
22 #include "napi/native_api.h"
23 #include "napi/native_node_api.h"
24 #include "napi_common.h"
25 #include "perm_active_status_customized_cbk.h"
26 #include "privacy_kit.h"
27 
28 namespace OHOS {
29 namespace Security {
30 namespace AccessToken {
31 struct PrivacyAsyncWorkData {
32     explicit PrivacyAsyncWorkData(napi_env envValue);
33     virtual ~PrivacyAsyncWorkData();
34 
35     napi_env        env = nullptr;
36     napi_async_work asyncWork = nullptr;
37     napi_deferred   deferred = nullptr;
38     napi_ref        callbackRef = nullptr;
39 };
40 
41 class PermActiveStatusPtr : public std::enable_shared_from_this<PermActiveStatusPtr>,
42     public PermActiveStatusCustomizedCbk {
43 public:
44     explicit PermActiveStatusPtr(const std::vector<std::string>& permList);
45     ~PermActiveStatusPtr() override;
46     void ActiveStatusChangeCallback(ActiveChangeResponse& result) override;
47     void SetEnv(const napi_env& env);
48     void SetCallbackRef(const napi_ref& ref);
49     void DeleteNapiRef();
50 private:
51     napi_env env_ = nullptr;
52     napi_ref ref_ = nullptr;
53 };
54 
55 struct PermActiveStatusWorker {
56     napi_env env = nullptr;
57     napi_ref ref = nullptr;
58     ActiveChangeResponse result;
59     std::shared_ptr<PermActiveStatusPtr> subscriber = nullptr; // this
60 };
61 
62 struct PermActiveChangeContext {
63     virtual ~PermActiveChangeContext();
64 
65     napi_env env = nullptr;
66     napi_ref callbackRef = nullptr;
67     std::string type;
68     std::shared_ptr<PermActiveStatusPtr> subscriber = nullptr;
69     std::thread::id threadId_;
70 };
71 
72 void UvQueueWorkActiveStatusChange(uv_work_t* work, int status);
73 bool ConvertActiveChangeResponse(napi_env env, napi_value value, const ActiveChangeResponse& result);
74 void NotifyChangeResponse(const PermActiveStatusWorker* permActiveStatusData);
75 }  // namespace AccessToken
76 }  // namespace Security
77 }  // namespace OHOS
78 #endif /*  INTERFACES_PRIVACY_KITS_NAPI_CONTEXT_COMMON_H */
79