• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 #define LOG_TAG "RdbNotifier"
17 
18 #include "rdb_notifier.h"
19 #include <ipc_skeleton.h>
20 #include "itypes_util.h"
21 #include "log_print.h"
22 
23 
24 namespace OHOS::DistributedRdb {
RdbNotifierProxy(const sptr<IRemoteObject> & object)25 RdbNotifierProxy::RdbNotifierProxy(const sptr<IRemoteObject> &object)
26     : IRemoteProxy<IRdbNotifier>(object)
27 {
28     ZLOGI("construct");
29 }
30 
~RdbNotifierProxy()31 RdbNotifierProxy::~RdbNotifierProxy() noexcept
32 {
33     ZLOGI("destroy");
34 }
35 
OnComplete(uint32_t seqNum,const SyncResult & result)36 int32_t RdbNotifierProxy::OnComplete(uint32_t seqNum, const SyncResult &result)
37 {
38     MessageParcel data;
39     if (!data.WriteInterfaceToken(IRdbNotifier::GetDescriptor())) {
40         ZLOGE("write descriptor failed");
41         return RDB_ERROR;
42     }
43     if (!data.WriteUint32(seqNum)) {
44         ZLOGE("write seq num failed");
45         return RDB_ERROR;
46     }
47     if (!DistributedKv::ITypesUtil::Marshalling(result, data)) {
48         return RDB_ERROR;
49     }
50 
51     MessageParcel reply;
52     MessageOption option(MessageOption::TF_ASYNC);
53     if (Remote()->SendRequest(RDB_NOTIFIER_CMD_SYNC_COMPLETE, data, reply, option) != 0) {
54         ZLOGE("send request failed");
55         return RDB_ERROR;
56     }
57     return RDB_OK;
58 }
59 
OnChange(const std::string & storeName,const std::vector<std::string> & devices)60 int RdbNotifierProxy::OnChange(const std::string& storeName, const std::vector<std::string> &devices)
61 {
62     MessageParcel data;
63     if (!data.WriteInterfaceToken(IRdbNotifier::GetDescriptor())) {
64         ZLOGE("write descriptor failed");
65         return RDB_ERROR;
66     }
67     if (!data.WriteString(storeName)) {
68         ZLOGE("write store name failed");
69         return RDB_ERROR;
70     }
71     if (!data.WriteStringVector(devices)) {
72         ZLOGE("write devices failed");
73         return RDB_ERROR;
74     }
75 
76     MessageParcel reply;
77     MessageOption option(MessageOption::TF_ASYNC);
78     if (Remote()->SendRequest(RDB_NOTIFIER_CMD_DATA_CHANGE, data, reply, option) != 0) {
79         ZLOGE("send request failed");
80         return RDB_ERROR;
81     }
82     return RDB_OK;
83 }
84 
RdbNotifierStub(const RdbSyncCompleteNotifier & completeNotifier,const RdbDataChangeNotifier & changeNotifier)85 RdbNotifierStub::RdbNotifierStub(const RdbSyncCompleteNotifier& completeNotifier,
86                                  const RdbDataChangeNotifier& changeNotifier)
87     : IRemoteStub<IRdbNotifier>(), completeNotifier_(completeNotifier), changeNotifier_(changeNotifier)
88 {
89     ZLOGI("construct");
90 }
91 
~RdbNotifierStub()92 RdbNotifierStub::~RdbNotifierStub() noexcept
93 {
94     ZLOGI("destroy");
95 }
96 
CheckInterfaceToken(MessageParcel & data)97 bool RdbNotifierStub::CheckInterfaceToken(MessageParcel& data)
98 {
99     auto localDescriptor = IRdbNotifier::GetDescriptor();
100     auto remoteDescriptor = data.ReadInterfaceToken();
101     if (remoteDescriptor != localDescriptor) {
102         ZLOGE("interface token is not equal");
103         return false;
104     }
105     return true;
106 }
107 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)108 int RdbNotifierStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
109                                      MessageOption &option)
110 {
111     ZLOGD("code:%{public}u, callingPid:%{public}d", code, IPCSkeleton::GetCallingPid());
112     if (!CheckInterfaceToken(data)) {
113         return RDB_ERROR;
114     }
115 
116     if (code >= 0 && code < RDB_NOTIFIER_CMD_MAX) {
117         return (this->*HANDLES[code])(data, reply);
118     }
119 
120     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
121 }
122 
OnCompleteInner(MessageParcel & data,MessageParcel & reply)123 int32_t RdbNotifierStub::OnCompleteInner(MessageParcel &data, MessageParcel &reply)
124 {
125     uint32_t seqNum;
126     if (!data.ReadUint32(seqNum)) {
127         ZLOGI("read seq num failed");
128         return RDB_ERROR;
129     }
130     SyncResult result;
131     if (!DistributedKv::ITypesUtil::Unmarshal(data, result)) {
132         ZLOGE("read sync result failed");
133         return RDB_ERROR;
134     }
135     return OnComplete(seqNum, result);
136 }
137 
OnComplete(uint32_t seqNum,const SyncResult & result)138 int32_t RdbNotifierStub::OnComplete(uint32_t seqNum, const SyncResult &result)
139 {
140     if (completeNotifier_) {
141         completeNotifier_(seqNum, result);
142     }
143     return RDB_OK;
144 }
145 
OnChangeInner(MessageParcel & data,MessageParcel & reply)146 int32_t RdbNotifierStub::OnChangeInner(MessageParcel &data, MessageParcel &reply)
147 {
148     std::string storeName;
149     if (!data.ReadString(storeName)) {
150         ZLOGE("read store name failed");
151         return RDB_ERROR;
152     }
153     std::vector<std::string> devices;
154     if (!data.ReadStringVector(&devices)) {
155         ZLOGE("read devices failed");
156         return RDB_ERROR;
157     }
158     return OnChange(storeName, devices);
159 }
160 
OnChange(const std::string & storeName,const std::vector<std::string> & devices)161 int32_t RdbNotifierStub::OnChange(const std::string& storeName, const std::vector<std::string> &devices)
162 {
163     if (changeNotifier_) {
164         changeNotifier_(storeName, devices);
165     }
166     return RDB_OK;
167 }
168 } // namespace OHOS::DistributedRdb
169