• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_render/form_render_connection.h"
17 
18 #include <cinttypes>
19 
20 #include "fms_log_wrapper.h"
21 #include "bms_mgr/form_bms_helper.h"
22 #include "data_center/form_cache_mgr.h"
23 #include "form_constants.h"
24 #include "form_mgr_errors.h"
25 #include "form_provider/form_supply_callback.h"
26 #include "form_render/form_render_mgr.h"
27 #include "status_mgr_center/form_status_task_mgr.h"
28 #include "form_host/form_host_task_mgr.h"
29 #include "common/util/form_util.h"
30 #include "ipc_skeleton.h"
31 #include "want.h"
32 #include "feature/memory_mgr/form_render_report.h"
33 
34 const int32_t MAX_FAILED_TIMES = 5;
35 
36 namespace OHOS {
37 namespace AppExecFwk {
FormRenderConnection(const FormRecord & formRecord,const WantParams & wantParams)38 FormRenderConnection::FormRenderConnection(
39     const FormRecord &formRecord, const WantParams &wantParams) : formRecord_(formRecord), wantParams_(wantParams)
40 {
41     SetFormId(formRecord.formId);
42     SetProviderKey(formRecord.bundleName, formRecord.abilityName);
43 }
44 
OnAbilityConnectDone(const AppExecFwk::ElementName & element,const sptr<IRemoteObject> & remoteObject,int resultCode)45 void FormRenderConnection::OnAbilityConnectDone(const AppExecFwk::ElementName &element,
46     const sptr<IRemoteObject> &remoteObject, int resultCode)
47 {
48     HILOG_INFO("ConnectDone, formId:%{public}" PRId64, GetFormId());
49     if (resultCode != ERR_OK) {
50         HILOG_ERROR("abilityName:%{public}s, formId:%{public}" PRId64 ", resultCode:%{public}d",
51             element.GetAbilityName().c_str(), GetFormId(), resultCode);
52         return;
53     }
54     if (remoteObject == nullptr || remoteObject->IsObjectDead()) {
55         failedTimes++;
56         HILOG_WARN("remoteObject is null or dead, formId:%{public}" PRId64 ", failedTimes:%{public}d",
57             GetFormId(), failedTimes);
58         if (failedTimes <= MAX_FAILED_TIMES) {
59             FormHostTaskMgr::GetInstance().PostConnectFRSFailedTaskToHost(
60                 GetFormId(), ERR_APPEXECFWK_FORM_RENDER_SERVICE_DIED);
61         }
62         return;
63     }
64     FormRenderReport::GetInstance().RecordFRSStart();
65     connectState_ = ConnectState::CONNECTED;
66     failedTimes = 0;
67     int32_t compileMode = 0;
68     if (!FormBmsHelper::GetInstance().GetCompileMode(formRecord_.bundleName, formRecord_.moduleName,
69         formRecord_.providerUserId, compileMode)) {
70         HILOG_ERROR("get compile mode failed");
71         return;
72     }
73 
74     FormRecord newRecord(formRecord_);
75     std::string cacheData;
76     std::map<std::string, std::pair<sptr<FormAshmem>, int32_t>> imageDataMap;
77     if (FormCacheMgr::GetInstance().GetData(formRecord_.formId, cacheData, imageDataMap)) {
78         newRecord.formProviderInfo.SetFormDataString(cacheData);
79         newRecord.formProviderInfo.SetImageDataMap(imageDataMap);
80     }
81 
82     sptr<FormRenderConnection> connection(this);
83     FormRenderMgr::GetInstance().AddConnection(GetFormId(), connection, newRecord);
84     FormRenderMgr::GetInstance().AddRenderDeathRecipient(remoteObject, newRecord);
85     Want want;
86     want.SetParams(wantParams_);
87     want.SetParam(Constants::FORM_CONNECT_ID, this->GetConnectId());
88     want.SetParam(Constants::FORM_COMPILE_MODE_KEY, compileMode);
89     FormStatusTaskMgr::GetInstance().PostRenderForm(newRecord, std::move(want), remoteObject);
90 }
91 
OnAbilityDisconnectDone(const AppExecFwk::ElementName & element,int resultCode)92 void FormRenderConnection::OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int resultCode)
93 {
94     HILOG_INFO("formId:%{public}" PRId64 ", resultCode:%{public}d, connectState:%{public}d",
95         GetFormId(), resultCode, connectState_);
96     // If connectState_ is CONNECTING, it means connect failed and host will try again, don't need to notify host
97     if (resultCode && connectState_ == ConnectState::CONNECTING) {
98         FormRenderMgr::GetInstance().RemoveConnection(GetFormId(), formRecord_);
99     }
100     connectState_ = ConnectState::DISCONNECTED;
101     failedTimes = 0;
102 }
103 
SetStateConnecting()104 void FormRenderConnection::SetStateConnecting()
105 {
106     connectState_ = ConnectState::CONNECTING;
107 }
108 
SetStateDisconnected()109 void FormRenderConnection::SetStateDisconnected()
110 {
111     connectState_ = ConnectState::DISCONNECTED;
112 }
113 
UpdateWantParams(const WantParams & wantParams)114 void FormRenderConnection::UpdateWantParams(const WantParams &wantParams)
115 {
116     wantParams_ = wantParams;
117 }
118 
UpdateFormRecord(const FormRecord & formRecord)119 void FormRenderConnection::UpdateFormRecord(const FormRecord &formRecord)
120 {
121     formRecord_ = formRecord;
122 }
123 }  // namespace AppExecFwk
124 }  // namespace OHOS
125