2 * Copyright (c) 2023 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include "launchpad-process-pool/loader_factory.hh"
22 #include <aul_keys.hh>
23 #include <user_tracer.hh>
26 #include "launchpad-process-pool/hydra_loader_context.hh"
27 #include "launchpad-process-pool/loader_context.hh"
28 #include "launchpad-process-pool/log_private.hh"
33 std::string GetAppTypeString(const std::shared_ptr<LoaderInfo>& info) {
34 std::string app_types;
35 for (auto& app_type : info->GetAppTypes()) {
36 if (!app_types.empty())
39 app_types += app_type;
45 constexpr char kInfoDirectoryPath[] = "/usr/share/aul";
47 std::shared_ptr<LoaderInfo> CreateDynamicLoaderInfo(tizen_base::Bundle b,
48 int loader_id, const std::string& caller_pid) {
49 auto loader_path = b.GetString(kAulLoaderPath);
50 if (loader_path.empty())
53 auto loader_name = loader_path + caller_pid + std::to_string(loader_id);
54 LoaderInfo::Builder builder;
55 builder.SetType(LoaderType::Dynamic);
56 builder.SetName(std::move(loader_name));
57 builder.SetExe(std::move(loader_path));
58 builder.SetDetectionMethod(LoaderMethod::Timeout | LoaderMethod::Visibility);
59 builder.SetActivationMethod(LoaderMethod::Request |
60 LoaderMethod::AvailableMemory);
61 builder.SetDeactivationMethod(LoaderMethod::TimeToLive |
62 LoaderMethod::OutOfMemory);
63 builder.SetTimeToLive(600);
64 builder.SetTimeout(2000);
65 builder.SetOnBoot(false);
66 builder.SetHydraMode(false);
67 return std::shared_ptr<LoaderInfo>(builder.Build());
72 LoaderFactory& LoaderFactory::GetInst() {
73 static LoaderFactory inst;
77 std::shared_ptr<LoaderContext> LoaderFactory::CreateLoaderContext(
78 LoaderInfoPtr info, std::shared_ptr<LoaderMount> loader_mount) {
79 auto app_types = GetAppTypeString(info);
80 int loader_id = static_cast<int>((info->GetExe() == "null") ?
81 PadLoaderId::Direct : PadLoaderId::Static);
82 LoaderContext* context;
84 if (info->IsHydraMode()) {
85 context = HydraLoaderContext::Builder()
86 .SetLoaderInfo(std::move(info))
87 .SetLoaderId(loader_id)
89 .SetLoaderMount(std::move(loader_mount));
91 context = LoaderContext::Builder()
92 .SetLoaderInfo(std::move(info))
93 .SetLoaderId(loader_id)
95 .SetLoaderMount(std::move(loader_mount));
97 } catch (const Exception& e) {
98 _E("Exception occurs. error(%s)", e.what());
102 if (context == nullptr)
105 _I("loader_type(%d), loader_id(%d), loader_name(%s), app_type(%s)",
106 context->GetType(), loader_id, context->GetLoaderName().c_str(),
108 UserTracer::Print("candidate slot. app-type(" + app_types + ") loader-type(" +
109 std::to_string(context->GetType()));
110 return std::shared_ptr<LoaderContext>(context);
113 std::shared_ptr<LoaderContext> LoaderFactory::CreateLoaderContext(
114 tizen_base::Bundle b, std::shared_ptr<LoaderMount> loader_mount) {
115 auto caller_pid = b.GetString(kAulCallerPid);
116 if (caller_pid.empty())
119 int loader_id = MakeDynamicLoaderId();
120 auto loader_info = CreateDynamicLoaderInfo(std::move(b), loader_id,
122 if (loader_info == nullptr)
125 LoaderContext* context;
127 context = LoaderContext::Builder()
128 .SetLoaderInfo(std::move(loader_info))
129 .SetLoaderId(loader_id)
130 .SetCallerPid(std::stoi(caller_pid))
132 .SetLoaderMount(std::move(loader_mount));
133 if (context == nullptr)
135 } catch (const Exception& e) {
136 _E("Exception occurs. error(%s)", e.what());
140 return std::shared_ptr<LoaderContext>(context);
143 std::shared_ptr<LoaderContext> LoaderFactory::CreateLoaderContext(
144 LoaderInfoPtr info, pid_t caller_pid,
145 std::shared_ptr<LoaderMount> loader_mount) {
146 int loader_id = MakeDynamicLoaderId();
147 LoaderContext* context;
149 context = LoaderContext::Builder()
150 .SetLoaderInfo(std::move(info))
151 .SetLoaderId(loader_id)
152 .SetCallerPid(caller_pid)
154 .SetLoaderMount(std::move(loader_mount));
155 if (context == nullptr)
157 } catch (const Exception& e) {
158 _E("Exception occurs. error(%s)", e.what());
162 return std::shared_ptr<LoaderContext>(context);
165 int LoaderFactory::MakeDynamicLoaderId() {
166 return ++dynamic_loader_id_;
169 } // namespace launchpad