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_connection.h"
17 
18 #include <cinttypes>
19 
20 #include "fms_log_wrapper.h"
21 #include "form_bms_helper.h"
22 #include "form_cache_mgr.h"
23 #include "form_constants.h"
24 #include "form_mgr_errors.h"
25 #include "form_supply_callback.h"
26 #include "form_render_mgr.h"
27 #include "form_task_mgr.h"
28 #include "form_util.h"
29 #include "ipc_skeleton.h"
30 #include "want.h"
31 
32 namespace OHOS {
33 namespace AppExecFwk {
FormRenderConnection(const FormRecord & formRecord,const WantParams & wantParams)34 FormRenderConnection::FormRenderConnection(
35     const FormRecord &formRecord, const WantParams &wantParams) : formRecord_(formRecord), wantParams_(wantParams)
36 {
37     SetFormId(formRecord.formId);
38     SetProviderKey(formRecord.bundleName, formRecord.abilityName);
39 }
40 
OnAbilityConnectDone(const AppExecFwk::ElementName & element,const sptr<IRemoteObject> & remoteObject,int resultCode)41 void FormRenderConnection::OnAbilityConnectDone(const AppExecFwk::ElementName &element,
42     const sptr<IRemoteObject> &remoteObject, int resultCode)
43 {
44     HILOG_INFO("ConnectDone, formId:%{public}" PRId64, GetFormId());
45     if (resultCode != ERR_OK) {
46         HILOG_ERROR("abilityName:%{public}s, formId:%{public}" PRId64 ", resultCode:%{public}d",
47             element.GetAbilityName().c_str(), GetFormId(), resultCode);
48         return;
49     }
50 
51     connectState_ = ConnectState::CONNECTED;
52     int32_t compileMode = 0;
53     if (!FormBmsHelper::GetInstance().GetCompileMode(formRecord_.bundleName, formRecord_.moduleName,
54         formRecord_.providerUserId, compileMode)) {
55         HILOG_ERROR("get compile mode failed");
56         return;
57     }
58 
59     FormRecord newRecord(formRecord_);
60     std::string cacheData;
61     std::map<std::string, std::pair<sptr<FormAshmem>, int32_t>> imageDataMap;
62     if (FormCacheMgr::GetInstance().GetData(formRecord_.formId, cacheData, imageDataMap)) {
63         newRecord.formProviderInfo.SetFormDataString(cacheData);
64         newRecord.formProviderInfo.SetImageDataMap(imageDataMap);
65     }
66 
67     sptr<FormRenderConnection> connection(this);
68     FormRenderMgr::GetInstance().AddConnection(GetFormId(), connection, newRecord);
69     FormRenderMgr::GetInstance().AddRenderDeathRecipient(remoteObject, newRecord);
70     Want want;
71     want.SetParams(wantParams_);
72     want.SetParam(Constants::FORM_CONNECT_ID, this->GetConnectId());
73     want.SetParam(Constants::FORM_COMPILE_MODE_KEY, compileMode);
74     FormTaskMgr::GetInstance().PostRenderForm(newRecord, std::move(want), remoteObject);
75 }
76 
OnAbilityDisconnectDone(const AppExecFwk::ElementName & element,int resultCode)77 void FormRenderConnection::OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int resultCode)
78 {
79     HILOG_DEBUG("element:%{public}s, resultCode:%{public}d, connectState:%{public}d",
80         element.GetURI().c_str(), resultCode, connectState_);
81     // If connectState_ is CONNECTING, it means connect failed and host will try again, don't need to notify host
82     if (resultCode && connectState_ == ConnectState::CONNECTING) {
83         FormRenderMgr::GetInstance().RemoveConnection(GetFormId(), formRecord_);
84     }
85     connectState_ = ConnectState::DISCONNECTED;
86 }
87 
SetStateConnecting()88 void FormRenderConnection::SetStateConnecting()
89 {
90     connectState_ = ConnectState::CONNECTING;
91 }
92 
SetStateDisconnected()93 void FormRenderConnection::SetStateDisconnected()
94 {
95     connectState_ = ConnectState::DISCONNECTED;
96 }
97 
UpdateWantParams(const WantParams & wantParams)98 void FormRenderConnection::UpdateWantParams(const WantParams &wantParams)
99 {
100     wantParams_ = wantParams;
101 }
102 
UpdateFormRecord(const FormRecord & formRecord)103 void FormRenderConnection::UpdateFormRecord(const FormRecord &formRecord)
104 {
105     formRecord_ = formRecord;
106 }
107 }  // namespace AppExecFwk
108 }  // namespace OHOS
109