• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "frameworks/core/pipeline/base/element_register.h"
27 #include "foundation/arkui/ace_engine/frameworks/core/common/ace_application_info.h"
28 
29 namespace OHOS::Ace {
30 
31 std::unique_ptr<ViewStackModel> ViewStackModel::instance_ = nullptr;
32 std::mutex ViewStackModel::mutex_;
33 
GetInstance()34 ViewStackModel* ViewStackModel::GetInstance()
35 {
36     if (!instance_) {
37         std::lock_guard<std::mutex> lock(mutex_);
38         if (!instance_) {
39 #ifdef NG_BUILD
40             instance_.reset(new NG::ViewStackModelNG());
41 #else
42             if (Container::IsCurrentUseNewPipeline()) {
43                 instance_.reset(new NG::ViewStackModelNG());
44             } else {
45                 instance_.reset(new Framework::ViewStackModelImpl());
46             }
47 #endif
48         }
49     }
50     return instance_.get();
51 }
52 
53 } // namespace OHOS::Ace
54 namespace OHOS::Ace::Framework {
55 
JSVisualState(const JSCallbackInfo & info)56 void JSViewStackProcessor::JSVisualState(const JSCallbackInfo& info)
57 {
58     LOGD("JSVisualState");
59     if ((info.Length() < 1) || (!info[0]->IsString())) {
60         LOGD("JSVisualState: is not a string.");
61         ViewStackModel::GetInstance()->ClearVisualState();
62         return;
63     }
64 
65     std::string state = info[0]->ToString();
66     VisualState visualState = JSViewStackProcessor::StringToVisualState(state);
67     ViewStackModel::GetInstance()->SetVisualState(visualState);
68 }
69 
70 // public static emthods exposed to JS
JSBind(BindingTarget globalObj)71 void JSViewStackProcessor::JSBind(BindingTarget globalObj)
72 {
73     LOGD("JSViewStackProcessor::Bind");
74     JSClass<JSViewStackProcessor>::Declare("ViewStackProcessor");
75     MethodOptions opt = MethodOptions::NONE;
76 
77     JSClass<JSViewStackProcessor>::StaticMethod(
78         "AllocateNewElmetIdForNextComponent", &JSViewStackProcessor::JsAllocateNewElmetIdForNextComponent, opt);
79     JSClass<JSViewStackProcessor>::StaticMethod(
80         "StartGetAccessRecordingFor", &JSViewStackProcessor::JsStartGetAccessRecordingFor, opt);
81     JSClass<JSViewStackProcessor>::StaticMethod(
82         "SetElmtIdToAccountFor", &JSViewStackProcessor::JsSetElmtIdToAccountFor, opt);
83     JSClass<JSViewStackProcessor>::StaticMethod(
84         "GetElmtIdToAccountFor", &JSViewStackProcessor::JsGetElmtIdToAccountFor, opt);
85     JSClass<JSViewStackProcessor>::StaticMethod(
86         "StopGetAccessRecording", &JSViewStackProcessor::JsStopGetAccessRecording, opt);
87     JSClass<JSViewStackProcessor>::StaticMethod(
88         "ImplicitPopBeforeContinue", &JSViewStackProcessor::JsImplicitPopBeforeContinue, opt);
89     JSClass<JSViewStackProcessor>::StaticMethod("visualState", JSVisualState, opt);
90     JSClass<JSViewStackProcessor>::StaticMethod("MakeUniqueId", &JSViewStackProcessor::JSMakeUniqueId, opt);
91     JSClass<JSViewStackProcessor>::StaticMethod("UsesNewPipeline", &JSViewStackProcessor::JsUsesNewPipeline, opt);
92     JSClass<JSViewStackProcessor>::StaticMethod("getApiVersion", &JSViewStackProcessor::JsGetApiVersion, opt);
93     JSClass<JSViewStackProcessor>::StaticMethod("GetAndPushFrameNode", &JSViewStackProcessor::JsGetAndPushFrameNode);
94     JSClass<JSViewStackProcessor>::StaticMethod("moveDeletedElmtIds", &JSViewStackProcessor::JsMoveDeletedElmtIds);
95     JSClass<JSViewStackProcessor>::Bind<>(globalObj);
96 }
97 
StringToVisualState(const std::string & stateString)98 VisualState JSViewStackProcessor::StringToVisualState(const std::string& stateString)
99 {
100     if (stateString == "normal") {
101         return VisualState::NORMAL;
102     }
103     if (stateString == "focused") {
104         return VisualState::FOCUSED;
105     }
106     if (stateString == "pressed" || stateString == "clicked") {
107         return VisualState::PRESSED;
108     }
109     if (stateString == "disabled") {
110         return VisualState::DISABLED;
111     }
112     if (stateString == "hover") {
113         return VisualState::HOVER;
114     }
115     if (stateString == "selected") {
116         return VisualState::SELECTED;
117     }
118     LOGE("Unknown visual state \"%{public}s\", resetting to UNDEFINED", stateString.c_str());
119     return VisualState::NOTSET;
120 }
121 
JsStartGetAccessRecordingFor(ElementIdType elmtId)122 void JSViewStackProcessor::JsStartGetAccessRecordingFor(ElementIdType elmtId)
123 {
124     ViewStackModel::GetInstance()->StartGetAccessRecordingFor(elmtId);
125 }
126 
JsGetElmtIdToAccountFor()127 int32_t JSViewStackProcessor::JsGetElmtIdToAccountFor()
128 {
129     return ViewStackModel::GetInstance()->GetElmtIdToAccountFor();
130 }
131 
JsSetElmtIdToAccountFor(ElementIdType elmtId)132 void JSViewStackProcessor::JsSetElmtIdToAccountFor(ElementIdType elmtId)
133 {
134     ViewStackModel::GetInstance()->SetElmtIdToAccountFor(elmtId);
135 }
136 
JsStopGetAccessRecording()137 void JSViewStackProcessor::JsStopGetAccessRecording()
138 {
139     return ViewStackModel::GetInstance()->StopGetAccessRecording();
140 }
141 
JsImplicitPopBeforeContinue()142 void JSViewStackProcessor::JsImplicitPopBeforeContinue()
143 {
144     ViewStackModel::GetInstance()->ImplicitPopBeforeContinue();
145 }
146 
JSMakeUniqueId(const JSCallbackInfo & info)147 void JSViewStackProcessor::JSMakeUniqueId(const JSCallbackInfo& info)
148 {
149     const auto result = ElementRegister::GetInstance()->MakeUniqueId();
150     info.SetReturnValue(JSRef<JSVal>::Make(ToJSValue(result)));
151 }
152 
JsMoveDeletedElmtIds(const JSCallbackInfo & info)153 void JSViewStackProcessor::JsMoveDeletedElmtIds(const JSCallbackInfo& info)
154 {
155     LOGD("JSViewStackProcessor, moving elmtIds of all deleted Elements from ElementRegister to JS caller:");
156     if (!info[0]->IsArray()) {
157         LOGE("info[0] is not array.");
158         return;
159     }
160     JSRef<JSArray> jsArr = JSRef<JSArray>::Cast(info[0]);
161 
162     RemovedElementsType removedElements;
163     ElementRegister::GetInstance()->MoveRemovedItems(removedElements);
164     size_t index = jsArr->Length();
165     for (const auto& rmElmtId : removedElements) {
166         // TS Object of type RemovedElementInfo:
167         JSRef<JSObject> jsObject = JSRef<JSObject>::New();
168         jsObject->SetPropertyObject("elmtId", JSRef<JSVal>::Make(ToJSValue(rmElmtId.first)));
169         jsObject->SetPropertyObject("tag", JSRef<JSVal>::Make(ToJSValue(rmElmtId.second)));
170         jsArr->SetValueAt(index++, jsObject);
171     }
172 }
173 
174 /**
175  * return true of current Container uses new Pipeline
176  */
JsUsesNewPipeline()177 bool JSViewStackProcessor::JsUsesNewPipeline()
178 {
179     return Container::IsCurrentUseNewPipeline();
180 }
181 
182 /**
183  * return the API version specified in the manifest.json
184  */
JsGetApiVersion()185 int32_t JSViewStackProcessor::JsGetApiVersion()
186 {
187     return AceApplicationInfo::GetInstance().GetApiTargetVersion();
188 }
189 
JsGetAndPushFrameNode(const JSCallbackInfo & info)190 void JSViewStackProcessor::JsGetAndPushFrameNode(const JSCallbackInfo& info)
191 {
192     if (info.Length() < 2) {
193         LOGE("The arg is wrong, it is supposed to have 2 arguments");
194         return;
195     }
196     if (!info[0]->IsString() || !info[1]->IsNumber()) {
197         LOGE("JsGetAndPushFrameNode() invalid args.");
198         return;
199     }
200     ViewStackModel::GetInstance()->GetAndPushFrameNode(info[0]->ToString(), info[1]->ToNumber<int32_t>());
201 }
202 
203 } // namespace OHOS::Ace::Framework
204