1 /*
2  * Copyright (c) 2021-2022 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 "ui_service_mgr_stub.h"
17 
18 #include "errors.h"
19 #include "ipc_skeleton.h"
20 #include "string_ex.h"
21 #include "tokenid_kit.h"
22 #include "ui_service_mgr_errors.h"
23 #include "ui_service_mgr_xcollie.h"
24 #include "ui_service_proxy.h"
25 #include "ui_service_stub.h"
26 
27 namespace OHOS::Ace {
28 constexpr uint32_t UI_MGR_SERVICE_TIMEOUT = 5;
29 
UIServiceMgrStub()30 UIServiceMgrStub::UIServiceMgrStub()
31 {
32     requestFuncMap_[REGISTER_CALLBACK] = &UIServiceMgrStub::RegisterCallBackInner;
33     requestFuncMap_[UNREGISTER_CALLBACK] = &UIServiceMgrStub::UnregisterCallBackInner;
34     requestFuncMap_[PUSH] = &UIServiceMgrStub::PushInner;
35     requestFuncMap_[REQUEST] = &UIServiceMgrStub::RequestInner;
36     requestFuncMap_[RETURN_REQUEST] = &UIServiceMgrStub::ReturnRequestInner;
37 }
38 
~UIServiceMgrStub()39 UIServiceMgrStub::~UIServiceMgrStub()
40 {
41     requestFuncMap_.clear();
42 }
43 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)44 int32_t UIServiceMgrStub::OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply,
45     MessageOption& option)
46 {
47     std::u16string descriptor = UIServiceMgrStub::GetDescriptor();
48     std::u16string remoteDescriptor = data.ReadInterfaceToken();
49     if (descriptor != remoteDescriptor) {
50         return ERR_INVALID_STATE;
51     }
52     auto itFunc = requestFuncMap_.find(code);
53     if (itFunc != requestFuncMap_.end()) {
54         auto requestFunc = itFunc->second;
55         if (requestFunc != nullptr) {
56             return (this->*requestFunc)(data, reply);
57         }
58     }
59     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
60 }
61 
IsSystemApp()62 bool UIServiceMgrStub::IsSystemApp()
63 {
64     uint64_t accessTokenIDEx = IPCSkeleton::GetCallingFullTokenID();
65     return Security::AccessToken::TokenIdKit::IsSystemAppByFullTokenID(accessTokenIDEx);
66 }
67 
RegisterCallBackInner(MessageParcel & data,MessageParcel & reply)68 int32_t UIServiceMgrStub::RegisterCallBackInner(MessageParcel& data, MessageParcel& reply)
69 {
70     UIServiceMgrXCollie uiServiceMgrXCollie("UISERVICE_REGISTER_CALLBACK", UI_MGR_SERVICE_TIMEOUT);
71     if (!IsSystemApp()) {
72         return ERR_PERMISSION_DENIED;
73     }
74     std::shared_ptr<AAFwk::Want> want(data.ReadParcelable<AAFwk::Want>());
75     if (want == nullptr) {
76         return ERR_INVALID_VALUE;
77     }
78 
79     auto object = data.ReadRemoteObject();
80     if (object == nullptr) {
81         return ERR_INVALID_VALUE;
82     }
83 
84     auto uiService = iface_cast<IUIService>(object);
85     int32_t result = RegisterCallBack(*want, uiService);
86     reply.WriteInt32(result);
87     return NO_ERROR;
88 }
89 
UnregisterCallBackInner(MessageParcel & data,MessageParcel & reply)90 int32_t UIServiceMgrStub::UnregisterCallBackInner(MessageParcel& data, MessageParcel& reply)
91 {
92     UIServiceMgrXCollie uiServiceMgrXCollie("UISERVICE_UNREGISTER_CALLBACK", UI_MGR_SERVICE_TIMEOUT);
93     if (!IsSystemApp()) {
94         return ERR_PERMISSION_DENIED;
95     }
96     std::shared_ptr<AAFwk::Want> want(data.ReadParcelable<AAFwk::Want>());
97     if (want == nullptr) {
98         return ERR_INVALID_VALUE;
99     }
100     int32_t result = UnregisterCallBack(*want);
101     reply.WriteInt32(result);
102     return NO_ERROR;
103 }
104 
PushInner(MessageParcel & data,MessageParcel & reply)105 int32_t UIServiceMgrStub::PushInner(MessageParcel& data, MessageParcel& reply)
106 {
107     UIServiceMgrXCollie uiServiceMgrXCollie("UISERVICE_PUSH", UI_MGR_SERVICE_TIMEOUT);
108     if (!IsSystemApp()) {
109         return ERR_PERMISSION_DENIED;
110     }
111     std::shared_ptr<AAFwk::Want> want(data.ReadParcelable<AAFwk::Want>());
112     if (want == nullptr) {
113         return ERR_INVALID_VALUE;
114     }
115     const std::string& name = data.ReadString();
116     const std::string& jsonPath = data.ReadString();
117     const std::string& dataStr = data.ReadString();
118     const std::string& extraData = data.ReadString();
119 
120     int32_t result = Push(*want, name, jsonPath, dataStr, extraData);
121     reply.WriteInt32(result);
122     return NO_ERROR;
123 }
124 
RequestInner(MessageParcel & data,MessageParcel & reply)125 int32_t UIServiceMgrStub::RequestInner(MessageParcel& data, MessageParcel& reply)
126 {
127     UIServiceMgrXCollie uiServiceMgrXCollie("UISERVICE_REQUEST", UI_MGR_SERVICE_TIMEOUT);
128     if (!IsSystemApp()) {
129         return ERR_PERMISSION_DENIED;
130     }
131     std::shared_ptr<AAFwk::Want> want(data.ReadParcelable<AAFwk::Want>());
132     if (want == nullptr) {
133         return ERR_INVALID_VALUE;
134     }
135     const std::string& name = data.ReadString();
136     const std::string& dataStr = data.ReadString();
137     int32_t result = Request(*want, name, dataStr);
138     reply.WriteInt32(result);
139     return NO_ERROR;
140 }
141 
ReturnRequestInner(MessageParcel & data,MessageParcel & reply)142 int32_t UIServiceMgrStub::ReturnRequestInner(MessageParcel& data, MessageParcel& reply)
143 {
144     UIServiceMgrXCollie uiServiceMgrXCollie("UISERVICE_RETURN_REQUEST", UI_MGR_SERVICE_TIMEOUT);
145     if (!IsSystemApp()) {
146         return ERR_PERMISSION_DENIED;
147     }
148     std::shared_ptr<AAFwk::Want> want(data.ReadParcelable<AAFwk::Want>());
149     if (want == nullptr) {
150         return ERR_INVALID_VALUE;
151     }
152     const std::string& source = data.ReadString();
153     const std::string& dataStr = data.ReadString();
154     const std::string& extraData = data.ReadString();
155     int32_t result = ReturnRequest(*want, source, dataStr, extraData);
156     reply.WriteInt32(result);
157     return NO_ERROR;
158 }
159 }
160