/* * Copyright (c) 2024 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef OHOS_ROSEN_EXTENSION_DATA_HANDLER_H #define OHOS_ROSEN_EXTENSION_DATA_HANDLER_H #include "data_handler_interface.h" #include #include #include #include #include #include namespace OHOS::Rosen::Extension { struct DataTransferConfig : public Parcelable { bool Marshalling(Parcel& parcel) const override; static DataTransferConfig* Unmarshalling(Parcel& parcel); std::string ToString() const; bool needSyncSend { false }; bool needReply { false }; SubSystemId subSystemId { SubSystemId::INVALID }; uint32_t customId { 0 }; }; using Task = std::function; class DataHandler : public IDataHandler { public: DataHandler() = default; virtual ~DataHandler() = default; DataHandlerErr SendDataSync(SubSystemId subSystemId, uint32_t customId, const AAFwk::Want& toSend, AAFwk::Want& reply) override; DataHandlerErr SendDataSync(SubSystemId subSystemId, uint32_t customId, const AAFwk::Want& toSend) override; DataHandlerErr SendDataAsync(SubSystemId subSystemId, uint32_t customId, const AAFwk::Want& toSend) override; DataHandlerErr RegisterDataConsumer(SubSystemId subSystemId, DataConsumeCallback&& callback) override; void UnregisterDataConsumer(SubSystemId subSystemId) override; void NotifyDataConsumer(MessageParcel& recieved, MessageParcel& reply); void SetEventHandler(const std::shared_ptr& eventHandler); void SetRemoteProxyObject(const sptr& remoteObject); protected: DataHandlerErr NotifyDataConsumer(AAFwk::Want&& data, std::optional& reply, const DataTransferConfig& config); virtual DataHandlerErr SendData(const AAFwk::Want& toSend, AAFwk::Want& reply, const DataTransferConfig& config) = 0; DataHandlerErr PrepareSendData(MessageParcel& data, const DataTransferConfig& config, const AAFwk::Want& toSend); virtual bool WriteInterfaceToken(MessageParcel& data) = 0; DataHandlerErr ParseReply(MessageParcel& recieved, AAFwk::Want& reply, const DataTransferConfig& config); void PostAsyncTask(Task&& task, const std::string& name, int64_t delayTime); bool IsProxyObject() const; protected: mutable std::mutex mutex_; std::unordered_map consumers_; std::shared_ptr eventHandler_; sptr remoteProxy_; }; } // namespace OHOS::Rosen::Extension #endif // OHOS_ROSEN_EXTENSION_DATA_HANDLER_H