1
2 /*
3 * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "form_acquire_connection.h"
18
19 #include <cinttypes>
20
21 #include "form_report.h"
22 #include "fms_log_wrapper.h"
23 #include "form_constants.h"
24 #include "form_supply_callback.h"
25 #include "form_task_mgr.h"
26 #include "form_util.h"
27 #include "want.h"
28
29 namespace OHOS {
30 namespace AppExecFwk {
FormAcquireConnection(const int64_t formId,const FormItemInfo & info,const WantParams & wantParams,const sptr<IRemoteObject> hostToken)31 FormAcquireConnection::FormAcquireConnection(const int64_t formId, const FormItemInfo &info,
32 const WantParams &wantParams, const sptr<IRemoteObject> hostToken) : info_(info), wantParams_(wantParams)
33 {
34 SetFormId(formId);
35 SetProviderKey(info.GetProviderBundleName(), info.GetAbilityName());
36 SetHostToken(hostToken);
37 }
38 /**
39 * @brief OnAbilityConnectDone, AbilityMs notify caller ability the result of connect.
40 * @param element service ability's ElementName.
41 * @param remoteObject the session proxy of service ability.
42 * @param resultCode ERR_OK on success, others on failure.
43 */
OnAbilityConnectDone(const AppExecFwk::ElementName & element,const sptr<IRemoteObject> & remoteObject,int resultCode)44 void FormAcquireConnection::OnAbilityConnectDone(const AppExecFwk::ElementName &element,
45 const sptr<IRemoteObject> &remoteObject, int resultCode)
46 {
47 HILOG_INFO("formId:%{public}" PRId64, GetFormId());
48 if (resultCode != ERR_OK) {
49 HILOG_ERROR("abilityName:%{public}s, formId:%{public}" PRId64 ", resultCode:%{public}d",
50 element.GetAbilityName().c_str(), GetFormId(), resultCode);
51 return;
52 }
53 FormReport::GetInstance().SetEndBindTime(GetFormId(), FormUtil::GetCurrentSteadyClockMillseconds());
54 onFormAppConnect();
55 #ifdef RES_SCHEDULE_ENABLE
56 OnFormAbilityConnectDoneCallback();
57 #endif
58 sptr<FormAcquireConnection> connection(this);
59 FormSupplyCallback::GetInstance()->AddConnection(connection);
60 Want want;
61 want.SetParams(wantParams_);
62 want.SetParam(Constants::PARAM_FORM_NAME_KEY, info_.GetFormName());
63 want.SetParam(Constants::PARAM_FORM_DIMENSION_KEY, info_.GetSpecificationId());
64 want.SetParam(Constants::PARAM_FORM_TEMPORARY_KEY, info_.IsTemporaryForm());
65 if (want.GetBoolParam(Constants::RECREATE_FORM_KEY, false)) {
66 want.SetParam(Constants::ACQUIRE_TYPE, Constants::ACQUIRE_TYPE_RECREATE_FORM);
67 } else {
68 want.SetParam(Constants::ACQUIRE_TYPE, Constants::ACQUIRE_TYPE_CREATE_FORM);
69 }
70 want.SetParam(Constants::FORM_CONNECT_ID, this->GetConnectId());
71 want.SetElementName(info_.GetDeviceId(), info_.GetProviderBundleName(),
72 info_.GetAbilityName(), info_.GetModuleName());
73 HILOG_DEBUG("deviceId:%{public}s, bundleName:%{public}s, abilityName:%{public}s",
74 info_.GetDeviceId().c_str(), info_.GetProviderBundleName().c_str(), info_.GetAbilityName().c_str());
75
76 FormTaskMgr::GetInstance().PostAcquireTask(GetFormId(), want, remoteObject);
77 FormReport::GetInstance().SetStartGetTime(GetFormId(), FormUtil::GetCurrentSteadyClockMillseconds());
78 if (GetHostToken() != nullptr) {
79 SetProviderToken(remoteObject);
80 }
81 }
82
83 /**
84 * @brief OnAbilityDisconnectDone, AbilityMs notify caller ability the result of disconnect.
85 * @param element service ability's ElementName.
86 * @param resultCode ERR_OK on success, others on failure.
87 */
OnAbilityDisconnectDone(const AppExecFwk::ElementName & element,int resultCode)88 void FormAcquireConnection::OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int resultCode)
89 {
90 FormAbilityConnection::OnAbilityDisconnectDone(element, resultCode);
91 #ifdef RES_SCHEDULE_ENABLE
92 OnFormAbilityDisconnectDoneCallback();
93 #endif
94 }
95
SetFormAbilityConnectCb(std::function<void (const std::string & bundleName)> && callback)96 void FormAcquireConnection::SetFormAbilityConnectCb(
97 std::function<void(const std::string &bundleName)> &&callback)
98 {
99 onFormAblityConnectCb_ = std::move(callback);
100 }
101
SetFormAbilityDisconnectCb(std::function<void (const std::string & bundleName)> && callback)102 void FormAcquireConnection::SetFormAbilityDisconnectCb(
103 std::function<void(const std::string &bundleName)> &&callback)
104 {
105 onFormAblityDisconnectCb_ = std::move(callback);
106 }
107
OnFormAbilityConnectDoneCallback()108 void FormAcquireConnection::OnFormAbilityConnectDoneCallback()
109 {
110 if (!onFormAblityConnectCb_) {
111 HILOG_ERROR("Empty form ability connect callback");
112 return;
113 }
114 onFormAblityConnectCb_(GetBundleName());
115 }
116
OnFormAbilityDisconnectDoneCallback()117 void FormAcquireConnection::OnFormAbilityDisconnectDoneCallback()
118 {
119 if (!onFormAblityDisconnectCb_) {
120 HILOG_ERROR("Empty form ability disconnect callback");
121 return;
122 }
123 onFormAblityDisconnectCb_(GetBundleName());
124 }
125 } // namespace AppExecFwk
126 } // namespace OHOS