1 /*
2  * Copyright (c) 2021-2024 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 "data_ability_observer_proxy.h"
17 #include "hilog_tag_wrapper.h"
18 #include "message_parcel.h"
19 
20 namespace OHOS {
21 namespace AAFwk {
DataAbilityObserverProxy(const sptr<IRemoteObject> & remote)22 DataAbilityObserverProxy::DataAbilityObserverProxy(const sptr<IRemoteObject> &remote)
23     : IRemoteProxy<IDataAbilityObserver>(remote)
24 {}
~DataAbilityObserverProxy()25 DataAbilityObserverProxy::~DataAbilityObserverProxy()
26 {}
27 /**
28  * @brief Called back to notify that the data being observed has changed.
29  *
30  * @param uri Indicates the path of the data to operate.
31  */
OnChange()32 void DataAbilityObserverProxy::OnChange()
33 {
34     OHOS::MessageParcel data;
35     OHOS::MessageParcel reply;
36     MessageOption option(MessageOption::TF_ASYNC);
37 
38     if (!data.WriteInterfaceToken(DataAbilityObserverProxy::GetDescriptor())) {
39         TAG_LOGE(AAFwkTag::DBOBSMGR, "write token false");
40         return;
41     }
42 
43     int result = SendTransactCmd(IDataAbilityObserver::DATA_ABILITY_OBSERVER_CHANGE, data, reply, option);
44     if (result != ERR_NONE) {
45         TAG_LOGE(AAFwkTag::DBOBSMGR, "error,result:%{public}d", result);
46     }
47 }
48 
49 /**
50  * @brief Called back to notify that the data being observed has changed.
51  *
52  * @param changeInfo Indicates the info of the data to operate.
53  */
OnChangeExt(const ChangeInfo & changeInfo)54 void DataAbilityObserverProxy::OnChangeExt(const ChangeInfo &changeInfo)
55 {
56     OHOS::MessageParcel data;
57     OHOS::MessageParcel reply;
58     MessageOption option(MessageOption::TF_ASYNC);
59 
60     if (!data.WriteInterfaceToken(DataAbilityObserverProxy::GetDescriptor())) {
61         TAG_LOGE(AAFwkTag::DBOBSMGR, "write token false");
62         return;
63     }
64 
65     if (!ChangeInfo::Marshalling(changeInfo, data)) {
66         TAG_LOGE(AAFwkTag::DBOBSMGR, "changeInfo marshalling failed");
67         return;
68     }
69 
70     int result = SendTransactCmd(IDataAbilityObserver::DATA_ABILITY_OBSERVER_CHANGE_EXT, data, reply, option);
71     if (result != ERR_NONE) {
72         TAG_LOGE(AAFwkTag::DBOBSMGR, "error result:%{public}d", result);
73     }
74 }
75 
76 /**
77  * @brief Called back to notify that the data being observed has changed.
78  *
79  * @param uri Indicates the path of the data to operate.
80  */
OnChangePreferences(const std::string & key)81 void DataAbilityObserverProxy::OnChangePreferences(const std::string &key)
82 {
83     OHOS::MessageParcel data;
84     OHOS::MessageParcel reply;
85     MessageOption option(MessageOption::TF_ASYNC);
86 
87     if (!data.WriteInterfaceToken(DataAbilityObserverProxy::GetDescriptor())) {
88         TAG_LOGE(AAFwkTag::DBOBSMGR, "write token false");
89         return;
90     }
91 
92     if (!data.WriteString(key)) {
93         TAG_LOGE(AAFwkTag::DBOBSMGR, "write string false");
94         return;
95     }
96 
97     int result =
98         SendTransactCmd(IDataAbilityObserver::DATA_ABILITY_OBSERVER_CHANGE_PREFERENCES, data, reply, option);
99     if (result != ERR_NONE) {
100         TAG_LOGE(AAFwkTag::DBOBSMGR, "error result:%{public}d", result);
101     }
102 }
103 
SendTransactCmd(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)104 int32_t DataAbilityObserverProxy::SendTransactCmd(uint32_t code, MessageParcel &data,
105     MessageParcel &reply, MessageOption &option)
106 {
107     sptr<IRemoteObject> remote = Remote();
108     if (remote == nullptr) {
109         TAG_LOGE(AAFwkTag::DBOBSMGR, "null remote");
110         return ERR_NULL_OBJECT;
111     }
112 
113     return remote->SendRequest(code, data, reply, option);
114 }
115 
116 }  // namespace AAFwk
117 }  // namespace OHOS
118