tizen 2.3.1 release
[framework/web/wearable/wrt-plugins-tizen.git] / src / MessagePort / MessagePortManagerProxy.h
1 //
2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 /**
19  * @file        MessagePortManagerProxy.h
20  * @version     0.1
21  * @brief
22  */
23
24 #ifndef _PLATFORM_MESSAGE_PORT_MESSAGE_PORT_MANAGER_WRAPPER_H_
25 #define _PLATFORM_MESSAGE_PORT_MESSAGE_PORT_MANAGER_WRAPPER_H_
26
27 #include <string>
28 #include <map>
29 #include <dpl/shared_ptr.h>
30 #include <dpl/singleton.h>
31 #include "ILocalMessagePort.h"
32 #include "IRemoteMessagePort.h"
33
34 #include <message-port.h>
35
36 namespace DeviceAPI {
37 namespace MessagePort {
38
39 #define UNDEFINED_LOCAL_PORT_ID (-1)
40
41 class MessagePortManagerProxy
42 {
43 public:
44     MessagePortManagerProxy();
45     virtual ~MessagePortManagerProxy();
46
47     LocalMessagePortPtr requestLocalMessagePort(std::string &messagePortName);
48     LocalMessagePortPtr requestTrustedLocalMessagePort(std::string &messagePortName);
49     RemoteMessagePortPtr requestRemoteMessagePort(std::string &appId, std::string &messagePortName);
50     RemoteMessagePortPtr requestTrustedRemoteMessagePort(std::string &appId, std::string &messagePortName);
51
52     void sendMessage(std::string appId, std::string name, bool isTrusted,
53             MessagePortDataItemMapPtr &data, int localPortId = UNDEFINED_LOCAL_PORT_ID);
54
55 protected:
56     LocalMessagePortPtr getLocalMessagePort(std::string &name, bool isTrusted);
57     RemoteMessagePortPtr getRemoteMessagePort(std::string &appId, std::string &name, bool isTrusted);
58
59 private:
60     void cacheLocalMessagePort(int id, std::string &name, bool isTrusted, LocalMessagePortPtr &localMessagePort);
61     LocalMessagePortPtr getCachedLocalMessagePort(std::string &name, bool isTrusted);
62     LocalMessagePortPtr getCachedLocalMessagePort(int id);
63
64     void cacheRemoteMessagePort(std::string &appId, std::string &name, bool isTrusted, RemoteMessagePortPtr &remoteMessagePort);
65     RemoteMessagePortPtr getCachedRemoteMessagePort(std::string &appId, std::string &name, bool isTrusted);
66
67     struct LocalMessagePortKey {
68         std::string name;
69         bool isTrusted;
70         bool operator<(const LocalMessagePortKey &t) const {
71             return (name < t.name || (name == t.name && isTrusted < t.isTrusted));
72         }
73     };
74     struct RemoteMessagePortKey {
75         std::string appId;
76         std::string name;
77         bool isTrusted;
78         bool operator<(const RemoteMessagePortKey &t) const {
79             if (appId < t.appId) return true;
80             if (appId == t.appId) {
81                 if (name < t.name) return true;
82                 if (name == t.name && isTrusted < t.isTrusted)
83                     return true;
84             }
85             return false;
86         }
87     };
88     typedef std::map<LocalMessagePortKey, LocalMessagePortPtr> LocalMessagePortMap;
89     typedef std::pair<LocalMessagePortKey, LocalMessagePortPtr> LocalMessagePortPair;
90     typedef std::map<int, LocalMessagePortPtr> LocalMessagePortIdMap;
91     typedef std::pair<int, LocalMessagePortPtr> LocalMessagePortIdPair;
92     typedef std::map<RemoteMessagePortKey, RemoteMessagePortPtr> RemoteMessagePortMap;
93     typedef std::pair<RemoteMessagePortKey, RemoteMessagePortPtr> RemoteMessagePortPair;
94
95     LocalMessagePortMap m_localMessagePortMap;
96     LocalMessagePortIdMap m_localMessagePortIdMap;
97     RemoteMessagePortMap m_remoteMessagePortMap;
98
99 public:
100     void messagePortMessageCb(int id, const char* remote_app_id, const char* remote_port,
101             bool trusted_message, bundle* data);
102     static void message_port_message_cb(int id, const char* remote_app_id,
103             const char* remote_port, bool trusted_message, bundle* data);
104 };
105 typedef DPL::Singleton<MessagePortManagerProxy> MessagePortManagerProxySingleton;
106
107 } // MessagePort
108 } // DeviceAPI
109
110 #endif // _PLATFORM_MESSAGE_PORT_MESSAGE_PORT_MANAGER_WRAPPER_H_