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
16 #include "dataobs_mgr_client.h"
17
18 #include "string_ex.h"
19
20 #include "hilog_wrapper.h"
21 #include "ipc_skeleton.h"
22 #include "iservice_registry.h"
23 #include "if_system_ability_manager.h"
24 #include "system_ability_definition.h"
25
26 namespace OHOS {
27 namespace AAFwk {
28 std::shared_ptr<DataObsMgrClient> DataObsMgrClient::instance_ = nullptr;
29 std::mutex DataObsMgrClient::mutex_;
30
GetInstance()31 std::shared_ptr<DataObsMgrClient> DataObsMgrClient::GetInstance()
32 {
33 if (instance_ == nullptr) {
34 std::lock_guard<std::mutex> lock_l(mutex_);
35 if (instance_ == nullptr) {
36 instance_ = std::make_shared<DataObsMgrClient>();
37 }
38 }
39 return instance_;
40 }
41
DataObsMgrClient()42 DataObsMgrClient::DataObsMgrClient()
43 {}
44
~DataObsMgrClient()45 DataObsMgrClient::~DataObsMgrClient()
46 {}
47
48 /**
49 * Registers an observer to DataObsMgr specified by the given Uri.
50 *
51 * @param uri, Indicates the path of the data to operate.
52 * @param dataObserver, Indicates the IDataAbilityObserver object.
53 *
54 * @return Returns ERR_OK on success, others on failure.
55 */
RegisterObserver(const Uri & uri,const sptr<IDataAbilityObserver> & dataObserver)56 ErrCode DataObsMgrClient::RegisterObserver(const Uri &uri, const sptr<IDataAbilityObserver> &dataObserver)
57 {
58 if (remoteObject_ == nullptr) {
59 ErrCode err = Connect();
60 if (err != ERR_OK) {
61 return DATAOBS_SERVICE_NOT_CONNECTED;
62 }
63 }
64 sptr<IDataObsMgr> doms = iface_cast<IDataObsMgr>(remoteObject_);
65 return doms->RegisterObserver(uri, dataObserver);
66 }
67
68 /**
69 * Deregisters an observer used for DataObsMgr specified by the given Uri.
70 *
71 * @param uri, Indicates the path of the data to operate.
72 * @param dataObserver, Indicates the IDataAbilityObserver object.
73 *
74 * @return Returns ERR_OK on success, others on failure.
75 */
UnregisterObserver(const Uri & uri,const sptr<IDataAbilityObserver> & dataObserver)76 ErrCode DataObsMgrClient::UnregisterObserver(const Uri &uri, const sptr<IDataAbilityObserver> &dataObserver)
77 {
78 if (remoteObject_ == nullptr) {
79 ErrCode err = Connect();
80 if (err != ERR_OK) {
81 return DATAOBS_SERVICE_NOT_CONNECTED;
82 }
83 }
84 sptr<IDataObsMgr> doms = iface_cast<IDataObsMgr>(remoteObject_);
85 return doms->UnregisterObserver(uri, dataObserver);
86 }
87
88 /**
89 * Notifies the registered observers of a change to the data resource specified by Uri.
90 *
91 * @param uri, Indicates the path of the data to operate.
92 *
93 * @return Returns ERR_OK on success, others on failure.
94 */
NotifyChange(const Uri & uri)95 ErrCode DataObsMgrClient::NotifyChange(const Uri &uri)
96 {
97 if (remoteObject_ == nullptr) {
98 ErrCode err = Connect();
99 if (err != ERR_OK) {
100 return DATAOBS_SERVICE_NOT_CONNECTED;
101 }
102 }
103 sptr<IDataObsMgr> doms = iface_cast<IDataObsMgr>(remoteObject_);
104 return doms->NotifyChange(uri);
105 }
106
107 /**
108 * Connect dataobs manager service.
109 *
110 * @return Returns ERR_OK on success, others on failure.
111 */
Connect()112 ErrCode DataObsMgrClient::Connect()
113 {
114 std::lock_guard<std::mutex> lock(mutex_);
115 if (remoteObject_ != nullptr) {
116 return ERR_OK;
117 }
118 sptr<ISystemAbilityManager> systemManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
119 if (systemManager == nullptr) {
120 HILOG_ERROR("%{private}s:fail to get Registry", __func__);
121 return GET_DATAOBS_SERVICE_FAILED;
122 }
123 remoteObject_ = systemManager->GetSystemAbility(DATAOBS_MGR_SERVICE_SA_ID);
124 if (remoteObject_ == nullptr) {
125 HILOG_ERROR("%{private}s:fail to connect DataObsMgrService", __func__);
126 return GET_DATAOBS_SERVICE_FAILED;
127 }
128 HILOG_DEBUG("connect DataObsMgrService success");
129 return ERR_OK;
130 }
131
132 } // namespace AAFwk
133 } // namespace OHOS
134