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 #include "native_callback_scope_manager.h" 17 #include "native_engine/native_engine.h" 18 NativeCallbackScope(NativeEngine * env)19NativeCallbackScope::NativeCallbackScope(NativeEngine* env) 20 { 21 env_ = env; 22 } 23 ~NativeCallbackScope()24NativeCallbackScope::~NativeCallbackScope() 25 { 26 Close(); 27 } 28 Close()29void NativeCallbackScope::Close() 30 { 31 if (!closed_) { 32 closed_ = true; 33 if (env_->IsStopping()) { 34 MarkAsFailed(); 35 } 36 } 37 } 38 NativeCallbackScopeManager()39NativeCallbackScopeManager::NativeCallbackScopeManager() {} 40 ~NativeCallbackScopeManager()41NativeCallbackScopeManager::~NativeCallbackScopeManager() {} 42 Open(NativeEngine * env)43NativeCallbackScope* NativeCallbackScopeManager::Open(NativeEngine* env) 44 { 45 NativeCallbackScope* scope = new (std::nothrow)NativeCallbackScope(env); 46 47 if (scope) { 48 asyncCallbackScopeDepth_++; 49 return scope; 50 } else { 51 return nullptr; 52 } 53 } 54 Close(NativeCallbackScope * scope)55void NativeCallbackScopeManager::Close(NativeCallbackScope* scope) 56 { 57 if (scope != nullptr) { 58 delete scope; 59 } 60 asyncCallbackScopeDepth_--; 61 } 62 IncrementOpenCallbackScopes()63size_t NativeCallbackScopeManager::IncrementOpenCallbackScopes() 64 { 65 openCallbackScopes_++; 66 return openCallbackScopes_; 67 } 68 DecrementOpenCallbackScopes()69size_t NativeCallbackScopeManager::DecrementOpenCallbackScopes() 70 { 71 openCallbackScopes_--; 72 return openCallbackScopes_; 73 } 74