1 /* 2 * Copyright (c) 2025 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 OBJECT_HELPER_H 17 #define OBJECT_HELPER_H 18 19 #include <functional> 20 #include <map> 21 #include <shared_mutex> 22 #ifdef ROSEN_OHOS 23 #include <parcel.h> 24 #endif 25 #include "utils/object.h" 26 27 namespace OHOS { 28 namespace Rosen { 29 namespace Drawing { 30 class Data; 31 32 class DRAWING_API ObjectHelper { 33 public: 34 #ifdef ROSEN_OHOS 35 using UnmarshallingFunc = std::function<std::shared_ptr<Object>(Parcel&, bool&, int32_t)>; 36 #endif 37 38 static constexpr int32_t MAX_NESTING_DEPTH = 800; 39 40 static ObjectHelper& Instance(); 41 42 #ifdef ROSEN_OHOS Register(int32_t type,int32_t subType,UnmarshallingFunc func)43 bool Register(int32_t type, int32_t subType, UnmarshallingFunc func) { 44 std::unique_lock<std::shared_mutex> lock(mapMutex_); 45 uint64_t key = (static_cast<uint64_t>(type) << 32) | static_cast<uint64_t>(subType); 46 funcMap_[key] = func; 47 return true; 48 } 49 50 /** 51 * @brief Get registered unmarshalling function for given type and subType 52 * @param type Object type 53 * @param subType Object subType 54 * @return UnmarshallingFunc or nullptr if not found 55 */ GetFunc(int32_t type,int32_t subType)56 UnmarshallingFunc GetFunc(int32_t type, int32_t subType) const { 57 std::shared_lock<std::shared_mutex> lock(mapMutex_); 58 uint64_t key = (static_cast<uint64_t>(type) << 32) | static_cast<uint64_t>(subType); 59 auto it = funcMap_.find(key); 60 return (it != funcMap_.end()) ? it->second : nullptr; 61 } 62 63 /** 64 * @brief Set external Data marshalling callback for advanced processing 65 * @param callback Function to handle Data marshalling with additional safety checks 66 */ SetDataMarshallingCallback(std::function<bool (Parcel &,std::shared_ptr<Data>)> callback)67 void SetDataMarshallingCallback( 68 std::function<bool(Parcel&, std::shared_ptr<Data>)> callback) { 69 dataMarshallingCallback_ = callback; 70 } 71 72 /** 73 * @brief Set external Data unmarshalling callback for advanced processing 74 * @param callback Function to handle Data unmarshalling with additional safety checks 75 */ SetDataUnmarshallingCallback(std::function<std::shared_ptr<Data> (Parcel &)> callback)76 void SetDataUnmarshallingCallback( 77 std::function<std::shared_ptr<Data>(Parcel&)> callback) { 78 dataUnmarshallingCallback_ = callback; 79 } 80 GetDataMarshallingCallback()81 std::function<bool(Parcel&, std::shared_ptr<Data>)> GetDataMarshallingCallback() const { 82 return dataMarshallingCallback_; 83 } 84 GetDataUnmarshallingCallback()85 std::function<std::shared_ptr<Data>(Parcel&)> GetDataUnmarshallingCallback() const { 86 return dataUnmarshallingCallback_; 87 } 88 #endif 89 90 private: 91 ObjectHelper() = default; 92 ~ObjectHelper() = default; 93 ObjectHelper(const ObjectHelper& other) = delete; 94 ObjectHelper(ObjectHelper&& other) = delete; 95 ObjectHelper& operator=(const ObjectHelper& other) = delete; 96 ObjectHelper& operator=(ObjectHelper&& other) = delete; 97 #ifdef ROSEN_OHOS 98 mutable std::shared_mutex mapMutex_; 99 std::map<uint64_t, UnmarshallingFunc> funcMap_; 100 std::function<bool(Parcel&, std::shared_ptr<Data>)> dataMarshallingCallback_; 101 std::function<std::shared_ptr<Data>(Parcel&)> dataUnmarshallingCallback_; 102 #endif 103 }; 104 105 // Registration macro for Object-based classes 106 #define OBJECT_UNMARSHALLING_REGISTER(className, type, subType) \ 107 static bool isRegistered##className = \ 108 OHOS::Rosen::Drawing::ObjectHelper::Instance().Register( \ 109 type, subType, [](Parcel& parcel, bool& isValid, int32_t depth) \ 110 -> std::shared_ptr<OHOS::Rosen::Drawing::Object> { \ 111 auto obj = className::CreateForUnmarshalling(); \ 112 return obj->Unmarshalling(parcel, isValid, depth) ? obj : nullptr; \ 113 }) 114 115 #ifdef ROSEN_OHOS 116 // Registration macro for Data callbacks - for use in specific cpp files 117 #define DATA_CALLBACKS_REGISTER(marshallingFunc, unmarshallingFunc) \ 118 static bool dataCallbacksRegistered = []() { \ 119 OHOS::Rosen::Drawing::ObjectHelper::Instance().SetDataMarshallingCallback(marshallingFunc); \ 120 OHOS::Rosen::Drawing::ObjectHelper::Instance().SetDataUnmarshallingCallback(unmarshallingFunc); \ 121 return true; \ 122 }() 123 #endif 124 125 } // namespace Drawing 126 } // namespace Rosen 127 } // namespace OHOS 128 129 #endif // OBJECT_HELPER_H