1 /*
2  * Copyright (c) 2023 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 "form_sandbox_render_mgr_inner.h"
17 
18 #include <mutex>
19 
20 #include "fms_log_wrapper.h"
21 #include "form_bms_helper.h"
22 #include "form_trust_mgr.h"
23 #include "ipc_skeleton.h"
24 #include "want.h"
25 
26 namespace OHOS {
27 namespace AppExecFwk {
28 namespace {
29 constexpr int32_t DLP_TYPE = 2;
30 }
31 using Want = OHOS::AAFwk::Want;
FormSandboxRenderMgrInner()32 FormSandboxRenderMgrInner::FormSandboxRenderMgrInner()
33 {
34 }
~FormSandboxRenderMgrInner()35 FormSandboxRenderMgrInner::~FormSandboxRenderMgrInner()
36 {
37 }
38 
IsSandboxFRSInstalled() const39 ErrCode FormSandboxRenderMgrInner::IsSandboxFRSInstalled() const
40 {
41     sptr<IBundleMgr> iBundleMgr = FormBmsHelper::GetInstance().GetBundleMgr();
42     if (iBundleMgr == nullptr) {
43         HILOG_ERROR("get IBundleMgr failed");
44         return ERR_APPEXECFWK_FORM_GET_BMS_FAILED;
45     }
46     BundleInfo info;
47     std::string identity = IPCSkeleton::ResetCallingIdentity();
48     ErrCode ret = iBundleMgr->GetSandboxBundleInfo(
49         Constants::FRS_BUNDLE_NAME, Constants::DEFAULT_SANDBOX_FRS_APP_INDEX, Constants::DEFAULT_USER_ID, info);
50     IPCSkeleton::SetCallingIdentity(identity);
51     return ret;
52 }
53 
InstallSandboxFRS(int32_t & appIndex) const54 ErrCode FormSandboxRenderMgrInner::InstallSandboxFRS(int32_t &appIndex) const
55 {
56     HILOG_INFO("start");
57     sptr<IBundleInstaller> bundleInstallerProxy = FormBmsHelper::GetInstance().GetBundleInstaller();
58     if (bundleInstallerProxy == nullptr) {
59         HILOG_ERROR("GetBundleInstaller failed");
60         return ERR_APPEXECFWK_FORM_GET_BMS_FAILED;
61     }
62     return bundleInstallerProxy->InstallSandboxApp(
63         Constants::FRS_BUNDLE_NAME, DLP_TYPE, GetUserId(), appIndex);
64 }
65 
RenderForm(const FormRecord & formRecord,Want & want,const sptr<IRemoteObject> & hostToken)66 ErrCode FormSandboxRenderMgrInner::RenderForm(
67     const FormRecord &formRecord, Want &want, const sptr<IRemoteObject> &hostToken)
68 {
69     HILOG_INFO("start");
70     {
71         std::lock_guard<std::mutex> lock(sandboxMutex_);
72         if (GetRenderRemoteObj() == nullptr && IsSandboxFRSInstalled() != ERR_OK) {
73             HILOG_INFO("sandbox frs not installed");
74             int32_t appIndex = 0;
75             std::string identity  = IPCSkeleton::ResetCallingIdentity();
76             ErrCode ret = InstallSandboxFRS(appIndex);
77             IPCSkeleton::SetCallingIdentity(identity);
78             if (ret != ERR_OK) {
79                 HILOG_ERROR("InstallSandboxFRS fail, ret:%{public}d", ret);
80                 return ret;
81             }
82             if (appIndex != Constants::DEFAULT_SANDBOX_FRS_APP_INDEX) {
83                 HILOG_ERROR("sandbox FRS already install, please check, appIndex:%{public}d", appIndex);
84                 return ERR_APPEXECFWK_FORM_INVALID_PARAM;
85             }
86         }
87     }
88 
89     return FormRenderMgrInner::RenderForm(formRecord, want, hostToken);
90 }
91 } // namespace AppExecFwk
92 } // namespace OHOS
93