Fix rpc_port_proxy_destroy() function
[platform/core/appfw/rpc-port.git] / src / proxy-internal.h
1 /*
2  * Copyright (c) 2017 Samsung Electronics Co., Ltd.
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 PROXY_INTERNAL_H_
18 #define PROXY_INTERNAL_H_
19
20 #include <glib.h>
21 #include <gio/gio.h>
22 #include <glib-unix.h>
23
24 #include <string>
25 #include <memory>
26
27 #include "fdbroker-internal.h"
28 #include "port-internal.h"
29
30 namespace rpc_port {
31 namespace internal {
32
33 class Proxy : public FdBroker::IEventWatcher {
34  public:
35   explicit Proxy(bool mock = false);
36   virtual ~Proxy();
37
38   class IEventListener {
39    public:
40     virtual void OnConnected(const std::string& endpoint, Port* port) = 0;
41     virtual void OnDisconnected(const std::string& endpoint) = 0;
42     virtual void OnRejected(const std::string& endpoint, int err_code) = 0;
43     virtual void OnReceived(const std::string& endpoint) = 0;
44   };
45
46   int Connect(std::string appid, std::string port_name, IEventListener* ev);
47   int ConnectSync(std::string appid, std::string port_name, IEventListener* ev);
48   void DisconnectPort();
49
50   std::shared_ptr<Port> GetPort() const {
51     return main_port_;
52   }
53
54   std::shared_ptr<Port> GetDelegatePort() const {
55     return delegate_port_;
56   }
57
58   const std::string& GetPortName() {
59     return port_name_;
60   }
61
62  private:
63   class ProxyPort : public Port {
64    public:
65     ProxyPort(Proxy* parent, int fd, const std::string& id, bool receive = true);
66     virtual ~ProxyPort();
67     void SetDisconnectedSource(int sourceId);
68     void SetSource(int sourceId);
69
70    private:
71     int Watch(bool receive);
72
73    private:
74     GIOChannel* gioc_ = nullptr;
75     int disconn_src_ = 0;
76     int src_ = 0;
77     Proxy* parent_ = nullptr;
78   };
79
80  private:
81   static gboolean OnSocketDisconnected(GIOChannel *gio, GIOCondition cond,
82                                        gpointer data);
83   static gboolean OnDataReceived(GIOChannel *gio, GIOCondition cond,
84                                  gpointer data);
85   static gboolean DbusNameTimeout(gpointer user_data);
86   void OnPortAppeared() override;
87   void OnPortVanished() override;
88   void OnPortRejected(int error) override;
89   void OnPortConnected() override;
90   void OnPortDisconnected(bool cancel = false) override;
91   void SetRealAppId(const std::string& alias_appid);
92
93  private:
94   std::string port_name_;
95   std::shared_ptr<ProxyPort> main_port_;
96   std::shared_ptr<ProxyPort> delegate_port_;
97   IEventListener* listener_ = nullptr;
98   std::shared_ptr<FdBroker> fd_broker_;
99   std::string target_appid_;
100   std::string real_appid_;
101   int fds_[2];
102   guint conn_timer_ = 0;
103 };
104
105 }  // namespace internal
106 }  // namespace rpc_port
107
108 #endif  // PROXY_INTERNAL_H_