/* * Copyright (c) 2018 Samsung Electronics Co., Ltd. * * Licensed under the Flora License, Version 1.1 (the License); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://floralicense.org/license/ * * 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 #include #include "task-factory.h" #include "config.h" #include "report-generator.h" #include "report-generator-system.h" #include "report-generator-load-avg.h" #include "report-generator-process.h" #include "report-generator-top.h" #include "log.h" #include "err-check.h" #include "macros.h" #include "ipc.h" struct base_task { task_t task; report_generator_t *report_gen; JsonBuilder *builder; JsonGenerator *generator; }; static task_t *create_system_report_task(); static task_t *create_load_avg_report_task(); static task_t *create_process_report_task(const config_data_process_t *config); static task_t *create_top_report_task(struct config_data_top options); static void base_task_init(struct base_task *base, report_generator_t *generator); static void base_task_execute(task_t *task); static void base_task_release(task_t *task); task_t *task_factory_create_task(const config_t *config) { switch (config->scope) { case SYSTEM: return create_system_report_task(); case LOAD_AVG: return create_load_avg_report_task(); case PROCESS: return create_process_report_task(&config->data.process); case TOP: return create_top_report_task(config->data.top); default: return NULL; } } static void base_task_init(struct base_task *base, report_generator_t *generator) { base->task.execute = base_task_execute; base->task.release = base_task_release; base->builder = json_builder_new(); base->generator = json_generator_new(); base->report_gen = generator; } static void base_task_execute(task_t *task) { struct base_task *_base_task = container_of(task, struct base_task, task); json_builder_reset(_base_task->builder); if (report_generator_refresh((report_generator_t*)_base_task->report_gen) != true) { return; } report_generator_generate((report_generator_t*)_base_task->report_gen, _base_task->builder); JsonNode *root = json_builder_get_root(_base_task->builder); json_generator_set_root(_base_task->generator, root); char *json_report = json_generator_to_data(_base_task->generator, NULL); ipc_send_report(json_report); g_free(json_report); } static void base_task_release(task_t *task) { struct base_task *_base_task = container_of(task, struct base_task, task); report_generator_release(_base_task->report_gen); g_object_unref(_base_task->builder); g_object_unref(_base_task->generator); } static task_t *create_system_report_task() { struct base_task *_task = (struct base_task*)g_malloc(sizeof(struct base_task)); report_generator_system_t *generator = report_generator_system_new_generator(); if (!generator) { free(_task); return NULL; } base_task_init(_task, (report_generator_t*)generator); return &_task->task; } static task_t *create_load_avg_report_task() { struct base_task *_task = (struct base_task*)g_malloc(sizeof(struct base_task)); report_generator_load_avg_t *generator = report_generator_load_avg_new_generator(); if (!generator) { free(_task); return NULL; } base_task_init(_task, (report_generator_t*)generator); return &_task->task; } static bool _report_generator_process_filter_cb(struct process *proc, void *user_data) { config_data_process_t *data = user_data; if (data->n_filters == 0) { return true; } for (int i = 0; i < data->n_filters; ++i) { struct filter *filter = &data->filters[i]; if (filter->app_id && process_get_appid(proc)) { if (strcmp(filter->app_id, process_get_appid(proc)) == 0) return true; } if (filter->exe && process_get_exe(proc)) { if (strcmp(filter->exe, process_get_exe(proc)) == 0) return true; } } return false; } static task_t *create_process_report_task(const config_data_process_t *config) { struct base_task *_task = (struct base_task*)g_malloc(sizeof(struct base_task)); report_generator_process_t *generator = report_generator_process_new_generator(); if (!generator) { free(_task); return NULL; } report_generator_process_set_filter(generator, _report_generator_process_filter_cb, config); base_task_init(_task, (report_generator_t*)generator); return &_task->task; } static task_t *create_top_report_task(struct config_data_top options) { report_generator_top_type_e type; switch (options.subject) { case TOP_SUBJECT_APPS: type = REPORT_GENERATOR_TOP_TYPE_APPS; break; case TOP_SUBJECT_ALL: type = REPORT_GENERATOR_TOP_TYPE_ALL; break; default: return NULL; } struct base_task *_task = (struct base_task*)g_malloc(sizeof(struct base_task)); report_generator_top_t *generator = report_generator_top_new_generator(type, options.top); if (!generator) { free(_task); return NULL; } base_task_init(_task, (report_generator_t*)generator); return &_task->task; }