1 /*
2 * Copyright (c) 2021-2022 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
16 #include "bridge/declarative_frontend/jsview/js_view_stack_processor.h"
17
18 #include "bridge/declarative_frontend/engine/bindings.h"
19 #include "bridge/declarative_frontend/engine/js_types.h"
20 #include "bridge/declarative_frontend/jsview/models/view_stack_model_impl.h"
21 #include "bridge/declarative_frontend/view_stack_processor.h"
22 #include "core/common/container.h"
23 #include "core/components_ng/base/view_stack_model.h"
24 #include "core/components_ng/base/view_stack_model_ng.h"
25 #include "core/components_ng/base/view_stack_processor.h"
26 #include "foundation/arkui/ace_engine/frameworks/core/common/ace_application_info.h"
27 #include "frameworks/core/common/layout_inspector.h"
28 #include "frameworks/core/pipeline/base/element_register.h"
29
30 namespace OHOS::Ace {
31
GetInstance()32 ViewStackModel* ViewStackModel::GetInstance()
33 {
34 #ifdef NG_BUILD
35 static NG::ViewStackModelNG instance;
36 return &instance;
37 #else
38 if (Container::IsCurrentUseNewPipeline()) {
39 static NG::ViewStackModelNG instance;
40 return &instance;
41 } else {
42 static Framework::ViewStackModelImpl instance;
43 return &instance;
44 }
45 #endif
46 }
47
48 } // namespace OHOS::Ace
49 namespace OHOS::Ace::Framework {
50
JSVisualState(const JSCallbackInfo & info)51 void JSViewStackProcessor::JSVisualState(const JSCallbackInfo& info)
52 {
53 if ((info.Length() < 1) || (!info[0]->IsString())) {
54 ViewStackModel::GetInstance()->ClearVisualState();
55 return;
56 }
57
58 std::string state = info[0]->ToString();
59 VisualState visualState = JSViewStackProcessor::StringToVisualState(state);
60 ViewStackModel::GetInstance()->SetVisualState(visualState);
61 }
62
63 // public static emthods exposed to JS
JSBind(BindingTarget globalObj)64 void JSViewStackProcessor::JSBind(BindingTarget globalObj)
65 {
66 JSClass<JSViewStackProcessor>::Declare("ViewStackProcessor");
67 MethodOptions opt = MethodOptions::NONE;
68
69 JSClass<JSViewStackProcessor>::StaticMethod(
70 "AllocateNewElmetIdForNextComponent", &JSViewStackProcessor::JsAllocateNewElmetIdForNextComponent, opt);
71 JSClass<JSViewStackProcessor>::StaticMethod(
72 "StartGetAccessRecordingFor", &JSViewStackProcessor::JsStartGetAccessRecordingFor, opt);
73 JSClass<JSViewStackProcessor>::StaticMethod(
74 "SetElmtIdToAccountFor", &JSViewStackProcessor::JsSetElmtIdToAccountFor, opt);
75 JSClass<JSViewStackProcessor>::StaticMethod(
76 "GetElmtIdToAccountFor", &JSViewStackProcessor::JsGetElmtIdToAccountFor, opt);
77 JSClass<JSViewStackProcessor>::StaticMethod(
78 "StopGetAccessRecording", &JSViewStackProcessor::JsStopGetAccessRecording, opt);
79 JSClass<JSViewStackProcessor>::StaticMethod(
80 "ImplicitPopBeforeContinue", &JSViewStackProcessor::JsImplicitPopBeforeContinue, opt);
81 JSClass<JSViewStackProcessor>::StaticMethod("visualState", JSVisualState, opt);
82 JSClass<JSViewStackProcessor>::StaticMethod("MakeUniqueId", &JSViewStackProcessor::JSMakeUniqueId, opt);
83 JSClass<JSViewStackProcessor>::StaticMethod("UsesNewPipeline", &JSViewStackProcessor::JsUsesNewPipeline, opt);
84 JSClass<JSViewStackProcessor>::StaticMethod("getApiVersion", &JSViewStackProcessor::JsGetApiVersion, opt);
85 JSClass<JSViewStackProcessor>::StaticMethod("GetAndPushFrameNode", &JSViewStackProcessor::JsGetAndPushFrameNode);
86 JSClass<JSViewStackProcessor>::StaticMethod("moveDeletedElmtIds", &JSViewStackProcessor::JsMoveDeletedElmtIds);
87 JSClass<JSViewStackProcessor>::StaticMethod("sendStateInfo", &JSViewStackProcessor::JsSendStateInfo);
88 JSClass<JSViewStackProcessor>::StaticMethod("PushPrebuildCompCmd",
89 &JSViewStackProcessor::JsPushPrebuildCompCmd, opt);
90 JSClass<JSViewStackProcessor>::StaticMethod("CheckIsPrebuildTimeout",
91 &JSViewStackProcessor::JsCheckIsPrebuildTimeout, opt);
92 JSClass<JSViewStackProcessor>::Bind<>(globalObj);
93 }
94
StringToVisualState(const std::string & stateString)95 VisualState JSViewStackProcessor::StringToVisualState(const std::string& stateString)
96 {
97 if (stateString == "normal") {
98 return VisualState::NORMAL;
99 }
100 if (stateString == "focused") {
101 return VisualState::FOCUSED;
102 }
103 if (stateString == "pressed" || stateString == "clicked") {
104 return VisualState::PRESSED;
105 }
106 if (stateString == "disabled") {
107 return VisualState::DISABLED;
108 }
109 if (stateString == "hover") {
110 return VisualState::HOVER;
111 }
112 if (stateString == "selected") {
113 return VisualState::SELECTED;
114 }
115 LOGE("Unknown visual state \"%{public}s\", resetting to UNDEFINED", stateString.c_str());
116 return VisualState::NOTSET;
117 }
118
JsStartGetAccessRecordingFor(ElementIdType elmtId)119 void JSViewStackProcessor::JsStartGetAccessRecordingFor(ElementIdType elmtId)
120 {
121 ViewStackModel::GetInstance()->StartGetAccessRecordingFor(elmtId);
122 }
123
JsGetElmtIdToAccountFor()124 int32_t JSViewStackProcessor::JsGetElmtIdToAccountFor()
125 {
126 return ViewStackModel::GetInstance()->GetElmtIdToAccountFor();
127 }
128
JsSetElmtIdToAccountFor(ElementIdType elmtId)129 void JSViewStackProcessor::JsSetElmtIdToAccountFor(ElementIdType elmtId)
130 {
131 ViewStackModel::GetInstance()->SetElmtIdToAccountFor(elmtId);
132 }
133
JsStopGetAccessRecording()134 void JSViewStackProcessor::JsStopGetAccessRecording()
135 {
136 return ViewStackModel::GetInstance()->StopGetAccessRecording();
137 }
138
JsImplicitPopBeforeContinue()139 void JSViewStackProcessor::JsImplicitPopBeforeContinue()
140 {
141 ViewStackModel::GetInstance()->ImplicitPopBeforeContinue();
142 }
143
JSMakeUniqueId(const JSCallbackInfo & info)144 void JSViewStackProcessor::JSMakeUniqueId(const JSCallbackInfo& info)
145 {
146 const auto result = ElementRegister::GetInstance()->MakeUniqueId();
147 info.SetReturnValue(JSRef<JSVal>::Make(ToJSValue(result)));
148 }
JsMoveDeletedElmtIds(const JSCallbackInfo & info)149 void JSViewStackProcessor::JsMoveDeletedElmtIds(const JSCallbackInfo& info)
150 {
151 auto jsArrInfo = info[0];
152 if (!jsArrInfo->IsArray()) {
153 return;
154 }
155 JSRef<JSArray> jsArr = JSRef<JSArray>::Cast(jsArrInfo);
156
157 RemovedElementsType removedElements;
158 ElementRegister::GetInstance()->MoveRemovedItems(removedElements);
159 size_t index = jsArr->Length();
160 for (const auto& rmElmtId : removedElements) {
161 jsArr->SetValueAt(index++, JSRef<JSVal>::Make(ToJSValue(rmElmtId)));
162 }
163 }
164
JsSendStateInfo(const std::string & stateInfo)165 void JSViewStackProcessor::JsSendStateInfo(const std::string& stateInfo)
166 {
167 #if defined(PREVIEW) || !defined(OHOS_PLATFORM)
168 return;
169 #else
170 if (!LayoutInspector::GetStateProfilerStatus()) {
171 return;
172 }
173 auto container = Container::CurrentSafely();
174 CHECK_NULL_VOID(container);
175 auto pipeline = container->GetPipelineContext();
176 CHECK_NULL_VOID(pipeline);
177 auto info = JsonUtil::ParseJsonString(stateInfo);
178 info->Put("timeStamp", GetCurrentTimestampMicroSecond());
179 info->Put("vsyncID", (int32_t)pipeline->GetFrameCount());
180 info->Put("processID", getpid());
181 info->Put("windowID", (int32_t)pipeline->GetWindowId());
182 TAG_LOGD(AceLogTag::ACE_STATE_MGMT, "ArkUI SendStateInfo %{public}s", info->ToString().c_str());
183 LayoutInspector::SendMessage(info->ToString());
184 #endif
185 }
186
187 /**
188 * return true of current Container uses new Pipeline
189 */
JsUsesNewPipeline()190 bool JSViewStackProcessor::JsUsesNewPipeline()
191 {
192 auto container = Container::Current();
193 return container ? container->IsUseNewPipeline() : AceApplicationInfo::GetInstance().IsUseNewPipeline();
194 }
195
196 /**
197 * return the API version specified in the manifest.json
198 */
JsGetApiVersion()199 int32_t JSViewStackProcessor::JsGetApiVersion()
200 {
201 return AceApplicationInfo::GetInstance().GetApiTargetVersion();
202 }
203
JsGetAndPushFrameNode(const JSCallbackInfo & info)204 void JSViewStackProcessor::JsGetAndPushFrameNode(const JSCallbackInfo& info)
205 {
206 if (info.Length() < 2) {
207 return;
208 }
209 if (!info[0]->IsString() || !info[1]->IsNumber()) {
210 return;
211 }
212 ViewStackModel::GetInstance()->GetAndPushFrameNode(info[0]->ToString(), info[1]->ToNumber<int32_t>());
213 }
214
JsPushPrebuildCompCmd(const JSCallbackInfo & info)215 void JSViewStackProcessor::JsPushPrebuildCompCmd(const JSCallbackInfo& info)
216 {
217 ViewStackModel::GetInstance()->PushPrebuildCompCmd();
218 }
219
JsCheckIsPrebuildTimeout()220 bool JSViewStackProcessor::JsCheckIsPrebuildTimeout()
221 {
222 return ViewStackModel::GetInstance()->CheckIsPrebuildTimeout();
223 }
224 } // namespace OHOS::Ace::Framework
225