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 #ifndef FOUNDATION_ACE_FRAMEWORKS_BRIDGE_JS_FRONTEND_ENGINE_JSI_JSI_ANIMATION_BRIDGE_H
17 #define FOUNDATION_ACE_FRAMEWORKS_BRIDGE_JS_FRONTEND_ENGINE_JSI_JSI_ANIMATION_BRIDGE_H
18 
19 #include <map>
20 #include <string>
21 
22 #include "frameworks/bridge/common/dom/dom_node.h"
23 #include "frameworks/bridge/js_frontend/engine/common/base_animation_bridge.h"
24 #include "frameworks/bridge/js_frontend/engine/jsi/jsi_engine.h"
25 #include "frameworks/bridge/js_frontend/js_command.h"
26 
27 namespace OHOS::Ace::Framework {
28 using std::shared_ptr;
29 
30 class JsiAnimationBridgeUtils {
31 public:
32     static shared_ptr<JsValue> CreateAnimationContext(shared_ptr<JsRuntime> runtime, int32_t pageId, NodeId nodeId);
33     static shared_ptr<JsValue> JsAnimationPlay(shared_ptr<JsRuntime> runtime, shared_ptr<JsValue> thisObj,
34         const std::vector<shared_ptr<JsValue>>& argv, int32_t argc);
35     static shared_ptr<JsValue> JsAnimationFinish(shared_ptr<JsRuntime> runtime, shared_ptr<JsValue> thisObj,
36         const std::vector<shared_ptr<JsValue>>& argv, int32_t argc);
37     static shared_ptr<JsValue> JsAnimationPause(shared_ptr<JsRuntime> runtime, shared_ptr<JsValue> thisObj,
38         const std::vector<shared_ptr<JsValue>>& argv, int32_t argc);
39     static shared_ptr<JsValue> JsAnimationCancel(shared_ptr<JsRuntime> runtime, shared_ptr<JsValue> thisObj,
40         const std::vector<shared_ptr<JsValue>>& argv, int32_t argc);
41     static shared_ptr<JsValue> JsAnimationReverse(shared_ptr<JsRuntime> runtime, shared_ptr<JsValue> thisObj,
42         const std::vector<shared_ptr<JsValue>>& argv, int32_t argc);
43     static shared_ptr<JsValue> JsAnimationPlayStateGet(shared_ptr<JsRuntime> runtime, shared_ptr<JsValue> thisObj,
44         const std::vector<shared_ptr<JsValue>>& argv, int32_t argc);
45     static shared_ptr<JsValue> JsAnimationStartTimeGet(shared_ptr<JsRuntime> runtime, shared_ptr<JsValue> thisObj,
46         const std::vector<shared_ptr<JsValue>>& argv, int32_t argc);
47     static shared_ptr<JsValue> JsAnimationPendingGet(shared_ptr<JsRuntime> runtime, shared_ptr<JsValue> thisObj,
48         const std::vector<shared_ptr<JsValue>>& argv, int32_t argc);
49     static shared_ptr<JsValue> JsAnimationPlayStateSet(shared_ptr<JsRuntime> runtime, shared_ptr<JsValue> thisObj,
50         const std::vector<shared_ptr<JsValue>>& argv, int32_t argc);
51     static shared_ptr<JsValue> JsAnimationStartTimeSet(shared_ptr<JsRuntime> runtime, shared_ptr<JsValue> thisObj,
52         const std::vector<shared_ptr<JsValue>>& argv, int32_t argc);
53     static shared_ptr<JsValue> JsAnimationPendingSet(shared_ptr<JsRuntime> runtime, shared_ptr<JsValue> thisObj,
54         const std::vector<shared_ptr<JsValue>>& argv, int32_t argc);
55 };
56 
57 class JsiAnimationBridge : public BaseAnimationBridge {
58     DECLARE_ACE_TYPE(JsiAnimationBridge, BaseAnimationBridge)
59 
60 public:
61     JsiAnimationBridge(const shared_ptr<JsRuntime>& runtime, const shared_ptr<JsValue>& animationContext,
62                        NodeId nodeId);
63     ~JsiAnimationBridge() override = default;
64 
OnJsEngineDestroy()65     void OnJsEngineDestroy() override
66     {
67         animationObject_.reset();
68     }
69 
GetJsObject()70     shared_ptr<JsValue> GetJsObject() const
71     {
72         return animationObject_;
73     }
74 
GetRuntime()75     shared_ptr<JsRuntime> GetRuntime() const
76     {
77         return runtime_;
78     }
79 
80     void JsCreateAnimation(const RefPtr<JsAcePage>& page, const std::string& param);
81     void SetPlayStateCallbacks(RefPtr<Animator>& animator);
82     void SetPlayStateCallbacksWithListenerId(RefPtr<Animator>& animator);
83 
84 private:
85     shared_ptr<JsRuntime> runtime_;
86     shared_ptr<JsValue> animationObject_;
87     NodeId nodeId_ = -1;
88     IdType finishListenerId_ = 0;
89     IdType idleListenerId_ = 0;
90 };
91 
92 class JsiAnimationBridgeTaskCreate : public AnimationBridgeTask {
93     DECLARE_ACE_TYPE(JsiAnimationBridgeTaskCreate, AnimationBridgeTask)
94 public:
95     JsiAnimationBridgeTaskCreate(
96         shared_ptr<JsRuntime> runtime, const RefPtr<JsiAnimationBridge>& bridge, std::string param);
97     ~JsiAnimationBridgeTaskCreate() override = default;
98     void AnimationBridgeTaskFunc(const RefPtr<JsAcePage>& page, NodeId nodeId) override;
99 
100 private:
101     RefPtr<JsiAnimationBridge> bridge_;
102     shared_ptr<JsRuntime> runtime_;
103     std::string param_;
104 };
105 
106 class JsiAnimationBridgeTaskOperation : public AnimationBridgeTask {
DECLARE_ACE_TYPE(JsiAnimationBridgeTaskOperation,AnimationBridgeTask)107     DECLARE_ACE_TYPE(JsiAnimationBridgeTaskOperation, AnimationBridgeTask)
108 public:
109     explicit JsiAnimationBridgeTaskOperation(AnimationOperation operation) : operation_(operation) {}
110     ~JsiAnimationBridgeTaskOperation() override = default;
111     void AnimationBridgeTaskFunc(const RefPtr<JsAcePage>& page, NodeId nodeId) override;
112 
113 private:
114     AnimationOperation operation_ = AnimationOperation::NONE;
115 };
116 
117 class JsiAnimationBridgeTaskStartTime : public AnimationBridgeTask {
DECLARE_ACE_TYPE(JsiAnimationBridgeTaskStartTime,AnimationBridgeTask)118     DECLARE_ACE_TYPE(JsiAnimationBridgeTaskStartTime, AnimationBridgeTask)
119 public:
120     explicit JsiAnimationBridgeTaskStartTime(std::string startTime) : startTime_(std::move(startTime)) {}
121     ~JsiAnimationBridgeTaskStartTime() override = default;
122     void AnimationBridgeTaskFunc(const RefPtr<JsAcePage>& page, NodeId nodeId) override;
123 
124 private:
125     std::string startTime_;
126 };
127 
128 } // namespace OHOS::Ace::Framework
129 
130 #endif // FOUNDATION_ACE_FRAMEWORKS_BRIDGE_JS_FRONTEND_ENGINE_JSI_JSI_ANIMATION_BRIDGE_H
131