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