summaryrefslogtreecommitdiff
path: root/src/service_app_main.c
blob: 24afe8f2b7c07aee4b5083960bb24f09f8497d1a (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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
/*
 * Copyright (c) 2011 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.
 */


#include <stdlib.h>
#include <unistd.h>
#include <bundle.h>
#include <aul.h>
#include <dlog.h>
#include <vconf-internal-keys.h>
#include <app_common.h>
#include <Eina.h>
#include <appcore-agent.h>

#include "service_app_private.h"
#include "service_app_extension.h"

#ifdef LOG_TAG
#undef LOG_TAG
#endif

#ifndef TIZEN_PATH_MAX
#define TIZEN_PATH_MAX 1024
#endif

#define LOG_TAG "CAPI_APPFW_APPLICATION"

typedef enum {
	SERVICE_APP_STATE_NOT_RUNNING, /* The application has been launched or was running but was terminated */
	SERVICE_APP_STATE_CREATING, /* The application is initializing the resources on service_app_create_cb callback */
	SERVICE_APP_STATE_RUNNING, /* The application is running in the foreground and background */
} service_app_state_e;

static int _service_app_get_id(char **id)
{
	static char id_buf[TIZEN_PATH_MAX] = {0, };
	int ret = -1;

	if (id == NULL)
		return service_app_error(APP_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);

	if (id_buf[0] == '\0') {
		ret = aul_app_get_appid_bypid(getpid(), id_buf, sizeof(id_buf));
		if (ret < 0)
			return service_app_error(APP_ERROR_INVALID_CONTEXT, __FUNCTION__, "failed to get the application ID");
	}

	if (id_buf[0] == '\0')
		return service_app_error(APP_ERROR_INVALID_CONTEXT, __FUNCTION__, "failed to get the application ID");

	*id = strdup(id_buf);
	if (*id == NULL)
		return service_app_error(APP_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL);

	return APP_ERROR_NONE;
}

static int _service_appget_package_app_name(const char *appid, char **name)
{
	char *name_token = NULL;

	if (appid == NULL)
		return service_app_error(APP_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);

	/* com.vendor.name -> name */
	name_token = strrchr(appid, '.');
	if (name_token == NULL)
		return service_app_error(APP_ERROR_INVALID_CONTEXT, __FUNCTION__, NULL);

	name_token++;

	*name = strdup(name_token);
	if (*name == NULL)
		return service_app_error(APP_ERROR_OUT_OF_MEMORY, __FUNCTION__, NULL);

	return APP_ERROR_NONE;
}

EXPORT_API void service_app_exit_without_restart(void)
{
	appcore_agent_terminate_without_restart();
}

#define SERVICE_APP_EVENT_MAX 6
static Eina_List *handler_list[SERVICE_APP_EVENT_MAX] = {NULL, };
static int handler_initialized = 0;
static int appcore_agent_initialized = 0;

struct app_event_handler {
	app_event_type_e type;
	app_event_cb cb;
	void *data;
};

struct app_event_info {
	app_event_type_e type;
	void *value;
};

struct service_app_context {
	char *package;
	char *service_app_name;
	service_app_state_e state;
	service_app_lifecycle_callback_s *callback;
	void *data;
};

static void _free_handler_list(void)
{
	int i;
	app_event_handler_h handler;

	for (i = 0; i < SERVICE_APP_EVENT_MAX; i++) {
		EINA_LIST_FREE(handler_list[i], handler)
		if (handler)
			free(handler);
	}

	eina_shutdown();
}

static int _service_app_low_memory(void *event_info, void *data)
{
	Eina_List *l;
	app_event_handler_h handler;
	struct app_event_info event;

	LOGI("service_app_low_memory");

	event.type = APP_EVENT_LOW_MEMORY;
	event.value = event_info;

	EINA_LIST_FOREACH(handler_list[APP_EVENT_LOW_MEMORY], l, handler) {
		handler->cb(&event, handler->data);
	}

	return APP_ERROR_NONE;
}

static int _service_app_low_battery(void *event_info, void *data)
{
	Eina_List *l;
	app_event_handler_h handler;
	struct app_event_info event;

	LOGI("service_app_low_battery");

	event.type = APP_EVENT_LOW_BATTERY;
	event.value = event_info;

	EINA_LIST_FOREACH(handler_list[APP_EVENT_LOW_BATTERY], l, handler) {
		handler->cb(&event, handler->data);
	}

	return APP_ERROR_NONE;
}

static int _service_app_lang_changed(void *event_info, void *data)
{
	Eina_List *l;
	app_event_handler_h handler;
	struct app_event_info event;

	LOGI("service_app_lang_changed");

	event.type = APP_EVENT_LANGUAGE_CHANGED;
	event.value = event_info;

	EINA_LIST_FOREACH(handler_list[APP_EVENT_LANGUAGE_CHANGED], l, handler) {
		handler->cb(&event, handler->data);
	}

	return APP_ERROR_NONE;
}

static int _service_app_region_changed(void *event_info, void *data)
{
	Eina_List *l;
	app_event_handler_h handler;
	struct app_event_info event;

	if (event_info == NULL) {
		LOGI("receive empy event, ignore it");
		return APP_ERROR_NONE;
	}

	LOGI("service_app_region_changed");

	event.type = APP_EVENT_REGION_FORMAT_CHANGED;
	event.value = event_info;

	EINA_LIST_FOREACH(handler_list[APP_EVENT_REGION_FORMAT_CHANGED], l, handler) {
		handler->cb(&event, handler->data);
	}

	return APP_ERROR_NONE;
}

static int _service_app_appcore_suspended_state_changed(void *event_info, void *data)
{
	Eina_List *l;
	app_event_handler_h handler;
	struct app_event_info event;

	LOGI("_service_app_appcore_suspended_state_changed");
	LOGD("[__SUSPEND__] suspended state: %d (0: suspend, 1: wake)", *(int *)event_info);

	event.type = APP_EVENT_SUSPENDED_STATE_CHANGED;
	event.value = event_info;

	EINA_LIST_FOREACH(handler_list[APP_EVENT_SUSPENDED_STATE_CHANGED], l, handler) {
		handler->cb(&event, handler->data);
	}

	return APP_ERROR_NONE;
}

static void _service_app_appcore_agent_set_event_cb(app_event_type_e event_type)
{
	switch (event_type) {
	case APP_EVENT_LOW_MEMORY:
		appcore_agent_set_event_callback(APPCORE_AGENT_EVENT_LOW_MEMORY, _service_app_low_memory, NULL);
		break;
	case APP_EVENT_LOW_BATTERY:
		appcore_agent_set_event_callback(APPCORE_AGENT_EVENT_LOW_BATTERY, _service_app_low_battery, NULL);
		break;
	case APP_EVENT_LANGUAGE_CHANGED:
		appcore_agent_set_event_callback(APPCORE_AGENT_EVENT_LANG_CHANGE, _service_app_lang_changed, NULL);
		break;
	case APP_EVENT_REGION_FORMAT_CHANGED:
		appcore_agent_set_event_callback(APPCORE_AGENT_EVENT_REGION_CHANGE, _service_app_region_changed, NULL);
		break;
	case APP_EVENT_SUSPENDED_STATE_CHANGED:
		LOGD("[__SUSPEND__]");
		appcore_agent_set_event_callback(APPCORE_AGENT_EVENT_SUSPENDED_STATE_CHANGE, _service_app_appcore_suspended_state_changed, NULL);
		break;
	default:
		break;
	}
}

static void _service_app_appcore_agent_unset_event_cb(app_event_type_e event_type)
{
	switch (event_type) {
	case APP_EVENT_LOW_MEMORY:
		appcore_agent_set_event_callback(APPCORE_AGENT_EVENT_LOW_MEMORY, NULL, NULL);
		break;
	case APP_EVENT_LOW_BATTERY:
		appcore_agent_set_event_callback(APPCORE_AGENT_EVENT_LOW_BATTERY, NULL, NULL);
		break;
	case APP_EVENT_LANGUAGE_CHANGED:
		appcore_agent_set_event_callback(APPCORE_AGENT_EVENT_LANG_CHANGE, NULL, NULL);
		break;
	case APP_EVENT_REGION_FORMAT_CHANGED:
		appcore_agent_set_event_callback(APPCORE_AGENT_EVENT_REGION_CHANGE, NULL, NULL);
		break;
	case APP_EVENT_SUSPENDED_STATE_CHANGED:
		LOGD("[__SUSPEND__]");
		appcore_agent_set_event_callback(APPCORE_AGENT_EVENT_SUSPENDED_STATE_CHANGE, NULL, NULL);
		break;
	default:
		break;
	}
}

static void _service_app_set_appcore_event_cb(void)
{
	_service_app_appcore_agent_set_event_cb(APP_EVENT_LOW_MEMORY);
	_service_app_appcore_agent_set_event_cb(APP_EVENT_LANGUAGE_CHANGED);
	_service_app_appcore_agent_set_event_cb(APP_EVENT_REGION_FORMAT_CHANGED);

	if (eina_list_count(handler_list[APP_EVENT_LOW_BATTERY]) > 0)
		_service_app_appcore_agent_set_event_cb(APP_EVENT_LOW_BATTERY);

	if (eina_list_count(handler_list[APP_EVENT_SUSPENDED_STATE_CHANGED]) > 0)
		_service_app_appcore_agent_set_event_cb(APP_EVENT_SUSPENDED_STATE_CHANGED);
}

static void _service_app_unset_appcore_event_cb(void)
{
	_service_app_appcore_agent_unset_event_cb(APP_EVENT_LOW_MEMORY);
	_service_app_appcore_agent_unset_event_cb(APP_EVENT_LANGUAGE_CHANGED);
	_service_app_appcore_agent_unset_event_cb(APP_EVENT_REGION_FORMAT_CHANGED);

	if (eina_list_count(handler_list[APP_EVENT_LOW_BATTERY]) > 0)
		_service_app_appcore_agent_unset_event_cb(APP_EVENT_LOW_BATTERY);

	if (eina_list_count(handler_list[APP_EVENT_SUSPENDED_STATE_CHANGED]) > 0)
		_service_app_appcore_agent_unset_event_cb(APP_EVENT_SUSPENDED_STATE_CHANGED);
}

static int _service_app_create(void *data)
{
	struct service_app_context *app_context = data;
	service_app_create_cb create_cb;

	if (app_context == NULL)
		return service_app_error(APP_ERROR_INVALID_CONTEXT, __FUNCTION__, NULL);

	appcore_agent_initialized = 1;
	_service_app_set_appcore_event_cb();

	create_cb = app_context->callback->create;
	if (create_cb == NULL || create_cb(app_context->data) == false)
		return service_app_error(APP_ERROR_INVALID_CONTEXT, __FUNCTION__, "service_app_create_cb() returns false");

	app_context->state = SERVICE_APP_STATE_RUNNING;

	return APP_ERROR_NONE;
}

static int _service_app_terminate(void *data)
{
	struct service_app_context *app_context = data;
	service_app_terminate_cb terminate_cb;

	if (app_context == NULL)
		return service_app_error(APP_ERROR_INVALID_CONTEXT, __FUNCTION__, NULL);

	terminate_cb = app_context->callback->terminate;
	if (terminate_cb != NULL)
		terminate_cb(app_context->data);

	_service_app_unset_appcore_event_cb();

	if (handler_initialized)
		_free_handler_list();

	return APP_ERROR_NONE;
}

static int _service_app_reset(app_control_h app_control, void *data)
{
	struct service_app_context *app_context = data;
	service_app_control_cb service_cb;

	if (app_context == NULL)
		return service_app_error(APP_ERROR_INVALID_CONTEXT, __FUNCTION__, NULL);

	service_cb = app_context->callback->app_control;
	if (service_cb != NULL)
		service_cb(app_control, app_context->data);

	return APP_ERROR_NONE;
}

EXPORT_API int service_app_main(int argc, char **argv, service_app_lifecycle_callback_s *callback, void *user_data)
{
	struct service_app_context app_context = {
		.state = SERVICE_APP_STATE_NOT_RUNNING,
		.callback = callback,
		.data = user_data
	};

	struct agentcore_ops appcore_context = {
		.data = &app_context,
		.create = _service_app_create,
		.terminate = _service_app_terminate,
		.app_control = _service_app_reset,
	};

	if (argc <= 0 || argv == NULL || callback == NULL)
		return service_app_error(APP_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);

	if (callback->create == NULL)
		return service_app_error(APP_ERROR_INVALID_PARAMETER, __FUNCTION__, "service_app_create_cb() callback must be registered");

	if (app_context.state != SERVICE_APP_STATE_NOT_RUNNING)
		return service_app_error(APP_ERROR_ALREADY_RUNNING, __FUNCTION__, NULL);

	if (_service_app_get_id(&(app_context.package)) == APP_ERROR_NONE) {
		if (_service_appget_package_app_name(app_context.package, &(app_context.service_app_name)) != APP_ERROR_NONE) {
			free(app_context.package);
			app_context.package = NULL;
		}
	}

	app_context.state = SERVICE_APP_STATE_CREATING;

	appcore_agent_main(argc, argv, &appcore_context);

	if (app_context.service_app_name)
		free(app_context.service_app_name);
	if (app_context.package)
		free(app_context.package);

	return APP_ERROR_NONE;
}

EXPORT_API void service_app_exit(void)
{
	appcore_agent_terminate();
}

EXPORT_API int service_app_add_event_handler(app_event_handler_h *event_handler, app_event_type_e event_type, app_event_cb callback, void *user_data)
{
	app_event_handler_h handler;
	Eina_List *l_itr;

	if (!handler_initialized) {
		eina_init();
		handler_initialized = 1;
	}

	if (event_handler == NULL || callback == NULL)
		return service_app_error(APP_ERROR_INVALID_PARAMETER, __FUNCTION__, "null parameter");

	if (event_type < APP_EVENT_LOW_MEMORY || event_type > APP_EVENT_SUSPENDED_STATE_CHANGED)
		return service_app_error(APP_ERROR_INVALID_PARAMETER, __FUNCTION__, "invalid event type");

	EINA_LIST_FOREACH(handler_list[event_type], l_itr, handler) {
		if (handler->cb == callback)
			return service_app_error(APP_ERROR_INVALID_PARAMETER, __FUNCTION__, "already registered");
	}

	handler = calloc(1, sizeof(struct app_event_handler));
	if (!handler)
		return service_app_error(APP_ERROR_OUT_OF_MEMORY, __FUNCTION__, "insufficient memory");

	handler->type = event_type;
	handler->cb = callback;
	handler->data = user_data;

	if (appcore_agent_initialized && eina_list_count(handler_list[event_type]) == 0)
		_service_app_appcore_agent_set_event_cb(event_type);

	handler_list[event_type] = eina_list_append(handler_list[event_type], handler);

	*event_handler = handler;

	return APP_ERROR_NONE;
}

EXPORT_API int service_app_remove_event_handler(app_event_handler_h event_handler)
{
	app_event_handler_h handler;
	app_event_type_e type;
	Eina_List *l_itr;
	Eina_List *l_next;

	if (event_handler == NULL)
		return service_app_error(APP_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);

	if (!handler_initialized) {
		LOGI("handler list is not initialzed");
		return APP_ERROR_NONE;
	}

	type = event_handler->type;
	if (type < APP_EVENT_LOW_MEMORY
			|| type > APP_EVENT_REGION_FORMAT_CHANGED
			|| type == APP_EVENT_DEVICE_ORIENTATION_CHANGED)
		return service_app_error(APP_ERROR_INVALID_PARAMETER, __FUNCTION__, NULL);

	EINA_LIST_FOREACH_SAFE(handler_list[type], l_itr, l_next, handler) {
		if (handler == event_handler) {
			free(handler);
			handler_list[type] = eina_list_remove_list(handler_list[type], l_itr);

			if (appcore_agent_initialized && eina_list_count(handler_list[type]) == 0)
				_service_app_appcore_agent_unset_event_cb(type);

			return APP_ERROR_NONE;
		}
	}

	return service_app_error(APP_ERROR_INVALID_PARAMETER, __FUNCTION__, "cannot find such handler");
}