[WRTjs] Refactor popup
[platform/framework/web/chromium-efl.git] / wrt / src / browser / wrt_ipc.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_IPC_H_
6 #define BROWSER_WRT_IPC_H_
7
8 #include <dbus/dbus.h>
9 #include <functional>
10 #include <map>
11 #include <string>
12
13 namespace wrt {
14
15 class InterProcessCommunication {
16  public:
17   InterProcessCommunication(bool is_daemon = false);
18   ~InterProcessCommunication();
19
20   class Message {
21    public:
22     Message(DBusConnection* connection,
23             DBusMessage* message,
24             InterProcessCommunication* ipc);
25
26     const char* Sender() const;
27     void CloseDBusConnection() const;
28     void Reply(const char* argument) const;
29
30    private:
31     DBusConnection* connection_;
32     DBusMessage* message_;
33     InterProcessCommunication* ipc_ = nullptr;
34   };
35
36   typedef std::function<void(const char* type, const char* argument, const Message& message)> MessageHandler;
37
38   void AddMessageHandler(const char* type, MessageHandler handler);
39   void SetExitOnDisconnect(bool exit_on_disconnect);
40   void Listen();
41   void CloseDbus();
42
43   bool HasDbusOwner(const char* dbus_name);
44   bool SendMessage(const char* type, const char* argument);
45   bool SendMessage(const char* receiver, const char* type, const char* argument);
46   bool SendMessageAndWaitReply(const char* type, const char* argument, std::string& reply_argument);
47   bool SendMessageAndWaitReply(const char* receiver, const char* type, const char* argument, std::string& reply_argument);
48
49  private:
50   static DBusHandlerResult OnMessage(DBusConnection* connection, DBusMessage* message, void* user_data);
51
52   DBusMessage* InternalSendMessage(const char* receiver, const char* type, const char* argument, bool need_reply);
53
54   DBusConnection* connection_;
55   std::map<const std::string, MessageHandler> message_handlers_;
56   bool will_close_ = false;
57 };
58
59 }  // namespace wrt
60
61 #endif  // BROWSER_WRT_IPC_H_