1 /*
2 * Copyright (c) 2021-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 "adapter/ohos/entrance/ace_form_ability.h"
17
18 #include "form_provider_client.h"
19
20 #include "adapter/ohos/entrance/pa_engine/pa_backend.h"
21
22 namespace OHOS::Ace {
23 using namespace OHOS::AAFwk;
24 using namespace OHOS::AppExecFwk;
25 using FormPlatformFinish = std::function<void()>;
26 class FormPlatformEventCallback final : public Platform::PlatformEventCallback {
27 public:
FormPlatformEventCallback(FormPlatformFinish onFinish)28 explicit FormPlatformEventCallback(FormPlatformFinish onFinish) : onFinish_(onFinish) {}
29
30 ~FormPlatformEventCallback() override = default;
31
32 void OnFinish() const override
33 {
34 TAG_LOGI(AceLogTag::ACE_FORM, "FormPlatformEventCallback OnFinish");
35 CHECK_NULL_VOID(onFinish_);
36 onFinish_();
37 }
38
39 void OnStatusBarBgColorChanged(uint32_t color) override
40 {
41 TAG_LOGI(AceLogTag::ACE_FORM, "FormPlatformEventCallback OnStatusBarBgColorChanged");
42 }
43
44 private:
45 FormPlatformFinish onFinish_;
46 };
47
48 const std::string AceFormAbility::START_PARAMS_KEY = "__startParams";
49 const std::string AceFormAbility::URI = "url";
50
51 REGISTER_AA(AceFormAbility)
52
AceFormAbility()53 AceFormAbility::AceFormAbility()
54 {
55 instanceId_ = Container::GenerateId<PA_FORM_CONTAINER>();
56 }
57
LoadFormEnv(const OHOS::AAFwk::Want& want)58 void AceFormAbility::LoadFormEnv(const OHOS::AAFwk::Want& want)
59 {
60 // get url
61 std::string parsedUrl;
62 if (want.HasParameter(URI)) {
63 parsedUrl = want.GetStringParam(URI);
64 } else {
65 parsedUrl = "form.js";
66 }
67
68 // get asset
69 auto packagePathStr = GetBundleCodePath();
70 auto moduleInfo = GetHapModuleInfo();
71 CHECK_NULL_VOID(moduleInfo);
72 packagePathStr += "/" + moduleInfo->package + "/";
73 std::shared_ptr<AbilityInfo> abilityInfo = GetAbilityInfo();
74
75 // init form ability
76 BackendType backendType = BackendType::FORM;
77 SrcLanguage srcLanguage = SrcLanguage::ETS;
78 if (abilityInfo != nullptr && !abilityInfo->srcLanguage.empty()) {
79 if (abilityInfo->srcLanguage == "js") {
80 srcLanguage = SrcLanguage::JS;
81 }
82 }
83
84 std::shared_ptr<Platform::WorkerPath> workerPath = std::make_shared<Platform::WorkerPath>();
85 workerPath->packagePathStr = packagePathStr;
86 std::vector<std::string> assetBasePathStr;
87
88 if (abilityInfo != nullptr && !abilityInfo->srcPath.empty()) {
89 assetBasePathStr = { "assets/js/" + abilityInfo->srcPath + "/", std::string("assets/js/") };
90 } else {
91 assetBasePathStr = { std::string("assets/js/default/"), std::string("assets/js/share/") };
92 }
93
94 workerPath->assetBasePathStr = assetBasePathStr;
95
96 Platform::PaContainerOptions options;
97 options.type = backendType;
98 options.language = srcLanguage;
99 options.hapPath = moduleInfo->hapPath;
100 options.workerPath = workerPath;
101
102 Platform::PaContainer::CreateContainer(
103 instanceId_, this, options, std::make_unique<FormPlatformEventCallback>([this]() { TerminateAbility(); }));
104 Platform::PaContainer::AddAssetPath(instanceId_, packagePathStr, moduleInfo->hapPath, assetBasePathStr);
105
106 // run form ability
107 Platform::PaContainer::RunPa(instanceId_, parsedUrl, want);
108 }
109
OnCreate(const OHOS::AAFwk::Want& want)110 OHOS::AppExecFwk::FormProviderInfo AceFormAbility::OnCreate(const OHOS::AAFwk::Want& want)
111 {
112 std::string formId = want.GetStringParam(AppExecFwk::Constants::PARAM_FORM_IDENTITY_KEY);
113 Platform::PaContainer::OnCreate(instanceId_, want);
114 OHOS::AppExecFwk::FormProviderInfo formProviderInfo;
115 formProviderInfo.SetFormData(Platform::PaContainer::GetFormData(instanceId_));
116 std::string formData = formProviderInfo.GetFormData().GetDataString();
117 TAG_LOGI(AceLogTag::ACE_FORM, "AceFormAbility OnCreate formId = %{public}s, formData: %{public}s", formId.c_str(),
118 formData.c_str());
119 return formProviderInfo;
120 }
121
OnDelete(const int64_t formId)122 void AceFormAbility::OnDelete(const int64_t formId)
123 {
124 TAG_LOGI(AceLogTag::ACE_FORM, "AceFormAbility OnDelete called: %{public}s", std::to_string(formId).c_str());
125 Platform::PaContainer::OnDelete(instanceId_, formId);
126 }
127
OnTriggerEvent(const int64_t formId, const std::string& message)128 void AceFormAbility::OnTriggerEvent(const int64_t formId, const std::string& message)
129 {
130 TAG_LOGI(AceLogTag::ACE_FORM, "AceFormAbility OnTriggerEvent called: %{public}s", std::to_string(formId).c_str());
131 Platform::PaContainer::OnTriggerEvent(instanceId_, formId, message);
132 }
133
OnAcquireFormState(const OHOS::AAFwk::Want& want)134 AppExecFwk::FormState AceFormAbility::OnAcquireFormState(const OHOS::AAFwk::Want& want)
135 {
136 TAG_LOGI(AceLogTag::ACE_FORM, "AceFormAbility OnAcquireState called");
137 int32_t formState = Platform::PaContainer::OnAcquireFormState(instanceId_, want);
138 if (formState <= (int32_t)AppExecFwk::FormState::UNKNOWN || formState > (int32_t)AppExecFwk::FormState::READY) {
139 return AppExecFwk::FormState::UNKNOWN;
140 } else {
141 return (AppExecFwk::FormState)formState;
142 }
143 }
144
OnUpdate(const int64_t formId, const WantParams& wantParams)145 void AceFormAbility::OnUpdate(const int64_t formId, const WantParams& wantParams)
146 {
147 TAG_LOGI(AceLogTag::ACE_FORM, "AceFormAbility OnUpdate params called: %{public}s", std::to_string(formId).c_str());
148 Platform::PaContainer::OnUpdate(instanceId_, formId);
149 }
150
OnCastTemptoNormal(const int64_t formId)151 void AceFormAbility::OnCastTemptoNormal(const int64_t formId)
152 {
153 TAG_LOGI(
154 AceLogTag::ACE_FORM, "AceFormAbility OnCastTemptoNormal called: %{public}s", std::to_string(formId).c_str());
155 Platform::PaContainer::OnCastTemptoNormal(instanceId_, formId);
156 }
157
OnVisibilityChanged(const std::map<int64_t, int32_t>& formEventsMap)158 void AceFormAbility::OnVisibilityChanged(const std::map<int64_t, int32_t>& formEventsMap)
159 {
160 TAG_LOGI(AceLogTag::ACE_FORM, "AceFormAbility OnVisibilityChanged called");
161 Platform::PaContainer::OnVisibilityChanged(instanceId_, formEventsMap);
162 }
163
OnShare(int64_t formId, OHOS::AAFwk::WantParams& wantParams)164 bool AceFormAbility::OnShare(int64_t formId, OHOS::AAFwk::WantParams& wantParams)
165 {
166 return Platform::PaContainer::OnShare(instanceId_, formId, wantParams);
167 }
168
OnStart(const OHOS::AAFwk::Want& want, sptr<AAFwk::SessionInfo> sessionInfo)169 void AceFormAbility::OnStart(const OHOS::AAFwk::Want& want, sptr<AAFwk::SessionInfo> sessionInfo)
170 {
171 TAG_LOGI(AceLogTag::ACE_FORM, "AceFormAbility OnStart start");
172 Ability::OnStart(want, sessionInfo);
173 LoadFormEnv(want);
174 }
175
OnStop()176 void AceFormAbility::OnStop()
177 {
178 TAG_LOGI(AceLogTag::ACE_FORM, "AceFormAbility OnStop start ");
179 Ability::OnStop();
180 }
181
OnConnect(const Want& want)182 sptr<IRemoteObject> AceFormAbility::OnConnect(const Want& want)
183 {
184 TAG_LOGI(AceLogTag::ACE_FORM, "AceFormAbility OnConnect start");
185 Ability::OnConnect(want);
186 return GetFormRemoteObject();
187 }
188
OnDisconnect(const Want& want)189 void AceFormAbility::OnDisconnect(const Want& want)
190 {
191 TAG_LOGI(AceLogTag::ACE_FORM, "AceFormAbility OnDisconnect start");
192 Ability::OnDisconnect(want);
193 }
194
GetFormRemoteObject()195 sptr<IRemoteObject> AceFormAbility::GetFormRemoteObject()
196 {
197 if (formProviderRemoteObject_ == nullptr) {
198 sptr<FormProviderClient> providerClient = new (std::nothrow) FormProviderClient();
199 std::shared_ptr<Ability> thisAbility = this->shared_from_this();
200 if (thisAbility == nullptr) {
201 TAG_LOGE(AceLogTag::ACE_FORM, "Get form remote object failed, ability is nullptr");
202 return nullptr;
203 }
204 providerClient->SetOwner(thisAbility);
205 formProviderRemoteObject_ = providerClient->AsObject();
206 }
207 return formProviderRemoteObject_;
208 }
209 } // namespace OHOS::Ace
210