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