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