• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     if (!container) {
57         LOGE("JSDumpRegister::AddListener container is null!");
58         return;
59     }
60     auto pipelineBase = container->GetPipelineContext();
61     auto pipelineContext = AceType::DynamicCast<NG::PipelineContext>(pipelineBase);
62     if (!pipelineContext) {
63         LOGE("JSDumpRegister::AddListener pipeline context is null!");
64         return;
65     }
66     JSRef<JSFunc> cb = JSRef<JSFunc>::Cast(info[0]);
67     auto listenerWrapper = [ctx = info.GetExecutionContext(), cb](const std::vector<std::string>& params) {
68         JAVASCRIPT_EXECUTION_SCOPE(ctx);
69         JSRef<JSArray> arr = JSRef<JSArray>::New();
70         for (size_t i = 0; i < params.size(); ++i) {
71             arr->SetValueAt(i, JSRef<JSVal>::Make(ToJSValue(params.at(i))));
72         }
73         JSRef<JSVal> argv = arr;
74         cb->Call(JSRef<JSVal>(), 1, &argv);
75     };
76     auto stageManager = pipelineContext->GetStageManager();
77     CHECK_NULL_VOID(stageManager);
78     auto pageNode = stageManager->GetLastPage();
79     CHECK_NULL_VOID(pageNode);
80     auto pagePattern = pageNode->GetPattern<NG::PagePattern>();
81     CHECK_NULL_VOID(pagePattern);
82     pagePattern->RegisterDumpInfoListener(listenerWrapper);
83 }
84 } // namespace OHOS::Ace::Framework
85