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 #ifndef LAUNCHPAD_PROCESS_POOL_LOADER_CONTEXT_HH_
18 #define LAUNCHPAD_PROCESS_POOL_LOADER_CONTEXT_HH_
21 #include <sys/types.h>
27 #include <app_info.hh>
28 #include <client_socket.hh>
29 #include <io_channel.hh>
30 #include <server_socket.hh>
32 #include "launchpad-process-pool/cpu_checker.hh"
33 #include "launchpad-process-pool/loader_info.hh"
34 #include "launchpad-process-pool/launcher_info.hh"
35 #include "launchpad-process-pool/loader_mount.hh"
39 class LoaderContext : public std::enable_shared_from_this<LoaderContext>,
40 public IOChannel::IEvent {
44 virtual ~Builder() = default;
46 Builder& SetLoaderInfo(std::shared_ptr<LoaderInfo> loader_info);
47 Builder& SetLoaderId(int loader_id);
48 Builder& SetCallerPid(pid_t caller_pid);
50 Builder& SetLoaderMount(std::shared_ptr<LoaderMount> loader_mount);
52 virtual operator LoaderContext*();
55 std::shared_ptr<LoaderInfo> loader_info_;
57 pid_t caller_pid_ = getpid();
58 bool activated_ = true;
59 std::shared_ptr<LoaderMount> loader_mount_;
64 virtual ~IEvent() = default;
65 virtual void OnTimeoutEvent(LoaderContext* context) = 0;
66 virtual void OnLoaderLaunched(LoaderContext* context) = 0;
67 virtual void OnLoaderPrepared(LoaderContext* context) = 0;
70 LoaderContext(std::shared_ptr<LoaderInfo> loader_info, int loader_id,
71 pid_t caller_pid, bool activated,
72 std::shared_ptr<LoaderMount> loader_mount);
73 virtual ~LoaderContext();
75 virtual void Listen();
76 virtual void Dispose();
77 virtual pid_t Prepare();
78 virtual pid_t Deploy(const AppInfo* app_info);
80 const std::string& GetLoaderName() const;
81 const std::string& GetLoaderPath() const;
82 const std::string& GetLoaderExtra() const;
84 bool IsPrepared() const;
86 void SetPid(pid_t pid);
87 pid_t GetCallerPid() const;
88 int GetLoaderId() const;
89 bool IsHydraMode() const;
90 bool IsActivated() const;
91 bool IsTouched() const;
92 bool IsOnBoot() const;
93 int GetOnBootTimeout() const;
94 LoaderMethod GetDetectionMethod() const;
95 void SetLoaderMethod(LoaderMethod method);
96 bool ShouldCheckAppInstallation() const;
97 void SetAppInstalled(bool installed);
98 bool IsAppInstalled() const;
99 CPUChecker* GetCPUChecker() const;
102 void SetOnBootTimer(int timeout);
103 bool ShouldWaitForFileCreation();
104 int IncreaseCPUCheckCount();
105 void ResetCPUCheckCount();
106 void UpdatePssMemory();
107 uint64_t GetPssMemory() const;
108 unsigned int GetScore() const;
109 bool CanBeDetected(LoaderMethod method) const;
110 bool CanActivate(LoaderMethod method) const;
111 void UpdateState(LoaderMethod method, bool force);
113 void SetEventListener(IEvent* listener);
116 uint32_t RefCount() const;
119 void SetPrepared(bool prepared);
120 void OnIOEventReceived(int fd, int condition) override;
121 int GetSchedPriority() const;
122 void CreateReadyFile();
128 bool CanDeactivate(LoaderMethod method) const;
130 void HandleLoaderEvent();
131 void HandleLoaderClientEvent(int condition);
133 static gboolean TimeoutCb(gpointer user_data);
134 static gboolean TimeToLiveCb(gpointer user_data);
135 static gboolean OnBootTimeoutCb(gpointer user_data);
138 bool prepared_ = false;
139 std::shared_ptr<LoaderInfo> loader_info_;
143 std::shared_ptr<LoaderMount> loader_mount_;
144 std::unique_ptr<CPUChecker> cpu_checker_;
146 std::string loader_extra_;
147 std::unique_ptr<ServerSocket> server_socket_;
148 std::unique_ptr<IOChannel> server_channel_;
149 std::unique_ptr<ClientSocket> client_socket_;
150 std::unique_ptr<IOChannel> client_channel_;
152 launchpad::LoaderMethod method_ = launchpad::LoaderMethod::None;
153 bool touched_ = false;
155 int cpu_check_count_ = 0;
157 guint live_timer_ = 0;
158 guint on_boot_timer_ = 0;
159 std::vector<std::string> condition_path_exists_;
160 IEvent* listener_ = nullptr;
161 uint32_t ref_count_ = 0;
162 bool ready_file_created_ = false;
165 } // namespace launchpad
167 #endif // LAUNCHPAD_PROCESS_POOL_LOADER_CONTEXT_HH_