• 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 
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 {
32 const std::string GPU_PROCESS_TYPE = "gpu-process";
AafwkAppMgrClientAdapterImpl()33 AafwkAppMgrClientAdapterImpl::AafwkAppMgrClientAdapterImpl()
34     : appMgrClient_(std::make_unique<AppExecFwk::AppMgrClient>())
35 {}
36 
StartRenderProcess(const std::string & renderParam,int32_t ipcFd,int32_t sharedFd,int32_t crashFd,pid_t & renderPid)37 int AafwkAppMgrClientAdapterImpl::StartRenderProcess(
38     const std::string& renderParam, int32_t ipcFd, int32_t sharedFd, int32_t crashFd, pid_t& renderPid)
39 {
40     if (appMgrClient_ == nullptr) {
41         WVLOG_E("app mgr client is nullptr.");
42         return -1;
43     }
44 
45     int retryCnt = 0;
46     int ret;
47     do {
48         ret = appMgrClient_->StartRenderProcess(renderParam, ipcFd, sharedFd, crashFd, renderPid, false);
49         if (ret == RET_ALREADY_EXIST_RENDER) {
50             WVLOG_E("app mgr client start render process failed, render process already exist.");
51             std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_FOR_MILLI_SECONDS_CNT));
52             continue;
53         }
54         if (ret != 0) {
55             WVLOG_E("app mgr client start render process failed, ret = %{public}d.", ret);
56             return -1;
57         }
58     } while (++retryCnt < START_RENDER_PROCESS_MAX_CNT && ret != 0);
59 
60     if (ret != 0) {
61         WVLOG_E("over max retry times, app mgr client start render process failed, ret = %{public}d.", ret);
62         return -1;
63     }
64 
65     return 0;
66 }
67 
AttachRenderProcess(std::shared_ptr<AafwkRenderSchedulerHostAdapter> adapter)68 void AafwkAppMgrClientAdapterImpl::AttachRenderProcess(std::shared_ptr<AafwkRenderSchedulerHostAdapter> adapter)
69 {
70     if (appMgrClient_ == nullptr) {
71         WVLOG_E("app mgr client is nullptr.");
72         return;
73     }
74 
75     if (adapter == nullptr) {
76         WVLOG_E("render scheduler is nullptr.");
77         return;
78     }
79 
80     AafwkRenderSchedulerImpl *renderScheduler = new (std::nothrow) AafwkRenderSchedulerImpl(adapter);
81     if (renderScheduler == nullptr) {
82         WVLOG_E("new AafwkRenderSchedulerImpl failed.");
83         return;
84     }
85 
86     appMgrClient_->AttachRenderProcess(renderScheduler);
87 }
88 
GetRenderProcessTerminationStatus(pid_t renderPid,int & status)89 int AafwkAppMgrClientAdapterImpl::GetRenderProcessTerminationStatus(pid_t renderPid, int &status)
90 {
91     if (appMgrClient_ == nullptr) {
92         WVLOG_E("app mgr client is nullptr.");
93         return -1;
94     }
95 
96     int retryCnt = 0;
97     do {
98         int ret = appMgrClient_->GetRenderProcessTerminationStatus(renderPid, status);
99         if (ret != 0) {
100             WVLOG_E("app mgr client get render process termination status failed, ret = %{public}d.", ret);
101             return -1;
102         }
103     } while (status < 0 && ++retryCnt < GET_TERMINATION_STATUS_MAX_CNT);
104 
105     if (status < 0) {
106         WVLOG_E("render process termination status invalid, status = %{public}d.", status);
107         return -1;
108     }
109 
110     return 0;
111 }
112 
StartChildProcess(const std::string & renderParam,int32_t ipcFd,int32_t sharedFd,int32_t crashFd,pid_t & renderPid,const std::string & processType)113 int AafwkAppMgrClientAdapterImpl::StartChildProcess(
114     const std::string& renderParam, int32_t ipcFd, int32_t sharedFd,
115     int32_t crashFd, pid_t& renderPid, const std::string& processType)
116 {
117     if (appMgrClient_ == nullptr) {
118         WVLOG_E("app mgr client is nullptr.");
119         return -1;
120     }
121 
122     bool isGPU = false;
123     if (processType == GPU_PROCESS_TYPE) {
124         isGPU = true;
125     }
126 
127     int retryCnt = 0;
128     int ret;
129     do {
130         ret = appMgrClient_->StartRenderProcess(renderParam, ipcFd, sharedFd, crashFd, renderPid, isGPU);
131         if (ret == RET_ALREADY_EXIST_RENDER) {
132             WVLOG_E("app mgr client start %{public}s process failed, process already exist.", processType.c_str());
133             std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_FOR_MILLI_SECONDS_CNT));
134             continue;
135         }
136         if (ret != 0) {
137             WVLOG_E("app mgr client start render process failed, ret = %{public}d.", ret);
138             return -1;
139         }
140     } while (++retryCnt < START_RENDER_PROCESS_MAX_CNT && ret != 0);
141 
142     if (ret != 0) {
143         WVLOG_E("over max retry times, app mgr client start render process failed, ret = %{public}d.", ret);
144         return -1;
145     }
146 
147     return 0;
148 }
149 
SaveBrowserConnect(std::shared_ptr<AafwkBrowserHostAdapter> adapter)150 void AafwkAppMgrClientAdapterImpl::SaveBrowserConnect(std::shared_ptr<AafwkBrowserHostAdapter> adapter)
151 {
152     if (appMgrClient_ == nullptr) {
153         WVLOG_E("app mgr client is nullptr.");
154         return;
155     }
156 
157     if (adapter == nullptr) {
158         WVLOG_E("browser connect is nullptr.");
159         return;
160     }
161 
162     aafwkBrowserHostImpl_ = new (std::nothrow) AafwkBrowserHostImpl(adapter);
163     if (aafwkBrowserHostImpl_ == nullptr) {
164         WVLOG_E("create new AafwkBrowserHostImpl failed!");
165     }
166     WVLOG_I("AafwkAppMgrClientAdapterImpl SaveBrowserConnect success!");
167     appMgrClient_->SaveBrowserChannel(aafwkBrowserHostImpl_);
168 }
169 } // namespace OHOS::NWeb
170