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