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