• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_NOLOG(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 int32_t AceServiceAbility::instanceId_ = 100000;
61 const std::string AceServiceAbility::START_PARAMS_KEY = "__startParams";
62 const std::string AceServiceAbility::URI = "url";
63 
REGISTER_AA(AceServiceAbility)64 REGISTER_AA(AceServiceAbility)
65 void AceServiceAbility::OnStart(const OHOS::AAFwk::Want& want, sptr<AAFwk::SessionInfo> sessionInfo)
66 {
67     Ability::OnStart(want, sessionInfo);
68     LOGI("AceServiceAbility::OnStart called");
69     // get url
70     std::string parsedUrl;
71     if (want.HasParameter(URI)) {
72         parsedUrl = want.GetStringParam(URI);
73     } else {
74         parsedUrl = "service.js";
75     }
76 
77     // get asset
78     auto packagePathStr = GetBundleCodePath();
79     auto moduleInfo = GetHapModuleInfo();
80     CHECK_NULL_VOID_NOLOG(moduleInfo);
81     packagePathStr += "/" + moduleInfo->package + "/";
82     auto abilityInfo = GetAbilityInfo();
83 
84     // init service
85     BackendType backendType = BackendType::SERVICE;
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 
96     std::vector<std::string> assetBasePathStr;
97     AceEngine::InitJsDumpHeadSignal();
98     if (abilityInfo != nullptr && !abilityInfo->srcPath.empty()) {
99         LOGI("AceServiceAbility::OnStar assetBasePathStr: %{public}s, parsedUrl: %{public}s",
100             abilityInfo->srcPath.c_str(), parsedUrl.c_str());
101         assetBasePathStr = { "assets/js/" + abilityInfo->srcPath + "/", std::string("assets/js/") };
102     } else {
103         LOGI("AceServiceAbility::OnStar parsedUrl: %{public}s", parsedUrl.c_str());
104         assetBasePathStr = { std::string("assets/js/default/"), std::string("assets/js/share/") };
105     }
106 
107     workerPath->assetBasePathStr = assetBasePathStr;
108 
109     Platform::PaContainerOptions options;
110     options.type = backendType;
111     options.language = srcLanguage;
112     options.hapPath = moduleInfo->hapPath;
113     options.workerPath = workerPath;
114 
115     Platform::PaContainer::CreateContainer(abilityId_, this, options,
116         std::make_unique<ServicePlatformEventCallback>([this]() { TerminateAbility(); }));
117     Platform::PaContainer::AddAssetPath(abilityId_, packagePathStr, moduleInfo->hapPath, assetBasePathStr);
118 
119     // run service
120     Platform::PaContainer::RunPa(abilityId_, parsedUrl, want);
121     LOGI("AceServiceAbility::OnStart called End");
122 }
123 
OnStop()124 void AceServiceAbility::OnStop()
125 {
126     LOGI("AceServiceAbility::OnStop called ");
127     Ability::OnStop();
128     Platform::PaContainer::DestroyContainer(abilityId_);
129     LOGI("AceServiceAbility::OnStop called End");
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     LOGI("AceServiceAbility::OnConnect end");
142     return ret;
143 }
144 
OnDisconnect(const Want & want)145 void AceServiceAbility::OnDisconnect(const Want& want)
146 {
147     LOGI("AceServiceAbility::OnDisconnect start");
148     Ability::OnDisconnect(want);
149     Platform::PaContainer::OnDisConnect(abilityId_, want);
150     LOGI("AceServiceAbility::OnDisconnect end");
151 }
152 
OnCommand(const AAFwk::Want & want,bool restart,int startId)153 void AceServiceAbility::OnCommand(const AAFwk::Want &want, bool restart, int startId)
154 {
155     LOGI("AceServiceAbility::OnCommand start");
156     Ability::OnCommand(want, restart, startId);
157     Platform::PaContainer::OnCommand(want, startId, abilityId_);
158     LOGI("AceServiceAbility::OnCommand end");
159 }
160 } // namespace Ace
161 } // namespace OHOS
162