1 /*
2 * Copyright (c) 2021 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 #define LOG_TAG "ISharedResultSetStub"
16 #include "ishared_result_set_stub.h"
17
18 #include <future>
19
20 #include "logger.h"
21 #include "rdb_errno.h"
22 #include "shared_block.h"
23 namespace OHOS::NativeRdb {
24 using namespace OHOS::Rdb;
25
26 std::function<sptr<ISharedResultSet>(std::shared_ptr<AbsSharedResultSet>, MessageParcel &)>
27 ISharedResultSet::providerCreator_ = ISharedResultSetStub::CreateStub;
28 constexpr ISharedResultSetStub::Handler ISharedResultSetStub::handlers[static_cast<uint32_t>(ResultSetCode::FUNC_BUTT)];
29
CreateStub(std::shared_ptr<AbsSharedResultSet> result,OHOS::MessageParcel & parcel)30 sptr<ISharedResultSet> ISharedResultSetStub::CreateStub(
31 std::shared_ptr<AbsSharedResultSet> result, OHOS::MessageParcel &parcel)
32 {
33 sptr<ISharedResultSet> stub = new (std::nothrow) ISharedResultSetStub(result);
34 if (stub == nullptr) {
35 LOG_ERROR("Stub is nullptr.");
36 return nullptr;
37 }
38 if (result == nullptr) {
39 LOG_ERROR("Result is nullptr.");
40 return nullptr;
41 }
42 parcel.WriteRemoteObject(stub->AsObject());
43 auto block = result->GetBlock();
44 if (block != nullptr) {
45 block->WriteMessageParcel(parcel);
46 }
47 return stub;
48 }
49
ISharedResultSetStub(std::shared_ptr<AbsSharedResultSet> resultSet)50 ISharedResultSetStub::ISharedResultSetStub(std::shared_ptr<AbsSharedResultSet> resultSet)
51 : resultSet_(std::move(resultSet))
52 {
53 }
~ISharedResultSetStub()54 ISharedResultSetStub::~ISharedResultSetStub()
55 {
56 }
57
OnRemoteRequest(uint32_t code,OHOS::MessageParcel & data,OHOS::MessageParcel & reply,OHOS::MessageOption & option)58 int ISharedResultSetStub::OnRemoteRequest(
59 uint32_t code, OHOS::MessageParcel &data, OHOS::MessageParcel &reply, OHOS::MessageOption &option)
60 {
61 if (GetDescriptor() != data.ReadInterfaceToken()) {
62 LOG_ERROR("IPC descriptor is not equal.");
63 return INVALID_FD;
64 }
65
66 if (code >= static_cast<uint32_t>(ResultSetCode::FUNC_BUTT)) {
67 LOG_ERROR("OnRemoteRequest method code(%{public}d) out of range.", code);
68 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
69 }
70 Handler handler = handlers[code];
71 if (handler == nullptr) {
72 LOG_ERROR("OnRemoteRequest method code(%{public}d) is not support.", code);
73 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
74 }
75 return (this->*handler)(data, reply);
76 }
77
HandleGetRowCountRequest(MessageParcel & data,MessageParcel & reply)78 int ISharedResultSetStub::HandleGetRowCountRequest(MessageParcel &data, MessageParcel &reply)
79 {
80 int count = -1;
81 int errCode = resultSet_->GetRowCount(count);
82 reply.WriteInt32(errCode);
83 if (errCode == E_OK) {
84 reply.WriteInt32(count);
85 }
86 LOG_DEBUG("HandleGetRowCountRequest call %{public}d", errCode);
87 return NO_ERROR;
88 }
89
HandleGetColumnNamesRequest(MessageParcel & data,MessageParcel & reply)90 int ISharedResultSetStub::HandleGetColumnNamesRequest(MessageParcel &data, MessageParcel &reply)
91 {
92 auto [errCode, names] = GetColumnNames();
93 reply.WriteInt32(errCode);
94 if (errCode == E_OK) {
95 reply.WriteStringVector(names);
96 }
97 LOG_DEBUG("HandleGetColumnNamesRequest call %{public}d", errCode);
98 return NO_ERROR;
99 }
100
HandleOnGoRequest(MessageParcel & data,MessageParcel & reply)101 int ISharedResultSetStub::HandleOnGoRequest(MessageParcel &data, MessageParcel &reply)
102 {
103 int oldRow = data.ReadInt32();
104 int newRow = data.ReadInt32();
105 int errCode = resultSet_->OnGo(oldRow, newRow);
106 reply.WriteInt32(errCode);
107 LOG_DEBUG("HandleOnGoRequest call %{public}d", errCode);
108 return NO_ERROR;
109 }
110
HandleCloseRequest(MessageParcel & data,MessageParcel & reply)111 int ISharedResultSetStub::HandleCloseRequest(MessageParcel &data, MessageParcel &reply)
112 {
113 int errCode = resultSet_->Close();
114 reply.WriteInt32(errCode);
115 LOG_DEBUG("HandleCloseRequest call %{public}d", errCode);
116 return NO_ERROR;
117 }
118
GetColumnNames()119 std::pair<int, std::vector<std::string>> ISharedResultSetStub::GetColumnNames()
120 {
121 return resultSet_->GetColumnNames();
122 }
123 } // namespace OHOS::NativeRdb