• 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 "dataobs_mgr_stub.h"
17 
18 #include "string_ex.h"
19 
20 #include "data_ability_observer_proxy.h"
21 #include "dataobs_mgr_errors.h"
22 #include "datashare_log.h"
23 #include "ipc_skeleton.h"
24 #include "common_utils.h"
25 #include "hilog_tag_wrapper.h"
26 
27 namespace OHOS {
28 namespace AAFwk {
29 using namespace DataShare;
30 using Uri = OHOS::Uri;
31 
32 const DataObsManagerStub::RequestFuncType DataObsManagerStub::HANDLES[TRANS_BUTT] = {
33     &DataObsManagerStub::RegisterObserverInner,
34     &DataObsManagerStub::UnregisterObserverInner,
35     &DataObsManagerStub::NotifyChangeInner,
36     &DataObsManagerStub::RegisterObserverExtInner,
37     &DataObsManagerStub::UnregisterObserverExtInner,
38     &DataObsManagerStub::UnregisterObserverExtALLInner,
39     &DataObsManagerStub::NotifyChangeExtInner,
40     &DataObsManagerStub::NotifyProcessObserverInner
41 };
42 
DataObsManagerStub()43 DataObsManagerStub::DataObsManagerStub() {}
44 
~DataObsManagerStub()45 DataObsManagerStub::~DataObsManagerStub() {}
46 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)47 int DataObsManagerStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
48 {
49     TAG_LOGD(AAFwkTag::DBOBSMGR, "code: %{public}d, flags: %{public}d, callingPid:%{public}d", code, option.GetFlags(),
50         IPCSkeleton::GetCallingPid());
51     std::u16string descriptor = DataObsManagerStub::GetDescriptor();
52     std::u16string remoteDescriptor = data.ReadInterfaceToken();
53     if (descriptor != remoteDescriptor) {
54         TAG_LOGE(AAFwkTag::DBOBSMGR,
55             "local descriptor≠remote, descriptor:%{public}s, remoteDescriptor:%{public}s",
56             CommonUtils::Anonymous(Str16ToStr8(descriptor)).c_str(),
57             CommonUtils::Anonymous(Str16ToStr8(remoteDescriptor)).c_str());
58         return ERR_INVALID_STATE;
59     }
60 
61     if (code < TRANS_HEAD || code >= TRANS_BUTT || HANDLES[code] == nullptr) {
62         TAG_LOGE(AAFwkTag::DBOBSMGR, "invalid code:%{public}u, BUTT:%{public}d", code, TRANS_BUTT);
63         return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
64     }
65     return (this->*HANDLES[code])(data, reply);
66 }
67 
RegisterObserverInner(MessageParcel & data,MessageParcel & reply)68 int DataObsManagerStub::RegisterObserverInner(MessageParcel &data, MessageParcel &reply)
69 {
70     Uri uri(data.ReadString());
71     if (uri.ToString().empty()) {
72         TAG_LOGE(AAFwkTag::DBOBSMGR, "invalid uri");
73         return IPC_STUB_INVALID_DATA_ERR;
74     }
75 
76     auto remote = data.ReadRemoteObject();
77     auto observer = remote == nullptr ? nullptr : iface_cast<IDataAbilityObserver>(remote);
78     int32_t result = RegisterObserver(uri, observer);
79     reply.WriteInt32(result);
80     return NO_ERROR;
81 }
82 
UnregisterObserverInner(MessageParcel & data,MessageParcel & reply)83 int DataObsManagerStub::UnregisterObserverInner(MessageParcel &data, MessageParcel &reply)
84 {
85     Uri uri(data.ReadString());
86     if (uri.ToString().empty()) {
87         TAG_LOGE(AAFwkTag::DBOBSMGR, "invalid uri");
88         return IPC_STUB_INVALID_DATA_ERR;
89     }
90 
91     auto remote = data.ReadRemoteObject();
92     auto observer = remote == nullptr ? nullptr : iface_cast<IDataAbilityObserver>(remote);
93     int32_t result = UnregisterObserver(uri, observer);
94     reply.WriteInt32(result);
95     return NO_ERROR;
96 }
97 
NotifyChangeInner(MessageParcel & data,MessageParcel & reply)98 int DataObsManagerStub::NotifyChangeInner(MessageParcel &data, MessageParcel &reply)
99 {
100     Uri uri(data.ReadString());
101     if (uri.ToString().empty()) {
102         TAG_LOGE(AAFwkTag::DBOBSMGR, "invalid uri");
103         return IPC_STUB_INVALID_DATA_ERR;
104     }
105 
106     int32_t result = NotifyChange(uri);
107     reply.WriteInt32(result);
108     return NO_ERROR;
109 }
110 
RegisterObserverExtInner(MessageParcel & data,MessageParcel & reply)111 int32_t DataObsManagerStub::RegisterObserverExtInner(MessageParcel &data, MessageParcel &reply)
112 {
113     Uri uri(data.ReadString());
114     if (uri.ToString().empty()) {
115         TAG_LOGE(AAFwkTag::DBOBSMGR, "invalid uri");
116         return IPC_STUB_INVALID_DATA_ERR;
117     }
118     auto remote = data.ReadRemoteObject();
119     auto observer = remote == nullptr ? nullptr : iface_cast<IDataAbilityObserver>(remote);
120     bool isDescendants = data.ReadBool();
121     reply.WriteInt32(RegisterObserverExt(uri, observer, isDescendants));
122     return SUCCESS;
123 }
124 
UnregisterObserverExtInner(MessageParcel & data,MessageParcel & reply)125 int32_t DataObsManagerStub::UnregisterObserverExtInner(MessageParcel &data, MessageParcel &reply)
126 {
127     Uri uri(data.ReadString());
128     if (uri.ToString().empty()) {
129         TAG_LOGE(AAFwkTag::DBOBSMGR, "invalid uri");
130         return IPC_STUB_INVALID_DATA_ERR;
131     }
132     auto remote = data.ReadRemoteObject();
133     auto observer = remote == nullptr ? nullptr : iface_cast<IDataAbilityObserver>(remote);
134 
135     reply.WriteInt32(UnregisterObserverExt(uri, observer));
136     return SUCCESS;
137 }
138 
UnregisterObserverExtALLInner(MessageParcel & data,MessageParcel & reply)139 int32_t DataObsManagerStub::UnregisterObserverExtALLInner(MessageParcel &data, MessageParcel &reply)
140 {
141     auto remote = data.ReadRemoteObject();
142     auto observer = remote == nullptr ? nullptr : iface_cast<IDataAbilityObserver>(remote);
143     reply.WriteInt32(UnregisterObserverExt(observer));
144     return SUCCESS;
145 }
146 
NotifyChangeExtInner(MessageParcel & data,MessageParcel & reply)147 int32_t DataObsManagerStub::NotifyChangeExtInner(MessageParcel &data, MessageParcel &reply)
148 {
149     ChangeInfo changeInfo;
150     if (!ChangeInfo::Unmarshalling(changeInfo, data)) {
151         LOG_ERROR("Failed to unmarshall changeInfo.");
152         return IPC_STUB_INVALID_DATA_ERR;
153     }
154 
155     reply.WriteInt32(NotifyChangeExt(changeInfo));
156     return SUCCESS;
157 }
158 
NotifyProcessObserverInner(MessageParcel & data,MessageParcel & reply)159 int32_t DataObsManagerStub::NotifyProcessObserverInner(MessageParcel &data, MessageParcel &reply)
160 {
161     std::string key = data.ReadString();
162     auto observer = data.ReadRemoteObject();
163     reply.WriteInt32(NotifyProcessObserver(key, observer));
164     return SUCCESS;
165 }
166 }  // namespace AAFwk
167 }  // namespace OHOS
168