Mount gadget resource paths for NUIGadget
[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 #include "launchpad-process-pool/loader_mount.hh"
36
37 namespace launchpad {
38
39 class LoaderContext : public std::enable_shared_from_this<LoaderContext>,
40                       public IOChannel::IEvent {
41  public:
42   class Builder {
43    public:
44     virtual ~Builder() = default;
45
46     Builder& SetLoaderInfo(std::shared_ptr<LoaderInfo> loader_info);
47     Builder& SetLoaderId(int loader_id);
48     Builder& SetCallerPid(pid_t caller_pid);
49     Builder& SetActive();
50     Builder& SetLoaderMount(std::shared_ptr<LoaderMount> loader_mount);
51
52     virtual operator LoaderContext*();
53
54    protected:
55     std::shared_ptr<LoaderInfo> loader_info_;
56     int loader_id_ = 0;
57     pid_t caller_pid_ = getpid();
58     bool activated_ = true;
59     std::shared_ptr<LoaderMount> loader_mount_;
60   };
61
62   class IEvent {
63    public:
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;
68   };
69
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();
74
75   virtual void Listen();
76   virtual void Dispose();
77   virtual pid_t Prepare();
78   virtual pid_t Deploy(const AppInfo* app_info);
79
80   const std::string& GetLoaderName() const;
81   const std::string& GetLoaderPath() const;
82   const std::string& GetLoaderExtra() const;
83   int GetType() const;
84   bool IsPrepared() const;
85   pid_t GetPid() 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;
100   void UnsetTimer();
101   void SetTimer();
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);
112   bool IsLaunchable();
113   void SetEventListener(IEvent* listener);
114   void Ref();
115   void Unref();
116   uint32_t RefCount() const;
117
118  protected:
119   void SetPrepared(bool prepared);
120   void OnIOEventReceived(int fd, int condition) override;
121   int GetSchedPriority() const;
122   void CreateReadyFile();
123
124  private:
125   void UpdateScore();
126   void Activate();
127   void Deactivate();
128   bool CanDeactivate(LoaderMethod method) const;
129   void SetLiveTimer();
130   void HandleLoaderEvent();
131   void HandleLoaderClientEvent(int condition);
132
133   static gboolean TimeoutCb(gpointer user_data);
134   static gboolean TimeToLiveCb(gpointer user_data);
135   static gboolean OnBootTimeoutCb(gpointer user_data);
136
137  private:
138   bool prepared_ = false;
139   std::shared_ptr<LoaderInfo> loader_info_;
140   int loader_id_;
141   pid_t caller_pid_;
142   bool activated_;
143   std::shared_ptr<LoaderMount> loader_mount_;
144   std::unique_ptr<CPUChecker> cpu_checker_;
145   unsigned int score_;
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_;
151   pid_t pid_ = 0;
152   launchpad::LoaderMethod method_ = launchpad::LoaderMethod::None;
153   bool touched_ = false;
154   uint64_t pss_ = 0;
155   int cpu_check_count_ = 0;
156   guint timer_ = 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;
163 };
164
165 }  // namespace launchpad
166
167 #endif  // LAUNCHPAD_PROCESS_POOL_LOADER_CONTEXT_HH_