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