1 /* 2 * Copyright (c) 2023 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 FOUNDATION_ACE_INTERFACE_INNERKITS_INSPECTOR_H 16 #define FOUNDATION_ACE_INTERFACE_INNERKITS_INSPECTOR_H 17 18 #include "napi/native_api.h" 19 #include "napi/native_common.h" 20 #include "napi/native_node_api.h" 21 22 #include "base/log/log.h" 23 #include "bridge/common/utils/engine_helper.h" 24 #include "bridge/js_frontend/engine/common/js_engine.h" 25 26 namespace OHOS::Ace::Napi { // namespace 27 using namespace OHOS::Ace::Framework; 28 29 enum class CalloutType { 30 LAYOUTCALLOUT = 0, 31 DRAWCALLOUT, 32 UNKNOW, 33 }; 34 35 class ComponentObserver { 36 public: ComponentObserver(const std::string & componentId)37 explicit ComponentObserver(const std::string& componentId) : componentId_(componentId) {} ~ComponentObserver()38 ~ComponentObserver() 39 { 40 if (env_ == nullptr) { 41 return; 42 } 43 for (auto& layoutitem : cbLayoutList_) { 44 napi_delete_reference(env_, layoutitem); 45 } 46 for (auto& drawitem : cbDrawList_) { 47 napi_delete_reference(env_, drawitem); 48 } 49 if (thisVarRef_ != nullptr) { 50 napi_delete_reference(env_, thisVarRef_); 51 } 52 53 auto jsEngine = EngineHelper::GetCurrentEngine(); 54 if (!jsEngine) { 55 LOGE("get jsEngine failed"); 56 return; 57 } 58 jsEngine->UnregisterLayoutInspectorCallback(layoutEvent_, componentId_); 59 jsEngine->UnregisterDrawInspectorCallback(drawEvent_, componentId_); 60 } 61 62 void Initialize(napi_env env, napi_value thisVar); 63 void callUserFunction(std::list<napi_ref>& cbList); 64 std::list<napi_ref>::iterator FindCbList(napi_value cb, CalloutType calloutType); 65 void NapiSerializer(napi_env& env, napi_value& result); 66 void AddCallbackToList( 67 napi_value cb, std::list<napi_ref>& cbList, CalloutType calloutType, napi_env env, napi_handle_scope scope); 68 void DeleteCallbackFromList( 69 size_t argc, std::list<napi_ref>& cbList, CalloutType calloutType, napi_value cb, napi_env env); 70 71 RefPtr<InspectorEvent> layoutEvent_; 72 RefPtr<InspectorEvent> drawEvent_; 73 std::string componentId_; 74 std::list<napi_ref> cbLayoutList_; 75 std::list<napi_ref> cbDrawList_; 76 napi_ref thisVarRef_ = nullptr; 77 78 private: 79 void FunctionOn(napi_env& env, napi_value result, const char* funName); 80 void FunctionOff(napi_env& env, napi_value result, const char* funName); 81 napi_env env_ = nullptr; 82 }; 83 } // namespace OHOS::Ace::Napi 84 #endif 85