• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "general_controller_provider_impl.h"
17 
18 #include "datashare_log.h"
19 #include "datashare_string_utils.h"
20 
21 namespace OHOS {
22 namespace DataShare {
Insert(const Uri & uri,const DataShareValuesBucket & value)23 int GeneralControllerProviderImpl::Insert(const Uri &uri, const DataShareValuesBucket &value)
24 {
25     auto connection = connection_;
26     if (connection == nullptr) {
27         LOG_ERROR("connection is nullptr");
28         return DATA_SHARE_ERROR;
29     }
30     auto proxy = connection->GetDataShareProxy(uri_, token_);
31     if (proxy == nullptr) {
32         LOG_ERROR("proxy is nullptr");
33         return DATA_SHARE_ERROR;
34     }
35     return proxy->Insert(uri, value);
36 }
37 
Update(const Uri & uri,const DataSharePredicates & predicates,const DataShareValuesBucket & value)38 int GeneralControllerProviderImpl::Update(const Uri &uri, const DataSharePredicates &predicates,
39     const DataShareValuesBucket &value)
40 {
41     auto connection = connection_;
42     if (connection == nullptr) {
43         LOG_ERROR("connection is nullptr");
44         return DATA_SHARE_ERROR;
45     }
46     auto proxy = connection->GetDataShareProxy(uri_, token_);
47     if (proxy == nullptr) {
48         LOG_ERROR("proxy is nullptr");
49         return DATA_SHARE_ERROR;
50     }
51     return proxy->Update(uri, predicates, value);
52 }
53 
Delete(const Uri & uri,const DataSharePredicates & predicates)54 int GeneralControllerProviderImpl::Delete(const Uri &uri, const DataSharePredicates &predicates)
55 {
56     auto connection = connection_;
57     if (connection == nullptr) {
58         LOG_ERROR("connection is nullptr");
59         return DATA_SHARE_ERROR;
60     }
61     auto proxy = connection->GetDataShareProxy(uri_, token_);
62     if (proxy == nullptr) {
63         LOG_ERROR("proxy is nullptr");
64         return DATA_SHARE_ERROR;
65     }
66     return proxy->Delete(uri, predicates);
67 }
68 
InsertEx(const Uri & uri,const DataShareValuesBucket & value)69 std::pair<int32_t, int32_t> GeneralControllerProviderImpl::InsertEx(const Uri &uri,
70     const DataShareValuesBucket &value)
71 {
72     auto connection = connection_;
73     if (connection == nullptr) {
74         LOG_ERROR("connection is nullptr");
75         return std::make_pair(DATA_SHARE_ERROR, 0);
76     }
77     auto proxy = connection->GetDataShareProxy(uri_, token_);
78     if (proxy == nullptr) {
79         LOG_ERROR("proxy is nullptr");
80         return std::make_pair(DATA_SHARE_ERROR, 0);
81     }
82     return proxy->InsertEx(uri, value);
83 }
84 
UpdateEx(const Uri & uri,const DataSharePredicates & predicates,const DataShareValuesBucket & value)85 std::pair<int32_t, int32_t> GeneralControllerProviderImpl::UpdateEx(
86     const Uri &uri, const DataSharePredicates &predicates, const DataShareValuesBucket &value)
87 {
88     auto connection = connection_;
89     if (connection == nullptr) {
90         LOG_ERROR("connection is nullptr");
91         return std::make_pair(DATA_SHARE_ERROR, 0);
92     }
93     auto proxy = connection->GetDataShareProxy(uri_, token_);
94     if (proxy == nullptr) {
95         LOG_ERROR("proxy is nullptr");
96         return std::make_pair(DATA_SHARE_ERROR, 0);
97     }
98     return proxy->UpdateEx(uri, predicates, value);
99 }
100 
DeleteEx(const Uri & uri,const DataSharePredicates & predicates)101 std::pair<int32_t, int32_t> GeneralControllerProviderImpl::DeleteEx(const Uri &uri,
102     const DataSharePredicates &predicates)
103 {
104     auto connection = connection_;
105     if (connection == nullptr) {
106         LOG_ERROR("connection is nullptr");
107         return std::make_pair(DATA_SHARE_ERROR, 0);
108     }
109     auto proxy = connection->GetDataShareProxy(uri_, token_);
110     if (proxy == nullptr) {
111         LOG_ERROR("proxy is nullptr");
112         return std::make_pair(DATA_SHARE_ERROR, 0);
113     }
114     return proxy->DeleteEx(uri, predicates);
115 }
116 
Query(const Uri & uri,const DataSharePredicates & predicates,std::vector<std::string> & columns,DatashareBusinessError & businessError,DataShareOption & option)117 std::shared_ptr<DataShareResultSet> GeneralControllerProviderImpl::Query(const Uri &uri,
118     const DataSharePredicates &predicates, std::vector<std::string> &columns,
119     DatashareBusinessError &businessError, DataShareOption &option)
120 {
121     (void)option;
122     auto connection = connection_;
123     if (connection == nullptr) {
124         LOG_ERROR("connection is nullptr");
125         return nullptr;
126     }
127     auto proxy = connection->GetDataShareProxy(uri_, token_);
128     if (proxy == nullptr) {
129         LOG_ERROR("proxy is nullptr");
130         return nullptr;
131     }
132     return proxy->Query(uri, predicates, columns, businessError);
133 }
134 
RegisterObserver(const Uri & uri,const sptr<AAFwk::IDataAbilityObserver> & dataObserver)135 int GeneralControllerProviderImpl::RegisterObserver(const Uri &uri,
136     const sptr<AAFwk::IDataAbilityObserver> &dataObserver)
137 {
138     auto connection = connection_;
139     if (connection == nullptr) {
140         LOG_ERROR("connection is nullptr");
141         return E_PROVIDER_CONN_NULL;
142     }
143     auto proxy = connection->GetDataShareProxy(uri_, token_);
144     if (proxy == nullptr) {
145         LOG_ERROR("proxy is nullptr");
146         return E_PROVIDER_NOT_CONNECTED;
147     }
148     // the non-silent proxy's RegisterObserver returns bool while this function returns int and 0 means ok
149     int ret = proxy->RegisterObserver(uri, dataObserver) ? E_OK : E_REGISTER_ERROR;
150     LOG_INFO("Register non-silent observer ret: %{public}d, uri: %{public}s", ret,
151         DataShareStringUtils::Anonymous(uri.ToString()).c_str());
152     return ret;
153 }
154 
UnregisterObserver(const Uri & uri,const sptr<AAFwk::IDataAbilityObserver> & dataObserver)155 int GeneralControllerProviderImpl::UnregisterObserver(const Uri &uri,
156     const sptr<AAFwk::IDataAbilityObserver> &dataObserver)
157 {
158     auto connection = connection_;
159     if (connection == nullptr) {
160         LOG_ERROR("connection is nullptr");
161         return E_PROVIDER_CONN_NULL;
162     }
163     auto proxy = connection->GetDataShareProxy(uri_, token_);
164     if (proxy == nullptr) {
165         LOG_ERROR("proxy is nullptr");
166         return E_PROVIDER_NOT_CONNECTED;
167     }
168     // the non-silent proxy's UnregisterObserver returns bool while this function returns int and 0 means ok
169     int ret = proxy->UnregisterObserver(uri, dataObserver) ? E_OK : E_REGISTER_ERROR;
170     LOG_INFO("Unregister non-silent observer ret: %{public}d, uri: %{public}s", ret,
171         DataShareStringUtils::Anonymous(uri.ToString()).c_str());
172     return ret;
173 }
174 
NotifyChange(const Uri & uri)175 void GeneralControllerProviderImpl::NotifyChange(const Uri &uri)
176 {
177     auto connection = connection_;
178     if (connection == nullptr) {
179         LOG_ERROR("connection is nullptr");
180         return;
181     }
182     auto proxy = connection->GetDataShareProxy(uri_, token_);
183     if (proxy == nullptr) {
184         LOG_ERROR("proxy is nullptr");
185         return;
186     }
187     proxy->NotifyChange(uri);
188 }
189 
190 // This function is supported only when using non-silent DataShareHelper
RegisterObserverExtProvider(const Uri & uri,const sptr<AAFwk::IDataAbilityObserver> & dataObserver,bool isDescendants)191 int GeneralControllerProviderImpl::RegisterObserverExtProvider(const Uri &uri,
192     const sptr<AAFwk::IDataAbilityObserver> &dataObserver, bool isDescendants)
193 {
194     auto connection = connection_;
195     if (connection == nullptr) {
196         LOG_ERROR("connection is nullptr");
197         return E_PROVIDER_CONN_NULL;
198     }
199     auto proxy = connection->GetDataShareProxy(uri_, token_);
200     if (proxy == nullptr) {
201         LOG_ERROR("proxy is nullptr");
202         return E_PROVIDER_NOT_CONNECTED;
203     }
204     // the non-silent proxy's RegisterObserverExtProvider returns int and 0 means ok
205     int ret = proxy->RegisterObserverExtProvider(uri, dataObserver, isDescendants, { false });
206     LOG_INFO("Register non-silent observerExt provider ret: %{public}d, uri: %{public}s", ret,
207         DataShareStringUtils::Anonymous(uri.ToString()).c_str());
208     // store the observer when successfully registered.
209     if (ret == E_OK) {
210         connection->UpdateObserverExtsProviderMap(uri, dataObserver, isDescendants);
211     }
212     return ret;
213 }
214 
215 // This function is supported only when using non-silent DataShareHelper
UnregisterObserverExtProvider(const Uri & uri,const sptr<AAFwk::IDataAbilityObserver> & dataObserver)216 int GeneralControllerProviderImpl::UnregisterObserverExtProvider(const Uri &uri,
217     const sptr<AAFwk::IDataAbilityObserver> &dataObserver)
218 {
219     auto connection = connection_;
220     if (connection == nullptr) {
221         LOG_ERROR("connection is nullptr");
222         return E_PROVIDER_CONN_NULL;
223     }
224     auto proxy = connection->GetDataShareProxy(uri_, token_);
225     if (proxy == nullptr) {
226         LOG_ERROR("proxy is nullptr");
227         return E_PROVIDER_NOT_CONNECTED;
228     }
229     // the non-silent proxy's UnregisterObserverExtProvider returns int and 0 means ok
230     int ret = proxy->UnregisterObserverExtProvider(uri, dataObserver);
231     LOG_INFO("Unregister non-silent observerExt provider ret: %{public}d, uri: %{public}s", ret,
232         DataShareStringUtils::Anonymous(uri.ToString()).c_str());
233     // remove the observer from storage when successfully unregistered.
234     if (ret == E_OK) {
235         connection->DeleteObserverExtsProviderMap(uri, dataObserver);
236     }
237     return ret;
238 }
239 
240 // This function is supported only when using non-silent DataShareHelper
NotifyChangeExtProvider(const ChangeInfo & changeInfo)241 int GeneralControllerProviderImpl::NotifyChangeExtProvider(const ChangeInfo &changeInfo)
242 {
243     auto connection = connection_;
244     if (connection == nullptr) {
245         LOG_ERROR("connection is nullptr");
246         return E_PROVIDER_CONN_NULL;
247     }
248     auto proxy = connection->GetDataShareProxy(uri_, token_);
249     if (proxy == nullptr) {
250         LOG_ERROR("proxy is nullptr");
251         return E_PROVIDER_NOT_CONNECTED;
252     }
253     // the non-silent proxy's NotifyChangeExtProvider returns int and 0 means ok
254     int ret = proxy->NotifyChangeExtProvider(changeInfo);
255     LOG_INFO("NotifyChangeExtProvider ret: %{public}d", ret);
256     return ret;
257 }
258 
GeneralControllerProviderImpl(std::shared_ptr<DataShareConnection> connection,const Uri & uri,const sptr<IRemoteObject> & token)259 GeneralControllerProviderImpl::GeneralControllerProviderImpl(std::shared_ptr<DataShareConnection> connection,
260     const Uri &uri, const sptr<IRemoteObject> &token) : connection_(connection), token_(token), uri_(uri)
261 {
262 }
263 } // namespace DataShare
264 } // namespace OHOS