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_stub.h"
17
18 #include "hilog_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 HILOG_DEBUG("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 HILOG_ERROR("local descriptor is not equal to remote, descriptor: %{public}s, remoteDescriptor: %{public}s",
45 CommonUtils::Anonymous(Str16ToStr8(descriptor)).c_str(),
46 CommonUtils::Anonymous(Str16ToStr8(remoteDescriptor)).c_str());
47 return ERR_INVALID_STATE;
48 }
49
50 if (code < TRANS_HEAD || code >= TRANS_BUTT || HANDLES[code] == nullptr) {
51 HILOG_ERROR("not support code:%u, BUTT:%d", code, TRANS_BUTT);
52 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
53 }
54 return (this->*HANDLES[code])(data, reply);
55 }
56
57 /**
58 * @brief Called back to notify that the data being observed has changed.
59 *
60 * @return Returns 0 on success, others on failure.
61 */
OnChangeInner(MessageParcel & data,MessageParcel & reply)62 int32_t DataAbilityObserverStub::OnChangeInner(MessageParcel &data, MessageParcel &reply)
63 {
64 OnChange();
65 return ERR_NONE;
66 }
67
68 /**
69 * @brief Called back to notify that the data being observed has changed.
70 *
71 * @return Returns 0 on success, others on failure.
72 */
OnChangeExtInner(MessageParcel & data,MessageParcel & reply)73 int32_t DataAbilityObserverStub::OnChangeExtInner(MessageParcel &data, MessageParcel &reply)
74 {
75 ChangeInfo changeInfo;
76 if (!ChangeInfo::Unmarshalling(changeInfo, data)) {
77 return IPC_STUB_INVALID_DATA_ERR;
78 }
79 OnChangeExt(changeInfo);
80 return ERR_NONE;
81 }
82
83 /**
84 * @brief Called back to notify that the data being observed has changed.
85 *
86 * @return Returns 0 on success, others on failure.
87 */
OnChangePreferencesInner(MessageParcel & data,MessageParcel & reply)88 int32_t DataAbilityObserverStub::OnChangePreferencesInner(MessageParcel &data, MessageParcel &reply)
89 {
90 std::string key = data.ReadString();
91 if (key.empty()) {
92 return IPC_STUB_INVALID_DATA_ERR;
93 }
94 OnChangePreferences(key);
95 return ERR_NONE;
96 }
97
OnRemoteDied(const wptr<IRemoteObject> & remote)98 void DataObsCallbackRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
99 {
100 if (handler_) {
101 handler_(remote);
102 }
103 }
104
DataObsCallbackRecipient(RemoteDiedHandler handler)105 DataObsCallbackRecipient::DataObsCallbackRecipient(RemoteDiedHandler handler) : handler_(handler) {}
106
~DataObsCallbackRecipient()107 DataObsCallbackRecipient::~DataObsCallbackRecipient() {}
108 } // namespace AAFwk
109 } // namespace OHOS
110