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_V8_V8_ANIMATION_BRIDGE_H 17 #define FOUNDATION_ACE_FRAMEWORKS_BRIDGE_JS_FRONTEND_ENGINE_V8_V8_ANIMATION_BRIDGE_H 18 19 #include <map> 20 #include <string> 21 #include <v8.h> 22 23 #include "frameworks/bridge/common/dom/dom_node.h" 24 #include "frameworks/bridge/js_frontend/engine/common/base_animation_bridge.h" 25 #include "frameworks/bridge/js_frontend/frontend_delegate.h" 26 #include "frameworks/bridge/js_frontend/js_command.h" 27 28 namespace OHOS::Ace::Framework { 29 30 class V8AnimationBridgeUtils { 31 public: 32 static v8::Local<v8::Object> CreateAnimationContext(v8::Local<v8::Context>& ctx, int32_t pageId, NodeId nodeId); 33 static void JsAnimationPlay(const v8::FunctionCallbackInfo<v8::Value>& args); 34 static void JsAnimationFinish(const v8::FunctionCallbackInfo<v8::Value>& args); 35 static void JsAnimationPause(const v8::FunctionCallbackInfo<v8::Value>& args); 36 static void JsAnimationCancel(const v8::FunctionCallbackInfo<v8::Value>& args); 37 static void JsAnimationReverse(const v8::FunctionCallbackInfo<v8::Value>& args); 38 static void JsAnimationPlayStateGet(const v8::FunctionCallbackInfo<v8::Value>& info); 39 static void JsAnimationStartTimeGet(const v8::FunctionCallbackInfo<v8::Value>& info); 40 static void JsAnimationPendingGet(const v8::FunctionCallbackInfo<v8::Value>& info); 41 static void JsAnimationPlayStateSet(const v8::FunctionCallbackInfo<v8::Value>& info); 42 static void JsAnimationStartTimeSet(const v8::FunctionCallbackInfo<v8::Value>& info); 43 static void JsAnimationPendingSet(const v8::FunctionCallbackInfo<v8::Value>& info); 44 }; 45 46 class V8AnimationBridge : public BaseAnimationBridge { 47 DECLARE_ACE_TYPE(V8AnimationBridge, BaseAnimationBridge) 48 49 public: 50 V8AnimationBridge(const v8::Local<v8::Context>& ctx, v8::Isolate* instance, v8::Local<v8::Object> animationContext, 51 NodeId nodeId); 52 ~V8AnimationBridge() override = default; GetJsObject()53 v8::Local<v8::Object> GetJsObject() 54 { 55 return animationObject_.Get(instance_); 56 } 57 GetContext()58 v8::Local<v8::Context> GetContext() const 59 { 60 return ctx_.Get(instance_); 61 } 62 63 void JsCreateAnimation(const RefPtr<JsAcePage>& page, const std::string& param); 64 void SetPlayStateCallbacks(RefPtr<Animator>& animator); 65 void SetPlayStateCallbacksWithListenerId(RefPtr<Animator>& animator); 66 67 private: 68 v8::Persistent<v8::Context> ctx_; 69 v8::Isolate* instance_ = nullptr; 70 v8::Persistent<v8::Object> animationObject_; 71 NodeId nodeId_ = -1; 72 IdType finishListenerId_ = 0; 73 IdType idleListenerId_ = 0; 74 }; 75 76 class V8AnimationBridgeTaskCreate : public AnimationBridgeTask { 77 DECLARE_ACE_TYPE(V8AnimationBridgeTaskCreate, AnimationBridgeTask) 78 public: 79 V8AnimationBridgeTaskCreate(const RefPtr<FrontendDelegate>& delegate, 80 const RefPtr<V8AnimationBridge>& bridge, std::string param); 81 ~V8AnimationBridgeTaskCreate() override = default; 82 void AnimationBridgeTaskFunc(const RefPtr<JsAcePage>& page, NodeId nodeId) override; 83 84 private: 85 RefPtr<V8AnimationBridge> bridge_; 86 WeakPtr<FrontendDelegate> delegate_; 87 std::string param_; 88 }; 89 90 class V8AnimationBridgeTaskOperation : public AnimationBridgeTask { DECLARE_ACE_TYPE(V8AnimationBridgeTaskOperation,AnimationBridgeTask)91 DECLARE_ACE_TYPE(V8AnimationBridgeTaskOperation, AnimationBridgeTask) 92 public: 93 explicit V8AnimationBridgeTaskOperation(AnimationOperation operation) : operation_(operation) {} 94 ~V8AnimationBridgeTaskOperation() override = default; 95 void AnimationBridgeTaskFunc(const RefPtr<JsAcePage>& page, NodeId nodeId) override; 96 97 private: 98 AnimationOperation operation_ = AnimationOperation::NONE; 99 }; 100 101 class V8AnimationBridgeTaskStartTime : public AnimationBridgeTask { DECLARE_ACE_TYPE(V8AnimationBridgeTaskStartTime,AnimationBridgeTask)102 DECLARE_ACE_TYPE(V8AnimationBridgeTaskStartTime, AnimationBridgeTask) 103 public: 104 explicit V8AnimationBridgeTaskStartTime(std::string startTime) : startTime_(std::move(startTime)) {} 105 ~V8AnimationBridgeTaskStartTime() override = default; 106 void AnimationBridgeTaskFunc(const RefPtr<JsAcePage>& page, NodeId nodeId) override; 107 108 private: 109 std::string startTime_; 110 }; 111 112 } // namespace OHOS::Ace::Framework 113 114 #endif // FOUNDATION_ACE_FRAMEWORKS_BRIDGE_JS_FRONTEND_ENGINE_V8_V8_ANIMATION_BRIDGE_H 115