• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_DATA_HANDLER_H
17 #define OHOS_ROSEN_DATA_HANDLER_H
18 
19 #include <cstdint>
20 #include <functional>
21 #include <optional>
22 
23 namespace OHOS::AAFwk {
24 class Want;
25 }  // namespace OHOS::AAFwk
26 
27 namespace OHOS::Rosen {
28 
29 enum class SubSystemId : uint8_t { WM_UIEXT = 0, ARKUI_UIEXT, ABILITY_UIEXT, INVALID };
30 
31 enum class DataHandlerErr : uint32_t {
32     // common
33     OK = 0,
34     INVALID_PARAMETER,
35     INTERNAL_ERROR,
36     DUPLICATE_REGISTRATION,
37     NULL_PTR,
38 
39     // ipc processing errors
40     IPC_SEND_FAILED,
41     WRITE_PARCEL_ERROR,
42     READ_PARCEL_ERROR,
43 
44     // callback errors
45     NO_CONSUME_CALLBACK,
46     INVALID_CALLBACK,
47 };
48 
49 using DataConsumeCallback =
50     std::function<int32_t(SubSystemId id, uint32_t customId, AAFwk::Want&& data, std::optional<AAFwk::Want>& reply)>;
51 
52 /**
53  * @class IDataHandler
54  * @brief Interface for handling data operations between subsystems by using the IPC.
55  *
56  * This class defines an interface for sending and receiving data between different
57  * subsystems, as well as registering and unregistering data consumers. It provides
58  * methods for both synchronous and asynchronous data transmission.
59  */
60 class IDataHandler {
61 public:
62     IDataHandler() = default;
63     virtual ~IDataHandler() = default;
64 
65     /**
66      * @brief Sends data synchronously to a specified subsystem and receives a reply.
67      *
68      * @param subSystemId The identifier of the target subsystem.
69      * @param customId A custom identifier for the data being sent.
70      * @param toSend The Want object containing the data to be sent.
71      * @param reply A reference to a Want object that will be filled with the reply data.
72      * @return DataHandlerErr::OK if the data was successfully sent and a reply was received, other errcode otherwise.
73      */
74     virtual DataHandlerErr SendDataSync(SubSystemId subSystemId, uint32_t customId, const AAFwk::Want& toSend,
75                                         AAFwk::Want& reply) = 0;
76 
77     /**
78      * @brief Sends data synchronously to a specified subsystem without reply.
79      *
80      * @param subSystemId The identifier of the target subsystem.
81      * @param customId A custom identifier for the data being sent.
82      * @param toSend The Want object containing the data to be sent.
83      * @return DataHandlerErr::OK if the data was successfully sent, other errcode otherwise.
84      */
85     virtual DataHandlerErr SendDataSync(SubSystemId subSystemId, uint32_t customId, const AAFwk::Want& toSend) = 0;
86 
87     /**
88      * @brief Sends data asynchronously to a specified subsystem without reply.
89      *
90      * @param subSystemId The identifier of the target subsystem.
91      * @param customId A custom identifier for the data being sent.
92      * @param toSend The Want object containing the data to be sent.
93      * @return DataHandlerErr::OK if the data was successfully sent, other errcode otherwise.
94      */
95     virtual DataHandlerErr SendDataAsync(SubSystemId subSystemId, uint32_t customId, const AAFwk::Want& toSend) = 0;
96 
97     /**
98      * @brief Registers a data consumer for a specific subsystemId.
99      *
100      * @param SubSystemId The identifier of the data to be consumed.
101      * @param callback A DataConsumerInfo object containing the callback function and options.
102      * @return DataHandlerErr::OK if the data consumer was successfully registered, other errcode otherwise.
103      */
104     virtual DataHandlerErr RegisterDataConsumer(SubSystemId subSystemId, DataConsumeCallback&& callback) = 0;
105 
106     /**
107      * @brief Unregisters a data consumer for a specific subSystemId.
108      *
109      * @param SubSystemId The identifier of the data to be consumed.
110      */
111     virtual void UnregisterDataConsumer(SubSystemId subSystemId) = 0;
112 };
113 }  // namespace OHOS::Rosen
114 
115 #endif  // OHOS_ROSEN_DATA_HANDLER_H