• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 "core/common/backend.h"
27 
28 namespace OHOS {
29 namespace Ace {
30 
31 using namespace OHOS::AAFwk;
32 using namespace OHOS::AppExecFwk;
33 
34 using ServicePlatformFinish = std::function<void()>;
35 class ServicePlatformEventCallback final : public Platform::PlatformEventCallback {
36 public:
ServicePlatformEventCallback(ServicePlatformFinish onFinish)37     explicit ServicePlatformEventCallback(ServicePlatformFinish onFinish) : onFinish_(onFinish) {}
38 
39     ~ServicePlatformEventCallback() = default;
40 
OnFinish() const41     virtual void OnFinish() const
42     {
43         LOGI("ServicePlatformEventCallback OnFinish");
44         if (onFinish_) {
45             onFinish_();
46         }
47     }
48 
OnStatusBarBgColorChanged(uint32_t color)49     virtual void OnStatusBarBgColorChanged(uint32_t color)
50     {
51         LOGI("ServicePlatformEventCallback OnStatusBarBgColorChanged");
52     }
53 
54 private:
55     ServicePlatformFinish onFinish_;
56 };
57 
58 int32_t AceServiceAbility::instanceId_ = 100000;
59 const std::string AceServiceAbility::START_PARAMS_KEY = "__startParams";
60 const std::string AceServiceAbility::URI = "url";
61 
REGISTER_AA(AceServiceAbility)62 REGISTER_AA(AceServiceAbility)
63 void AceServiceAbility::OnStart(const OHOS::AAFwk::Want& want)
64 {
65     Ability::OnStart(want);
66     LOGI("AceServiceAbility::OnStart called");
67     // get url
68     std::string parsedUrl;
69     if (want.HasParameter(URI)) {
70         parsedUrl = want.GetStringParam(URI);
71     } else {
72         parsedUrl = "service.js";
73     }
74 
75     // get asset
76     auto packagePathStr = GetBundleCodePath();
77     auto moduleInfo = GetHapModuleInfo();
78     if (moduleInfo != nullptr) {
79         packagePathStr += "/" + moduleInfo->package + "/";
80     }
81 
82     // init service
83     BackendType backendType = BackendType::SERVICE;
84     bool isArkApp = GetIsArkFromConfig(packagePathStr);
85     Platform::PaContainer::CreateContainer(abilityId_, backendType, isArkApp, this,
86         std::make_unique<ServicePlatformEventCallback>([this]() { TerminateAbility(); }));
87 
88     std::shared_ptr<AbilityInfo> info = GetAbilityInfo();
89     if (info != nullptr && !info->srcPath.empty()) {
90         LOGI("AceServiceAbility::OnStar assetBasePathStr: %{public}s, parsedUrl: %{public}s",
91             info->srcPath.c_str(), parsedUrl.c_str());
92         auto assetBasePathStr = { "assets/js/" + info->srcPath + "/" };
93         Platform::PaContainer::AddAssetPath(abilityId_, packagePathStr, assetBasePathStr);
94     } else {
95         LOGI("AceServiceAbility::OnStar parsedUrl: %{public}s", parsedUrl.c_str());
96         auto assetBasePathStr = { std::string("assets/js/default/"), std::string("assets/js/share/") };
97         Platform::PaContainer::AddAssetPath(abilityId_, packagePathStr, assetBasePathStr);
98     }
99     std::shared_ptr<ApplicationInfo> appInfo = GetApplicationInfo();
100     if (appInfo) {
101         std::string nativeLibraryPath = appInfo->nativeLibraryPath;
102         if (!nativeLibraryPath.empty()) {
103             if (nativeLibraryPath.back() == '/') {
104                 nativeLibraryPath.pop_back();
105             }
106             std::string libPath = GetBundleCodePath();
107             if (libPath.back() == '/') {
108                 libPath += nativeLibraryPath;
109             } else {
110                 libPath += "/" + nativeLibraryPath;
111             }
112             LOGI("napi lib path = %{private}s", libPath.c_str());
113             Platform::PaContainer::AddLibPath(abilityId_, libPath);
114         }
115     }
116 
117     // run service
118     Platform::PaContainer::RunPa(abilityId_, parsedUrl, want);
119     LOGI("AceServiceAbility::OnStart called End");
120 }
121 
OnStop()122 void AceServiceAbility::OnStop()
123 {
124     LOGI("AceServiceAbility::OnStop called ");
125     Ability::OnStop();
126     Platform::PaContainer::DestroyContainer(abilityId_);
127     LOGI("AceServiceAbility::OnStop called End");
128 }
129 
OnConnect(const Want & want)130 sptr<IRemoteObject> AceServiceAbility::OnConnect(const Want& want)
131 {
132     LOGI("AceServiceAbility::OnConnect start");
133     Ability::OnConnect(want);
134     auto ret = Platform::PaContainer::OnConnect(abilityId_, want);
135     if (ret == nullptr) {
136         LOGE("AceServiceAbility::OnConnect, the iremoteObject is null");
137         return nullptr;
138     }
139     LOGI("AceServiceAbility::OnConnect end");
140     return ret;
141 }
142 
OnDisconnect(const Want & want)143 void AceServiceAbility::OnDisconnect(const Want& want)
144 {
145     LOGI("AceServiceAbility::OnDisconnect start");
146     Ability::OnDisconnect(want);
147     Platform::PaContainer::OnDisConnect(abilityId_, want);
148     LOGI("AceServiceAbility::OnDisconnect end");
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     LOGI("AceServiceAbility::OnCommand end");
157 }
158 
159 } // namespace Ace
160 } // namespace OHOS
161