1 /*
2 * Copyright (c) 2023 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 #include "ext_special_controller.h"
17 #include "datashare_log.h"
18
19 namespace OHOS {
20 namespace DataShare {
21 constexpr int INVALID_VALUE = -1;
OpenFile(const Uri & uri,const std::string & mode)22 int ExtSpecialController::OpenFile(const Uri &uri, const std::string &mode)
23 {
24 auto connection = connection_;
25 if (connection == nullptr) {
26 LOG_ERROR("connection is nullptr");
27 return INVALID_VALUE;
28 }
29 auto proxy = connection->GetDataShareProxy(uri_, token_);
30 if (proxy == nullptr) {
31 LOG_ERROR("proxy is nullptr");
32 return INVALID_VALUE;
33 }
34 return proxy->OpenFile(uri, mode);
35 }
36
OpenFileWithErrCode(const Uri & uri,const std::string & mode,int32_t & errCode)37 int ExtSpecialController::OpenFileWithErrCode(const Uri &uri, const std::string &mode, int32_t &errCode)
38 {
39 auto connection = connection_;
40 if (connection == nullptr) {
41 LOG_ERROR("connection is nullptr");
42 return INVALID_VALUE;
43 }
44 auto proxy = connection->GetDataShareProxy(uri_, token_);
45 if (proxy == nullptr) {
46 LOG_ERROR("proxy is nullptr");
47 return INVALID_VALUE;
48 }
49 return proxy->OpenFileWithErrCode(uri, mode, errCode);
50 }
51
OpenRawFile(const Uri & uri,const std::string & mode)52 int ExtSpecialController::OpenRawFile(const Uri &uri, const std::string &mode)
53 {
54 auto connection = connection_;
55 if (connection == nullptr) {
56 LOG_ERROR("connection is nullptr");
57 return INVALID_VALUE;
58 }
59 auto proxy = connection->GetDataShareProxy(uri_, token_);
60 if (proxy == nullptr) {
61 LOG_ERROR("proxy is nullptr");
62 return INVALID_VALUE;
63 }
64 return proxy->OpenRawFile(uri, mode);
65 }
66
GetType(const Uri & uri)67 std::string ExtSpecialController::GetType(const Uri &uri)
68 {
69 auto connection = connection_;
70 if (connection == nullptr) {
71 LOG_ERROR("connection is nullptr");
72 return "";
73 }
74 auto proxy = connection->GetDataShareProxy(uri_, token_);
75 if (proxy == nullptr) {
76 LOG_ERROR("proxy is nullptr");
77 return "";
78 }
79 return proxy->GetType(uri);
80 }
81
BatchInsert(const Uri & uri,const std::vector<DataShareValuesBucket> & values)82 int ExtSpecialController::BatchInsert(const Uri &uri, const std::vector<DataShareValuesBucket> &values)
83 {
84 auto connection = connection_;
85 if (connection == nullptr) {
86 LOG_ERROR("connection is nullptr");
87 return INVALID_VALUE;
88 }
89 auto proxy = connection->GetDataShareProxy(uri_, token_);
90 if (proxy == nullptr) {
91 LOG_ERROR("proxy is nullptr");
92 return INVALID_VALUE;
93 }
94 return proxy->BatchInsert(uri, values);
95 }
96
BatchUpdate(const UpdateOperations & operations,std::vector<BatchUpdateResult> & results)97 int ExtSpecialController::BatchUpdate(const UpdateOperations &operations,
98 std::vector<BatchUpdateResult> &results)
99 {
100 auto connection = connection_;
101 if (connection == nullptr) {
102 LOG_ERROR("connection is nullptr");
103 return INVALID_VALUE;
104 }
105 auto proxy = connection->GetDataShareProxy(uri_, token_);
106 if (proxy == nullptr) {
107 LOG_ERROR("proxy is nullptr");
108 return INVALID_VALUE;
109 }
110 return proxy->BatchUpdate(operations, results);
111 }
112
InsertExt(Uri & uri,const DataShareValuesBucket & value,std::string & result)113 int ExtSpecialController::InsertExt(Uri &uri, const DataShareValuesBucket &value, std::string &result)
114 {
115 auto connection = connection_;
116 if (connection == nullptr) {
117 LOG_ERROR("connection is nullptr");
118 return INVALID_VALUE;
119 }
120 auto proxy = connection->GetDataShareProxy(uri_, token_);
121 if (proxy == nullptr) {
122 LOG_ERROR("proxy is nullptr");
123 return INVALID_VALUE;
124 }
125 return proxy->InsertExt(uri, value, result);
126 }
127
ExecuteBatch(const std::vector<OperationStatement> & statements,ExecResultSet & result)128 int ExtSpecialController::ExecuteBatch(const std::vector<OperationStatement> &statements, ExecResultSet &result)
129 {
130 auto connection = connection_;
131 if (connection == nullptr) {
132 LOG_ERROR("connection is nullptr");
133 return INVALID_VALUE;
134 }
135 auto proxy = connection->GetDataShareProxy(uri_, token_);
136 if (proxy == nullptr) {
137 LOG_ERROR("proxy is nullptr");
138 return INVALID_VALUE;
139 }
140 return proxy->ExecuteBatch(statements, result);
141 }
142
NormalizeUri(const Uri & uri)143 Uri ExtSpecialController::NormalizeUri(const Uri &uri)
144 {
145 auto connection = connection_;
146 if (connection == nullptr) {
147 LOG_ERROR("connection is nullptr");
148 return Uri("");
149 }
150 auto proxy = connection->GetDataShareProxy(uri_, token_);
151 if (proxy == nullptr) {
152 LOG_ERROR("proxy is nullptr");
153 return Uri("");
154 }
155 return proxy->NormalizeUri(uri);
156 }
157
DenormalizeUri(const Uri & uri)158 Uri ExtSpecialController::DenormalizeUri(const Uri &uri)
159 {
160 auto connection = connection_;
161 if (connection == nullptr) {
162 LOG_ERROR("connection is nullptr");
163 return Uri("");
164 }
165 auto proxy = connection->GetDataShareProxy(uri_, token_);
166 if (proxy == nullptr) {
167 LOG_ERROR("proxy is nullptr");
168 return Uri("");
169 }
170 return proxy->DenormalizeUri(uri);
171 }
172
GetFileTypes(const Uri & uri,const std::string & mimeTypeFilter)173 std::vector<std::string> ExtSpecialController::GetFileTypes(const Uri &uri, const std::string &mimeTypeFilter)
174 {
175 auto connection = connection_;
176 if (connection == nullptr) {
177 LOG_ERROR("connection is nullptr");
178 return std::vector<std::string>();
179 }
180 auto proxy = connection->GetDataShareProxy(uri_, token_);
181 if (proxy == nullptr) {
182 LOG_ERROR("proxy is nullptr");
183 return std::vector<std::string>();
184 }
185 return proxy->GetFileTypes(uri, mimeTypeFilter);
186 }
187
UserDefineFunc(MessageParcel & data,MessageParcel & reply,MessageOption & option)188 int32_t ExtSpecialController::UserDefineFunc(
189 MessageParcel &data, MessageParcel &reply, MessageOption &option)
190 {
191 auto connection = connection_;
192 if (connection == nullptr) {
193 LOG_ERROR("connection is nullptr");
194 return INVALID_VALUE;
195 }
196 auto proxy = connection->GetDataShareProxy(uri_, token_);
197 if (proxy == nullptr) {
198 LOG_ERROR("proxy is nullptr");
199 return INVALID_VALUE;
200 }
201 return proxy->UserDefineFunc(data, reply, option);
202 }
203
ExtSpecialController(std::shared_ptr<DataShareConnection> connection,const Uri & uri,const sptr<IRemoteObject> & token)204 ExtSpecialController::ExtSpecialController(
205 std::shared_ptr<DataShareConnection> connection, const Uri &uri, const sptr<IRemoteObject> &token)
206 : connection_(connection), token_(token), uri_(uri)
207 {
208 }
209 } // namespace DataShare
210 } // namespace OHOS
211