summaryrefslogtreecommitdiff
path: root/src/ui/popup-bin/popup.cpp
blob: b3256b1c1685316e18a1f62d1024c2ee145e24d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
 * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */
/*
 * @file        popup.cpp
 * @author      Janusz Kozerski (j.kozerski@samsung.com)
 * @version     1.0
 */

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <vector>
#include <libintl.h>
#include <sys/select.h>
#include <time.h>

#include <Elementary.h>

#include <popup.h>
#include "common/log.h"
#include <cchecker/UIBackend.h>
#include <cchecker/popup-runner.h>
#include <cchecker/dpl/serialization.h>
#include <cchecker/dpl/errno_string.h>

using namespace CCHECKER::UI;

namespace { // anonymous

void on_done(void)
{
	// Quit the efl-mainloop
	LogDebug("elm_exit()");
	elm_exit();
}

void keep_answer(void *data, Evas_Object * /* obj */, void * /* event_info */)
{
	LogDebug("keep_answer");

	if (NULL == data) {
		LogError("data is NULL; return");
		return;
	}

	struct cert_checker_popup_data *pdp = static_cast <struct cert_checker_popup_data *>(data);

	pdp->result = response_e::DONT_UNINSTALL;

	on_done();
}

void uninstall_answer(void *data, Evas_Object * /* obj */, void * /* event_info */)
{
	LogDebug("uninstall_answer");

	if (NULL == data) {
		LogError("data is NULL; return");
		return;
	}

	struct cert_checker_popup_data *pdp = static_cast <struct cert_checker_popup_data *>(data);

	pdp->result = response_e::UNINSTALL;

	on_done();
}

void show_popup(struct cert_checker_popup_data *pdp)
{
	LogDebug("show_popup()");

	if (NULL == pdp) {
		LogError("pdp is NULL; return");
		return;
	}

	pdp->win = elm_win_add(NULL,
						   dgettext(SERVICE_NAME, "SID_TITLE_OCSP_VERIFICATION_FAILED"),
						   ELM_WIN_NOTIFICATION);
	elm_policy_set(ELM_POLICY_QUIT, ELM_POLICY_QUIT_LAST_WINDOW_CLOSED);
	elm_win_autodel_set(pdp->win, EINA_TRUE);
	evas_object_show(pdp->win);
	elm_win_indicator_opacity_set(pdp->win, ELM_WIN_INDICATOR_TRANSLUCENT);
	pdp->popup = elm_popup_add(pdp->win);
	pdp->box = elm_box_add(pdp->popup);
	evas_object_size_hint_weight_set(pdp->box, EVAS_HINT_EXPAND, 0);
	evas_object_size_hint_align_set(pdp->box, EVAS_HINT_FILL, 0.0);
	pdp->title = elm_label_add(pdp->popup);
	elm_object_style_set(pdp->title, "elm.text.title");
	elm_object_text_set(pdp->title, dgettext(SERVICE_NAME, "SID_TITLE_OCSP_VERIFICATION_FAILED"));
	evas_object_show(pdp->title);
	elm_box_pack_end(pdp->box, pdp->title);
	pdp->content = elm_label_add(pdp->popup);
	elm_object_style_set(pdp->content, "elm.swallow.content");
	elm_label_line_wrap_set(pdp->content, ELM_WRAP_MIXED);
	char *buff = NULL;
	int ret;

	// Set message
	// App ID may be absent, so in that case we need to use only package ID
	if (pdp->app_id == std::string(CCHECKER::TEMP_APP_ID)) {
		char *content = dgettext(SERVICE_NAME, "SID_CONTENT_OCSP_PACKAGE VERIFICATION_FAILED");
		ret = asprintf(&buff, content, pdp->pkg_id.c_str());
	} else {
		char *content = dgettext(SERVICE_NAME, "SID_CONTENT_OCSP_VERIFICATION_FAILED");
		ret = asprintf(&buff, content, pdp->app_id.c_str(), pdp->pkg_id.c_str());
	}

	if (-1 == ret) {
		LogError("asprintf failed - returned -1");
		evas_object_del(pdp->content);
		evas_object_del(pdp->popup);
		evas_object_del(pdp->win);
		return;
	}

	elm_object_text_set(pdp->content, buff);
	LogDebug("Popup label: " << buff);
	free(buff);
	evas_object_size_hint_weight_set(pdp->content, EVAS_HINT_EXPAND, 0.0);
	evas_object_size_hint_align_set(pdp->content, EVAS_HINT_FILL, EVAS_HINT_FILL);
	evas_object_show(pdp->content);
	elm_box_pack_end(pdp->box, pdp->content);
	elm_object_part_content_set(pdp->popup, "default", pdp->box);
	pdp->keep_button = elm_button_add(pdp->popup);
	elm_object_style_set(pdp->keep_button, "elm.swallow.content.button1");
	elm_object_text_set(pdp->keep_button, dgettext(SERVICE_NAME, "SID_BTN_OCSP_KEEP_APP"));
	elm_object_part_content_set(pdp->popup, "button1", pdp->keep_button);
	evas_object_smart_callback_add(pdp->keep_button, "clicked", keep_answer, pdp);
	pdp->uninstall_button = elm_button_add(pdp->popup);
	elm_object_style_set(pdp->uninstall_button, "elm.swallow.content.button2");
	elm_object_text_set(pdp->uninstall_button, dgettext(SERVICE_NAME, "SID_BTN_OCSP_UNINSTALL_APP"));
	elm_object_part_content_set(pdp->popup, "button2  ", pdp->uninstall_button);
	evas_object_smart_callback_add(pdp->uninstall_button, "clicked", uninstall_answer, pdp);
	evas_object_show(pdp->popup);
	// Showing the popup window
	evas_object_show(pdp->win);
	// Run the efl mainloop
	elm_run();
	// Shutdown elementary
	LogDebug("elm_shutdown()");
	elm_shutdown();
}

static int wait_for_parent_info(int pipe_in)
{
	// wait for parameters from pipe_in
	// timeout is set for 10 seconds
	struct timeval timeout = {10L, 0L};
	fd_set readfds;
	FD_ZERO(&readfds);
	FD_SET(pipe_in, &readfds);
	int sel = select(pipe_in + 1, &readfds, NULL, NULL, &timeout);

	if (sel == -1) {
		int error = errno;
		LogError("Cannot get info from parent. Exit popup");
		LogError("Error: " << CCHECKER::GetErrnoString(error));
		close(pipe_in);
		return -1;
	} else if (sel == 0) {
		LogError("Timeout reached! Exit popup - ERROR");
		close(pipe_in);
		return -1;
	}

	return 0;
}

void deserialize(cert_checker_popup_data *pdp, char *line, ssize_t line_length)
{
	BinaryStream stream;
	stream.Write(line_length, static_cast <void *>(line));
	std::string app_id;
	std::string pkg_id;
	LogDebug("------- Deserialization -------");
	// Deserialization order:
	// app_id, pkg_id
	CCHECKER::Deserialization::Deserialize(stream, app_id);
	LogDebug("app_id : " << app_id.c_str());
	pdp->app_id = app_id.c_str();
	CCHECKER::Deserialization::Deserialize(stream, pkg_id);
	LogDebug("pkg_id : " << pkg_id.c_str());
	pdp->pkg_id = pkg_id.c_str();
}

} // anonymous

EAPI_MAIN int
elm_main(int argc, char **argv)
{
	// int pipe_in and int pipe_out should be passed to Popup via args.
	// These parameters should be passed to Popup via pipe_in:
	// std::string    app_id
	// std::string    pkg_id
	struct cert_checker_popup_data pd;
	struct cert_checker_popup_data *pdp = &pd;
	LogDebug("############################ popup binary ################################");
	setlocale(LC_ALL, "");

	if (argc < 3) {
		LogError("To few args passed in exec to popup-bin - should be at least 3:");
		LogError("(binary-name, pipe_in, pipe_out)");
		LogError("return ERROR");
		return popup_status::EXIT_ERROR;
	}

	LogDebug("Passed args: " << argv[0] << ", " << argv[1] << ", " << argv[2]);
	int pipe_in;
	int pipe_out;

	// Parsing args (pipe_in, pipe_out)
	if (0 == sscanf(argv[1], "%d", &pipe_in)) {
		LogError("Error while parsing pipe_in; return ERROR");
		return popup_status::EXIT_ERROR;
	}

	if (0 == sscanf(argv[2], "%d", &pipe_out)) {
		LogError("Error while parsing pipe_out; return ERROR");
		return popup_status::EXIT_ERROR;
	}

	LogDebug("Parsed pipes: IN: " << pipe_in << ", OUT: " <<  pipe_out);

	if (wait_for_parent_info(pipe_in) == -1) {
		close(pipe_out);
		return popup_status::EXIT_ERROR;
	}

	int  buff_size = 1024;
	char line[buff_size];
	ssize_t count = 0;

	do {
		count = TEMP_FAILURE_RETRY(read(pipe_in, line, buff_size));
	} while (0 == count);

	if (count < 0) {
		int error = errno;
		close(pipe_in);
		close(pipe_out);
		LogError("read returned a negative value (" << count << ")");
		LogError("error: " << CCHECKER::GetErrnoString(error));
		LogError("Exit popup - ERROR");
		return popup_status::EXIT_ERROR;
	}

	LogDebug("Read bytes : " << count);
	close(pipe_in); // cleanup
	deserialize(pdp, line, count);
	pdp->result = response_e::RESPONSE_ERROR;
	show_popup(pdp); // Showing popup
	// sending validation_result to popup-runner
	BinaryStream stream_out;
	LogDebug("pdp->result : " << pdp->result);
	CCHECKER::Serialization::Serialize(stream_out, pdp->result);

	if (-1 == TEMP_FAILURE_RETRY(write(pipe_out, stream_out.char_pointer(), stream_out.size()))) {
		LogError("Write to pipe failed!");
		close(pipe_out);
		return popup_status::EXIT_ERROR;
	}

	close(pipe_out);
	LogDebug("############################ /popup binary ################################");
	LogDebug("Return: " << popup_status::NO_ERROR);
	return popup_status::NO_ERROR;
}
ELM_MAIN()