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 "aafwk_app_mgr_client_adapter_impl.h"
17
18 #include <chrono>
19 #include <thread>
20
21 #include "aafwk_render_scheduler_impl.h"
22 #include "nweb_log.h"
23
24 namespace {
25 constexpr int GET_TERMINATION_STATUS_MAX_CNT = 5;
26 constexpr int START_RENDER_PROCESS_MAX_CNT = 10;
27 constexpr int SLEEP_FOR_MILLI_SECONDS_CNT = 10;
28 constexpr int RET_ALREADY_EXIST_RENDER = 8454244; // copy from ability_runtime
29 } // namespace
30
31 namespace OHOS::NWeb {
AafwkAppMgrClientAdapterImpl()32 AafwkAppMgrClientAdapterImpl::AafwkAppMgrClientAdapterImpl()
33 : appMgrClient_(std::make_unique<AppExecFwk::AppMgrClient>())
34 {}
35
StartRenderProcess(const std::string & renderParam,int32_t ipcFd,int32_t sharedFd,int32_t crashFd,pid_t & renderPid)36 int AafwkAppMgrClientAdapterImpl::StartRenderProcess(
37 const std::string& renderParam, int32_t ipcFd, int32_t sharedFd, int32_t crashFd, pid_t& renderPid)
38 {
39 if (appMgrClient_ == nullptr) {
40 WVLOG_E("app mgr client is nullptr.");
41 return -1;
42 }
43
44 int retryCnt = 0;
45 int ret;
46 do {
47 ret = appMgrClient_->StartRenderProcess(renderParam, ipcFd, sharedFd, crashFd, renderPid);
48 if (ret == RET_ALREADY_EXIST_RENDER) {
49 WVLOG_E("app mgr client start render process failed, render process already exist.");
50 std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_FOR_MILLI_SECONDS_CNT));
51 continue;
52 }
53 if (ret != 0) {
54 WVLOG_E("app mgr client start render process failed, ret = %{public}d.", ret);
55 return -1;
56 }
57 } while (++retryCnt < START_RENDER_PROCESS_MAX_CNT && ret != 0);
58
59 if (ret != 0) {
60 WVLOG_E("over max retry times, app mgr client start render process failed, ret = %{public}d.", ret);
61 return -1;
62 }
63
64 return 0;
65 }
66
AttachRenderProcess(std::shared_ptr<AafwkRenderSchedulerHostAdapter> adapter)67 void AafwkAppMgrClientAdapterImpl::AttachRenderProcess(std::shared_ptr<AafwkRenderSchedulerHostAdapter> adapter)
68 {
69 if (appMgrClient_ == nullptr) {
70 WVLOG_E("app mgr client is nullptr.");
71 return;
72 }
73
74 if (adapter == nullptr) {
75 WVLOG_E("render scheduler is nullptr.");
76 return;
77 }
78
79 AafwkRenderSchedulerImpl *renderScheduler = new (std::nothrow) AafwkRenderSchedulerImpl(adapter);
80 if (renderScheduler == nullptr) {
81 WVLOG_E("new AafwkRenderSchedulerImpl failed.");
82 return;
83 }
84
85 appMgrClient_->AttachRenderProcess(renderScheduler);
86 }
87
GetRenderProcessTerminationStatus(pid_t renderPid,int & status)88 int AafwkAppMgrClientAdapterImpl::GetRenderProcessTerminationStatus(pid_t renderPid, int &status)
89 {
90 if (appMgrClient_ == nullptr) {
91 WVLOG_E("app mgr client is nullptr.");
92 return -1;
93 }
94
95 int retryCnt = 0;
96 do {
97 int ret = appMgrClient_->GetRenderProcessTerminationStatus(renderPid, status);
98 if (ret != 0) {
99 WVLOG_E("app mgr client get render process termination status failed, ret = %{public}d.", ret);
100 return -1;
101 }
102 } while (status < 0 && ++retryCnt < GET_TERMINATION_STATUS_MAX_CNT);
103
104 if (status < 0) {
105 WVLOG_E("render process termination status invalid, status = %{public}d.", status);
106 return -1;
107 }
108
109 return 0;
110 }
111 } // namespace OHOS::NWeb
112