• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 IMF_FRAMEWORKS_SERVICE_RESPONSE_DATA_H
17 #define IMF_FRAMEWORKS_SERVICE_RESPONSE_DATA_H
18 
19 #include <cstdint>
20 #include <future>
21 #include <unordered_map>
22 #include <variant>
23 
24 #include "input_method_property.h"
25 #include "input_method_utils.h"
26 #include "response_data_util.h"
27 
28 namespace OHOS {
29 namespace MiscServices {
30 using RequestId = uint32_t;
31 using RequestFunc = std::function<int32_t(RequestId)>;
32 
33 struct StartInputResponse : public Parcelable {
34     sptr<IRemoteObject> agent{ nullptr };
35     int64_t pid{ 0 };
36     std::string bundleName;
37     void Set(sptr<IRemoteObject> imeAgent, int64_t imePid, const std::string &imeBundleName);
38     bool ReadFromParcel(Parcel &in);
39     bool Marshalling(Parcel &out) const override;
40     static StartInputResponse *Unmarshalling(Parcel &in);
41 };
42 
43 struct InputStartInfo : public Parcelable {
44     bool isInputStart{ false };
45     uint32_t callingWindowId{ 0 };
46     int32_t requestKeyboardReason{ 0 };
47     void Set(bool inputStart, uint32_t id, int32_t reason);
48     bool ReadFromParcel(Parcel &in);
49     bool Marshalling(Parcel &out) const override;
50     static InputStartInfo *Unmarshalling(Parcel &in);
51 };
52 
53 enum ServiceDataType : int32_t {
54     TYPE_MONOSTATE = 0,
55 
56     // basic types
57     TYPE_BOOL = 1,
58     TYPE_INT32 = 2,
59     TYPE_UINT32 = 3,
60     TYPE_INT64 = 4,
61     TYPE_UINT64 = 5,
62     TYPE_REMOTE_OBJECT = 6,
63 
64     // inner types
65     TYPE_PROPERTY = 7,
66     TYPE_PROPERTIES = 8,
67     TYPE_SUB_PROPERTY = 9,
68     TYPE_SUB_PROPERTIES = 10,
69     TYPE_START_INPUT_RESPONSE = 11,
70     TYPE_INPUT_START_INFO = 12,
71 
72     TYPE_END,
73 };
74 
75 using ServiceResponseData =
76     std::variant<std::monostate, bool, int32_t, uint32_t, int64_t, uint64_t, sptr<IRemoteObject>, Property,
77         std::vector<Property>, SubProperty, std::vector<SubProperty>, StartInputResponse, InputStartInfo>;
78 
79 struct ServiceResponse {
80     int32_t result{ 0 };
81     ServiceResponseData responseData{ std::monostate{} };
82 };
83 
84 struct PendingRequest {
85     std::promise<ServiceResponse> promise;
86 };
87 
88 using UnmarshalFunc = std::function<void(Parcel &, ServiceResponseData &)>;
89 struct ServiceResponseDataInner : public Parcelable {
90 public:
91     bool ReadFromParcel(Parcel &in);
92     bool Marshalling(Parcel &out) const override;
93     static ServiceResponseDataInner *Unmarshalling(Parcel &in);
94     ServiceResponseData data;
95 
96 private:
97     static const std::unordered_map<int32_t, UnmarshalFunc> UNMARSHAL_FUNCTION_MAP;
98 };
99 
100 struct ServiceResponseWriter {
101     Parcel &out;
102     bool result = true;
103 
ServiceResponseWriterServiceResponseWriter104     explicit ServiceResponseWriter(Parcel &parcel) : out(parcel)
105     {
106     }
107 
operatorServiceResponseWriter108     void operator()(std::monostate val)
109     {
110         IMSA_HILOGD("no need to marshal");
111         result = true;
112     }
operatorServiceResponseWriter113     void operator()(bool val)
114     {
115         result = out.WriteBool(val);
116     }
operatorServiceResponseWriter117     void operator()(int32_t val)
118     {
119         result = out.WriteInt32(val);
120     }
operatorServiceResponseWriter121     void operator()(uint32_t val)
122     {
123         result = out.WriteUint32(val);
124     }
operatorServiceResponseWriter125     void operator()(int64_t val)
126     {
127         result = out.WriteInt64(val);
128     }
operatorServiceResponseWriter129     void operator()(uint64_t val)
130     {
131         result = out.WriteUint64(val);
132     }
operatorServiceResponseWriter133     void operator()(sptr<IRemoteObject> val)
134     {
135         result = static_cast<MessageParcel *>(&out)->WriteRemoteObject(val);
136     }
operatorServiceResponseWriter137     void operator()(const Property &val)
138     {
139         result = val.Marshalling(out);
140     }
operatorServiceResponseWriter141     void operator()(const std::vector<Property> &val)
142     {
143         result = ResponseDataUtil::Marshall(val, out);
144     }
operatorServiceResponseWriter145     void operator()(const SubProperty &val)
146     {
147         result = val.Marshalling(out);
148     }
operatorServiceResponseWriter149     void operator()(const std::vector<SubProperty> &val)
150     {
151         result = ResponseDataUtil::Marshall(val, out);
152     }
operatorServiceResponseWriter153     void operator()(const StartInputResponse &val)
154     {
155         result = val.Marshalling(out);
156     }
operatorServiceResponseWriter157     void operator()(const InputStartInfo &val)
158     {
159         result = val.Marshalling(out);
160     }
161 };
162 } // namespace MiscServices
163 } // namespace OHOS
164 
165 #endif // IMF_FRAMEWORKS_SERVICE_RESPONSE_DATA_H
166