1 /*
2  * Copyright (c) 2023 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 "observer_callback_stub.h"
17 
18 #include <string>
19 #include <memory>
20 #include "user_access_tracer.h"
21 #include "accesstoken_kit.h"
22 #include "file_access_framework_errno.h"
23 #include "hilog_wrapper.h"
24 #include "hitrace_meter.h"
25 #include "ipc_object_stub.h"
26 #include "ipc_skeleton.h"
27 #include "ipc_types.h"
28 #include "message_parcel.h"
29 
30 namespace OHOS {
31 namespace FileAccessFwk {
~ObserverCallbackStub()32 ObserverCallbackStub::~ObserverCallbackStub()
33 {
34     stubFuncMap_.clear();
35 }
36 
InitStubFuncMap()37 void ObserverCallbackStub::InitStubFuncMap()
38 {
39     if (stubFuncMap_.size() == 0) {
40         stubFuncMap_[CMD_ONCHANGE] = &ObserverCallbackStub::OnChangeStub;
41     }
42 }
43 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)44 int32_t ObserverCallbackStub::OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply,
45     MessageOption& option)
46 {
47     UserAccessTracer trace;
48     trace.Start("OnRemoteRequest");
49     InitStubFuncMap();
50     std::u16string descriptor = ObserverCallbackStub::GetDescriptor();
51     std::u16string remoteDescriptor = data.ReadInterfaceToken();
52     if (descriptor != remoteDescriptor) {
53         return ERR_INVALID_STATE;
54     }
55 
56     const auto &itFunc = stubFuncMap_.find(code);
57     if (itFunc != stubFuncMap_.end()) {
58         return (this->*(itFunc->second))(data, reply);
59     }
60 
61     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
62 }
63 
OnChangeStub(MessageParcel & data,MessageParcel & reply)64 int32_t ObserverCallbackStub::OnChangeStub(MessageParcel &data, MessageParcel &reply)
65 {
66     UserAccessTracer trace;
67     trace.Start("OnChangeStub");
68     std::shared_ptr<NotifyMessage> notifyMessage(data.ReadParcelable<NotifyMessage>());
69     if (notifyMessage == nullptr) {
70         HILOG_ERROR("OnChange uri is nullptr");
71         return E_URIS;
72     }
73 
74     OnChange(*notifyMessage);
75     int ret = ERR_OK;
76     if (!reply.WriteInt32(ret)) {
77         HILOG_ERROR("Parameter OnChangeStub fail to WriteInt32 ret");
78         return E_IPCS;
79     }
80     return ERR_OK;
81 }
82 }
83 }