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 "dataobs_mgr_stub.h"
17
18 #include "errors.h"
19 #include "string_ex.h"
20
21 #include "data_ability_observer_proxy.h"
22 #include "data_ability_observer_stub.h"
23 #include "dataobs_mgr_errors.h"
24
25 namespace OHOS {
26 namespace AAFwk {
27 using Uri = OHOS::Uri;
DataObsManagerStub()28 DataObsManagerStub::DataObsManagerStub()
29 {
30 requestFuncMap_[REGISTER_OBSERVER] = &DataObsManagerStub::RegisterObserverInner;
31 requestFuncMap_[UNREGISTER_OBSERVER] = &DataObsManagerStub::UnregisterObserverInner;
32 requestFuncMap_[NOTIFY_CHANGE] = &DataObsManagerStub::NotifyChangeInner;
33 }
34
~DataObsManagerStub()35 DataObsManagerStub::~DataObsManagerStub()
36 {
37 requestFuncMap_.clear();
38 }
39
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)40 int DataObsManagerStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
41 {
42 HILOG_DEBUG("DataObsManagerStub::OnRemoteRequest, cmd = %d, flags= %d", code, option.GetFlags());
43 std::u16string descriptor = DataObsManagerStub::GetDescriptor();
44 std::u16string remoteDescriptor = data.ReadInterfaceToken();
45 if (descriptor != remoteDescriptor) {
46 HILOG_INFO("local descriptor is not equal to remote");
47 return ERR_INVALID_STATE;
48 }
49
50 auto itFunc = requestFuncMap_.find(code);
51 if (itFunc != requestFuncMap_.end()) {
52 auto requestFunc = itFunc->second;
53 if (requestFunc != nullptr) {
54 return (this->*requestFunc)(data, reply);
55 }
56 }
57 HILOG_WARN("DataObsManagerStub::OnRemoteRequest, default case, need check.");
58 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
59 }
60
RegisterObserverInner(MessageParcel & data,MessageParcel & reply)61 int DataObsManagerStub::RegisterObserverInner(MessageParcel &data, MessageParcel &reply)
62 {
63 Uri *uri = data.ReadParcelable<Uri>();
64 if (uri == nullptr) {
65 HILOG_ERROR("DataObsManagerStub: uri is nullptr");
66 return ERR_INVALID_VALUE;
67 }
68
69 auto observer = iface_cast<IDataAbilityObserver>(data.ReadRemoteObject());
70 int32_t result = RegisterObserver(*uri, observer);
71 reply.WriteInt32(result);
72 if (uri != nullptr) {
73 delete uri;
74 }
75 return NO_ERROR;
76 }
77
UnregisterObserverInner(MessageParcel & data,MessageParcel & reply)78 int DataObsManagerStub::UnregisterObserverInner(MessageParcel &data, MessageParcel &reply)
79 {
80 Uri *uri = data.ReadParcelable<Uri>();
81 if (uri == nullptr) {
82 HILOG_ERROR("DataObsManagerStub: uri is nullptr");
83 return ERR_INVALID_VALUE;
84 }
85
86 auto observer = iface_cast<IDataAbilityObserver>(data.ReadRemoteObject());
87 int32_t result = UnregisterObserver(*uri, observer);
88 reply.WriteInt32(result);
89 if (uri != nullptr) {
90 delete uri;
91 }
92 return NO_ERROR;
93 }
94
NotifyChangeInner(MessageParcel & data,MessageParcel & reply)95 int DataObsManagerStub::NotifyChangeInner(MessageParcel &data, MessageParcel &reply)
96 {
97 Uri *uri = data.ReadParcelable<Uri>();
98 if (uri == nullptr) {
99 HILOG_ERROR("DataObsManagerStub: uri is nullptr");
100 return ERR_INVALID_VALUE;
101 }
102
103 int32_t result = NotifyChange(*uri);
104 reply.WriteInt32(result);
105 if (uri != nullptr) {
106 delete uri;
107 }
108 return NO_ERROR;
109 }
110 } // namespace AAFwk
111 } // namespace OHOS
112