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 #ifndef FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_JS_VIEW_JS_NAVIGATION_STACK_H 16 #define FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_JS_VIEW_JS_NAVIGATION_STACK_H 17 18 #include <functional> 19 #include <stdint.h> 20 21 #include "bridge/declarative_frontend/engine/js_types.h" 22 #include "bridge/declarative_frontend/jsview/js_utils.h" 23 #include "core/components_ng/pattern/navigation/navigation_stack.h" 24 #include "core/components_ng/pattern/navrouter/navdestination_group_node.h" 25 26 namespace OHOS::Ace::Framework { 27 28 struct HomePathInfo { 29 std::string name; 30 JSRef<JSVal> param; 31 }; 32 33 struct NavPathInfoUINode { NavPathInfoUINodeNavPathInfoUINode34 NavPathInfoUINode(const std::string& name, const JSRef<JSVal>& param, RefPtr<NG::UINode>& uiNode, int32_t index) 35 { 36 this->name = name; 37 this->param = param; 38 this->uiNode = uiNode; 39 this->index = index; 40 } 41 std::string name; 42 JSRef<JSVal> param; 43 RefPtr<NG::UINode> uiNode; 44 int32_t index = -1; 45 }; 46 47 class JSRouteInfo : public NG::RouteInfo { 48 DECLARE_ACE_TYPE(JSRouteInfo, NG::RouteInfo); 49 public: 50 JSRouteInfo() = default; 51 ~JSRouteInfo() override = default; 52 53 std::string GetName() override; 54 void SetName(const std::string& name); 55 void SetParam(const JSRef<JSVal>& param); 56 JSRef<JSVal> GetParam() const; 57 58 protected: 59 std::string name_; 60 JSRef<JSVal> param_; 61 }; 62 63 class JSNavigationStack : public NG::NavigationStack { 64 DECLARE_ACE_TYPE(JSNavigationStack, NG::NavigationStack); 65 public: 66 JSNavigationStack() = default; 67 ~JSNavigationStack() override = default; 68 SetOnStateChangedCallback(std::function<void ()> callback)69 void SetOnStateChangedCallback(std::function<void()> callback) override 70 { 71 onStateChangedCallback_ = callback; 72 } 73 UpdateStackInfo(const RefPtr<NavigationStack> & newStack)74 void UpdateStackInfo(const RefPtr<NavigationStack>& newStack) override 75 { 76 auto newJsStack = AceType::DynamicCast<JSNavigationStack>(newStack); 77 if (newJsStack) { 78 SetDataSourceObj(newJsStack->GetDataSourceObj()); 79 } 80 } 81 void SetDataSourceObj(const JSRef<JSObject>& dataSourceObj); 82 const JSRef<JSObject>& GetDataSourceObj(); 83 void SetNavDestBuilderFunc(const JSRef<JSFunc>& navDestBuilderFunc); 84 bool IsEmpty() override; 85 void Pop() override; 86 void Push(const std::string& name, const RefPtr<NG::RouteInfo>& routeInfo = nullptr) override; 87 void Push(const std::string& name, int32_t index) override; 88 void PushName(const std::string& name, const JSRef<JSVal>& param); 89 void RemoveName(const std::string& name) override; 90 void RemoveIndex(int32_t index) override; 91 void RemoveInvalidPage(int32_t index); 92 void Clear() override; 93 int32_t GetReplaceValue() const override; 94 void UpdateReplaceValue(int32_t isReplace) const override; 95 bool GetAnimatedValue() const override; 96 void UpdateAnimatedValue(bool animated) override; 97 bool GetDisableAnimation() const override; 98 std::vector<std::string> GetAllPathName() override; 99 std::vector<int32_t> GetAllPathIndex() override; 100 void InitNavPathIndex(const std::vector<std::string>& pathNames) override; 101 void SetDestinationIdToJsStack(int32_t index, const std::string& navDestinationId) override; 102 bool CreateNodeByIndex(int32_t index, const WeakPtr<NG::UINode>& customNode, RefPtr<NG::UINode>& node) override; 103 RefPtr<NG::UINode> CreateNodeByRouteInfo(const RefPtr<NG::RouteInfo>& routeInfo, 104 const WeakPtr<NG::UINode>& node) override; 105 void SetJSExecutionContext(const JSExecutionContext& context); 106 std::string GetRouteParam() const override; 107 void OnAttachToParent(RefPtr<NG::NavigationStack> parent) override; 108 void OnDetachFromParent() override; 109 std::vector<std::string> DumpStackInfo() const override; 110 void FireNavigationInterception(bool isBefore, const RefPtr<NG::NavDestinationContext>& from, 111 const RefPtr<NG::NavDestinationContext>& to, NG::NavigationOperation operation, bool isAnimated) override; 112 void FireNavigationModeChange(NG::NavigationMode mode) override; 113 JSRef<JSVal> GetParamByIndex(int32_t index) const; 114 int32_t GetJsIndexFromNativeIndex(int32_t index) override; 115 void MoveIndexToTop(int32_t index) override; 116 void UpdatePathInfoIfNeeded(RefPtr<NG::UINode>& uiNode, int32_t index) override; 117 void RecoveryNavigationStack() override; 118 bool NeedBuildNewInstance(int32_t index) override; 119 void SetNeedBuildNewInstance(int32_t index, bool need) override; 120 void SetIsEntryByIndex(int32_t index, bool isEntry) override; 121 122 std::string GetStringifyParamByIndex(int32_t index) const override; 123 std::string GetSerializedParamSafely(int32_t index) const override; 124 void SetPathArray(const std::vector<NG::NavdestinationRecoveryInfo>& navdestinationsInfo) override; 125 bool IsFromRecovery(int32_t index) override; 126 void SetFromRecovery(int32_t index, bool fromRecovery) override; 127 int32_t GetRecoveredDestinationMode(int32_t index) override; 128 void SetRecoveryFromReplaceDestination(int32_t index, bool value) override; 129 bool CheckIsReplacedDestination(int32_t index, std::string& replacedName, int32_t& replacedIndex) override; 130 131 bool HasSingletonMoved() override; 132 bool IsTopFromSingletonMoved() override; 133 void ResetSingletonMoved() override; 134 135 uint64_t GetNavDestinationIdInt(int32_t index) override; 136 bool GetIsForceSet(int32_t index) override; 137 void ResetIsForceSetFlag(int32_t index) override; 138 void PushIntentNavDestination(const std::string& name, const std::string& params, bool needTransition) override; 139 void CallPushDestinationInner(const NG::NavdestinationRecoveryInfo& navdestinationsInfo) override; 140 141 void RemoveByIndexes(const std::vector<int32_t>& indexes) override; 142 SetHomePathInfo(HomePathInfo && pathInfo)143 void SetHomePathInfo(HomePathInfo&& pathInfo) 144 { 145 homePathInfo_ = std::move(pathInfo); 146 } 147 bool CreateHomeDestination(const WeakPtr<NG::UINode>& customNode, RefPtr<NG::UINode>& node) override; 148 149 protected: 150 JSRef<JSObject> dataSourceObj_; 151 JSRef<JSFunc> navDestBuilderFunc_; 152 JSExecutionContext executionContext_; 153 std::function<void()> onStateChangedCallback_; 154 155 private: 156 JSRef<JSArray> GetJsPathArray() const; 157 JSRef<JSObject> GetJsPathInfo(int32_t index) const; 158 std::string GetNameByIndex(int32_t index); 159 JSRef<JSVal> GetOnPopByIndex(int32_t index) const; 160 bool GetIsEntryByIndex(int32_t index); 161 JSRef<JSObject> CreatePathInfoWithNecessaryProperty(const RefPtr<NG::NavDestinationContext>& context); 162 bool GetNavDestinationNodeInUINode(RefPtr<NG::UINode> node, RefPtr<NG::NavDestinationGroupNode>& desNode); 163 int32_t GetSize() const override; 164 void SetJSParentStack(JSRef<JSVal> parent); 165 std::string ConvertParamToString(const JSRef<JSVal>& param, bool needLimit = false) const; 166 void ParseJsObject( 167 std::unique_ptr<JsonValue>& json, const JSRef<JSObject>& obj, int32_t depthLimit, bool needLimit) const; 168 void UpdateOnStateChangedCallback(JSRef<JSObject> obj, std::function<void()> callback); 169 170 int LoadDestination(const std::string& name, const JSRef<JSVal>& param, const WeakPtr<NG::UINode>& customNode, 171 RefPtr<NG::UINode>& node, RefPtr<NG::NavDestinationGroupNode>& desNode); 172 bool LoadDestinationByBuilder(const std::string& name, const JSRef<JSVal>& param, RefPtr<NG::UINode>& node, 173 RefPtr<NG::NavDestinationGroupNode>& desNode); 174 bool GetFlagByIndex(int32_t index) const; 175 bool CheckAndGetInterceptionFunc(const std::string& name, JSRef<JSFunc>& func); 176 177 bool GetNeedUpdatePathInfo(int32_t index); 178 void SetNeedUpdatePathInfo(int32_t index, bool need); 179 int32_t ExecuteBuilderByConfig(const std::string& name, 180 const WeakPtr<NG::UINode>& customNode, const JSRef<JSVal>& param); 181 182 JSRef<JSArray> GetPathArray(); 183 JSRef<JSObject> GetPathInfo(int32_t index); 184 185 std::string ErrorToMessage(int32_t errorCode); 186 187 bool RemoveDestinationIfNeeded(const JSRef<JSObject>& param, int32_t errorCode, int32_t index); 188 189 bool ExecutePopCallbackInStack(const JSRef<JSVal>& param); 190 bool ExecutePopCallback(const RefPtr<NG::UINode>& uiNode, uint64_t navDestinationId, const JSRef<JSVal>& param); 191 void ExecutePopCallbackForHomeNavDestination(const JSRef<JSVal>& param); 192 193 private: 194 JSRef<JSObject> thisObj_; 195 196 std::optional<HomePathInfo> homePathInfo_; 197 WeakPtr<NG::NavDestinationGroupNode> homeDestinationNode_; 198 }; 199 } // namespace OHOS::Ace::Framework 200 201 #endif // FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_JS_VIEW_JS_NAVIGATION_STACK_H 202