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_stub.h"
17 
18 #include "hilog_tag_wrapper.h"
19 #include "ipc_skeleton.h"
20 #include "common_utils.h"
21 #include "string_ex.h"
22 
23 namespace OHOS {
24 namespace AAFwk {
25 
26 const DataAbilityObserverStub::RequestFuncType DataAbilityObserverStub::HANDLES[TRANS_BUTT] = {
27     &DataAbilityObserverStub::OnChangeInner,
28     &DataAbilityObserverStub::OnChangeExtInner,
29     &DataAbilityObserverStub::OnChangePreferencesInner,
30 };
31 
DataAbilityObserverStub()32 DataAbilityObserverStub::DataAbilityObserverStub() {}
33 
~DataAbilityObserverStub()34 DataAbilityObserverStub::~DataAbilityObserverStub() {}
35 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)36 int DataAbilityObserverStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
37     MessageOption &option)
38 {
39     TAG_LOGD(AAFwkTag::DBOBSMGR, "code: %{public}d, flags: %{public}d, callingPid:%{public}d", code, option.GetFlags(),
40         IPCSkeleton::GetCallingPid());
41     std::u16string descriptor = DataAbilityObserverStub::GetDescriptor();
42     std::u16string remoteDescriptor = data.ReadInterfaceToken();
43     if (descriptor != remoteDescriptor) {
44         TAG_LOGE(AAFwkTag::DBOBSMGR,
45             "local descriptor≠remote, descriptor:%{public}s, remoteDescriptor:%{public}s",
46             CommonUtils::Anonymous(Str16ToStr8(descriptor)).c_str(),
47             CommonUtils::Anonymous(Str16ToStr8(remoteDescriptor)).c_str());
48         return ERR_INVALID_STATE;
49     }
50 
51     if (code < TRANS_HEAD || code >= TRANS_BUTT || HANDLES[code] == nullptr) {
52         TAG_LOGE(AAFwkTag::DBOBSMGR, "invalid code:%u, BUTT:%d", code, TRANS_BUTT);
53         return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
54     }
55     return (this->*HANDLES[code])(data, reply);
56 }
57 
58 /**
59  * @brief Called back to notify that the data being observed has changed.
60  *
61  * @return Returns 0 on success, others on failure.
62  */
OnChangeInner(MessageParcel & data,MessageParcel & reply)63 int32_t DataAbilityObserverStub::OnChangeInner(MessageParcel &data, MessageParcel &reply)
64 {
65     OnChange();
66     return ERR_NONE;
67 }
68 
69 /**
70  * @brief Called back to notify that the data being observed has changed.
71  *
72  * @return Returns 0 on success, others on failure.
73  */
OnChangeExtInner(MessageParcel & data,MessageParcel & reply)74 int32_t DataAbilityObserverStub::OnChangeExtInner(MessageParcel &data, MessageParcel &reply)
75 {
76     ChangeInfo changeInfo;
77     if (!ChangeInfo::Unmarshalling(changeInfo, data)) {
78         return IPC_STUB_INVALID_DATA_ERR;
79     }
80     OnChangeExt(changeInfo);
81     return ERR_NONE;
82 }
83 
84 /**
85  * @brief Called back to notify that the data being observed has changed.
86  *
87  * @return Returns 0 on success, others on failure.
88  */
OnChangePreferencesInner(MessageParcel & data,MessageParcel & reply)89 int32_t DataAbilityObserverStub::OnChangePreferencesInner(MessageParcel &data, MessageParcel &reply)
90 {
91     std::string key = data.ReadString();
92     if (key.empty()) {
93         return IPC_STUB_INVALID_DATA_ERR;
94     }
95     OnChangePreferences(key);
96     return ERR_NONE;
97 }
98 
OnRemoteDied(const wptr<IRemoteObject> & remote)99 void DataObsCallbackRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
100 {
101     if (handler_) {
102         handler_(remote);
103     }
104 }
105 
DataObsCallbackRecipient(RemoteDiedHandler handler)106 DataObsCallbackRecipient::DataObsCallbackRecipient(RemoteDiedHandler handler) : handler_(handler) {}
107 
~DataObsCallbackRecipient()108 DataObsCallbackRecipient::~DataObsCallbackRecipient() {}
109 }  // namespace AAFwk
110 }  // namespace OHOS
111