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 "data_ability_observer_proxy.h"
17 #include "hilog_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 auto remote = Remote();
35 if (remote == nullptr) {
36 HILOG_ERROR("remote is nullptr");
37 return;
38 }
39
40 OHOS::MessageParcel data;
41 OHOS::MessageParcel reply;
42 MessageOption option(MessageOption::TF_ASYNC);
43
44 if (!data.WriteInterfaceToken(DataAbilityObserverProxy::GetDescriptor())) {
45 HILOG_ERROR("data.WriteInterfaceToken(GetDescriptor()) return false");
46 return;
47 }
48
49 int result = remote->SendRequest(IDataAbilityObserver::DATA_ABILITY_OBSERVER_CHANGE, data, reply, option);
50 if (result != ERR_NONE) {
51 HILOG_ERROR("SendRequest error, result=%{public}d", result);
52 }
53 }
54
55 /**
56 * @brief Called back to notify that the data being observed has changed.
57 *
58 * @param changeInfo Indicates the info of the data to operate.
59 */
OnChangeExt(const ChangeInfo & changeInfo)60 void DataAbilityObserverProxy::OnChangeExt(const ChangeInfo &changeInfo)
61 {
62 auto remote = Remote();
63 if (remote == nullptr) {
64 HILOG_ERROR("remote is nullptr");
65 return;
66 }
67
68 OHOS::MessageParcel data;
69 OHOS::MessageParcel reply;
70 MessageOption option(MessageOption::TF_ASYNC);
71
72 if (!data.WriteInterfaceToken(DataAbilityObserverProxy::GetDescriptor())) {
73 HILOG_ERROR("data.WriteInterfaceToken(GetDescriptor()) return false");
74 return;
75 }
76
77 if (!ChangeInfo::Marshalling(changeInfo, data)) {
78 HILOG_ERROR("changeInfo marshalling failed");
79 return;
80 }
81
82 int result = remote->SendRequest(IDataAbilityObserver::DATA_ABILITY_OBSERVER_CHANGE_EXT, data, reply, option);
83 if (result != ERR_NONE) {
84 HILOG_ERROR("SendRequest error, result=%{public}d", result);
85 }
86 }
87
88 /**
89 * @brief Called back to notify that the data being observed has changed.
90 *
91 * @param uri Indicates the path of the data to operate.
92 */
OnChangePreferences(const std::string & key)93 void DataAbilityObserverProxy::OnChangePreferences(const std::string &key)
94 {
95 auto remote = Remote();
96 if (remote == nullptr) {
97 HILOG_ERROR("remote is nullptr");
98 return;
99 }
100
101 OHOS::MessageParcel data;
102 OHOS::MessageParcel reply;
103 MessageOption option(MessageOption::TF_ASYNC);
104
105 if (!data.WriteInterfaceToken(DataAbilityObserverProxy::GetDescriptor())) {
106 HILOG_ERROR("data.WriteInterfaceToken(GetDescriptor()) return false");
107 return;
108 }
109
110 if (!data.WriteString(key)) {
111 HILOG_ERROR("data.WriteString(key) return false");
112 return;
113 }
114
115 int result =
116 remote->SendRequest(IDataAbilityObserver::DATA_ABILITY_OBSERVER_CHANGE_PREFERENCES, data, reply, option);
117 if (result != ERR_NONE) {
118 HILOG_ERROR("SendRequest error, result=%{public}d", result);
119 }
120 }
121
122 } // namespace AAFwk
123 } // namespace OHOS
124