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 #define LOG_TAG "KVDBNotifierStub"
17 
18 #include "kvdb_notifier_stub.h"
19 #include <chrono>
20 #include <ctime>
21 #include <cinttypes>
22 #include <ipc_skeleton.h>
23 #include <map>
24 #include "itypes_util.h"
25 #include "log_print.h"
26 #include "message_parcel.h"
27 #include "message_option.h"
28 #include "store_util.h"
29 #include "types.h"
30 
31 namespace OHOS {
32 namespace DistributedKv {
33 const KVDBNotifierStub::Handler
34     KVDBNotifierStub::HANDLERS[static_cast<uint32_t>(KVDBNotifierCode::TRANS_BUTT)] = {
35     &KVDBNotifierStub::OnSyncCompleted,
36     &KVDBNotifierStub::OnCloudSyncCompleted,
37     &KVDBNotifierStub::OnOnRemoteChange,
38     &KVDBNotifierStub::OnOnSwitchChange,
39 };
40 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)41 int32_t KVDBNotifierStub::OnRemoteRequest(
42     uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
43 {
44     ZLOGI("code:%{public}u, callingPid:%{public}d", code, IPCSkeleton::GetCallingPid());
45     std::u16string local = KVDBNotifierStub::GetDescriptor();
46     std::u16string remote = data.ReadInterfaceToken();
47     if (local != remote) {
48         ZLOGE("local descriptor is not equal to remote");
49         return -1;
50     }
51     if (code >= static_cast<uint32_t>(KVDBNotifierCode::TRANS_HEAD) &&
52         code < static_cast<uint32_t>(KVDBNotifierCode::TRANS_BUTT) && HANDLERS[code] != nullptr) {
53         return (this->*HANDLERS[code])(data, reply);
54     }
55     ZLOGE("not support code:%{public}u, BUTT:%{public}d",
56         code, static_cast<uint32_t>(KVDBNotifierCode::TRANS_BUTT));
57     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
58 }
59 
OnSyncCompleted(MessageParcel & data,MessageParcel & reply)60 int32_t KVDBNotifierStub::OnSyncCompleted(MessageParcel& data, MessageParcel& reply)
61 {
62     std::map<std::string, Status> results;
63     uint64_t sequenceId;
64     if (!ITypesUtil::Unmarshal(data, results, sequenceId)) {
65         ZLOGE("Unmarshal results size:%{public}zu, sequenceId:%{public}" PRIu64, results.size(), sequenceId);
66         return IPC_STUB_INVALID_DATA_ERR;
67     }
68     SyncCompleted(std::move(results), sequenceId);
69     return ERR_NONE;
70 }
71 
OnCloudSyncCompleted(MessageParcel & data,MessageParcel & reply)72 int32_t KVDBNotifierStub::OnCloudSyncCompleted(MessageParcel& data, MessageParcel& reply)
73 {
74     ProgressDetail detail;
75     uint64_t sequenceId;
76     if (!ITypesUtil::Unmarshal(data, sequenceId, detail)) {
77         ZLOGE("Unmarshal sequenceId:%{public}" PRIu64, sequenceId);
78         return IPC_STUB_INVALID_DATA_ERR;
79     }
80     SyncCompleted(sequenceId, std::move(detail));
81     return ERR_NONE;
82 }
83 
OnOnRemoteChange(MessageParcel & data,MessageParcel & reply)84 int32_t KVDBNotifierStub::OnOnRemoteChange(MessageParcel& data, MessageParcel& reply)
85 {
86     std::map<std::string, bool> mask;
87     int32_t dataType;
88     if (!ITypesUtil::Unmarshal(data, mask, dataType)) {
89         ZLOGE("Unmarshal fail mask size:%{public}zu", mask.size());
90         return IPC_STUB_INVALID_DATA_ERR;
91     }
92     if (dataType < static_cast<int>(DataType::TYPE_STATICS) || dataType > static_cast<int>(DataType::TYPE_DYNAMICAL)) {
93         ZLOGE("Invalid dataType:%{public}d", dataType);
94         return IPC_STUB_INVALID_DATA_ERR;
95     }
96     OnRemoteChange(std::move(mask), dataType);
97     return ERR_NONE;
98 }
99 
OnOnSwitchChange(MessageParcel & data,MessageParcel & reply)100 int32_t KVDBNotifierStub::OnOnSwitchChange(MessageParcel& data, MessageParcel& reply)
101 {
102     SwitchNotification notification;
103     if (!ITypesUtil::Unmarshal(data, notification)) {
104         ZLOGE("Unmarshal fail");
105         return IPC_STUB_INVALID_DATA_ERR;
106     }
107     OnSwitchChange(std::move(notification));
108     return ERR_NONE;
109 }
110 }  // namespace DistributedKv
111 }  // namespace OHOS
112