• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 {
22 static constexpr uint32_t MESSAGE_MAX_COUNT = 50;
DataAbilityObserverProxy(const sptr<IRemoteObject> & remote)23 DataAbilityObserverProxy::DataAbilityObserverProxy(const sptr<IRemoteObject> &remote)
24     : IRemoteProxy<IDataAbilityObserver>(remote)
25 {}
~DataAbilityObserverProxy()26 DataAbilityObserverProxy::~DataAbilityObserverProxy()
27 {}
28 
29 /**
30  * @brief Set the message option.
31  *
32  * @param option Indicates the option of message.
33  */
SetMessageOption(MessageOption & option)34 void DataAbilityObserverProxy::SetMessageOption(MessageOption &option)
35 {
36     std::lock_guard<std::mutex> lock(countMutex_);
37     // Send a wakeup IPC every 50 times. Otherwise, send a non-wakeup IPC.
38     if (messageCount_ >= MESSAGE_MAX_COUNT) {
39         option = MessageOption(MessageOption::TF_ASYNC);
40         messageCount_ = 0;
41     } else {
42         option = MessageOption(MessageOption::TF_ASYNC | MessageOption::TF_ASYNC_WAKEUP_LATER);
43         messageCount_++;
44     }
45 }
46 
47 /**
48  * @brief Called back to notify that the data being observed has changed.
49  *
50  * @param uri Indicates the path of the data to operate.
51  */
OnChange()52 void DataAbilityObserverProxy::OnChange()
53 {
54     OHOS::MessageParcel data;
55     OHOS::MessageParcel reply;
56     MessageOption option;
57     SetMessageOption(option);
58 
59     if (!data.WriteInterfaceToken(DataAbilityObserverProxy::GetDescriptor())) {
60         TAG_LOGE(AAFwkTag::DBOBSMGR, "write token false");
61         return;
62     }
63 
64     int result = SendTransactCmd(IDataAbilityObserver::DATA_ABILITY_OBSERVER_CHANGE, data, reply, option);
65     if (result != ERR_NONE) {
66         TAG_LOGE(AAFwkTag::DBOBSMGR, "error,result:%{public}d", result);
67     }
68 }
69 
70 /**
71  * @brief Called back to notify that the data being observed has changed.
72  *
73  * @param changeInfo Indicates the info of the data to operate.
74  */
OnChangeExt(const ChangeInfo & changeInfo)75 void DataAbilityObserverProxy::OnChangeExt(const ChangeInfo &changeInfo)
76 {
77     OHOS::MessageParcel data;
78     OHOS::MessageParcel reply;
79     MessageOption option;
80     SetMessageOption(option);
81 
82     if (!data.WriteInterfaceToken(DataAbilityObserverProxy::GetDescriptor())) {
83         TAG_LOGE(AAFwkTag::DBOBSMGR, "write token false");
84         return;
85     }
86 
87     if (!ChangeInfo::Marshalling(changeInfo, data)) {
88         TAG_LOGE(AAFwkTag::DBOBSMGR, "changeInfo marshalling fail");
89         return;
90     }
91 
92     int result = SendTransactCmd(IDataAbilityObserver::DATA_ABILITY_OBSERVER_CHANGE_EXT, data, reply, option);
93     if (result != ERR_NONE) {
94         TAG_LOGE(AAFwkTag::DBOBSMGR, "error result:%{public}d", result);
95     }
96 }
97 
98 /**
99  * @brief Called back to notify that the data being observed has changed.
100  *
101  * @param uri Indicates the path of the data to operate.
102  */
OnChangePreferences(const std::string & key)103 void DataAbilityObserverProxy::OnChangePreferences(const std::string &key)
104 {
105     OHOS::MessageParcel data;
106     OHOS::MessageParcel reply;
107     MessageOption option;
108     SetMessageOption(option);
109 
110     if (!data.WriteInterfaceToken(DataAbilityObserverProxy::GetDescriptor())) {
111         TAG_LOGE(AAFwkTag::DBOBSMGR, "write token false");
112         return;
113     }
114 
115     if (!data.WriteString(key)) {
116         TAG_LOGE(AAFwkTag::DBOBSMGR, "write string false");
117         return;
118     }
119 
120     int result =
121         SendTransactCmd(IDataAbilityObserver::DATA_ABILITY_OBSERVER_CHANGE_PREFERENCES, data, reply, option);
122     if (result != ERR_NONE) {
123         TAG_LOGE(AAFwkTag::DBOBSMGR, "error result:%{public}d", result);
124     }
125 }
126 
SendTransactCmd(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)127 int32_t DataAbilityObserverProxy::SendTransactCmd(uint32_t code, MessageParcel &data,
128     MessageParcel &reply, MessageOption &option)
129 {
130     sptr<IRemoteObject> remote = Remote();
131     if (remote == nullptr) {
132         TAG_LOGE(AAFwkTag::DBOBSMGR, "null remote");
133         return ERR_NULL_OBJECT;
134     }
135 
136     return remote->SendRequest(code, data, reply, option);
137 }
138 
139 }  // namespace AAFwk
140 }  // namespace OHOS
141