• 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 FOUNDATION_ACE_NAPI_NATIVE_ENGINE_ARK_FINALIZERS_PACK_H
17 #define FOUNDATION_ACE_NAPI_NATIVE_ENGINE_ARK_FINALIZERS_PACK_H
18 
19 #include "ecmascript/napi/include/jsnapi_expo.h"
20 
21 #include "interfaces/inner_api/napi/native_node_api.h"
22 
23 class NativeEngine;
24 
25 using RefFinalizer = std::pair<NapiNativeFinalize, std::tuple<NativeEngine*, void*, void*>>;
26 using RefAsyncFinalizer = std::pair<NapiNativeFinalize, std::pair<void*, void*>>;
27 using ArkFinalizersPackFinishNotify = std::function<void(size_t totalNativeBindingSize)>;
28 using ArkCrashHolder = panda::ArkCrashHolder;
29 
30 class ArkFinalizersPack {
31 public:
32     ArkFinalizersPack() = default;
33     ~ArkFinalizersPack() = default;
34     DEFAULT_MOVE_SEMANTIC(ArkFinalizersPack);
35     DEFAULT_COPY_SEMANTIC(ArkFinalizersPack);
36 
Clear()37     void Clear()
38     {
39         finalizers_.clear();
40         totalNativeBindingSize_ = 0;
41         notify_ = nullptr;
42     }
Empty()43     bool Empty() const
44     {
45         return finalizers_.empty();
46     }
RegisterFinishNotify(ArkFinalizersPackFinishNotify notify)47     void RegisterFinishNotify(ArkFinalizersPackFinishNotify notify)
48     {
49         notify_ = notify;
50     }
GetNumFinalizers()51     size_t GetNumFinalizers() const
52     {
53         return finalizers_.size();
54     }
ProcessAll()55     void ProcessAll() const
56     {
57         INIT_CRASH_HOLDER(holder, "NAPI");
58         for (auto &iter : finalizers_) {
59             NapiNativeFinalize callback = iter.first;
60             auto &[p0, p1, p2] = iter.second;
61             holder.UpdateCallbackPtr(reinterpret_cast<uintptr_t>(callback));
62             callback(reinterpret_cast<napi_env>(p0), p1, p2);
63         }
64         NotifyFinish();
65     }
GetTotalNativeBindingSize()66     size_t GetTotalNativeBindingSize() const
67     {
68         return totalNativeBindingSize_;
69     }
AddFinalizer(RefFinalizer & finalizer,size_t nativeBindingSize)70     void AddFinalizer(RefFinalizer &finalizer, size_t nativeBindingSize)
71     {
72         finalizers_.emplace_back(finalizer);
73         totalNativeBindingSize_ += nativeBindingSize;
74     }
75 private:
NotifyFinish()76     void NotifyFinish() const
77     {
78         if (notify_ != nullptr) {
79             notify_(totalNativeBindingSize_);
80         }
81     }
82     std::vector<RefFinalizer> finalizers_ {};
83     size_t totalNativeBindingSize_ {0};
84     ArkFinalizersPackFinishNotify notify_ {nullptr};
85 };
86 
87 #endif /* FOUNDATION_ACE_NAPI_NATIVE_ENGINE_ARK_FINALIZERS_PACK_H */
88