[M120 Migration][WRTjs][Service] Separate service app datas from WRTServiceManager
[platform/framework/web/chromium-efl.git] / wrt / src / service / wrt_service_manager.h
1 // Copyright 2020 Samsung Electronics. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef BROWSER_WRT_SERVICE_MANAGER_H_
6 #define BROWSER_WRT_SERVICE_MANAGER_H_
7
8 #include <memory>
9 #include <queue>
10 #include <string>
11 #include <unordered_map>
12 #include <vector>
13
14 #include "uv.h"
15 #include "wrt/src/browser/wrt_ipc.h"
16 #include "wrt/src/common/application_data.h"
17 #include "wrt/src/service/wrt_service.h"
18
19 namespace wrt {
20
21 class WRTServiceManager {
22  public:
23   static WRTServiceManager* Get(bool can_create = true);
24
25   void CheckTimeout();
26   void CloseDbusHandler();
27   void DelayShutdown(int exit_count = 120);
28   void FinalizeService(const std::string& internal_id);
29   void InitServiceApp(int argc, char** argv);
30   void KillLauncherProcess(const std::string& internal_id);
31   void StartService(const std::string& internal_id,
32                     const std::string& source_name,
33                     const std::string& pkg_path);
34   void StopService(const std::string& internal_id);
35   void RequestQuit();
36   void RunBuiltinService(const std::string& service_name,
37                          const std::string& internal_id,
38                          const std::string& pkg_path);
39
40   void SetServiceModel(WRTService::ServiceType service_model);
41   void Shutdown();
42
43   bool CheckLauncherAlive(const std::string& internal_id);
44   bool FinishStartingService(const std::string& internal_id);
45   bool IsStandaloneService();
46   bool IsQuitting() { return quit_requested_; }
47   bool SendMessage(const char* type, const char* argument);
48   bool SendMessage(const char* receiver, const char* type, const char* argument);
49   bool DidStartService(const std::string& internal_id);
50
51   WRTService::ServiceType GetServiceModel();
52
53   std::string GetAppControlData(std::string& app_id);
54   std::string GetRuntimeVariable(const std::string& runtime_key);
55   std::string SendMessageAndWaitReply(const char* type, const char* argument);
56
57   ApplicationData* GetApplicationData(const std::string& internal_id);
58
59  private:
60   WRTServiceManager();
61   ~WRTServiceManager();
62
63   static void OnBuiltinService(uv_async_t* handle);
64   static void OnServiceStarted(uv_async_t* handle);
65   static void OnServiceStopped(uv_async_t* handle);
66
67   void InitAulHandler(int argc, char** argv);
68   void CloseUvHandlers();
69   void CreateUvHandlers();
70   bool CheckParentProcessValid(int pid);
71   bool HandleMessageQueue();
72   int RegisterDbusHandler();
73
74   uv_async_t handle_adder_ = {};
75   uv_async_t handle_remover_ = {};
76   uv_async_t handle_builtin_ = {};
77   uv_mutex_t mutex_;
78
79   std::vector<std::string> prepared_to_start_services_;
80   std::vector<std::string> prepared_to_stop_services_;
81   std::unordered_map<std::string, std::string> ui_source_names_;
82
83   wrt::InterProcessCommunication ipc_;
84   std::string internal_id_;
85   std::string removed_internal_id;
86
87   uv_thread_t dbus_thread_ = {};
88   uv_timer_t timeout_handle_ = {};
89   WRTService::ServiceType service_model_ = WRTService::NONE;
90   unsigned int exit_count_ = 120;
91   bool quit_requested_ = false;
92   int ppid_ = 0;
93 };
94
95 }  // namespace wrt
96
97 #endif  // BROWSER_WRT_SERVICE_MANAGER_H_