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::GetCurrentEngineSafely(); 54 if (!jsEngine) { 55 return; 56 } 57 jsEngine->UnregisterLayoutInspectorCallback(layoutEvent_, componentId_); 58 jsEngine->UnregisterDrawInspectorCallback(drawEvent_, componentId_); 59 } 60 61 void Initialize(napi_env env, napi_value thisVar); 62 void callUserFunction(std::list<napi_ref>& cbList); 63 std::list<napi_ref>::iterator FindCbList(napi_value cb, CalloutType calloutType); 64 void NapiSerializer(napi_env& env, napi_value& result); 65 void AddCallbackToList( 66 napi_value cb, std::list<napi_ref>& cbList, CalloutType calloutType, napi_env env, napi_handle_scope scope); 67 void DeleteCallbackFromList( 68 size_t argc, std::list<napi_ref>& cbList, CalloutType calloutType, napi_value cb, napi_env env); 69 70 RefPtr<InspectorEvent> layoutEvent_; 71 RefPtr<InspectorEvent> drawEvent_; 72 std::string componentId_; 73 std::list<napi_ref> cbLayoutList_; 74 std::list<napi_ref> cbDrawList_; 75 napi_ref thisVarRef_ = nullptr; 76 77 private: 78 void FunctionOn(napi_env& env, napi_value result, const char* funName); 79 void FunctionOff(napi_env& env, napi_value result, const char* funName); 80 napi_env env_ = nullptr; 81 }; 82 } // namespace OHOS::Ace::Napi 83 #endif 84