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"
38 class LoaderContext : public std::enable_shared_from_this<LoaderContext>,
39 public IOChannel::IEvent {
43 virtual ~Builder() = default;
45 Builder& SetLoaderInfo(std::shared_ptr<LoaderInfo> loader_info);
46 Builder& SetLoaderId(int loader_id);
47 Builder& SetCallerPid(pid_t caller_pid);
50 virtual operator LoaderContext*();
53 std::shared_ptr<LoaderInfo> loader_info_;
55 pid_t caller_pid_ = getpid();
56 bool activated_ = true;
61 virtual ~IEvent() = default;
62 virtual void OnTimeoutEvent(LoaderContext* context) = 0;
63 virtual void OnLoaderLaunched(LoaderContext* context) = 0;
64 virtual void OnLoaderPrepared(LoaderContext* context) = 0;
67 LoaderContext(std::shared_ptr<LoaderInfo> loader_info, int loader_id,
68 pid_t caller_pid, bool activated);
69 virtual ~LoaderContext();
71 virtual void Listen();
72 virtual void Dispose();
73 virtual pid_t Prepare();
74 virtual pid_t Deploy(const AppInfo* app_info);
76 const std::string& GetLoaderName() const;
77 const std::string& GetLoaderPath() const;
78 const std::string& GetLoaderExtra() const;
80 bool IsPrepared() const;
82 void SetPid(pid_t pid);
83 pid_t GetCallerPid() const;
84 int GetLoaderId() const;
85 bool IsHydraMode() const;
86 bool IsActivated() const;
87 bool IsTouched() const;
88 bool IsOnBoot() const;
89 int GetOnBootTimeout() const;
90 LoaderMethod GetDetectionMethod() const;
91 void SetLoaderMethod(LoaderMethod method);
92 bool ShouldCheckAppInstallation() const;
93 void SetAppInstalled(bool installed);
94 bool IsAppInstalled() const;
95 CPUChecker* GetCPUChecker() const;
98 void SetOnBootTimer(int timeout);
99 bool ShouldWaitForFileCreation();
100 int IncreaseCPUCheckCount();
101 void ResetCPUCheckCount();
102 void UpdatePssMemory();
103 uint64_t GetPssMemory() const;
104 unsigned int GetScore() const;
105 bool CanBeDetected(LoaderMethod method) const;
106 bool CanActivate(LoaderMethod method) const;
107 void UpdateState(LoaderMethod method, bool force);
109 void SetEventListener(IEvent* listener);
112 uint32_t RefCount() const;
115 void SetPrepared(bool prepared);
116 void OnIOEventReceived(int fd, int condition) override;
117 int GetSchedPriority() const;
118 void CreateReadyFile();
124 bool CanDeactivate(LoaderMethod method) const;
126 void HandleLoaderEvent();
127 void HandleLoaderClientEvent(int condition);
129 static gboolean TimeoutCb(gpointer user_data);
130 static gboolean TimeToLiveCb(gpointer user_data);
131 static gboolean OnBootTimeoutCb(gpointer user_data);
134 bool prepared_ = false;
135 std::shared_ptr<LoaderInfo> loader_info_;
139 std::unique_ptr<CPUChecker> cpu_checker_;
141 std::string loader_extra_;
142 std::unique_ptr<ServerSocket> server_socket_;
143 std::unique_ptr<IOChannel> server_channel_;
144 std::unique_ptr<ClientSocket> client_socket_;
145 std::unique_ptr<IOChannel> client_channel_;
147 launchpad::LoaderMethod method_ = launchpad::LoaderMethod::None;
148 bool touched_ = false;
150 int cpu_check_count_ = 0;
152 guint live_timer_ = 0;
153 guint on_boot_timer_ = 0;
154 std::vector<std::string> condition_path_exists_;
155 IEvent* listener_ = nullptr;
156 uint32_t ref_count_ = 0;
159 } // namespace launchpad
161 #endif // LAUNCHPAD_PROCESS_POOL_LOADER_CONTEXT_HH_