1 /* 2 * Copyright (c) 2021 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_CALLBACK_SCOPE_MANAGER_NATIVE_CALLBACK_SCOPE_MANAGER_H 17 #define FOUNDATION_ACE_NAPI_CALLBACK_SCOPE_MANAGER_NATIVE_CALLBACK_SCOPE_MANAGER_H 18 19 #include <cstddef> 20 21 class NativeEngine; 22 23 class NativeCallbackScope { 24 public: 25 explicit NativeCallbackScope(NativeEngine* env); 26 virtual ~NativeCallbackScope(); 27 void Close(); 28 Failed()29 inline bool Failed() const 30 { 31 return failed_; 32 } MarkAsFailed()33 inline void MarkAsFailed() 34 { 35 failed_ = true; 36 } 37 38 private: 39 bool closed_ = false; 40 bool failed_ = false; 41 NativeEngine* env_ = nullptr; 42 }; 43 44 class NativeCallbackScopeManager { 45 public: 46 NativeCallbackScopeManager(); 47 virtual ~NativeCallbackScopeManager(); 48 49 NativeCallbackScope* Open(NativeEngine* env); 50 void Close(NativeCallbackScope* scope); 51 52 size_t IncrementOpenCallbackScopes(); 53 size_t DecrementOpenCallbackScopes(); 54 GetOpenCallbackScopes()55 size_t GetOpenCallbackScopes() const 56 { 57 return openCallbackScopes_; 58 } 59 GetAsyncCallbackScopeDepth()60 size_t GetAsyncCallbackScopeDepth() const 61 { 62 return asyncCallbackScopeDepth_; 63 } 64 65 private: 66 size_t openCallbackScopes_ = 0; 67 size_t asyncCallbackScopeDepth_ = 0; 68 }; 69 70 #endif /* FOUNDATION_ACE_NAPI_CALLBACK_SCOPE_MANAGER_NATIVE_CALLBACK_SCOPE_MANAGER_H */