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_service_ability.h"
17
18 #include "res_config.h"
19 #include "resource_manager.h"
20 #include "session_info.h"
21
22 #include "adapter/ohos/entrance/pa_container.h"
23 #include "adapter/ohos/entrance/pa_engine/pa_backend.h"
24 #include "adapter/ohos/entrance/platform_event_callback.h"
25 #include "adapter/ohos/entrance/utils.h"
26 #include "base/log/log.h"
27 #include "base/utils/utils.h"
28 #include "core/common/ace_engine.h"
29 #include "core/common/backend.h"
30
31 namespace OHOS {
32 namespace Ace {
33
34 using namespace OHOS::AAFwk;
35 using namespace OHOS::AppExecFwk;
36
37 using ServicePlatformFinish = std::function<void()>;
38 class ServicePlatformEventCallback final : public Platform::PlatformEventCallback {
39 public:
ServicePlatformEventCallback(ServicePlatformFinish onFinish)40 explicit ServicePlatformEventCallback(ServicePlatformFinish onFinish) : onFinish_(onFinish) {}
41
42 ~ServicePlatformEventCallback() override = default;
43
OnFinish() const44 void OnFinish() const override
45 {
46 LOGI("ServicePlatformEventCallback OnFinish");
47 CHECK_NULL_VOID(onFinish_);
48 onFinish_();
49 }
50
OnStatusBarBgColorChanged(uint32_t color)51 void OnStatusBarBgColorChanged(uint32_t color) override
52 {
53 LOGI("ServicePlatformEventCallback OnStatusBarBgColorChanged");
54 }
55
56 private:
57 ServicePlatformFinish onFinish_;
58 };
59
60 const std::string AceServiceAbility::START_PARAMS_KEY = "__startParams";
61 const std::string AceServiceAbility::URI = "url";
62
REGISTER_AA(AceServiceAbility)63 REGISTER_AA(AceServiceAbility)
64
65 AceServiceAbility::AceServiceAbility()
66 {
67 abilityId_ = Container::GenerateId<PA_SERVICE_CONTAINER>();
68 }
69
OnStart(const OHOS::AAFwk::Want & want,sptr<AAFwk::SessionInfo> sessionInfo)70 void AceServiceAbility::OnStart(const OHOS::AAFwk::Want& want, sptr<AAFwk::SessionInfo> sessionInfo)
71 {
72 Ability::OnStart(want, sessionInfo);
73 LOGI("AceServiceAbility OnStart called");
74 // get url
75 std::string parsedUrl;
76 if (want.HasParameter(URI)) {
77 parsedUrl = want.GetStringParam(URI);
78 } else {
79 parsedUrl = "service.js";
80 }
81
82 // get asset
83 auto packagePathStr = GetBundleCodePath();
84 auto moduleInfo = GetHapModuleInfo();
85 CHECK_NULL_VOID(moduleInfo);
86 packagePathStr += "/" + moduleInfo->package + "/";
87 auto abilityInfo = GetAbilityInfo();
88
89 // init service
90 BackendType backendType = BackendType::SERVICE;
91 SrcLanguage srcLanguage = SrcLanguage::ETS;
92 if (abilityInfo != nullptr && !abilityInfo->srcLanguage.empty()) {
93 if (abilityInfo->srcLanguage == "js") {
94 srcLanguage = SrcLanguage::JS;
95 }
96 }
97
98 std::shared_ptr<Platform::WorkerPath> workerPath = std::make_shared<Platform::WorkerPath>();
99 workerPath->packagePathStr = packagePathStr;
100
101 std::vector<std::string> assetBasePathStr;
102 AceEngine::InitJsDumpHeadSignal();
103 if (abilityInfo != nullptr && !abilityInfo->srcPath.empty()) {
104 assetBasePathStr = { "assets/js/" + abilityInfo->srcPath + "/", std::string("assets/js/") };
105 } else {
106 assetBasePathStr = { std::string("assets/js/default/"), std::string("assets/js/share/") };
107 }
108
109 workerPath->assetBasePathStr = assetBasePathStr;
110
111 Platform::PaContainerOptions options;
112 options.type = backendType;
113 options.language = srcLanguage;
114 options.hapPath = moduleInfo->hapPath;
115 options.workerPath = workerPath;
116
117 Platform::PaContainer::CreateContainer(abilityId_, this, options,
118 std::make_unique<ServicePlatformEventCallback>([this]() { TerminateAbility(); }));
119 Platform::PaContainer::AddAssetPath(abilityId_, packagePathStr, moduleInfo->hapPath, assetBasePathStr);
120
121 // run service
122 Platform::PaContainer::RunPa(abilityId_, parsedUrl, want);
123 }
124
OnStop()125 void AceServiceAbility::OnStop()
126 {
127 LOGI("AceServiceAbility OnStop called ");
128 Ability::OnStop();
129 Platform::PaContainer::DestroyContainer(abilityId_);
130 }
131
OnConnect(const Want & want)132 sptr<IRemoteObject> AceServiceAbility::OnConnect(const Want& want)
133 {
134 LOGI("AceServiceAbility OnConnect start");
135 Ability::OnConnect(want);
136 auto ret = Platform::PaContainer::OnConnect(abilityId_, want);
137 if (ret == nullptr) {
138 LOGE("AceServiceAbility OnConnect, the iremoteObject is null");
139 return nullptr;
140 }
141 return ret;
142 }
143
OnDisconnect(const Want & want)144 void AceServiceAbility::OnDisconnect(const Want& want)
145 {
146 LOGI("AceServiceAbility OnDisconnect start");
147 Ability::OnDisconnect(want);
148 Platform::PaContainer::OnDisConnect(abilityId_, want);
149 }
150
OnCommand(const AAFwk::Want & want,bool restart,int startId)151 void AceServiceAbility::OnCommand(const AAFwk::Want &want, bool restart, int startId)
152 {
153 LOGI("AceServiceAbility OnCommand start");
154 Ability::OnCommand(want, restart, startId);
155 Platform::PaContainer::OnCommand(want, startId, abilityId_);
156 }
157 } // namespace Ace
158 } // namespace OHOS
159