Add the 'privilege' parameter to popup response callback
[platform/core/security/askuser.git] / src / client / api / askuser-notification-client.cpp
1 /*
2  *  Copyright (c) 2017 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 /**
18  * @file        askuser-notification-client.cpp
19  * @author      Piotr Sawicki <p.sawicki2@partner.samsung.com>
20  * @brief       This file contains the implementation of the askuser-notification client API.
21  */
22
23 #include <memory>
24
25 #include <log/alog.h>
26 #include <askuser-notification-client.h>
27 #include <attributes/attributes.h>
28
29 #include <ApiInterface.h>
30 #include <ApiInterfaceImpl.h>
31 #include <PopupCallbackClosure.h>
32 #include <StatusCallbackClosure.h>
33 #include <TryCatch.h>
34
35 struct askuser_client {
36     AskUser::Client::ApiInterface *impl;
37
38     explicit askuser_client(AskUser::Client::ApiInterface *_impl) : impl(_impl) {
39     }
40
41     ~askuser_client() {
42         delete impl;
43     }
44 };
45
46 API
47 int askuser_client_initialize(askuser_client **pp_client,
48                               askuser_status_callback status_callback, void *p_user_data)
49 {
50     if (!pp_client) {
51         return ASKUSER_API_INVALID_PARAM;
52     }
53
54     init_agent_log();
55
56     return AskUser::Client::tryCatch([&]() {
57         AskUser::Client::StatusCallbackClosure closure(status_callback, p_user_data);
58
59         std::unique_ptr<AskUser::Client::ApiInterface> ptr;
60         ptr.reset(new AskUser::Client::ApiInterfaceImpl(closure));
61
62         *pp_client = new askuser_client(ptr.get());
63
64         ptr.release();
65
66         return ASKUSER_API_SUCCESS;
67     });
68 }
69
70 API
71 void askuser_client_finalize(askuser_client *p_client)
72 {
73     if (!p_client) {
74         return;
75     }
76
77     AskUser::Client::tryCatch([&]() {
78         delete p_client;
79         return ASKUSER_API_SUCCESS;
80     });
81 }
82
83 API
84 int askuser_client_process(askuser_client *p_client, int fd, int events)
85 {
86     if (!p_client) {
87         return ASKUSER_API_INVALID_PARAM;
88     }
89
90     return AskUser::Client::tryCatch([&]() {
91         return p_client->impl->process(fd, events);
92     });
93 }
94
95 API
96 int askuser_client_check_privilege(askuser_client *p_client,
97                                    const char *privilege, askuser_check_result *p_result)
98 {
99     if (!p_client || !privilege || !p_result) {
100         return ASKUSER_API_INVALID_PARAM;
101     }
102
103     return AskUser::Client::tryCatch([&]() {
104         *p_result = p_client->impl->checkPrivilege(privilege);
105         return ASKUSER_API_SUCCESS;
106     });
107 }
108
109 API
110 int askuser_client_popup_request(askuser_client *p_client, const char *privilege,
111                                  askuser_popup_response_callback response_callback,
112                                  void *p_user_data, int *p_request_id)
113 {
114     if (!p_client || !privilege || !response_callback) {
115         return ASKUSER_API_INVALID_PARAM;
116     }
117
118     return AskUser::Client::tryCatch([&]() {
119         if (p_client->impl->popupRequestInProgress(privilege)) {
120             return ASKUSER_API_ALREADY_IN_PROGRESS;
121         }
122
123         AskUser::Client::PopupCallbackClosure closure(response_callback, privilege, p_user_data);
124         AskUser::Client::RequestId id = p_client->impl->popupRequest(closure, privilege);
125
126         if (p_request_id) {
127             *p_request_id = id;
128         }
129
130         return ASKUSER_API_SUCCESS;
131     });
132 }