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
OpenRawFile(const Uri & uri,const std::string & mode)37 int ExtSpecialController::OpenRawFile(const Uri &uri, const std::string &mode)
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->OpenRawFile(uri, mode);
50 }
51
GetType(const Uri & uri)52 std::string ExtSpecialController::GetType(const Uri &uri)
53 {
54 auto connection = connection_;
55 if (connection == nullptr) {
56 LOG_ERROR("connection is nullptr");
57 return "";
58 }
59 auto proxy = connection->GetDataShareProxy(uri_, token_);
60 if (proxy == nullptr) {
61 LOG_ERROR("proxy is nullptr");
62 return "";
63 }
64 return proxy->GetType(uri);
65 }
66
BatchInsert(const Uri & uri,const std::vector<DataShareValuesBucket> & values)67 int ExtSpecialController::BatchInsert(const Uri &uri, const std::vector<DataShareValuesBucket> &values)
68 {
69 auto connection = connection_;
70 if (connection == nullptr) {
71 LOG_ERROR("connection is nullptr");
72 return INVALID_VALUE;
73 }
74 auto proxy = connection->GetDataShareProxy(uri_, token_);
75 if (proxy == nullptr) {
76 LOG_ERROR("proxy is nullptr");
77 return INVALID_VALUE;
78 }
79 return proxy->BatchInsert(uri, values);
80 }
81
InsertExt(Uri & uri,const DataShareValuesBucket & value,std::string & result)82 int ExtSpecialController::InsertExt(Uri &uri, const DataShareValuesBucket &value, std::string &result)
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->InsertExt(uri, value, result);
95 }
96
ExecuteBatch(const std::vector<OperationStatement> & statements,ExecResultSet & result)97 int ExtSpecialController::ExecuteBatch(const std::vector<OperationStatement> &statements, ExecResultSet &result)
98 {
99 auto connection = connection_;
100 if (connection == nullptr) {
101 LOG_ERROR("connection is nullptr");
102 return INVALID_VALUE;
103 }
104 auto proxy = connection->GetDataShareProxy(uri_, token_);
105 if (proxy == nullptr) {
106 LOG_ERROR("proxy is nullptr");
107 return INVALID_VALUE;
108 }
109 return proxy->ExecuteBatch(statements, result);
110 }
111
NormalizeUri(const Uri & uri)112 Uri ExtSpecialController::NormalizeUri(const Uri &uri)
113 {
114 auto connection = connection_;
115 if (connection == nullptr) {
116 LOG_ERROR("connection is nullptr");
117 return Uri("");
118 }
119 auto proxy = connection->GetDataShareProxy(uri_, token_);
120 if (proxy == nullptr) {
121 LOG_ERROR("proxy is nullptr");
122 return Uri("");
123 }
124 return proxy->NormalizeUri(uri);
125 }
126
DenormalizeUri(const Uri & uri)127 Uri ExtSpecialController::DenormalizeUri(const Uri &uri)
128 {
129 auto connection = connection_;
130 if (connection == nullptr) {
131 LOG_ERROR("connection is nullptr");
132 return Uri("");
133 }
134 auto proxy = connection->GetDataShareProxy(uri_, token_);
135 if (proxy == nullptr) {
136 LOG_ERROR("proxy is nullptr");
137 return Uri("");
138 }
139 return proxy->DenormalizeUri(uri);
140 }
141
GetFileTypes(const Uri & uri,const std::string & mimeTypeFilter)142 std::vector<std::string> ExtSpecialController::GetFileTypes(const Uri &uri, const std::string &mimeTypeFilter)
143 {
144 auto connection = connection_;
145 if (connection == nullptr) {
146 LOG_ERROR("connection is nullptr");
147 return std::vector<std::string>();
148 }
149 auto proxy = connection->GetDataShareProxy(uri_, token_);
150 if (proxy == nullptr) {
151 LOG_ERROR("proxy is nullptr");
152 return std::vector<std::string>();
153 }
154 return proxy->GetFileTypes(uri, mimeTypeFilter);
155 }
156
ExtSpecialController(std::shared_ptr<DataShareConnection> connection,const Uri & uri,const sptr<IRemoteObject> & token)157 ExtSpecialController::ExtSpecialController(std::shared_ptr<DataShareConnection> connection, const Uri &uri,
158 const sptr<IRemoteObject> &token)
159 : connection_(connection), token_(token), uri_(uri)
160 {
161 }
162 } // namespace DataShare
163 } // namespace OHOS
164