1bba3622a5a46ae009f2800570a804a1c37a06d6
[platform/core/appfw/launchpad.git] / src / launchpad-process-pool / loader_context.hh
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 #ifndef LAUNCHPAD_PROCESS_POOL_LOADER_CONTEXT_HH_
18 #define LAUNCHPAD_PROCESS_POOL_LOADER_CONTEXT_HH_
19
20 #include <glib.h>
21 #include <sys/types.h>
22
23 #include <memory>
24 #include <string>
25 #include <vector>
26
27 #include <app_info.hh>
28 #include <client_socket.hh>
29 #include <io_channel.hh>
30 #include <server_socket.hh>
31
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
36 namespace launchpad {
37
38 class LoaderContext : public std::enable_shared_from_this<LoaderContext>,
39                       public IOChannel::IEvent {
40  public:
41   class Builder {
42    public:
43     virtual ~Builder() = default;
44
45     Builder& SetLoaderInfo(std::shared_ptr<LoaderInfo> loader_info);
46     Builder& SetLoaderId(int loader_id);
47     Builder& SetCallerPid(pid_t caller_pid);
48     Builder& SetActive();
49
50     virtual operator LoaderContext*();
51
52    protected:
53     std::shared_ptr<LoaderInfo> loader_info_;
54     int loader_id_ = 0;
55     pid_t caller_pid_ = getpid();
56     bool activated_ = true;
57   };
58
59   class IEvent {
60    public:
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;
65   };
66
67   LoaderContext(std::shared_ptr<LoaderInfo> loader_info, int loader_id,
68       pid_t caller_pid, bool activated);
69   virtual ~LoaderContext();
70
71   virtual void Listen();
72   virtual void Dispose();
73   virtual pid_t Prepare();
74   virtual pid_t Deploy(const AppInfo* app_info);
75
76   const std::string& GetLoaderName() const;
77   const std::string& GetLoaderPath() const;
78   const std::string& GetLoaderExtra() const;
79   int GetType() const;
80   bool IsPrepared() const;
81   pid_t GetPid() 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;
96   void UnsetTimer();
97   void SetTimer();
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);
108   bool IsLaunchable();
109   void SetEventListener(IEvent* listener);
110   void Ref();
111   void Unref();
112   uint32_t RefCount() const;
113
114  protected:
115   void SetPrepared(bool prepared);
116   void OnIOEventReceived(int fd, int condition) override;
117   int GetSchedPriority() const;
118   void CreateReadyFile();
119
120  private:
121   void UpdateScore();
122   void Activate();
123   void Deactivate();
124   bool CanDeactivate(LoaderMethod method) const;
125   void SetLiveTimer();
126   void HandleLoaderEvent();
127   void HandleLoaderClientEvent(int condition);
128
129   static gboolean TimeoutCb(gpointer user_data);
130   static gboolean TimeToLiveCb(gpointer user_data);
131   static gboolean OnBootTimeoutCb(gpointer user_data);
132
133  private:
134   bool prepared_ = false;
135   std::shared_ptr<LoaderInfo> loader_info_;
136   int loader_id_;
137   pid_t caller_pid_;
138   bool activated_;
139   std::unique_ptr<CPUChecker> cpu_checker_;
140   unsigned int score_;
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_;
146   pid_t pid_ = 0;
147   launchpad::LoaderMethod method_ = launchpad::LoaderMethod::None;
148   bool touched_ = false;
149   uint64_t pss_ = 0;
150   int cpu_check_count_ = 0;
151   guint timer_ = 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;
157 };
158
159 }  // namespace launchpad
160
161 #endif  // LAUNCHPAD_PROCESS_POOL_LOADER_CONTEXT_HH_