#include "application_manager.h"
-#include <glib.h>
#include <sys/stat.h>
#include <unistd.h>
+#include <thread>
#include <type_traits>
#include <app_control_internal.h>
{SYSTEM_EVENT_DATA_ROAMING_STATE, EVENT_KEY_DATA_ROAMING_STATE},
{SYSTEM_EVENT_FONT_SET, EVENT_KEY_FONT_SET}};
-#if defined(TIZEN_MOBILE) || defined(TIZEN_WEARABLE)
+#if defined(TIZEN_MOBILE) || defined(TIZEN_WEARABLE) || defined(TIZEN_COMMON)
const int kMaximumBatteryRetrievedObjects = 30;
const int kMaximumAppsRetrievedObjects = 10;
const int kDefaultPeriodOfTime = 30;
#endif
}
+std::set<LaunchAppControlCallbackData*> ApplicationManager::launch_app_control_set_global_{};
+std::mutex ApplicationManager::launch_app_control_set_mutex_{};
+
ApplicationManager::ApplicationManager(ApplicationInstance& instance)
: pkgmgr_client_handle_(nullptr),
pkgmgr_client_uninstall_handle_(nullptr),
LoggerE("app_manager_event_destroy failed, error: %d", ret);
}
}
+
+ // Calling order of reply_callback and result_callback of launchAppControl
+ // cannot be determined, reply_callback depends on application scenario, which
+ // cannot be predicted. Moreover reply_callback is optional and can be called or not,
+ // thus callback_data is relesed here at the end of application lifetime.
+ std::lock_guard<std::mutex> lock(launch_app_control_set_mutex_);
+ std::for_each(launch_app_control_set_.begin(), launch_app_control_set_.end(),
+ [](LaunchAppControlCallbackData* const& data) {
+ LoggerD("Releasing callback data: %p", data);
+ delete data;
+ launch_app_control_set_global_.erase(data);
+ });
}
void ApplicationManager::GetCurrentApplication(const std::string& app_id, picojson::object* out) {
reply_callback_id = reply.get<std::string>();
}
- struct LaunchAppControlCallbackData {
- ApplicationInstance* instance;
- std::shared_ptr<picojson::value> response;
- std::string reply_callback_id;
- }* launch_app_user_data = new (std::nothrow)
+ LaunchAppControlCallbackData* launch_app_user_data = new (std::nothrow)
LaunchAppControlCallbackData{&this->instance_, response, reply_callback_id};
if (!launch_app_user_data) {
LaunchAppControlCallbackData* callback_data =
static_cast<LaunchAppControlCallbackData*>(user_data);
- if (!callback_data) {
- LoggerD("reply_callback failed: user_data is nullptr");
+
+ std::lock_guard<std::mutex> lock(launch_app_control_set_mutex_);
+ if (!launch_app_control_set_global_.count(callback_data)) {
+ LoggerE("Invalid callback_data: %p", callback_data);
return;
}
}
Instance::PostMessage(callback_data->instance, return_value.serialize().c_str());
- delete callback_data;
+ // Calling order of reply_callback and result_callback cannot be determined,
+ // thus callback_data is not released here, but stored for release in destructor of
+ // ApplicationManager
};
}
LaunchAppControlCallbackData* callback_data =
static_cast<LaunchAppControlCallbackData*>(user_data);
+ std::lock_guard<std::mutex> lock(launch_app_control_set_mutex_);
+ if (!launch_app_control_set_global_.count(callback_data)) {
+ LoggerE("Invalid callback_data: %p", callback_data);
+ return;
+ }
+
auto result = utils::TranslateAppControlError(launch_result);
if (result.IsError()) {
Instance::PostMessage(callback_data->instance, callback_data->response->serialize().c_str());
- if (result.IsError() || (callback_data->reply_callback_id).empty()) {
- delete callback_data;
- }
+ // Calling order of reply_callback and result_callback cannot be determined,
+ // thus callback_data is not released here, but stored for release in destructor of
+ // ApplicationManager
};
/*
delete launch_app_user_data;
AsyncResponse(launch_result, &response);
} else {
- LoggerD("App launched");
+ // Calling order of reply_callback and result_callback cannot be determined,
+ // reply_callback depends on application scenario, which cannot be predicted.
+ // Moreover reply_callback is optional and can be called or not,
+ // thus callback_data is stored for releasing in destructor of ApplicationManager
+ // at the end of application lifetime.
+ // We can not be sure whether callbach will not fire after instance destruction,
+ // we store all user_data pointers in static launch_app_control_set_global_ to check
+ // if they are still valid.
+ std::lock_guard<std::mutex> lock(launch_app_control_set_mutex_);
+ launch_app_control_set_.insert(launch_app_user_data);
+ launch_app_control_set_global_.insert(launch_app_user_data);
+ LoggerD("App launched, data %p stored for later release", launch_app_user_data);
}
}
-
namespace {
PlatformResult TranslateLaunchError(app_control_error_e return_code) {
};
/*
- * TODO: Observe how often app_control_send_launch_request_async tries to launch the application.
+ * TODO: Observe how often app_control_send_resume_request tries to launch the application.
* Previous implementation, using synchronous app_control_send_launch_request,
* tries to launch the application 3 times, before reporting an error.
- * New implementation, using app_control_send_launch_request_async makes only one attempt.
+ * New implementation, using app_control_send_resume_request makes only one attempt.
* If problems, such as failed application start occur, multiple attempts may solve the problem.
*/
- auto launch_result =
- TranslateLaunchError(static_cast<app_control_error_e>(app_control_send_resume_request(
- app_control_ptr.get(), result_callback, launch_user_data)));
+ auto launch_result = TranslateLaunchError(static_cast<app_control_error_e>(
+ app_control_send_resume_request(app_control_ptr.get(), result_callback, launch_user_data)));
if (launch_result.IsError()) {
delete launch_user_data;
ReportSuccess(result, *out);
}
-#if defined(TIZEN_MOBILE) || defined(TIZEN_WEARABLE)
+#if defined(TIZEN_MOBILE) || defined(TIZEN_WEARABLE) || defined(TIZEN_COMMON)
PlatformResult ApplicationManager::BatteryUsageFilter(const picojson::value& args,
const context_history_filter_h filter,
context_history_data_e* data_type_out) {
void ApplicationManager::GetBatteryUsageInfo(const picojson::value& args, picojson::object* out) {
ScopeLogger();
-#if defined(TIZEN_MOBILE) || defined(TIZEN_WEARABLE)
+#if defined(TIZEN_MOBILE) || defined(TIZEN_WEARABLE) || defined(TIZEN_COMMON)
int callback_id = -1;
const auto& callback = args.get(kCallbackId);
if (callback.is<double>()) {
void ApplicationManager::GetAppsUsageInfo(const picojson::value& args, picojson::object* out) {
ScopeLogger();
-#if defined(TIZEN_MOBILE) || defined(TIZEN_WEARABLE)
+#if defined(TIZEN_MOBILE) || defined(TIZEN_WEARABLE) || defined(TIZEN_COMMON)
int callback_id = static_cast<int>(args.get(kCallbackId).get<double>());
auto get_apps_usage = [args](const std::shared_ptr<picojson::value>& response) -> void {
manager->status_callback_(&event);
}
-#if defined(TIZEN_MOBILE) || defined(TIZEN_WEARABLE)
+#if defined(TIZEN_MOBILE) || defined(TIZEN_WEARABLE) || defined(TIZEN_COMMON)
PlatformResult ApplicationManager::GetContextHistory(
const picojson::value& args, picojson::object* out,
common::PlatformResult (*modify_filter_cb)(const picojson::value&,