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 "frameworks/bridge/declarative_frontend/engine/v8/v8_function_destroy_helper.h"
17
18 namespace OHOS::Ace::Framework {
19
20 std::unordered_map<v8::Isolate*,
21 std::vector<std::function<void(v8::Isolate*)>>> V8FunctionDestroyCallbackHelper::destroyCallbacks_;
22
23 std::mutex V8FunctionDestroyCallbackHelper::helperMutex_;
24
AddDestroyCallback(std::function<void (v8::Isolate *)> callback,v8::Isolate * isolate)25 void V8FunctionDestroyCallbackHelper::AddDestroyCallback(std::function<void(v8::Isolate*)> callback,
26 v8::Isolate* isolate)
27 {
28 std::lock_guard<std::mutex> lock(helperMutex_);
29 if (destroyCallbacks_.find(isolate) != destroyCallbacks_.end()) {
30 destroyCallbacks_[isolate].emplace_back(callback);
31 } else {
32 std::vector<std::function<void(v8::Isolate*)>> destroyCallbacks;
33 destroyCallbacks.emplace_back(callback);
34 destroyCallbacks_[isolate] = destroyCallbacks;
35 }
36 }
37
DeleteFunctionTemplate(v8::Isolate * isolate)38 void V8FunctionDestroyCallbackHelper::DeleteFunctionTemplate(v8::Isolate* isolate)
39 {
40 LOGI("Erase function template, destroy callback size %{public}d", static_cast<int32_t>(destroyCallbacks_.size()));
41 std::lock_guard<std::mutex> lock(helperMutex_);
42 if (destroyCallbacks_.find(isolate) == destroyCallbacks_.end()) {
43 return;
44 }
45 auto& destroyCallbacks = destroyCallbacks_[isolate];
46 for (auto&& destroyCallback : destroyCallbacks) {
47 if (destroyCallback) {
48 destroyCallback(isolate);
49 }
50 }
51 destroyCallbacks_.erase(isolate);
52 }
53
54 } // namespace OHOS::Ace::Framework
55