1 /* 2 * Copyright (c) 2024 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 OHOS_ROSEN_EXTENSION_DATA_HANDLER_H 17 #define OHOS_ROSEN_EXTENSION_DATA_HANDLER_H 18 19 #include "data_handler_interface.h" 20 21 #include <cstdint> 22 #include <mutex> 23 #include <optional> 24 25 #include <event_handler.h> 26 #include <message_parcel.h> 27 #include <want.h> 28 29 namespace OHOS::Rosen::Extension { 30 31 struct DataTransferConfig : public Parcelable { 32 bool Marshalling(Parcel& parcel) const override; 33 static DataTransferConfig* Unmarshalling(Parcel& parcel); 34 std::string ToString() const; 35 36 bool needSyncSend { false }; 37 bool needReply { false }; 38 SubSystemId subSystemId { SubSystemId::INVALID }; 39 uint32_t customId { 0 }; 40 }; 41 42 using Task = std::function<void()>; 43 44 class DataHandler : public IDataHandler { 45 public: 46 DataHandler() = default; 47 virtual ~DataHandler() = default; 48 49 DataHandlerErr SendDataSync(SubSystemId subSystemId, uint32_t customId, const AAFwk::Want& toSend, 50 AAFwk::Want& reply) override; 51 DataHandlerErr SendDataSync(SubSystemId subSystemId, uint32_t customId, const AAFwk::Want& toSend) override; 52 DataHandlerErr SendDataAsync(SubSystemId subSystemId, uint32_t customId, const AAFwk::Want& toSend) override; 53 DataHandlerErr RegisterDataConsumer(SubSystemId subSystemId, DataConsumeCallback&& callback) override; 54 void UnregisterDataConsumer(SubSystemId subSystemId) override; 55 void NotifyDataConsumer(MessageParcel& recieved, MessageParcel& reply); 56 void SetEventHandler(const std::shared_ptr<AppExecFwk::EventHandler>& eventHandler); 57 void SetRemoteProxyObject(const sptr<IRemoteObject>& remoteObject); 58 59 protected: 60 DataHandlerErr NotifyDataConsumer(AAFwk::Want&& data, std::optional<AAFwk::Want>& reply, 61 const DataTransferConfig& config); 62 virtual DataHandlerErr SendData(const AAFwk::Want& toSend, AAFwk::Want& reply, 63 const DataTransferConfig& config) = 0; 64 DataHandlerErr PrepareSendData(MessageParcel& data, const DataTransferConfig& config, const AAFwk::Want& toSend); 65 virtual bool WriteInterfaceToken(MessageParcel& data) = 0; 66 DataHandlerErr ParseReply(MessageParcel& recieved, AAFwk::Want& reply, const DataTransferConfig& config); 67 void PostAsyncTask(Task&& task, const std::string& name, int64_t delayTime); 68 bool IsProxyObject() const; 69 70 protected: 71 mutable std::mutex mutex_; 72 std::unordered_map<SubSystemId, DataConsumeCallback> consumers_; 73 std::shared_ptr<AppExecFwk::EventHandler> eventHandler_; 74 sptr<IRemoteObject> remoteProxy_; 75 }; 76 77 } // namespace OHOS::Rosen::Extension 78 79 #endif // OHOS_ROSEN_EXTENSION_DATA_HANDLER_H