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