Mount gadget resource paths for NUIGadget
[platform/core/appfw/launchpad.git] / src / launchpad-process-pool / loader_factory.cc
1 /*
2  * Copyright (c) 2023 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include "launchpad-process-pool/loader_factory.hh"
18
19 #include <string>
20 #include <utility>
21
22 #include <aul_keys.hh>
23 #include <user_tracer.hh>
24 #include <types.hh>
25
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"
29
30 namespace launchpad {
31 namespace {
32
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())
37       app_types += "|";
38
39     app_types += app_type;
40   }
41
42   return app_types;
43 }
44
45 constexpr char kInfoDirectoryPath[] = "/usr/share/aul";
46
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())
51     return nullptr;
52
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());
68 }
69
70 }  // namespace
71
72 LoaderFactory& LoaderFactory::GetInst() {
73   static LoaderFactory inst;
74   return inst;
75 }
76
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;
83   try {
84     if (info->IsHydraMode()) {
85       context = HydraLoaderContext::Builder()
86         .SetLoaderInfo(std::move(info))
87         .SetLoaderId(loader_id)
88         .SetActive()
89         .SetLoaderMount(std::move(loader_mount));
90     } else {
91       context = LoaderContext::Builder()
92         .SetLoaderInfo(std::move(info))
93         .SetLoaderId(loader_id)
94         .SetActive()
95         .SetLoaderMount(std::move(loader_mount));
96     }
97   } catch (const Exception& e) {
98     _E("Exception occurs. error(%s)", e.what());
99     return nullptr;
100   }
101
102   if (context == nullptr)
103     return nullptr;
104
105   _I("loader_type(%d), loader_id(%d), loader_name(%s), app_type(%s)",
106       context->GetType(), loader_id, context->GetLoaderName().c_str(),
107       app_types.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);
111 }
112
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())
117     return nullptr;
118
119   int loader_id = MakeDynamicLoaderId();
120   auto loader_info = CreateDynamicLoaderInfo(std::move(b), loader_id,
121       caller_pid);
122   if (loader_info == nullptr)
123     return nullptr;
124
125   LoaderContext* context;
126   try {
127     context = LoaderContext::Builder()
128         .SetLoaderInfo(std::move(loader_info))
129         .SetLoaderId(loader_id)
130         .SetCallerPid(std::stoi(caller_pid))
131         .SetActive()
132         .SetLoaderMount(std::move(loader_mount));
133     if (context == nullptr)
134         return nullptr;
135   } catch (const Exception& e) {
136     _E("Exception occurs. error(%s)", e.what());
137     return nullptr;
138   }
139
140   return std::shared_ptr<LoaderContext>(context);
141 }
142
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;
148   try {
149     context = LoaderContext::Builder()
150         .SetLoaderInfo(std::move(info))
151         .SetLoaderId(loader_id)
152         .SetCallerPid(caller_pid)
153         .SetActive()
154         .SetLoaderMount(std::move(loader_mount));
155     if (context == nullptr)
156       return nullptr;
157   } catch (const Exception& e) {
158     _E("Exception occurs. error(%s)", e.what());
159     return nullptr;
160   }
161
162   return std::shared_ptr<LoaderContext>(context);
163 }
164
165 int LoaderFactory::MakeDynamicLoaderId() {
166   return ++dynamic_loader_id_;
167 }
168
169 }  // namespace launchpad