• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 "preload_system_so_startup_task.h"
17 
18 #include "event_report.h"
19 #include "hilog_tag_wrapper.h"
20 #include "native_module_manager.h"
21 
22 namespace OHOS {
23 namespace AbilityRuntime {
24 namespace {
25 constexpr char OHM_URL_OHOS_PREFIX[] = "@ohos:";
26 constexpr char OHM_URL_APP_COLON_TAG = ':';
27 
StringStartWith(const std::string & str,const std::string & startStr)28 bool StringStartWith(const std::string& str, const std::string& startStr)
29 {
30     size_t startStrLen = startStr.length();
31     return ((str.length() > startStrLen) && (str.compare(0, startStrLen, startStr) == 0));
32 }
33 
ParseSystemSoOhmUrl(const std::string & ohmUrl,std::string & soName)34 int32_t ParseSystemSoOhmUrl(const std::string& ohmUrl, std::string& soName)
35 {
36     // @ohos:<moduleName>
37     size_t pos = ohmUrl.find(OHM_URL_APP_COLON_TAG);
38     if (pos == std::string::npos) {
39         TAG_LOGE(AAFwkTag::STARTUP, "invalid app ohmUrl: %{public}s", ohmUrl.c_str());
40         return ERR_STARTUP_INVALID_VALUE;
41     }
42     soName = ohmUrl.substr(pos + 1);
43 
44     return ERR_OK;
45 }
46 
ParseOhmUrl(const std::string & ohmUrl,std::string & soName)47 int32_t ParseOhmUrl(const std::string& ohmUrl, std::string& soName)
48 {
49     if (ohmUrl.empty()) {
50         TAG_LOGE(AAFwkTag::STARTUP, "ohmUrl is empty");
51         return ERR_STARTUP_INVALID_VALUE;
52     }
53 
54     if (!StringStartWith(ohmUrl, OHM_URL_OHOS_PREFIX)) {
55         TAG_LOGE(AAFwkTag::STARTUP, "invalid ohmUrl: %{public}s", ohmUrl.c_str());
56         return ERR_STARTUP_INVALID_VALUE;
57     }
58 
59     return ParseSystemSoOhmUrl(ohmUrl, soName);
60 }
61 } // namespace
62 const std::string PreloadSystemSoStartupTask::TASK_TYPE = "PreloadSystemSo";
63 
PreloadSystemSoStartupTask(const std::string & name,const std::string & ohmUrl)64 PreloadSystemSoStartupTask::PreloadSystemSoStartupTask(const std::string& name, const std::string& ohmUrl)
65     : PreloadSoStartupTask(name, ohmUrl)
66 {
67     SetWaitOnMainThread(false);
68     SetCallCreateOnMainThread(false);
69 }
70 
71 PreloadSystemSoStartupTask::~PreloadSystemSoStartupTask() = default;
72 
GetType() const73 const std::string &PreloadSystemSoStartupTask::GetType() const
74 {
75     return TASK_TYPE;
76 }
77 
RunTaskInit(std::unique_ptr<StartupTaskResultCallback> callback)78 int32_t PreloadSystemSoStartupTask::RunTaskInit(std::unique_ptr<StartupTaskResultCallback> callback)
79 {
80     std::string soName;
81     int32_t code = ParseOhmUrl(ohmUrl_, soName);
82     AAFwk::EventInfo eventInfo;
83     if (code != ERR_OK) {
84         TAG_LOGW(AAFwkTag::STARTUP,
85             "task %{public}s, parse ohmUrl failed: %{public}s", name_.c_str(), ohmUrl_.c_str());
86         return ERR_OK;
87     }
88     TAG_LOGI(AAFwkTag::STARTUP, "task: %{public}s, soName: %{public}s", name_.c_str(), soName.c_str());
89 
90     NativeModuleManager* moduleManager = NativeModuleManager::GetInstance();
91     if (moduleManager == nullptr) {
92         TAG_LOGE(AAFwkTag::STARTUP, "moduleManager is null");
93         OnCompletedCallback::OnCallback(std::move(callback), ERR_STARTUP_INTERNAL_ERROR);
94         eventInfo.errCode = ERR_NATIVE_MODULE_MANAGER_CONSTRUCTION;
95         eventInfo.errReason = "moduleManager is null";
96         AAFwk::EventReport::SendLaunchFrameworkEvent(
97             AAFwk::EventName::STARTUP_TASK_ERROR, HiSysEventType::FAULT, eventInfo);
98         return ERR_STARTUP_INTERNAL_ERROR;
99     }
100 
101     std::string errInfo;
102     NativeModule* module = moduleManager->LoadNativeModule(soName.c_str(), nullptr, false, errInfo, false, "");
103     if (module == nullptr) {
104         TAG_LOGW(AAFwkTag::STARTUP, "module is null, errInfo: %{public}s", errInfo.c_str());
105         OnCompletedCallback::OnCallback(std::move(callback), ERR_OK);
106         eventInfo.errCode = ERR_LOAD_NATIVE_MODULE;
107         eventInfo.errReason = errInfo;
108         AAFwk::EventReport::SendLaunchFrameworkEvent(
109             AAFwk::EventName::STARTUP_TASK_ERROR, HiSysEventType::FAULT, eventInfo);
110         return ERR_STARTUP_INTERNAL_ERROR;
111     }
112     OnCompletedCallback::OnCallback(std::move(callback), ERR_OK);
113     return ERR_OK;
114 }
115 } // namespace AbilityRuntime
116 } // namespace OHOS
117