• 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_service_impl.h"
17 
18 #include "dataobs_mgr_client.h"
19 #include "datashare_log.h"
20 
21 namespace OHOS {
22 namespace DataShare {
23 constexpr int INVALID_VALUE = -1;
Insert(const Uri & uri,const DataShareValuesBucket & value)24 int GeneralControllerServiceImpl::Insert(const Uri &uri, const DataShareValuesBucket &value)
25 {
26     auto proxy = DataShareManagerImpl::GetInstance().GetServiceProxy();
27     if (proxy == nullptr) {
28         LOG_ERROR("proxy is nullptr");
29         return INVALID_VALUE;
30     }
31     return proxy->Insert(uri, value);
32 }
33 
Update(const Uri & uri,const DataSharePredicates & predicates,const DataShareValuesBucket & value)34 int GeneralControllerServiceImpl::Update(const Uri &uri, const DataSharePredicates &predicates,
35     const DataShareValuesBucket &value)
36 {
37     auto proxy = DataShareManagerImpl::GetInstance().GetServiceProxy();
38     if (proxy == nullptr) {
39         LOG_ERROR("proxy is nullptr");
40         return INVALID_VALUE;
41     }
42     return proxy->Update(uri, predicates, value);
43 }
44 
Delete(const Uri & uri,const DataSharePredicates & predicates)45 int GeneralControllerServiceImpl::Delete(const Uri &uri, const DataSharePredicates &predicates)
46 {
47     auto proxy = DataShareManagerImpl::GetInstance().GetServiceProxy();
48     if (proxy == nullptr) {
49         LOG_ERROR("proxy is nullptr");
50         return INVALID_VALUE;
51     }
52     return proxy->Delete(uri, predicates);
53 }
54 
Query(const Uri & uri,const DataSharePredicates & predicates,std::vector<std::string> & columns,DatashareBusinessError & businessError)55 std::shared_ptr<DataShareResultSet> GeneralControllerServiceImpl::Query(const Uri &uri,
56     const DataSharePredicates &predicates, std::vector<std::string> &columns, DatashareBusinessError &businessError)
57 {
58     auto proxy = DataShareManagerImpl::GetInstance().GetServiceProxy();
59     if (proxy == nullptr) {
60         LOG_ERROR("proxy is nullptr");
61         return nullptr;
62     }
63     return proxy->Query(uri, predicates, columns, businessError);
64 }
65 
RegisterObserver(const Uri & uri,const sptr<AAFwk::IDataAbilityObserver> & dataObserver)66 void GeneralControllerServiceImpl::RegisterObserver(const Uri &uri,
67     const sptr<AAFwk::IDataAbilityObserver> &dataObserver)
68 {
69     auto obsMgrClient = OHOS::AAFwk::DataObsMgrClient::GetInstance();
70     if (obsMgrClient == nullptr) {
71         LOG_ERROR("get DataObsMgrClient failed");
72         return;
73     }
74     ErrCode ret = obsMgrClient->RegisterObserver(uri, dataObserver);
75     if (ret != ERR_OK) {
76         LOG_ERROR("RegisterObserver failed");
77     }
78 }
79 
UnregisterObserver(const Uri & uri,const sptr<AAFwk::IDataAbilityObserver> & dataObserver)80 void GeneralControllerServiceImpl::UnregisterObserver(const Uri &uri,
81     const sptr<AAFwk::IDataAbilityObserver> &dataObserver)
82 {
83     auto obsMgrClient = OHOS::AAFwk::DataObsMgrClient::GetInstance();
84     if (obsMgrClient == nullptr) {
85         LOG_ERROR("get DataObsMgrClient failed");
86         return;
87     }
88     ErrCode ret = obsMgrClient->UnregisterObserver(uri, dataObserver);
89     if (ret != ERR_OK) {
90         LOG_ERROR("UnregisterObserver failed");
91     }
92 }
93 
NotifyChange(const Uri & uri)94 void GeneralControllerServiceImpl::NotifyChange(const Uri &uri)
95 {
96     auto proxy = DataShareManagerImpl::GetInstance().GetServiceProxy();
97     if (proxy == nullptr) {
98         LOG_ERROR("proxy is nullptr");
99         return;
100     }
101     proxy->Notify(uri.ToString());
102 }
103 } // namespace DataShare
104 } // namespace OHOS