• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 
21 #include "adapter/ohos/entrance/pa_container.h"
22 #include "adapter/ohos/entrance/pa_engine/pa_backend.h"
23 #include "adapter/ohos/entrance/platform_event_callback.h"
24 #include "adapter/ohos/entrance/utils.h"
25 #include "base/log/log.h"
26 #include "base/utils/utils.h"
27 #include "core/common/ace_engine.h"
28 #include "core/common/backend.h"
29 
30 namespace OHOS {
31 namespace Ace {
32 
33 using namespace OHOS::AAFwk;
34 using namespace OHOS::AppExecFwk;
35 
36 using ServicePlatformFinish = std::function<void()>;
37 class ServicePlatformEventCallback final : public Platform::PlatformEventCallback {
38 public:
ServicePlatformEventCallback(ServicePlatformFinish onFinish)39     explicit ServicePlatformEventCallback(ServicePlatformFinish onFinish) : onFinish_(onFinish) {}
40 
41     ~ServicePlatformEventCallback() override = default;
42 
OnFinish() const43     void OnFinish() const override
44     {
45         LOGI("ServicePlatformEventCallback OnFinish");
46         CHECK_NULL_VOID_NOLOG(onFinish_);
47         onFinish_();
48     }
49 
OnStatusBarBgColorChanged(uint32_t color)50     void OnStatusBarBgColorChanged(uint32_t color) override
51     {
52         LOGI("ServicePlatformEventCallback OnStatusBarBgColorChanged");
53     }
54 
55 private:
56     ServicePlatformFinish onFinish_;
57 };
58 
59 int32_t AceServiceAbility::instanceId_ = 100000;
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 void AceServiceAbility::OnStart(const OHOS::AAFwk::Want& want)
65 {
66     Ability::OnStart(want);
67     LOGI("AceServiceAbility::OnStart called");
68     // get url
69     std::string parsedUrl;
70     if (want.HasParameter(URI)) {
71         parsedUrl = want.GetStringParam(URI);
72     } else {
73         parsedUrl = "service.js";
74     }
75 
76     // get asset
77     auto packagePathStr = GetBundleCodePath();
78     auto moduleInfo = GetHapModuleInfo();
79     CHECK_NULL_VOID_NOLOG(moduleInfo);
80     packagePathStr += "/" + moduleInfo->package + "/";
81 
82     // init service
83     bool isHap = moduleInfo ? !moduleInfo->hapPath.empty() : false;
84     std::string& packagePath = isHap ? moduleInfo->hapPath : packagePathStr;
85     BackendType backendType = BackendType::SERVICE;
86     bool isArkApp = GetIsArkFromConfig(packagePath, isHap);
87 
88     Platform::PaContainer::CreateContainer(abilityId_, backendType, isArkApp, this,
89         std::make_unique<ServicePlatformEventCallback>([this]() { TerminateAbility(); }));
90 
91     AceEngine::InitJsDumpHeadSignal();
92     std::shared_ptr<AbilityInfo> info = GetAbilityInfo();
93     if (info != nullptr && !info->srcPath.empty()) {
94         LOGI("AceServiceAbility::OnStar assetBasePathStr: %{public}s, parsedUrl: %{public}s",
95             info->srcPath.c_str(), parsedUrl.c_str());
96         auto assetBasePathStr = { "assets/js/" + info->srcPath + "/" };
97         Platform::PaContainer::AddAssetPath(abilityId_, packagePathStr, moduleInfo->hapPath, assetBasePathStr);
98     } else {
99         LOGI("AceServiceAbility::OnStar parsedUrl: %{public}s", parsedUrl.c_str());
100         auto assetBasePathStr = { std::string("assets/js/default/"), std::string("assets/js/share/") };
101         Platform::PaContainer::AddAssetPath(abilityId_, packagePathStr, moduleInfo->hapPath, assetBasePathStr);
102     }
103     std::shared_ptr<ApplicationInfo> appInfo = GetApplicationInfo();
104     if (appInfo) {
105         /* Note: DO NOT modify the sequence of adding libPath  */
106         std::string nativeLibraryPath = appInfo->nativeLibraryPath;
107         std::string quickFixLibraryPath = appInfo->appQuickFix.deployedAppqfInfo.nativeLibraryPath;
108         std::vector<std::string> libPaths;
109         if (!quickFixLibraryPath.empty()) {
110             std::string libPath = GenerateFullPath(GetBundleCodePath(), quickFixLibraryPath);
111             libPaths.push_back(libPath);
112             LOGI("napi quick fix lib path = %{private}s", libPath.c_str());
113         }
114         if (!nativeLibraryPath.empty()) {
115             std::string libPath = GenerateFullPath(GetBundleCodePath(), nativeLibraryPath);
116             libPaths.push_back(libPath);
117             LOGI("napi lib path = %{private}s", libPath.c_str());
118         }
119         if (!libPaths.empty()) {
120             Platform::PaContainer::AddLibPath(abilityId_, libPaths);
121         }
122     }
123 
124     // run service
125     Platform::PaContainer::RunPa(abilityId_, parsedUrl, want);
126     LOGI("AceServiceAbility::OnStart called End");
127 }
128 
OnStop()129 void AceServiceAbility::OnStop()
130 {
131     LOGI("AceServiceAbility::OnStop called ");
132     Ability::OnStop();
133     Platform::PaContainer::DestroyContainer(abilityId_);
134     LOGI("AceServiceAbility::OnStop called End");
135 }
136 
OnConnect(const Want & want)137 sptr<IRemoteObject> AceServiceAbility::OnConnect(const Want& want)
138 {
139     LOGI("AceServiceAbility::OnConnect start");
140     Ability::OnConnect(want);
141     auto ret = Platform::PaContainer::OnConnect(abilityId_, want);
142     if (ret == nullptr) {
143         LOGE("AceServiceAbility::OnConnect, the iremoteObject is null");
144         return nullptr;
145     }
146     LOGI("AceServiceAbility::OnConnect end");
147     return ret;
148 }
149 
OnDisconnect(const Want & want)150 void AceServiceAbility::OnDisconnect(const Want& want)
151 {
152     LOGI("AceServiceAbility::OnDisconnect start");
153     Ability::OnDisconnect(want);
154     Platform::PaContainer::OnDisConnect(abilityId_, want);
155     LOGI("AceServiceAbility::OnDisconnect end");
156 }
157 
OnCommand(const AAFwk::Want & want,bool restart,int startId)158 void AceServiceAbility::OnCommand(const AAFwk::Want &want, bool restart, int startId)
159 {
160     LOGI("AceServiceAbility::OnCommand start");
161     Ability::OnCommand(want, restart, startId);
162     Platform::PaContainer::OnCommand(want, startId, abilityId_);
163     LOGI("AceServiceAbility::OnCommand end");
164 }
165 } // namespace Ace
166 } // namespace OHOS
167