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 #include "frameworks/bridge/declarative_frontend/jsview/js_dump_log.h"
16 
17 #include "base/log/dump_log.h"
18 #include "base/log/log_wrapper.h"
19 #include "bridge/declarative_frontend/engine/js_execution_scope_defines.h"
20 #include "core/pipeline_ng/pipeline_context.h"
21 #include "frameworks/core/components_ng/pattern/stage/page_pattern.h"
22 
23 namespace OHOS::Ace::Framework {
JSBind(BindingTarget globalObj)24 void JSDumpLog::JSBind(BindingTarget globalObj)
25 {
26     JSClass<JSDumpLog>::Declare("DumpLog");
27     JSClass<JSDumpLog>::StaticMethod("print", &JSDumpLog::Print);
28     JSClass<JSDumpLog>::Bind(globalObj);
29 }
30 
Print(const JSCallbackInfo & info)31 void JSDumpLog::Print(const JSCallbackInfo& info)
32 {
33     if (!info[0]->IsNumber() || !info[1]->IsString()) {
34         LOGE("JSDumpLog::Print invalid arguments, expected a number and a string");
35         return;
36     }
37 
38     DumpLog::GetInstance().Print(info[0]->ToNumber<int32_t>(), info[1]->ToString());
39 }
40 
JSBind(BindingTarget globalObj)41 void JSDumpRegister::JSBind(BindingTarget globalObj)
42 {
43     JSClass<JSDumpRegister>::Declare("DumpRegister");
44     JSClass<JSDumpRegister>::StaticMethod("addListener", &JSDumpRegister::AddListener);
45     JSClass<JSDumpRegister>::Bind(globalObj);
46 }
47 
AddListener(const JSCallbackInfo & info)48 void JSDumpRegister::AddListener(const JSCallbackInfo& info)
49 {
50     if (!info[0]->IsFunction()) {
51         LOGE("JSDumpRegister::AddListener not a function!");
52         return;
53     }
54 
55     auto container = Container::Current();
56     auto pipelineBase = container->GetPipelineContext();
57     auto pipelineContext = AceType::DynamicCast<NG::PipelineContext>(pipelineBase);
58     if (!pipelineContext) {
59         LOGE("JSDumpRegister::AddListener pipeline context is null!");
60         return;
61     }
62     JSRef<JSFunc> cb = JSRef<JSFunc>::Cast(info[0]);
63     auto listenerWrapper = [ctx = info.GetExecutionContext(), cb](const std::vector<std::string>& params) {
64         JAVASCRIPT_EXECUTION_SCOPE(ctx);
65         JSRef<JSArray> arr = JSRef<JSArray>::New();
66         for (size_t i = 0; i < params.size(); ++i) {
67             arr->SetValueAt(i, JSRef<JSVal>::Make(ToJSValue(params.at(i))));
68         }
69         JSRef<JSVal> argv = arr;
70         cb->Call(JSRef<JSVal>(), 1, &argv);
71     };
72     auto stageManager = pipelineContext->GetStageManager();
73     CHECK_NULL_VOID(stageManager);
74     auto pageNode = stageManager->GetLastPage();
75     CHECK_NULL_VOID(pageNode);
76     auto pagePattern = pageNode->GetPattern<NG::PagePattern>();
77     CHECK_NULL_VOID(pagePattern);
78     pagePattern->RegisterDumpInfoListener(listenerWrapper);
79 }
80 } // namespace OHOS::Ace::Framework
81