tizen 2.3.1 release
[framework/web/mobile/wrt-security.git] / ace_popup_validation / src / ace_api_popup_validation.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  * @file        ace_api_popup_validation.cpp
18  * @author      Tomasz Swierczek (t.swierczek@samsung.com)
19  * @version     1.0
20  * @brief       This file contains implementation of ACE popup validation API.
21  */
22
23 #include <string>
24 #include <vector>
25 #include <dpl/log/log.h>
26 #include "SecurityCommunicationClient.h"
27 #include "popup_response_server_api.h"
28 #ifdef DBUS_CONNECTION
29 #include "security_daemon_dbus_config.h"
30 #endif
31 #include "ace_api_popup_validation.h"
32
33 namespace {
34 static WrtSecurity::Communication::Client *communicationClient = NULL;
35 static const int VALIDITY_ONCE_VALUE = 0;
36 static const int VALIDITY_SESSION_VALUE = 1;
37 static const int VALIDITY_ALWAYS_VALUE = 1;
38 } // anonymous
39
40 ace_return_t ace_popup_validation_initialize(void)
41 {
42     if (NULL != communicationClient) {
43         LogError("ace_api_popup_validation already initialized");
44         return ACE_INTERNAL_ERROR;
45     }
46     Try {
47         communicationClient = new WrtSecurity::Communication::Client(
48                    WrtSecurity::PopupServerApi::INTERFACE_NAME());
49     } Catch (WrtSecurity::Communication::Client::Exception::SecurityCommunicationClientException) {
50         LogError("Can't connect to daemon");
51         return ACE_INTERNAL_ERROR;
52     }
53
54     return ACE_OK; 
55 }
56
57 ace_return_t ace_popup_validation_shutdown(void)
58 {
59     if (NULL == communicationClient) {
60         LogError("ace_api_popup_validation not initialized");
61         return ACE_INTERNAL_ERROR;
62     }
63     delete communicationClient;
64     communicationClient = NULL;
65
66     return ACE_OK;
67 }
68
69 ace_return_t ace_validate_answer(ace_bool_t answer,
70                                  ace_validity_t validity,
71                                  const ace_resource_t resource_name,
72                                  const ace_session_id_t session_id,
73                                  const ace_param_list_t* param_list,
74                                  ace_widget_handle_t handle,
75                                  ace_bool_t* validation_result)
76 {
77     if (NULL == resource_name ||
78         NULL == session_id ||
79         NULL == param_list ||
80         NULL == validation_result)
81     {
82         LogError("NULL argument(s) passed");
83         return ACE_INVALID_ARGUMENTS;
84     }
85
86     bool dbusAnswer = answer == ACE_TRUE;
87     int dbusValidity = 0;
88
89     switch (validity) {
90     case ACE_ONCE: {
91         dbusValidity = VALIDITY_ONCE_VALUE;
92         //static_cast<int>(Prompt::Validity::ONCE);
93         break; }
94     case ACE_SESSION: {
95         dbusValidity = VALIDITY_SESSION_VALUE;
96         //static_cast<int>(Prompt::Validity::SESSION);
97         break; }
98     case ACE_ALWAYS: {
99         dbusValidity = VALIDITY_ALWAYS_VALUE;
100         //static_cast<int>(Prompt::Validity::ALWAYS);
101         break; }
102     default: {
103         LogError("Invalid validity passed");
104         return ACE_INVALID_ARGUMENTS; }
105     }
106
107     std::string subjectId;
108     std::string resourceId(resource_name);
109     std::string sessionId(session_id);
110     std::vector<std::string> keys, values;
111     unsigned int i;
112     for (i = 0; i < param_list->count; ++i) {
113         keys.push_back(std::string(param_list->items[i].name));
114         values.push_back(std::string(param_list->items[i].value));
115     }
116
117     bool response = false;
118     Try{
119         communicationClient->call(WrtSecurity::PopupServerApi::VALIDATION_METHOD(),
120                          dbusAnswer,
121                          dbusValidity,
122                          handle,
123                          subjectId,
124                          resourceId,
125                          keys,
126                          values,
127                          sessionId,
128                          &response);
129     } Catch (WrtSecurity::Communication::Client::Exception::SecurityCommunicationClientException) {
130         LogError("Can't call daemon");
131         return ACE_INTERNAL_ERROR;
132     }
133
134     *validation_result = response ? ACE_TRUE : ACE_FALSE;
135
136     return ACE_OK;
137 }