Add to enable TIZEN_ASAN_ACTIVATION
[platform/core/appfw/launchpad.git] / src / launchpad-process-pool / file_monitor.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 FILE_MONITOR_
18 #define FILE_MONITOR_
19
20 #include <gio/gio.h>
21 #include <glib.h>
22 #include <sys/inotify.h>
23
24 #include <filesystem>
25 #include <string_view>
26
27 namespace launchpad {
28
29 class FileMonitor {
30  public:
31   enum class FileMonitorEvent : int {
32     Created = IN_CREATE,
33     Modified = IN_MODIFY,
34     Deleted = IN_DELETE,
35   };
36
37   class IEvent {
38    public:
39     virtual void OnFileChanged(FileMonitorEvent event) = 0;
40   };
41
42   FileMonitor(const std::string_view file_path, IEvent* listener);
43   ~FileMonitor();
44   FileMonitor(const FileMonitor&) = delete;
45   FileMonitor& operator=(const FileMonitor&) = delete;
46
47  private:
48   std::filesystem::path parent_path_;
49   std::filesystem::path file_name_;
50   int fd_ = 0;
51   int wd_ = 0;
52   GIOChannel* channel_ = nullptr;
53   int tag_ = 0;
54   bool disposed_ = false;
55   IEvent* listener_;
56
57   void Dispose();
58   static gboolean OnEventReceived(GIOChannel* channel,
59                                  GIOCondition cond,
60                                  gpointer user_data);
61 };
62
63 }  // namespace launchpad
64
65 #endif  // FILE_MONITOR_