• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025-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 ANTIFRAUD_HSDR_HELPER_H
17 #define ANTIFRAUD_HSDR_HELPER_H
18 
19 #include <functional>
20 #include <mutex>
21 
22 #include "ability_connect_callback_stub.h"
23 #include "ffrt.h"
24 #include "iremote_broker.h"
25 #include "iremote_object.h"
26 #include "iremote_proxy.h"
27 #include "message_parcel.h"
28 #include "singleton.h"
29 
30 namespace OHOS {
31 namespace Telephony {
32 const std::string HSDR_BUNDLE_NAME = "com.hmos.hsdr";
33 const std::string HSDR_ABILITY_NAME = "HSDRService";
34 constexpr int HSDR_USERID = 100;
35 
36 enum class HsdrCommands {
37     COMMAND_QUERY_CURRENT_INFO = 1,
38     COMMAND_CLOUD_CONNECT = 2,
39     COMMAND_UCS_REQUEST = 3,
40     COMMAND_FILE_REQUEST = 4,
41 };
42 
43 enum class UcsMethod {
44     Encrypt = 0,
45     Decrypt = 1,
46     Sign = 2,
47     Unsign = 3,
48 };
49 
50 struct HsdrRequest {
51 public:
52     std::string requestId_;
53     std::string serviceName_;
54     HsdrCommands command_;
55     std::string body_;
56 };
57 
58 struct HsdrResponse {
59 public:
60     std::string requestId_;
61     std::string body_;
62 };
63 
64 using ConnectedCallback = std::function<void(sptr<IRemoteObject>)>;
65 using OnResponse = std::function<void(const HsdrResponse &)>;
66 using OnError = std::function<void(int)>;
67 
68 class HsdrCallback : public IRemoteBroker {
69 public:
70     DECLARE_INTERFACE_DESCRIPTOR(u"OHOS.Security.CloudConnectCallback");
71 };
72 
73 class HsdrCallbackStub : public IRemoteStub<HsdrCallback> {
74 public:
HsdrCallbackStub(const std::string & requestId,OnResponse onResponse,OnError onError)75     HsdrCallbackStub(const std::string &requestId, OnResponse onResponse, OnError onError)
76         : requestId_(requestId), onResponse_(onResponse), onError_(onError) {}
77 
78     ~HsdrCallbackStub() = default;
79 
80     int32_t OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override;
81 
82 private:
83     std::string requestId_;
84     OnResponse onResponse_;
85     OnError onError_;
86 };
87 
88 class HsdrConnection : public AAFwk::AbilityConnectionStub {
89 public:
HsdrConnection(ConnectedCallback connectedCallback)90     explicit HsdrConnection(ConnectedCallback connectedCallback) : connectedCallback_(connectedCallback) {}
91 
92     ~HsdrConnection() = default;
93 
94     void OnAbilityConnectDone(
95         const AppExecFwk::ElementName &element, const sptr<IRemoteObject> &remoteObject, int resultCode) override;
96 
97     void OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int resultCode) override;
98 
99     bool IsAlive();
100 
101     sptr<IRemoteObject> GetAbilityProxy();
102 
103 private:
104     ConnectedCallback connectedCallback_;
105     ffrt::mutex remoteProxyMutex_;
106     sptr<IRemoteObject> remoteObject_;
107 };
108 
109 class HsdrHelper {
110 DECLARE_DELAYED_REF_SINGLETON(HsdrHelper);
111 public:
112     int ConnectHsdr(ConnectedCallback connectedCallback);
113 
114     void DisconnectHsdr();
115 
116 private:
117     ffrt::mutex connectionMutex_;
118     sptr<HsdrConnection> connection_;
119 };
120 
121 class HsdrInterface : public IRemoteBroker {
122 public:
123     DECLARE_INTERFACE_DESCRIPTOR(u"OHOS.Security.HSDR");
124 
125     virtual int RequestHsdrServiceSync(const HsdrRequest &request, std::string &result) = 0;
126 
127     virtual int RequestHsdrServiceAsync(sptr<IRemoteObject> callbackStub, const HsdrRequest &request) = 0;
128 };
129 
130 class HsdrProxy : IRemoteProxy<HsdrInterface> {
131 public:
HsdrProxy(const sptr<IRemoteObject> & remote)132     explicit HsdrProxy(const sptr<IRemoteObject> &remote) : IRemoteProxy<HsdrInterface>(remote) {}
133 
~HsdrProxy()134     virtual ~HsdrProxy() {}
135 
136     virtual int RequestHsdrServiceSync(const HsdrRequest &request, std::string &result);
137 
138     virtual int RequestHsdrServiceAsync(sptr<IRemoteObject> callbackStub, const HsdrRequest &request);
139 private:
140     bool WriteMessageParcel(MessageParcel &data, const HsdrRequest &request);
141 };
142 
143 } // namespace Telephony
144 } // namespace OHOS
145 
146 #endif