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 "bridge/common/utils/engine_helper.h"
23 #include "bridge/js_frontend/engine/common/js_engine.h"
24 
25 namespace OHOS::Ace::Napi { // namespace
26 using namespace OHOS::Ace::Framework;
27 
28 enum class CalloutType {
29     LAYOUTCALLOUT = 0,
30     DRAWCALLOUT,
31     UNKNOW,
32 };
33 
34 class ComponentObserver {
35 public:
ComponentObserver(const std::string & componentId)36     explicit ComponentObserver(const std::string& componentId) : componentId_(componentId) {}
~ComponentObserver()37     ~ComponentObserver() {}
38 
39     void Initialize(napi_env env, napi_value thisVar);
40     void callUserFunction(napi_env env, std::list<napi_ref>& cbList);
41     std::list<napi_ref>::iterator FindCbList(napi_env env, napi_value cb, CalloutType calloutType);
42     void NapiSerializer(napi_env& env, napi_value& result);
43     void AddCallbackToList(
44         napi_value cb, std::list<napi_ref>& cbList, CalloutType calloutType, napi_env env, napi_handle_scope scope);
45     void DeleteCallbackFromList(
46         size_t argc, std::list<napi_ref>& cbList, CalloutType calloutType, napi_value cb, napi_env env);
47     void Destroy(napi_env env);
48 
49     RefPtr<InspectorEvent> layoutEvent_;
50     RefPtr<InspectorEvent> drawEvent_;
51     std::string componentId_;
52     std::list<napi_ref> cbLayoutList_;
53     std::list<napi_ref> cbDrawList_;
SetEngine(const RefPtr<Framework::JsEngine> & engine)54     void SetEngine(const RefPtr<Framework::JsEngine>& engine)
55     {
56         weakEngine_ = engine;
57     }
58 
59 private:
60     void FunctionOn(napi_env& env, napi_value result, const char* funName);
61     void FunctionOff(napi_env& env, napi_value result, const char* funName);
62     WeakPtr<Framework::JsEngine> weakEngine_;
63 };
64 } // namespace OHOS::Ace::Napi
65 #endif
66