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