2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
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.
22 * @file session_manager.h
23 * @author Lukasz Wrzosek (l.wrzosek@samsung.com)
25 * @brief Header file for session management like starting/invalidating
28 #include <session_manager.h>
29 #include <dpl/log/log.h>
33 static const int SESSION_ID_LENGTH = 32;
35 using namespace WrtDB;
37 class SessionManager::Impl
40 std::map<SessionId, WidgetHandle> sessionIdWidgetIdMap;
41 std::map<WidgetHandle, SessionId> widgetHandleSessionIdMap;
44 srandom(static_cast<int>(time(0)));
49 SessionId 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));
58 while (sessionIdWidgetIdMap.find(session_id) !=
59 sessionIdWidgetIdMap.end());
60 LogDebug("Generated session_id " << session_id);
64 SessionId startNewSession (WidgetHandle widget_id)
66 SessionId session_id = generate();
67 invalidateSession(widget_id); // in this order it is impossible to genreate the same session_id :)
69 sessionIdWidgetIdMap[session_id] = widget_id;
70 widgetHandleSessionIdMap[widget_id] = session_id;
75 SessionId getSessionId (WidgetHandle widget_id)
77 if (widgetHandleSessionIdMap.find(widget_id) !=
78 widgetHandleSessionIdMap.end()) {
79 return widgetHandleSessionIdMap[widget_id];
81 return std::string("");
85 void invalidateSession (WidgetHandle widget_id)
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);
95 void invalidateSession (SessionId session_id)
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);
106 SessionManager::SessionManager()
108 impl = new SessionManager::Impl;
111 SessionManager::~SessionManager()
116 SessionId SessionManager::startNewSession (WidgetHandle widget_id)
118 LogDebug("startNewSession for widget_id " << widget_id);
119 return impl->startNewSession(widget_id);
122 SessionId SessionManager::startNewSession (WidgetModel *model)
124 LogDebug("startNewSession for widget_id " << model->Handle.Get());
125 return impl->startNewSession(model->Handle.Get());
128 SessionId SessionManager::getSessionId (WidgetHandle widget_id)
131 "GetSession for widget_id " << widget_id << " : " <<
132 impl->getSessionId(widget_id));
133 return impl->getSessionId(widget_id);
136 SessionId SessionManager::getSessionId (WidgetModel *model)
138 LogDebug("GetSession for widget_id " << model->Handle.Get() << " : " <<
139 impl->getSessionId(model->Handle.Get()));
140 return impl->getSessionId(model->Handle.Get());
143 void SessionManager::invalidateSession (WidgetHandle widget_id)
145 LogDebug("Invalidating by widget_id " << widget_id);
146 impl->invalidateSession(widget_id);
149 void SessionManager::invalidateSession (WidgetModel *model)
151 LogDebug("Invalidating by widget_id " << model->Handle.Get());
152 impl->invalidateSession(model->Handle.Get());
155 void SessionManager::invalidateSession (SessionId session_id)
157 LogDebug("Invalidating by session_id " << session_id);
158 impl->invalidateSession(session_id);