• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 "interfaces/inner_api/ui_session/ui_content_stub.h"
17 
18 #include "ipc_skeleton.h"
19 #include "accesstoken_kit.h"
20 
21 #include "interfaces/inner_api/ui_session/ui_session_manager.h"
22 #include "ui_content_errors.h"
23 
24 #include "adapter/ohos/entrance/ui_session/include/ui_service_hilog.h"
25 
26 namespace OHOS::Ace {
IsSACalling() const27 bool UiContentStub::IsSACalling() const
28 {
29     const auto tokenId = IPCSkeleton::GetCallingTokenID();
30     const auto flag = Security::AccessToken::AccessTokenKit::GetTokenTypeFlag(tokenId);
31     if (flag == Security::AccessToken::ATokenTypeEnum::TOKEN_NATIVE) {
32         LOGD("SA called, tokenId:%{private}u, flag:%{public}u", tokenId, flag);
33         return true;
34     }
35     LOGW("Not SA called, tokenId:%{private}u, flag:%{public}u", tokenId, flag);
36     return false;
37 }
38 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)39 int32_t UiContentStub::OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option)
40 {
41     if (!IsSACalling()) {
42         return -1;
43     }
44     if (data.ReadInterfaceToken() != GetDescriptor()) {
45         LOGW("ui_session InterfaceToken check failed");
46         return -1;
47     }
48     switch (code) {
49         case UI_CONTENT_SERVICE_GET_TREE: {
50             GetInspectorTreeInner(data, reply, option);
51             break;
52         }
53         case UI_CONTENT_CONNECT: {
54             ConnectInner(data, reply, option);
55             break;
56         }
57         case REGISTER_CLICK_EVENT: {
58             RegisterClickEventCallbackInner(data, reply, option);
59             break;
60         }
61         case REGISTER_SEARCH_EVENT: {
62             RegisterSearchEventCallbackInner(data, reply, option);
63             break;
64         }
65         case REGISTER_ROUTER_CHANGE_EVENT: {
66             RegisterRouterChangeEventCallbackInner(data, reply, option);
67             break;
68         }
69         case REGISTER_COMPONENT_EVENT: {
70             RegisterComponentChangeEventCallbackInner(data, reply, option);
71             break;
72         }
73         case SENDCOMMAND_ASYNC_EVENT: {
74             SendCommandInnerAsync(data, reply, option);
75             break;
76         }
77         case SENDCOMMAND_EVENT: {
78             SendCommandInner(data, reply, option);
79             break;
80         }
81         case UNREGISTER_CLICK_EVENT: {
82             UnregisterClickEventCallbackInner(data, reply, option);
83             break;
84         }
85         case UNREGISTER_SEARCH_EVENT: {
86             UnregisterSearchEventCallbackInner(data, reply, option);
87             break;
88         }
89         case UNREGISTER_ROUTER_CHANGE_EVENT: {
90             UnregisterRouterChangeEventCallbackInner(data, reply, option);
91             break;
92         }
93         case UNREGISTER_COMPONENT_EVENT: {
94             UnregisterComponentChangeEventCallbackInner(data, reply, option);
95             break;
96         }
97         case UNREGISTER_WEB_UNFOCUS_EVENT: {
98             UnregisterWebUnfocusEventCallbackInner(data, reply, option);
99             break;
100         }
101         case RESET_ALL_TEXT: {
102             ResetTranslateTextAllInner(data, reply, option);
103             break;
104         }
105         case RESET_TEXT_BY_ID: {
106             ResetTranslateTextInner(data, reply, option);
107             break;
108         }
109         case GET_WEB_VIEW_LANGUAGE: {
110             GetWebViewCurrentLanguageInner(data, reply, option);
111             break;
112         }
113         case GET_WEB_TRANSLATE_TEXT: {
114             GetWebViewTranslateTextInner(data, reply, option);
115             break;
116         }
117         case CONTINUE_GET_WEB_TEXT: {
118             StartWebViewTranslateInner(data, reply, option);
119             break;
120         }
121         case SEND_TRANSLATE_RESULT: {
122             SendTranslateResultInner(data, reply, option);
123             break;
124         }
125         case END_WEB_TRANSLATE: {
126             EndWebViewTranslateInner(data, reply, option);
127             break;
128         }
129         case SEND_TRANSLATE_RESULT_STR: {
130             SendTranslateResultStrInner(data, reply, option);
131             break;
132         }
133         case GET_CURRENT_PAGE_NAME: {
134             GetCurrentPageNameInner(data, reply, option);
135             break;
136         }
137         case GET_CURRENT_SHOWING_IMAGE: {
138             GetCurrentImagesShowingInner(data, reply, option);
139             break;
140         }
141         case GET_VISIBLE_TREE: {
142             GetVisibleInspectorTreeInner(data, reply, option);
143             break;
144         }
145         case SEND_COMMAND: {
146             SendCommandKeyCodeInner(data, reply, option);
147             break;
148         }
149         case EXE_APP_AI_FUNCTION: {
150             ExeAppAIFunctionInner(data, reply, option);
151             break;
152         }
153         default: {
154             LOGI("ui_session unknown transaction code %{public}d", code);
155             return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
156         }
157     }
158     return 0;
159 }
160 
GetInspectorTreeInner(MessageParcel & data,MessageParcel & reply,MessageOption & option)161 int32_t UiContentStub::GetInspectorTreeInner(MessageParcel& data, MessageParcel& reply, MessageOption& option)
162 {
163     GetInspectorTree(nullptr);
164     return NO_ERROR;
165 }
166 
ConnectInner(MessageParcel & data,MessageParcel & reply,MessageOption & option)167 int32_t UiContentStub::ConnectInner(MessageParcel& data, MessageParcel& reply, MessageOption& option)
168 {
169     sptr<IRemoteObject> report = data.ReadRemoteObject();
170     if (report == nullptr) {
171         LOGW("read reportStub object is nullptr,connect failed");
172         return FAILED;
173     }
174     int32_t processId = data.ReadInt32();
175     UiSessionManager::GetInstance()->SaveReportStub(report, processId);
176     UiSessionManager::GetInstance()->SendBaseInfo(processId);
177     return NO_ERROR;
178 }
179 
RegisterClickEventCallbackInner(MessageParcel & data,MessageParcel & reply,MessageOption & option)180 int32_t UiContentStub::RegisterClickEventCallbackInner(MessageParcel& data, MessageParcel& reply, MessageOption& option)
181 {
182     reply.WriteInt32(RegisterClickEventCallback(nullptr));
183     return NO_ERROR;
184 }
185 
RegisterRouterChangeEventCallbackInner(MessageParcel & data,MessageParcel & reply,MessageOption & option)186 int32_t UiContentStub::RegisterRouterChangeEventCallbackInner(
187     MessageParcel& data, MessageParcel& reply, MessageOption& option)
188 {
189     reply.WriteInt32(RegisterRouterChangeEventCallback(nullptr));
190     return NO_ERROR;
191 }
192 
RegisterSearchEventCallbackInner(MessageParcel & data,MessageParcel & reply,MessageOption & option)193 int32_t UiContentStub::RegisterSearchEventCallbackInner(
194     MessageParcel& data, MessageParcel& reply, MessageOption& option)
195 {
196     reply.WriteInt32(RegisterSearchEventCallback(nullptr));
197     return NO_ERROR;
198 }
199 
RegisterComponentChangeEventCallbackInner(MessageParcel & data,MessageParcel & reply,MessageOption & option)200 int32_t UiContentStub::RegisterComponentChangeEventCallbackInner(
201     MessageParcel& data, MessageParcel& reply, MessageOption& option)
202 {
203     reply.WriteInt32(RegisterComponentChangeEventCallback(nullptr));
204     return NO_ERROR;
205 }
206 
RegisterWebUnfocusEventCallbackInner(MessageParcel & data,MessageParcel & reply,MessageOption & option)207 int32_t UiContentStub::RegisterWebUnfocusEventCallbackInner(
208     MessageParcel& data, MessageParcel& reply, MessageOption& option)
209 {
210     reply.WriteInt32(RegisterWebUnfocusEventCallback(nullptr));
211     return NO_ERROR;
212 }
213 
SendCommandInner(MessageParcel & data,MessageParcel & reply,MessageOption & option)214 int32_t UiContentStub::SendCommandInner(
215     MessageParcel& data, MessageParcel& reply, MessageOption& option)
216 {
217     int32_t id = data.ReadInt32();
218     std::string command = data.ReadString();
219     reply.WriteInt32(SendCommand(id, command));
220     return NO_ERROR;
221 }
222 
SendCommandInnerAsync(MessageParcel & data,MessageParcel & reply,MessageOption & option)223 int32_t UiContentStub::SendCommandInnerAsync(
224     MessageParcel& data, MessageParcel& reply, MessageOption& option)
225 {
226     int32_t id = data.ReadInt32();
227     return SendCommandAsync(id, data.ReadString());
228 }
229 
SendCommandKeyCodeInner(MessageParcel & data,MessageParcel & reply,MessageOption & option)230 int32_t UiContentStub::SendCommandKeyCodeInner(MessageParcel& data, MessageParcel& reply, MessageOption& option)
231 {
232     std::string command = data.ReadString();
233     reply.WriteInt32(SendCommand(command));
234     return NO_ERROR;
235 }
236 
UnregisterClickEventCallbackInner(MessageParcel & data,MessageParcel & reply,MessageOption & option)237 int32_t UiContentStub::UnregisterClickEventCallbackInner(
238     MessageParcel& data, MessageParcel& reply, MessageOption& option)
239 {
240     reply.WriteInt32(UnregisterClickEventCallback());
241     return NO_ERROR;
242 }
243 
UnregisterSearchEventCallbackInner(MessageParcel & data,MessageParcel & reply,MessageOption & option)244 int32_t UiContentStub::UnregisterSearchEventCallbackInner(
245     MessageParcel& data, MessageParcel& reply, MessageOption& option)
246 {
247     reply.WriteInt32(UnregisterSearchEventCallback());
248     return NO_ERROR;
249 }
250 
UnregisterRouterChangeEventCallbackInner(MessageParcel & data,MessageParcel & reply,MessageOption & option)251 int32_t UiContentStub::UnregisterRouterChangeEventCallbackInner(
252     MessageParcel& data, MessageParcel& reply, MessageOption& option)
253 {
254     reply.WriteInt32(UnregisterRouterChangeEventCallback());
255     return NO_ERROR;
256 }
257 
UnregisterComponentChangeEventCallbackInner(MessageParcel & data,MessageParcel & reply,MessageOption & option)258 int32_t UiContentStub::UnregisterComponentChangeEventCallbackInner(
259     MessageParcel& data, MessageParcel& reply, MessageOption& option)
260 {
261     reply.WriteInt32(UnregisterComponentChangeEventCallback());
262     return NO_ERROR;
263 }
264 
UnregisterWebUnfocusEventCallbackInner(MessageParcel & data,MessageParcel & reply,MessageOption & option)265 int32_t UiContentStub::UnregisterWebUnfocusEventCallbackInner(
266     MessageParcel& data, MessageParcel& reply, MessageOption& option)
267 {
268     reply.WriteInt32(UnregisterWebUnfocusEventCallback());
269     return NO_ERROR;
270 }
271 
ResetTranslateTextAllInner(MessageParcel & data,MessageParcel & reply,MessageOption & option)272 int32_t UiContentStub::ResetTranslateTextAllInner(MessageParcel& data, MessageParcel& reply, MessageOption& option)
273 {
274     reply.WriteInt32(ResetTranslateTextAll());
275     return NO_ERROR;
276 }
277 
ResetTranslateTextInner(MessageParcel & data,MessageParcel & reply,MessageOption & option)278 int32_t UiContentStub::ResetTranslateTextInner(MessageParcel& data, MessageParcel& reply, MessageOption& option)
279 {
280     int32_t nodeId = data.ReadInt32();
281     reply.WriteInt32(ResetTranslateText(nodeId));
282     return NO_ERROR;
283 }
284 
GetWebViewCurrentLanguageInner(MessageParcel & data,MessageParcel & reply,MessageOption & option)285 int32_t UiContentStub::GetWebViewCurrentLanguageInner(MessageParcel& data, MessageParcel& reply, MessageOption& option)
286 {
287     int32_t processId = data.ReadInt32();
288     UiSessionManager::GetInstance()->SaveProcessId("translate", processId);
289     reply.WriteInt32(GetWebViewCurrentLanguage(nullptr));
290     return NO_ERROR;
291 }
292 
GetWebViewTranslateTextInner(MessageParcel & data,MessageParcel & reply,MessageOption & option)293 int32_t UiContentStub::GetWebViewTranslateTextInner(MessageParcel& data, MessageParcel& reply, MessageOption& option)
294 {
295     std::string extraData = data.ReadString();
296     reply.WriteInt32(GetWebViewTranslateText(extraData, nullptr));
297     return NO_ERROR;
298 }
299 
StartWebViewTranslateInner(MessageParcel & data,MessageParcel & reply,MessageOption & option)300 int32_t UiContentStub::StartWebViewTranslateInner(MessageParcel& data, MessageParcel& reply, MessageOption& option)
301 {
302     std::string extraData = data.ReadString();
303     int32_t processId = data.ReadInt32();
304     UiSessionManager::GetInstance()->SaveProcessId("translate", processId);
305     reply.WriteInt32(StartWebViewTranslate(extraData, nullptr));
306     return NO_ERROR;
307 }
308 
SendTranslateResultInner(MessageParcel & data,MessageParcel & reply,MessageOption & option)309 int32_t UiContentStub::SendTranslateResultInner(MessageParcel& data, MessageParcel& reply, MessageOption& option)
310 {
311     int32_t nodeId = data.ReadInt32();
312     std::vector<std::string> results;
313     data.ReadStringVector(&results);
314     std::vector<int32_t> ids;
315     data.ReadInt32Vector(&ids);
316     reply.WriteInt32(SendTranslateResult(nodeId, results, ids));
317     return NO_ERROR;
318 }
EndWebViewTranslateInner(MessageParcel & data,MessageParcel & reply,MessageOption & option)319 int32_t UiContentStub::EndWebViewTranslateInner(MessageParcel& data, MessageParcel& reply, MessageOption& option)
320 {
321     reply.WriteInt32(EndWebViewTranslate());
322     return NO_ERROR;
323 }
324 
SendTranslateResultStrInner(MessageParcel & data,MessageParcel & reply,MessageOption & option)325 int32_t UiContentStub::SendTranslateResultStrInner(MessageParcel& data, MessageParcel& reply, MessageOption& option)
326 {
327     int32_t nodeId = data.ReadInt32();
328     std::string result = data.ReadString();
329     reply.WriteInt32(SendTranslateResult(nodeId, result));
330     return NO_ERROR;
331 }
332 
GetCurrentPageNameInner(MessageParcel & data,MessageParcel & reply,MessageOption & option)333 int32_t UiContentStub::GetCurrentPageNameInner(MessageParcel& data, MessageParcel& reply, MessageOption& option)
334 {
335     reply.WriteInt32(GetCurrentPageName(nullptr));
336     return NO_ERROR;
337 }
338 
GetCurrentImagesShowingInner(MessageParcel & data,MessageParcel & reply,MessageOption & option)339 int32_t UiContentStub::GetCurrentImagesShowingInner(MessageParcel& data, MessageParcel& reply, MessageOption& option)
340 {
341     int32_t processId = data.ReadInt32();
342     UiSessionManager::GetInstance()->SaveProcessId("pixel", processId);
343     reply.WriteInt32(GetCurrentImagesShowing(nullptr));
344     return NO_ERROR;
345 }
346 
GetVisibleInspectorTreeInner(MessageParcel & data,MessageParcel & reply,MessageOption & option)347 int32_t UiContentStub::GetVisibleInspectorTreeInner(MessageParcel& data, MessageParcel& reply, MessageOption& option)
348 {
349     GetVisibleInspectorTree(nullptr);
350     return NO_ERROR;
351 }
352 
ExeAppAIFunctionInner(MessageParcel & data,MessageParcel & reply,MessageOption & option)353 int32_t UiContentStub::ExeAppAIFunctionInner(MessageParcel& data, MessageParcel& reply, MessageOption& option)
354 {
355     std::string funcName = data.ReadString();
356     std::string params = data.ReadString();
357     reply.WriteInt32(ExeAppAIFunction(funcName, params, nullptr));
358     return NO_ERROR;
359 }
360 } // namespace OHOS::Ace
361