upload tizen1.0 source
[platform/framework/web/wrt.git] / src / access-control / DEPRECATED / session_manager.cpp
1 /*
2  * Copyright (c) 2011 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  * This class simply redirects the access requests to access control engine.
18  * The aim is to hide access control engine specific details from WRT modules.
19  * It also implements WRT_INTERFACE.h interfaces, so that ACE could access
20  * WRT specific and other information during the decision making.
21  *
22  * @file    session_manager.h
23  * @author  Lukasz Wrzosek (l.wrzosek@samsung.com)
24  * @version 1.0
25  * @brief   Header file for session management like starting/invalidating
26  */
27
28 #include <session_manager.h>
29 #include <dpl/log/log.h>
30
31 #include <map>
32
33 static const int SESSION_ID_LENGTH = 32;
34
35 using namespace WrtDB;
36
37 class SessionManager::Impl
38 {
39   public:
40     std::map<SessionId, WidgetHandle> sessionIdWidgetIdMap;
41     std::map<WidgetHandle, SessionId> widgetHandleSessionIdMap;
42
43     Impl() {
44         srandom(static_cast<int>(time(0)));
45     }
46
47     SessionId generate()
48     {
49         SessionId session_id = "";
50         do {
51             session_id = "";
52             for (int i = 0; i < SESSION_ID_LENGTH; i++) {
53                 int c = random() % 16;
54                 session_id.append(1, c < 10 ? static_cast<char>('0' + c)
55                                   : static_cast<char>('A' + c - 10));
56             }
57         }
58         while (sessionIdWidgetIdMap.find(session_id) !=
59                sessionIdWidgetIdMap.end());
60         LogDebug("Generated session_id " << session_id);
61         return session_id;
62     }
63
64     SessionId startNewSession (WidgetHandle widget_id)
65     {
66         SessionId session_id = generate();
67         invalidateSession(widget_id);   // in this order it is impossible to genreate the same session_id :)
68
69         sessionIdWidgetIdMap[session_id] = widget_id;
70         widgetHandleSessionIdMap[widget_id] = session_id;
71
72         return session_id;
73     }
74
75     SessionId getSessionId (WidgetHandle widget_id)
76     {
77         if (widgetHandleSessionIdMap.find(widget_id) !=
78             widgetHandleSessionIdMap.end()) {
79             return widgetHandleSessionIdMap[widget_id];
80         } else {
81             return std::string("");
82         }
83     }
84
85     void invalidateSession (WidgetHandle widget_id)
86     {
87         if (widgetHandleSessionIdMap.find(widget_id) !=
88             widgetHandleSessionIdMap.end()) {
89             SessionId session_id = widgetHandleSessionIdMap[widget_id];
90             sessionIdWidgetIdMap.erase(session_id);
91             widgetHandleSessionIdMap.erase(widget_id);
92         }
93     }
94
95     void invalidateSession (SessionId session_id)
96     {
97         if (sessionIdWidgetIdMap.find(session_id) !=
98             sessionIdWidgetIdMap.end()) {
99             WidgetHandle widget_id = sessionIdWidgetIdMap[session_id];
100             sessionIdWidgetIdMap.erase(session_id);
101             widgetHandleSessionIdMap.erase(widget_id);
102         }
103     }
104 };
105
106 SessionManager::SessionManager()
107 {
108     impl = new SessionManager::Impl;
109 }
110
111 SessionManager::~SessionManager()
112 {
113     delete impl;
114 }
115
116 SessionId SessionManager::startNewSession (WidgetHandle widget_id)
117 {
118     LogDebug("startNewSession for widget_id " << widget_id);
119     return impl->startNewSession(widget_id);
120 }
121
122 SessionId SessionManager::startNewSession (WidgetModel *model)
123 {
124     LogDebug("startNewSession for widget_id " << model->Handle.Get());
125     return impl->startNewSession(model->Handle.Get());
126 }
127
128 SessionId SessionManager::getSessionId (WidgetHandle widget_id)
129 {
130     LogDebug(
131         "GetSession for widget_id " << widget_id << " : " <<
132         impl->getSessionId(widget_id));
133     return impl->getSessionId(widget_id);
134 }
135
136 SessionId SessionManager::getSessionId (WidgetModel *model)
137 {
138     LogDebug("GetSession for widget_id " << model->Handle.Get() << " : " <<
139              impl->getSessionId(model->Handle.Get()));
140     return impl->getSessionId(model->Handle.Get());
141 }
142
143 void SessionManager::invalidateSession (WidgetHandle widget_id)
144 {
145     LogDebug("Invalidating by widget_id " << widget_id);
146     impl->invalidateSession(widget_id);
147 }
148
149 void SessionManager::invalidateSession (WidgetModel *model)
150 {
151     LogDebug("Invalidating by widget_id " << model->Handle.Get());
152     impl->invalidateSession(model->Handle.Get());
153 }
154
155 void SessionManager::invalidateSession (SessionId session_id)
156 {
157     LogDebug("Invalidating by session_id " << session_id);
158     impl->invalidateSession(session_id);
159 }
160