Fix build error
[platform/core/appfw/pkgmgr-info.git] / src / common / db_change_observer.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 #include <gio/gio.h>
18 #include <glib.h>
19 #include <sys/inotify.h>
20
21 #include <mutex>
22 #include <string>
23 #include <shared_mutex>
24
25 #ifndef COMMON_DB_CHANGE_OBSERVER_HH_
26 #define COMMON_DB_CHANGE_OBSERVER_HH_
27
28 namespace pkgmgr_common {
29
30 #ifndef EXPORT_API
31 #define EXPORT_API __attribute__((visibility("default")))
32 #endif
33
34 class EXPORT_API DbChangeObserver {
35  public:
36   class IEvent {
37    public:
38     virtual ~IEvent() = default;
39     virtual void OnDbChanged() = 0;
40   };
41   static DbChangeObserver& GetInst();
42   bool Listen();
43   void StopListening();
44   bool IsChanged();
45   void SetChanged(bool changed);
46   void RegisterEvent(IEvent* listener);
47   void UnRegisterEvent();
48   bool GetDisposed();
49
50  private:
51   DbChangeObserver();
52   ~DbChangeObserver();
53
54   int fd_ = 0;
55   int wd_ = 0;
56   GIOChannel* channel_ = nullptr;
57   int tag_ = 0;
58   bool changed_ = false;
59   bool disposed_ = true;
60   std::shared_mutex lock_;
61   std::string global_parser_db_path_;
62   IEvent* listener_ = nullptr;
63
64   void Dispose();
65   bool SetGlobalParserDbPath();
66   static gboolean OnReceiveEvent(GIOChannel* channel, GIOCondition cond,
67       gpointer user_data);
68 };
69
70 }  // namespace pkgmgr_common
71
72 #endif  // COMMON_DB_CHANGE_OBSERVER_HH_